1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_hal_rtc.c
4   * @author  MCD Application Team
5   * @brief   RTC HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Real-Time Clock (RTC) peripheral:
8   *           + Initialization/de-initialization functions
9   *           + Calendar (Time and Date) configuration
10   *           + Alarms (Alarm A and Alarm B) configuration
11   *           + WakeUp Timer configuration
12   *           + TimeStamp configuration
13   *           + Tampers configuration
14   *           + Backup Data Registers configuration
15   *           + RTC Tamper and TimeStamp Pins Selection
16   *           + Interrupts and flags management
17   *
18   ******************************************************************************
19   * @attention
20   *
21   * Copyright (c) 2022 STMicroelectronics.
22   * All rights reserved.
23   *
24   * This software is licensed under terms that can be found in the LICENSE file
25   * in the root directory of this software component.
26   * If no LICENSE file comes with this software, it is provided AS-IS.
27   *
28   ******************************************************************************
29   @verbatim
30  ===============================================================================
31                           ##### RTC Operating Condition #####
32  ===============================================================================
33   [..] The real-time clock (RTC) and the RTC backup registers can be powered
34        from the VBAT voltage when the main VDD supply is powered off.
35        To retain the content of the RTC backup registers and supply the RTC
36        when VDD is turned off, VBAT pin can be connected to an optional
37        standby voltage supplied by a battery or by another source.
38 
39                    ##### Backup Domain Reset #####
40  ===============================================================================
41   [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
42        to their reset values.
43        A backup domain reset is generated when one of the following events occurs:
44     (#) Software reset, triggered by setting the BDRST bit in the
45         RCC Backup domain control register (RCC_BDCR).
46     (#) VDD or VBAT power on, if both supplies have previously been powered off.
47     (#) Tamper detection event resets all data backup registers.
48 
49                    ##### Backup Domain Access #####
50   ==================================================================
51   [..] After reset, the backup domain (RTC registers and RTC backup data registers)
52        is protected against possible unwanted write accesses.
53   [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
54     (+) Enable the Power Controller (PWR) AHB4 interface clock using the
55         __HAL_RCC_PWR_CLK_ENABLE() function.
56     (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
57     (+) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
58         PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
59     (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
60 
61                   ##### How to use RTC Driver #####
62  ===================================================================
63   [..]
64     (+) Enable the RTC domain access (see description in the section above).
65     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
66         format using the HAL_RTC_Init() function.
67 
68   *** Time and Date configuration ***
69   ===================================
70   [..]
71     (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
72         and HAL_RTC_SetDate() functions.
73     (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
74 
75   *** Alarm configuration ***
76   ===========================
77   [..]
78     (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
79             You can also configure the RTC Alarm with interrupt mode using the
80             HAL_RTC_SetAlarm_IT() function.
81     (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
82 
83                   ##### RTC and low power modes #####
84   ==================================================================
85   [..] The MCU can be woken up from a low power mode by an RTC alternate
86        function.
87   [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
88        RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
89        These RTC alternate functions can wake up the system from the Stop and
90        Standby low power modes.
91   [..] The system can also wake up from low power modes without depending
92        on an external interrupt (Auto-wakeup mode), by using the RTC alarm
93        or the RTC wakeup events.
94   [..] The RTC provides a programmable time base for waking up from the
95        Stop or Standby mode at regular intervals.
96        Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
97        is LSE or LSI.
98 
99   *** Callback registration ***
100   =============================================
101   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
102   not defined, the callback registration feature is not available and all callbacks
103   are set to the corresponding weak functions. This is the recommended configuration
104   in order to optimize memory/code consumption footprint/performances.
105 
106   The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
107   allows the user to configure dynamically the driver callbacks.
108   Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
109 
110   Function HAL_RTC_RegisterCallback() allows to register following callbacks:
111     (+) AlarmAEventCallback           : RTC Alarm A Event callback.
112     (+) AlarmBEventCallback           : RTC Alarm B Event callback.
113     (+) TimeStampEventCallback        : RTC TimeStamp Event callback.
114     (+) WakeUpTimerEventCallback      : RTC WakeUpTimer Event callback.
115     (+) SSRUEventCallback             : RTC SSRU Event callback.
116     (+) Tamper1EventCallback          : RTC Tamper 1 Event callback.
117     (+) Tamper2EventCallback          : RTC Tamper 2 Event callback.
118     (+) Tamper3EventCallback          : RTC Tamper 3 Event callback.
119     (+) Tamper4EventCallback          : RTC Tamper 4 Event callback.
120     (+) Tamper5EventCallback          : RTC Tamper 5 Event callback.
121     (+) Tamper6EventCallback          : RTC Tamper 6 Event callback.
122     (+) InternalTamper3EventCallback  : RTC InternalTamper 3 Event callback.
123     (+) InternalTamper5EventCallback  : RTC InternalTamper 5 Event callback.
124     (+) InternalTamper6EventCallback  : RTC InternalTamper 6 Event callback.
125     (+) InternalTamper7EventCallback  : RTC InternalTamper 7 Event callback.
126     (+) InternalTamper8EventCallback  : RTC InternalTamper 8 Event callback.
127     (+) InternalTamper9EventCallback  : RTC InternalTamper 9 Event callback.
128     (+) InternalTamper11EventCallback : RTC InternalTamper 11 Event callback.
129     (+) InternalTamper12EventCallback : RTC InternalTamper 12 Event callback.
130     (+) InternalTamper13EventCallback : RTC InternalTamper 13 Event callback.
131     (+) MspInitCallback               : RTC MspInit callback.
132     (+) MspDeInitCallback             : RTC MspDeInit callback.
133   This function takes as parameters the HAL peripheral handle, the Callback ID
134   and a pointer to the user callback function.
135 
136   Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
137   weak function.
138   HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
139   and the Callback ID.
140   This function allows to reset following callbacks:
141     (+) AlarmAEventCallback           : RTC Alarm A Event callback.
142     (+) AlarmBEventCallback           : RTC Alarm B Event callback.
143     (+) TimeStampEventCallback        : RTC TimeStamp Event callback.
144     (+) WakeUpTimerEventCallback      : RTC WakeUpTimer Event callback.
145     (+) SSRUEventCallback             : RTC SSRU Event callback.
146     (+) Tamper1EventCallback          : RTC Tamper 1 Event callback.
147     (+) Tamper2EventCallback          : RTC Tamper 2 Event callback.
148     (+) Tamper3EventCallback          : RTC Tamper 3 Event callback.
149     (+) Tamper4EventCallback          : RTC Tamper 4 Event callback.
150     (+) Tamper5EventCallback          : RTC Tamper 5 Event callback.
151     (+) Tamper6EventCallback          : RTC Tamper 6 Event callback.
152     (+) InternalTamper3EventCallback  : RTC InternalTamper 3 Event callback.
153     (+) InternalTamper5EventCallback  : RTC InternalTamper 5 Event callback.
154     (+) InternalTamper6EventCallback  : RTC InternalTamper 6 Event callback.
155     (+) InternalTamper7EventCallback  : RTC InternalTamper 7 Event callback.
156     (+) InternalTamper8EventCallback  : RTC InternalTamper 8 Event callback.
157     (+) InternalTamper9EventCallback  : RTC InternalTamper 9 Event callback.
158     (+) InternalTamper11EventCallback : RTC InternalTamper 11 Event callback.
159     (+) InternalTamper12EventCallback : RTC InternalTamper 12 Event callback.
160     (+) InternalTamper13EventCallback : RTC InternalTamper 13 Event callback.
161     (+) MspInitCallback               : RTC MspInit callback.
162     (+) MspDeInitCallback             : RTC MspDeInit callback.
163 
164   By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
165   all callbacks are set to the corresponding weak functions :
166   examples AlarmAEventCallback(), TimeStampEventCallback().
167   Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
168   in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
169   (not registered beforehand).
170   If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
171   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
172 
173   Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
174   Exception done MspInit/MspDeInit that can be registered/unregistered
175   in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
176   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
177   In that case first register the MspInit/MspDeInit user callbacks
178   using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
179   or HAL_RTC_Init() function.
180 
181   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
182   not defined, the callback registration feature is not available and all callbacks
183   are set to the corresponding weak functions.
184 
185   @endverbatim
186   */
187 
188 /* Includes ------------------------------------------------------------------*/
189 #include "stm32wbaxx_hal.h"
190 
191 /** @addtogroup STM32WBAxx_HAL_Driver
192   * @{
193   */
194 
195 
196 /** @addtogroup RTC
197   * @brief RTC HAL module driver
198   * @{
199   */
200 
201 #ifdef HAL_RTC_MODULE_ENABLED
202 
203 /* Private typedef -----------------------------------------------------------*/
204 /* Private define ------------------------------------------------------------*/
205 /* Private macro -------------------------------------------------------------*/
206 /* Private variables ---------------------------------------------------------*/
207 /* Private function prototypes -----------------------------------------------*/
208 /* Exported functions --------------------------------------------------------*/
209 
210 /** @addtogroup RTC_Exported_Functions
211   * @{
212   */
213 
214 /** @addtogroup RTC_Exported_Functions_Group1
215   *  @brief    Initialization and Configuration functions
216   *
217 @verbatim
218  ===============================================================================
219               ##### Initialization and de-initialization functions #####
220  ===============================================================================
221    [..] This section provides functions allowing to initialize and configure the
222          RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
223          RTC registers Write protection, enter and exit the RTC initialization mode,
224          RTC registers synchronization check and reference clock detection enable.
225          (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
226              It is split into 2 programmable prescalers to minimize power consumption.
227              (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
228              (++) When both prescalers are used, it is recommended to configure the
229                  asynchronous prescaler to a high value to minimize power consumption.
230          (#) All RTC registers are Write protected. Writing to the RTC registers
231              is enabled by writing a key into the Write Protection register, RTC_WPR.
232          (#) To configure the RTC Calendar, user application should enter
233              initialization mode. In this mode, the calendar counter is stopped
234              and its value can be updated. When the initialization sequence is
235              complete, the calendar restarts counting after 4 RTCCLK cycles.
236          (#) To read the calendar through the shadow registers after Calendar
237              initialization, calendar update or after wakeup from low power modes
238              the software must first clear the RSF flag. The software must then
239              wait until it is set again before reading the calendar, which means
240              that the calendar registers have been correctly copied into the
241              RTC_TR and RTC_DR shadow registers. The HAL_RTC_WaitForSynchro() function
242              implements the above software sequence (RSF clear and RSF check).
243 
244 @endverbatim
245   * @{
246   */
247 
248 /**
249   * @brief  Initialize the RTC peripheral
250   * @param  hrtc RTC handle
251   * @retval HAL status
252   */
HAL_RTC_Init(RTC_HandleTypeDef * hrtc)253 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
254 {
255   HAL_StatusTypeDef status = HAL_ERROR;
256 
257   /* Check the RTC peripheral state */
258   if (hrtc != NULL)
259   {
260     /* Check the parameters */
261     assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
262     assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
263     assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
264     assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
265 #if defined(RTC_CR_OSEL)
266     assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
267     assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
268     assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
269     assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
270 #endif /* RTC_CR_OSEL */
271 #if defined(RTC_CR_OUT2EN)
272     assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
273 #endif /* RTC_CR_OUT2EN */
274     assert_param(IS_RTC_BINARY_MODE(hrtc->Init.BinMode));
275     assert_param(IS_RTC_BINARY_MIX_BCDU(hrtc->Init.BinMixBcdU));
276 
277 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
278     if (hrtc->State == HAL_RTC_STATE_RESET)
279     {
280       /* Allocate lock resource and initialize it */
281       hrtc->Lock = HAL_UNLOCKED;
282 
283       /* Legacy weak AlarmAEventCallback */
284       hrtc->AlarmAEventCallback           = HAL_RTC_AlarmAEventCallback;
285       /* Legacy weak AlarmBEventCallback */
286       hrtc->AlarmBEventCallback           = HAL_RTCEx_AlarmBEventCallback;
287       /* Legacy weak TimeStampEventCallback */
288       hrtc->TimeStampEventCallback        = HAL_RTCEx_TimeStampEventCallback;
289       /* Legacy weak WakeUpTimerEventCallback */
290       hrtc->WakeUpTimerEventCallback      = HAL_RTCEx_WakeUpTimerEventCallback;
291       /* Legacy weak SSRUEventCallback */
292       hrtc->SSRUEventCallback             = HAL_RTCEx_SSRUEventCallback;
293       /* Legacy weak Tamper1EventCallback */
294       hrtc->Tamper1EventCallback          = HAL_RTCEx_Tamper1EventCallback;
295       /* Legacy weak Tamper2EventCallback */
296       hrtc->Tamper2EventCallback          = HAL_RTCEx_Tamper2EventCallback;
297       /* Legacy weak Tamper3EventCallback */
298       hrtc->Tamper3EventCallback          = HAL_RTCEx_Tamper3EventCallback;
299       /* Legacy weak Tamper4EventCallback */
300       hrtc->Tamper4EventCallback          = HAL_RTCEx_Tamper4EventCallback;
301       /* Legacy weak Tamper5EventCallback */
302       hrtc->Tamper5EventCallback          = HAL_RTCEx_Tamper5EventCallback;
303       /* Legacy weak Tamper6EventCallback */
304       hrtc->Tamper6EventCallback          = HAL_RTCEx_Tamper6EventCallback;
305       /* Legacy weak InternalTamper3EventCallback */
306       hrtc->InternalTamper3EventCallback  = HAL_RTCEx_InternalTamper3EventCallback;
307       /* Legacy weak InternalTamper5EventCallback */
308       hrtc->InternalTamper5EventCallback  = HAL_RTCEx_InternalTamper5EventCallback;
309       /* Legacy weak InternalTamper6EventCallback */
310       hrtc->InternalTamper6EventCallback  = HAL_RTCEx_InternalTamper6EventCallback;
311       /* Legacy weak InternalTamper7EventCallback */
312       hrtc->InternalTamper7EventCallback  = HAL_RTCEx_InternalTamper7EventCallback;
313       /* Legacy weak InternalTamper8EventCallback */
314       hrtc->InternalTamper8EventCallback  = HAL_RTCEx_InternalTamper8EventCallback;
315       /* Legacy weak InternalTamper9EventCallback */
316       hrtc->InternalTamper9EventCallback  = HAL_RTCEx_InternalTamper9EventCallback;
317       /* Legacy weak InternalTamper11EventCallback */
318       hrtc->InternalTamper11EventCallback = HAL_RTCEx_InternalTamper11EventCallback;
319       /* Legacy weak InternalTamper12EventCallback */
320       hrtc->InternalTamper12EventCallback = HAL_RTCEx_InternalTamper12EventCallback;
321       /* Legacy weak InternalTamper13EventCallback */
322       hrtc->InternalTamper13EventCallback = HAL_RTCEx_InternalTamper13EventCallback;
323 
324       if (hrtc->MspInitCallback == NULL)
325       {
326         hrtc->MspInitCallback = HAL_RTC_MspInit;
327       }
328       /* Init the low level hardware */
329       hrtc->MspInitCallback(hrtc);
330 
331       if (hrtc->MspDeInitCallback == NULL)
332       {
333         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
334       }
335     }
336 #else
337     if (hrtc->State == HAL_RTC_STATE_RESET)
338     {
339       /* Allocate lock resource and initialize it */
340       hrtc->Lock = HAL_UNLOCKED;
341 
342       /* Initialize RTC MSP */
343       HAL_RTC_MspInit(hrtc);
344     }
345 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
346 
347     /* Set RTC state */
348     hrtc->State = HAL_RTC_STATE_BUSY;
349 
350     /* Check if the calendar has been not initialized */
351     if (__HAL_RTC_IS_CALENDAR_INITIALIZED(hrtc) == 0U)
352     {
353       /* Disable the write protection for RTC registers */
354       __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
355 
356       /* Enter Initialization mode */
357       status = RTC_EnterInitMode(hrtc);
358       if (status == HAL_OK)
359       {
360 #if defined(RTC_CR_OSEL)
361         /* Clear RTC_CR FMT, OSEL and POL Bits */
362         CLEAR_BIT(RTC->CR, (RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE));
363 
364         /* Set RTC_CR register */
365         SET_BIT(RTC->CR, (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity));
366 #else
367         /* Clear RTC_CR FMT Bits */
368         CLEAR_BIT(RTC->CR, RTC_CR_FMT);
369 
370         /* Set RTC_CR register */
371         SET_BIT(RTC->CR, hrtc->Init.HourFormat);
372 #endif /* RTC_CR_OSEL */
373 
374         /* Configure the RTC PRER */
375         WRITE_REG(RTC->PRER, ((hrtc->Init.SynchPrediv) | (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos)));
376 
377         /* Configure the Binary mode */
378         MODIFY_REG(RTC->ICSR, RTC_ICSR_BIN | RTC_ICSR_BCDU, hrtc->Init.BinMode | hrtc->Init.BinMixBcdU);
379 
380         /* Exit Initialization mode */
381         status = RTC_ExitInitMode(hrtc);
382 
383 #if defined(RTC_CR_OSEL)
384         if (status == HAL_OK)
385         {
386 #if defined(RTC_CR_OUT2EN)
387           MODIFY_REG(RTC->CR, \
388                      RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN, \
389                      hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
390 #else
391           MODIFY_REG(RTC->CR, \
392                      RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE, \
393                      hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType);
394 #endif /* RTC_CR_OUT2EN */
395         }
396 #endif /* RTC_CR_OSEL */
397       }
398 
399       /* Enable the write protection for RTC registers */
400       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
401 
402     }
403     else
404     {
405       /* Calendar is already initialized */
406       /* Set flag to OK */
407       status = HAL_OK;
408     }
409 
410     if (status == HAL_OK)
411     {
412       /* Change RTC state */
413       hrtc->State = HAL_RTC_STATE_READY;
414     }
415   }
416 
417   return status;
418 }
419 
420 /**
421   * @brief  DeInitialize the RTC peripheral.
422   * @note   This function does not reset the RTC Backup Data registers.
423   * @param  hrtc RTC handle
424   * @retval HAL status
425   */
HAL_RTC_DeInit(RTC_HandleTypeDef * hrtc)426 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
427 {
428   HAL_StatusTypeDef status;
429 
430   /* Set RTC state */
431   hrtc->State = HAL_RTC_STATE_BUSY;
432 
433   /* Disable the write protection for RTC registers */
434   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
435 
436   /* Enter Initialization mode */
437   status = RTC_EnterInitMode(hrtc);
438   if (status == HAL_OK)
439   {
440     /* Reset all RTC CR register bits */
441     CLEAR_REG(RTC->CR);
442     WRITE_REG(RTC->DR, (uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
443     CLEAR_REG(RTC->TR);
444     WRITE_REG(RTC->WUTR, RTC_WUTR_WUT);
445     WRITE_REG(RTC->PRER, ((uint32_t)(RTC_PRER_PREDIV_A | 0xFFU)));
446     CLEAR_REG(RTC->ALRMAR);
447     CLEAR_REG(RTC->ALRMBR);
448     CLEAR_REG(RTC->SHIFTR);
449     CLEAR_REG(RTC->CALR);
450     CLEAR_REG(RTC->ALRMASSR);
451     CLEAR_REG(RTC->ALRMBSSR);
452     WRITE_REG(RTC->SCR, RTC_SCR_CTSOVF | RTC_SCR_CTSF | RTC_SCR_CWUTF | RTC_SCR_CALRBF | RTC_SCR_CALRAF);
453 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
454     CLEAR_REG(RTC->SECCFGR);
455 #endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
456 #if defined (RTC_PRIVCFGR_ALRAPRIV)
457     CLEAR_REG(RTC->PRIVCFGR);
458 #endif /* RTC_PRIVCFGR_ALRAPRIV */
459 
460     /* Exit initialization mode */
461     status = RTC_ExitInitMode(hrtc);
462     if (status == HAL_OK)
463     {
464       /* Reset TAMP registers */
465       CLEAR_REG(TAMP->CR1);
466       CLEAR_REG(TAMP->CR2);
467       CLEAR_REG(TAMP->CR3);
468       CLEAR_REG(TAMP->FLTCR);
469       WRITE_REG(TAMP->ATCR1, TAMP_ATCR1_ATCKSEL);
470       CLEAR_REG(TAMP->ATOR);
471       CLEAR_REG(TAMP->ATCR2);
472 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
473       CLEAR_REG(TAMP->SECCFGR);
474 #endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
475 #if defined (TAMP_PRIVCFGR_TAMPPRIV)
476       CLEAR_REG(TAMP->PRIVCFGR);
477 #endif /* TAMP_PRIVCFGR_TAMPPRIV */
478     }
479   }
480 
481   /* Enable the write protection for RTC registers */
482   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
483 
484   if (status == HAL_OK)
485   {
486 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
487     if (hrtc->MspDeInitCallback == NULL)
488     {
489       hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
490     }
491 
492     /* DeInit the low level hardware: CLOCK, NVIC.*/
493     hrtc->MspDeInitCallback(hrtc);
494 
495 #else
496     /* De-Initialize RTC MSP */
497     HAL_RTC_MspDeInit(hrtc);
498 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
499 
500     /* Change RTC state */
501     hrtc->State = HAL_RTC_STATE_RESET;
502   }
503 
504   /* Release Lock */
505   __HAL_UNLOCK(hrtc);
506 
507   return status;
508 }
509 
510 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
511 /**
512   * @brief  Register a User RTC Callback
513   *         To be used instead of the weak predefined callback
514   * @param  hrtc RTC handle
515   * @param  CallbackID ID of the callback to be registered
516   *         This parameter can be one of the following values:
517   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID            Alarm A Event Callback ID
518   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID            Alarm B Event Callback ID
519   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID          TimeStamp Event Callback ID
520   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID        WakeUp Timer Event Callback ID
521   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID            Tamper 1 Callback ID
522   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID            Tamper 2 Callback ID
523   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID            Tamper 3 Callback ID
524   *          @arg @ref HAL_RTC_TAMPER4_EVENT_CB_ID            Tamper 4 Callback ID
525   *          @arg @ref HAL_RTC_TAMPER5_EVENT_CB_ID            Tamper 5 Callback ID
526   *          @arg @ref HAL_RTC_TAMPER6_EVENT_CB_ID            Tamper 6 Callback ID
527   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID   Internal Tamper 3 Callback ID
528   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID   Internal Tamper 5 Callback ID
529   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID   Internal Tamper 6 Callback ID
530   *          @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID   Internal Tamper 7 Callback ID
531   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID   Internal Tamper 8 Callback ID
532   *          @arg @ref HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID   Internal Tamper 9 Callback ID
533   *          @arg @ref HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID  Internal Tamper 11 Callback ID
534   *          @arg @ref HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID  Internal Tamper 12 Callback ID
535   *          @arg @ref HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID  Internal Tamper 13 Callback ID
536   *          @arg @ref HAL_RTC_MSPINIT_CB_ID                  Msp Init callback ID
537   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID                Msp DeInit callback ID
538   * @param  pCallback pointer to the Callback function
539   * @note  The HAL_RTC_RegisterCallback() may be called before HAL_RTC_Init() in HAL_RTC_STATE_RESET
540   *        to register callbacks for HAL_RTC_MSPINIT_CB_ID and HAL_RTC_MSPDEINIT_CB_ID.
541   * @retval HAL status
542   */
HAL_RTC_RegisterCallback(RTC_HandleTypeDef * hrtc,HAL_RTC_CallbackIDTypeDef CallbackID,pRTC_CallbackTypeDef pCallback)543 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID,
544                                            pRTC_CallbackTypeDef pCallback)
545 {
546   HAL_StatusTypeDef status = HAL_OK;
547 
548   if (pCallback == NULL)
549   {
550     return HAL_ERROR;
551   }
552 
553   if (HAL_RTC_STATE_READY == hrtc->State)
554   {
555     switch (CallbackID)
556     {
557       case HAL_RTC_ALARM_A_EVENT_CB_ID :
558         hrtc->AlarmAEventCallback = pCallback;
559         break;
560 
561       case HAL_RTC_ALARM_B_EVENT_CB_ID :
562         hrtc->AlarmBEventCallback = pCallback;
563         break;
564 
565       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
566         hrtc->TimeStampEventCallback = pCallback;
567         break;
568 
569       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
570         hrtc->WakeUpTimerEventCallback = pCallback;
571         break;
572 
573       case HAL_RTC_SSRU_EVENT_CB_ID :
574         hrtc->SSRUEventCallback = pCallback;
575         break;
576 
577       case HAL_RTC_TAMPER1_EVENT_CB_ID :
578         hrtc->Tamper1EventCallback = pCallback;
579         break;
580 
581       case HAL_RTC_TAMPER2_EVENT_CB_ID :
582         hrtc->Tamper2EventCallback = pCallback;
583         break;
584 
585       case HAL_RTC_TAMPER3_EVENT_CB_ID :
586         hrtc->Tamper3EventCallback = pCallback;
587         break;
588 
589       case HAL_RTC_TAMPER4_EVENT_CB_ID :
590         hrtc->Tamper4EventCallback = pCallback;
591         break;
592 
593       case HAL_RTC_TAMPER5_EVENT_CB_ID :
594         hrtc->Tamper5EventCallback = pCallback;
595         break;
596 
597       case HAL_RTC_TAMPER6_EVENT_CB_ID :
598         hrtc->Tamper6EventCallback = pCallback;
599         break;
600 
601       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
602         hrtc->InternalTamper3EventCallback = pCallback;
603         break;
604 
605       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
606         hrtc->InternalTamper5EventCallback = pCallback;
607         break;
608 
609       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
610         hrtc->InternalTamper6EventCallback = pCallback;
611         break;
612 
613       case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
614         hrtc->InternalTamper7EventCallback = pCallback;
615         break;
616 
617       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
618         hrtc->InternalTamper8EventCallback = pCallback;
619         break;
620 
621       case HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID :
622         hrtc->InternalTamper9EventCallback = pCallback;
623         break;
624 
625       case HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID :
626         hrtc->InternalTamper11EventCallback = pCallback;
627         break;
628 
629       case HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID :
630         hrtc->InternalTamper12EventCallback = pCallback;
631         break;
632 
633       case HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID :
634         hrtc->InternalTamper13EventCallback = pCallback;
635         break;
636 
637       case HAL_RTC_MSPINIT_CB_ID :
638         hrtc->MspInitCallback = pCallback;
639         break;
640 
641       case HAL_RTC_MSPDEINIT_CB_ID :
642         hrtc->MspDeInitCallback = pCallback;
643         break;
644 
645       default :
646         /* Return error status */
647         status = HAL_ERROR;
648         break;
649     }
650   }
651   else if (HAL_RTC_STATE_RESET == hrtc->State)
652   {
653     switch (CallbackID)
654     {
655       case HAL_RTC_MSPINIT_CB_ID :
656         hrtc->MspInitCallback = pCallback;
657         break;
658 
659       case HAL_RTC_MSPDEINIT_CB_ID :
660         hrtc->MspDeInitCallback = pCallback;
661         break;
662 
663       default :
664         /* Return error status */
665         status = HAL_ERROR;
666         break;
667     }
668   }
669   else
670   {
671     /* Return error status */
672     status = HAL_ERROR;
673   }
674 
675   return status;
676 }
677 
678 /**
679   * @brief  Unregister an RTC Callback
680   *         RTC callback is redirected to the weak predefined callback
681   * @param  hrtc RTC handle
682   * @param  CallbackID ID of the callback to be unregistered
683   *         This parameter can be one of the following values:
684   *         This parameter can be one of the following values:
685   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID            Alarm A Event Callback ID
686   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID            Alarm B Event Callback ID
687   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID          TimeStamp Event Callback ID
688   *          @arg @ref HAL_RTC_SSRU_EVENT_CB_ID               SSRU Callback ID
689   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID        WakeUp Timer Event Callback ID
690   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID            Tamper 1 Callback ID
691   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID            Tamper 2 Callback ID
692   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID            Tamper 3 Callback ID
693   *          @arg @ref HAL_RTC_TAMPER4_EVENT_CB_ID            Tamper 4 Callback ID
694   *          @arg @ref HAL_RTC_TAMPER5_EVENT_CB_ID            Tamper 5 Callback ID
695   *          @arg @ref HAL_RTC_TAMPER6_EVENT_CB_ID            Tamper 6 Callback ID
696   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID   Internal Tamper 3 Callback ID
697   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID   Internal Tamper 5 Callback ID
698   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID   Internal Tamper 6 Callback ID
699   *          @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID   Internal Tamper 7 Callback ID
700   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID   Internal Tamper 8 Callback ID
701   *          @arg @ref HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID   Internal Tamper 9 Callback ID
702   *          @arg @ref HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID  Internal Tamper 11 Callback ID
703   *          @arg @ref HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID  Internal Tamper 12 Callback ID
704   *          @arg @ref HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID  Internal Tamper 13 Callback ID
705   *          @arg @ref HAL_RTC_MSPINIT_CB_ID                  Msp Init callback ID
706   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID                Msp DeInit callback ID
707   * @note  The HAL_RTC_UnRegisterCallback() may be called before HAL_RTC_Init() in HAL_RTC_STATE_RESET
708   *        to un-register callbacks for HAL_RTC_MSPINIT_CB_ID and HAL_RTC_MSPDEINIT_CB_ID.
709   * @retval HAL status
710   */
HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef * hrtc,HAL_RTC_CallbackIDTypeDef CallbackID)711 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
712 {
713   HAL_StatusTypeDef status = HAL_OK;
714 
715   if (HAL_RTC_STATE_READY == hrtc->State)
716   {
717     switch (CallbackID)
718     {
719       case HAL_RTC_ALARM_A_EVENT_CB_ID :
720         /* Legacy weak AlarmAEventCallback */
721         hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback;
722         break;
723 
724       case HAL_RTC_ALARM_B_EVENT_CB_ID :
725         /* Legacy weak AlarmBEventCallback */
726         hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback;
727         break;
728 
729       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
730         /* Legacy weak TimeStampEventCallback */
731         hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback;
732         break;
733 
734       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
735         /* Legacy weak WakeUpTimerEventCallback */
736         hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback;
737         break;
738 
739       case HAL_RTC_SSRU_EVENT_CB_ID :
740         /* Legacy weak SSRUEventCallback */
741         hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback;
742         break;
743 
744       case HAL_RTC_TAMPER1_EVENT_CB_ID :
745         /* Legacy weak Tamper1EventCallback */
746         hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback;
747         break;
748 
749       case HAL_RTC_TAMPER2_EVENT_CB_ID :
750         /* Legacy weak Tamper2EventCallback */
751         hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback;
752         break;
753 
754       case HAL_RTC_TAMPER3_EVENT_CB_ID :
755         /* Legacy weak Tamper3EventCallback */
756         hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback;
757         break;
758 
759       case HAL_RTC_TAMPER4_EVENT_CB_ID :
760         /* Legacy weak Tamper4EventCallback */
761         hrtc->Tamper4EventCallback = HAL_RTCEx_Tamper4EventCallback;
762         break;
763 
764       case HAL_RTC_TAMPER5_EVENT_CB_ID :
765         /* Legacy weak Tamper5EventCallback */
766         hrtc->Tamper5EventCallback = HAL_RTCEx_Tamper5EventCallback;
767         break;
768 
769       case HAL_RTC_TAMPER6_EVENT_CB_ID :
770         /* Legacy weak Tamper6EventCallback */
771         hrtc->Tamper6EventCallback = HAL_RTCEx_Tamper6EventCallback;
772         break;
773 
774       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
775         /* Legacy weak InternalTamper3EventCallback */
776         hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
777         break;
778 
779       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
780         /* Legacy weak InternalTamper5EventCallback */
781         hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
782         break;
783 
784       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
785         /* Legacy weak InternalTamper6EventCallback */
786         hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
787         break;
788 
789       case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
790         /* Legacy weak InternalTamper7EventCallback */
791         hrtc->InternalTamper7EventCallback = HAL_RTCEx_InternalTamper7EventCallback;
792         break;
793 
794       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
795         /* Legacy weak InternalTamper8EventCallback */
796         hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
797         break;
798 
799       case HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID :
800         /* Legacy weak InternalTamper9EventCallback */
801         hrtc->InternalTamper9EventCallback = HAL_RTCEx_InternalTamper9EventCallback;
802         break;
803 
804       case HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID :
805         /* Legacy weak InternalTamper11EventCallback */
806         hrtc->InternalTamper11EventCallback = HAL_RTCEx_InternalTamper11EventCallback;
807         break;
808 
809       case HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID :
810         /* Legacy weak InternalTamper12EventCallback */
811         hrtc->InternalTamper12EventCallback = HAL_RTCEx_InternalTamper12EventCallback;
812         break;
813 
814       case HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID :
815         /* Legacy weak InternalTamper13EventCallback */
816         hrtc->InternalTamper13EventCallback = HAL_RTCEx_InternalTamper13EventCallback;
817         break;
818 
819       case HAL_RTC_MSPINIT_CB_ID :
820         hrtc->MspInitCallback = HAL_RTC_MspInit;
821         break;
822 
823       case HAL_RTC_MSPDEINIT_CB_ID :
824         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
825         break;
826 
827       default :
828         /* Return error status */
829         status = HAL_ERROR;
830         break;
831     }
832   }
833   else if (HAL_RTC_STATE_RESET == hrtc->State)
834   {
835     switch (CallbackID)
836     {
837       case HAL_RTC_MSPINIT_CB_ID :
838         hrtc->MspInitCallback = HAL_RTC_MspInit;
839         break;
840 
841       case HAL_RTC_MSPDEINIT_CB_ID :
842         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
843         break;
844 
845       default :
846         /* Return error status */
847         status = HAL_ERROR;
848         break;
849     }
850   }
851   else
852   {
853     /* Return error status */
854     status = HAL_ERROR;
855   }
856 
857   return status;
858 }
859 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
860 
861 /**
862   * @brief  Initialize the RTC MSP.
863   * @param  hrtc RTC handle
864   * @retval None
865   */
HAL_RTC_MspInit(RTC_HandleTypeDef * hrtc)866 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
867 {
868   /* Prevent unused argument(s) compilation warning */
869   UNUSED(hrtc);
870 
871   /* NOTE : This function should not be modified, when the callback is needed,
872             the HAL_RTC_MspInit could be implemented in the user file
873    */
874 }
875 
876 /**
877   * @brief  DeInitialize the RTC MSP.
878   * @param  hrtc RTC handle
879   * @retval None
880   */
HAL_RTC_MspDeInit(RTC_HandleTypeDef * hrtc)881 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
882 {
883   /* Prevent unused argument(s) compilation warning */
884   UNUSED(hrtc);
885 
886   /* NOTE : This function should not be modified, when the callback is needed,
887             the HAL_RTC_MspDeInit could be implemented in the user file
888    */
889 }
890 
891 /**
892   * @}
893   */
894 
895 /** @addtogroup RTC_Exported_Functions_Group2
896   *  @brief   RTC Time and Date functions
897   *
898 @verbatim
899  ===============================================================================
900                  ##### RTC Time and Date functions #####
901  ===============================================================================
902 
903  [..] This section provides functions allowing to configure Time and Date features
904 
905 @endverbatim
906   * @{
907   */
908 
909 /**
910   * @brief  Set RTC current time.
911   * @param  hrtc RTC handle
912   * @param  sTime Pointer to Time structure
913   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used and RTC_SSR will be automatically
914   *          reset to 0xFFFFFFFF
915   *          else sTime->SubSeconds is not used and RTC_SSR will be automatically reset to the
916   *          A 7-bit async prescaler (RTC_PRER_PREDIV_A)
917   * @param  Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
918   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
919   *          else this parameter can be one of the following values
920   *             @arg RTC_FORMAT_BIN: Binary format
921   *             @arg RTC_FORMAT_BCD: BCD format
922   * @retval HAL status
923   */
HAL_RTC_SetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)924 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
925 {
926   uint32_t tmpreg;
927   HAL_StatusTypeDef status;
928 
929 #ifdef USE_FULL_ASSERT
930   /* Check the parameters depending of the Binary mode with 32-bit free-running counter configuration */
931   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
932   {
933     /* Check the parameters */
934     assert_param(IS_RTC_FORMAT(Format));
935   }
936 #endif /* USE_FULL_ASSERT */
937 
938   /* Process Locked */
939   __HAL_LOCK(hrtc);
940 
941   /* Change RTC state */
942   hrtc->State = HAL_RTC_STATE_BUSY;
943 
944   /* Disable the write protection for RTC registers */
945   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
946 
947   /* Enter Initialization mode */
948   status = RTC_EnterInitMode(hrtc);
949   if (status == HAL_OK)
950   {
951     /* Check Binary mode ((32-bit free-running counter) */
952     if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
953     {
954       if (Format == RTC_FORMAT_BIN)
955       {
956         if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
957         {
958           assert_param(IS_RTC_HOUR12(sTime->Hours));
959           assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
960         }
961         else
962         {
963           sTime->TimeFormat = 0x00U;
964           assert_param(IS_RTC_HOUR24(sTime->Hours));
965         }
966         assert_param(IS_RTC_MINUTES(sTime->Minutes));
967         assert_param(IS_RTC_SECONDS(sTime->Seconds));
968 
969         tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
970                             ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
971                             ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
972                             (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
973       }
974       else
975       {
976         if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
977         {
978           assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
979           assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
980         }
981         else
982         {
983           sTime->TimeFormat = 0x00U;
984           assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
985         }
986         assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
987         assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
988         tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
989                   ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
990                   ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
991                   ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
992       }
993 
994       /* Set the RTC_TR register */
995       WRITE_REG(RTC->TR, (tmpreg & RTC_TR_RESERVED_MASK));
996 
997       /* Clear the bits to be configured */
998       CLEAR_BIT(RTC->CR, RTC_CR_BKP);
999     }
1000 
1001     /* Exit Initialization mode */
1002     status = RTC_ExitInitMode(hrtc);
1003   }
1004 
1005   /* Enable the write protection for RTC registers */
1006   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1007 
1008   if (status == HAL_OK)
1009   {
1010     /* Change RTC state */
1011     hrtc->State = HAL_RTC_STATE_READY;
1012   }
1013 
1014   /* Process Unlocked */
1015   __HAL_UNLOCK(hrtc);
1016 
1017   return status;
1018 }
1019 
1020 /**
1021   * @brief  Get RTC current time.
1022   * @note  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
1023   *        value in second fraction ratio with time unit following generic formula:
1024   *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
1025   *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
1026   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1027   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1028   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read
1029   *        to ensure consistency between the time and date values.
1030   * @param  hrtc RTC handle
1031   * @param  sTime
1032   *          if Binary mode is RTC_BINARY_ONLY, sTime->SubSeconds only is updated
1033   *          else
1034   *             Pointer to Time structure with Hours, Minutes and Seconds fields returned
1035   *               with input format (BIN or BCD), also SubSeconds field returning the
1036   *               RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
1037   *               factor to be used for second fraction ratio computation.
1038   * @param  Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
1039   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1040   *          else this parameter can be one of the following values:
1041   *            @arg RTC_FORMAT_BIN: Binary format
1042   *            @arg RTC_FORMAT_BCD: BCD format
1043   * @retval HAL status
1044   */
HAL_RTC_GetTime(const RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)1045 HAL_StatusTypeDef HAL_RTC_GetTime(const RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1046 {
1047   uint32_t tmpreg;
1048 
1049   /* Prevent unused argument(s) compilation warning */
1050   UNUSED(hrtc);
1051 
1052   /* Get subseconds structure field from the corresponding register */
1053   sTime->SubSeconds = READ_REG(RTC->SSR);
1054 
1055   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
1056   {
1057     /* Check the parameters */
1058     assert_param(IS_RTC_FORMAT(Format));
1059 
1060     /* Get SecondFraction structure field from the corresponding register field */
1061     sTime->SecondFraction = (uint32_t)(READ_REG(RTC->PRER) & RTC_PRER_PREDIV_S);
1062 
1063     /* Get the TR register */
1064     tmpreg = (uint32_t)(READ_REG(RTC->TR) & RTC_TR_RESERVED_MASK);
1065 
1066     /* Fill the structure fields with the read parameters */
1067     sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
1068     sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
1069     sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
1070     sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
1071 
1072     /* Check the input parameters format */
1073     if (Format == RTC_FORMAT_BIN)
1074     {
1075       /* Convert the time structure parameters to Binary format */
1076       sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
1077       sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
1078       sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
1079     }
1080   }
1081 
1082   return HAL_OK;
1083 }
1084 
1085 /**
1086   * @brief  Set RTC current date.
1087   * @param  hrtc RTC handle
1088   * @param  sDate Pointer to date structure
1089   * @param  Format Format of sDate->Year, sDate->Month and sDate->Weekday.
1090   *          This parameter can be one of the following values:
1091   *            @arg RTC_FORMAT_BIN: Binary format
1092   *            @arg RTC_FORMAT_BCD: BCD format
1093   * @retval HAL status
1094   */
HAL_RTC_SetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1095 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1096 {
1097   uint32_t datetmpreg;
1098   HAL_StatusTypeDef status;
1099 
1100   /* Check the parameters */
1101   assert_param(IS_RTC_FORMAT(Format));
1102 
1103   /* Process Locked */
1104   __HAL_LOCK(hrtc);
1105 
1106   /* Change RTC state */
1107   hrtc->State = HAL_RTC_STATE_BUSY;
1108 
1109   if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1110   {
1111     sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1112   }
1113 
1114   assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
1115 
1116   if (Format == RTC_FORMAT_BIN)
1117   {
1118     assert_param(IS_RTC_YEAR(sDate->Year));
1119     assert_param(IS_RTC_MONTH(sDate->Month));
1120     assert_param(IS_RTC_DATE(sDate->Date));
1121 
1122     datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1123                   ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1124                   ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1125                   ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1126   }
1127   else
1128   {
1129     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1130     assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1131     assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1132 
1133     datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1134                   (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1135                   (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1136                   (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1137   }
1138 
1139   /* Disable the write protection for RTC registers */
1140   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1141 
1142   /* Enter Initialization mode */
1143   status = RTC_EnterInitMode(hrtc);
1144   if (status == HAL_OK)
1145   {
1146     /* Set the RTC_DR register */
1147     WRITE_REG(RTC->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK));
1148 
1149     /* Exit Initialization mode */
1150     status = RTC_ExitInitMode(hrtc);
1151   }
1152 
1153   /* Enable the write protection for RTC registers */
1154   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1155 
1156   if (status == HAL_OK)
1157   {
1158     /* Change RTC state */
1159     hrtc->State = HAL_RTC_STATE_READY;
1160   }
1161 
1162   /* Process Unlocked */
1163   __HAL_UNLOCK(hrtc);
1164 
1165   return status;
1166 }
1167 
1168 /**
1169   * @brief  Get RTC current date.
1170   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1171   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1172   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1173   * @param  hrtc RTC handle
1174   * @param  sDate Pointer to Date structure
1175   * @param  Format Format of sDate->Year, sDate->Month and sDate->Weekday.
1176   *          This parameter can be one of the following values:
1177   *            @arg RTC_FORMAT_BIN: Binary format
1178   *            @arg RTC_FORMAT_BCD: BCD format
1179   * @retval HAL status
1180   */
HAL_RTC_GetDate(const RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1181 HAL_StatusTypeDef HAL_RTC_GetDate(const RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1182 {
1183   uint32_t datetmpreg;
1184 
1185   /* Prevent unused argument(s) compilation warning */
1186   UNUSED(hrtc);
1187 
1188   /* Check the parameters */
1189   assert_param(IS_RTC_FORMAT(Format));
1190 
1191   /* Get the DR register */
1192   datetmpreg = (uint32_t)(READ_REG(RTC->DR) & RTC_DR_RESERVED_MASK);
1193 
1194   /* Fill the structure fields with the read parameters */
1195   sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1196   sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1197   sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1198   sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1199 
1200   /* Check the input parameters format */
1201   if (Format == RTC_FORMAT_BIN)
1202   {
1203     /* Convert the date structure parameters to Binary format */
1204     sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1205     sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1206     sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1207   }
1208   return HAL_OK;
1209 }
1210 
1211 /**
1212   * @brief  Daylight Saving Time, Add one hour to the calendar in one single operation
1213   *         without going through the initialization procedure.
1214   * @param  hrtc RTC handle
1215   * @retval None
1216   */
HAL_RTC_DST_Add1Hour(const RTC_HandleTypeDef * hrtc)1217 void HAL_RTC_DST_Add1Hour(const RTC_HandleTypeDef *hrtc)
1218 {
1219   /* Prevent unused argument(s) compilation warning */
1220   UNUSED(hrtc);
1221 
1222   /* Disable the write protection for RTC registers */
1223   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1224 
1225   /* Set RTC_CR_ADD1H Bit */
1226   SET_BIT(RTC->CR, RTC_CR_ADD1H);
1227 
1228   /* Enable the write protection for RTC registers */
1229   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1230 }
1231 
1232 /**
1233   * @brief  Daylight Saving Time, Subtract one hour from the calendar in one
1234   *         single operation without going through the initialization procedure.
1235   * @param  hrtc RTC handle
1236   * @retval None
1237   */
HAL_RTC_DST_Sub1Hour(const RTC_HandleTypeDef * hrtc)1238 void HAL_RTC_DST_Sub1Hour(const RTC_HandleTypeDef *hrtc)
1239 {
1240   /* Prevent unused argument(s) compilation warning */
1241   UNUSED(hrtc);
1242 
1243   /* Disable the write protection for RTC registers */
1244   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1245 
1246   /* Set RTC_CR_SUB1H Bit */
1247   SET_BIT(RTC->CR, RTC_CR_SUB1H);
1248 
1249   /* Enable the write protection for RTC registers */
1250   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1251 }
1252 
1253 /**
1254   * @brief  Daylight Saving Time, Set the store operation bit.
1255   * @note   It can be used by the software in order to memorize the DST status.
1256   * @param  hrtc RTC handle
1257   * @retval None
1258   */
HAL_RTC_DST_SetStoreOperation(const RTC_HandleTypeDef * hrtc)1259 void HAL_RTC_DST_SetStoreOperation(const RTC_HandleTypeDef *hrtc)
1260 {
1261   /* Prevent unused argument(s) compilation warning */
1262   UNUSED(hrtc);
1263 
1264   /* Set RTC_CR_BKP Bit */
1265   SET_BIT(RTC->CR, RTC_CR_BKP);
1266 }
1267 
1268 /**
1269   * @brief  Daylight Saving Time, Clear the store operation bit.
1270   * @param  hrtc RTC handle
1271   * @retval None
1272   */
HAL_RTC_DST_ClearStoreOperation(const RTC_HandleTypeDef * hrtc)1273 void HAL_RTC_DST_ClearStoreOperation(const RTC_HandleTypeDef *hrtc)
1274 {
1275   /* Prevent unused argument(s) compilation warning */
1276   UNUSED(hrtc);
1277 
1278   /* Clear RTC_CR_BKP Bit */
1279   CLEAR_BIT(RTC->CR, RTC_CR_BKP);
1280 }
1281 
1282 /**
1283   * @brief  Daylight Saving Time, Read the store operation bit.
1284   * @param  hrtc RTC handle
1285   * @retval operation see RTC_StoreOperation_Definitions
1286   */
HAL_RTC_DST_ReadStoreOperation(const RTC_HandleTypeDef * hrtc)1287 uint32_t HAL_RTC_DST_ReadStoreOperation(const RTC_HandleTypeDef *hrtc)
1288 {
1289   /* Prevent unused argument(s) compilation warning */
1290   UNUSED(hrtc);
1291 
1292   /* Get RTC_CR_BKP Bit */
1293   return READ_BIT(RTC->CR, RTC_CR_BKP);
1294 }
1295 
1296 
1297 /**
1298   * @}
1299   */
1300 
1301 /** @addtogroup RTC_Exported_Functions_Group3
1302   *  @brief   RTC Alarm functions
1303   *
1304 @verbatim
1305  ===============================================================================
1306                  ##### RTC Alarm functions #####
1307  ===============================================================================
1308 
1309  [..] This section provides functions allowing to configure Alarm feature
1310 
1311 @endverbatim
1312   * @{
1313   */
1314 /**
1315   * @brief  Set the specified RTC Alarm.
1316   * @param  hrtc RTC handle
1317   * @param  sAlarm Pointer to Alarm structure
1318   *          if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1319   *             sAlarm->AlarmTime.SubSeconds
1320   *             sAlarm->AlarmSubSecondMask
1321   *             sAlarm->BinaryAutoClr
1322   * @param  Format of the entered parameters.
1323   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1324   *          else this parameter can be one of the following values
1325   *             @arg RTC_FORMAT_BIN: Binary format
1326   *             @arg RTC_FORMAT_BCD: BCD format
1327   * @retval HAL status
1328   */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1329 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1330 {
1331   uint32_t tmpreg = 0;
1332   uint32_t binary_mode;
1333 
1334   /* Process Locked */
1335   __HAL_LOCK(hrtc);
1336 
1337   /* Change RTC state */
1338   hrtc->State = HAL_RTC_STATE_BUSY;
1339 
1340 #ifdef  USE_FULL_ASSERT
1341   /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration) */
1342   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1343   {
1344     assert_param(IS_RTC_FORMAT(Format));
1345     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1346     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1347     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1348     assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1349     assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1350   }
1351   else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1352   {
1353     assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1354     assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1355   }
1356   else /* RTC_BINARY_MIX */
1357   {
1358     assert_param(IS_RTC_FORMAT(Format));
1359     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1360     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1361     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1362     /* In Binary Mix Mode, the RTC can not generate an alarm on a match
1363        involving all calendar items + the upper SSR bits */
1364     assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <=
1365                  (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1366   }
1367 #endif /* USE_FULL_ASSERT */
1368 
1369   /* Get Binary mode (32-bit free-running counter configuration) */
1370   binary_mode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1371 
1372   if (binary_mode != RTC_BINARY_ONLY)
1373   {
1374     if (Format == RTC_FORMAT_BIN)
1375     {
1376       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1377       {
1378         assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1379         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1380       }
1381       else
1382       {
1383         sAlarm->AlarmTime.TimeFormat = 0x00U;
1384         assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1385       }
1386       assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1387       assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1388 
1389       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1390       {
1391         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1392       }
1393       else
1394       {
1395         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1396       }
1397       tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1398                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1399                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1400                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1401                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1402                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1403                 ((uint32_t)sAlarm->AlarmMask));
1404     }
1405     else /* format BCD */
1406     {
1407       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1408       {
1409         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1410         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1411       }
1412       else
1413       {
1414         sAlarm->AlarmTime.TimeFormat = 0x00U;
1415         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1416       }
1417 
1418       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1419       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1420 
1421 #ifdef  USE_FULL_ASSERT
1422       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1423       {
1424         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1425       }
1426       else
1427       {
1428         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1429       }
1430 
1431 #endif /* USE_FULL_ASSERT */
1432       tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1433                 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1434                 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1435                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1436                 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1437                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1438                 ((uint32_t)sAlarm->AlarmMask));
1439     }
1440   }
1441 
1442   /* Configure the Alarm register */
1443   if (sAlarm->Alarm == RTC_ALARM_A)
1444   {
1445     /* Disable the Alarm A interrupt */
1446 
1447     /* In case of interrupt mode is used, the interrupt source must disabled */
1448     CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
1449 
1450     /* Clear flag alarm A */
1451     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1452 
1453     if (binary_mode == RTC_BINARY_ONLY)
1454     {
1455       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1456     }
1457     else
1458     {
1459       WRITE_REG(RTC->ALRMAR, tmpreg);
1460       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1461     }
1462 
1463     WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1464 
1465     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1466     {
1467       /* Configure the  Alarm A output clear */
1468       SET_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1469     }
1470     else
1471     {
1472       /* Disable the  Alarm A  output clear */
1473       CLEAR_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1474     }
1475     /* Configure the Alarm state: Enable Alarm */
1476     SET_BIT(RTC->CR, RTC_CR_ALRAE);
1477   }
1478   else
1479   {
1480     /* Disable the Alarm B interrupt */
1481 
1482     /* In case of interrupt mode is used, the interrupt source must disabled */
1483     CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
1484 
1485     /* Clear flag alarm B */
1486     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1487 
1488     if (binary_mode == RTC_BINARY_ONLY)
1489     {
1490       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1491     }
1492     else
1493     {
1494       WRITE_REG(RTC->ALRMBR, tmpreg);
1495 
1496       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1497     }
1498 
1499     WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1500 
1501     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1502     {
1503       /* Configure the  Alarm B output clear */
1504       SET_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1505     }
1506     else
1507     {
1508       /* Disable the  Alarm B output clear */
1509       CLEAR_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1510     }
1511 
1512     /* Configure the Alarm state: Enable Alarm */
1513     SET_BIT(RTC->CR, RTC_CR_ALRBE);
1514   }
1515 
1516   /* Change RTC state */
1517   hrtc->State = HAL_RTC_STATE_READY;
1518 
1519   /* Process Unlocked */
1520   __HAL_UNLOCK(hrtc);
1521 
1522   return HAL_OK;
1523 }
1524 
1525 /**
1526   * @brief  Set the specified RTC Alarm with Interrupt.
1527   * @param  hrtc RTC handle
1528   * @param  sAlarm Pointer to Alarm structure
1529   *          if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1530   *             sAlarm->AlarmTime.SubSeconds
1531   *             sAlarm->AlarmSubSecondMask
1532   *             sAlarm->BinaryAutoClr
1533   * @param  Format Specifies the format of the entered parameters.
1534   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1535   *          else this parameter can be one of the following values
1536   *             @arg RTC_FORMAT_BIN: Binary format
1537   *             @arg RTC_FORMAT_BCD: BCD format
1538   * @retval HAL status
1539   */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1540 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1541 {
1542   uint32_t tmpreg = 0;
1543   uint32_t binary_mode;
1544 
1545   /* Process Locked */
1546   __HAL_LOCK(hrtc);
1547 
1548   /* Change RTC state */
1549   hrtc->State = HAL_RTC_STATE_BUSY;
1550 
1551 #ifdef  USE_FULL_ASSERT
1552   /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration) */
1553   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1554   {
1555     assert_param(IS_RTC_FORMAT(Format));
1556     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1557     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1558     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1559     assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1560     assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1561   }
1562   else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1563   {
1564     assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1565     assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1566   }
1567   else /* RTC_BINARY_MIX */
1568   {
1569     assert_param(IS_RTC_FORMAT(Format));
1570     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1571     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1572     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1573     /* In Binary Mix Mode, the RTC can not generate an alarm on a match
1574       involving all calendar items + the upper SSR bits */
1575     assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <=
1576                  (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1577   }
1578 #endif /* USE_FULL_ASSERT */
1579 
1580   /* Get Binary mode (32-bit free-running counter configuration) */
1581   binary_mode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1582 
1583   if (binary_mode != RTC_BINARY_ONLY)
1584   {
1585     if (Format == RTC_FORMAT_BIN)
1586     {
1587       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1588       {
1589         assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1590         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1591       }
1592       else
1593       {
1594         sAlarm->AlarmTime.TimeFormat = 0x00U;
1595         assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1596       }
1597       assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1598       assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1599 
1600       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1601       {
1602         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1603       }
1604       else
1605       {
1606         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1607       }
1608       tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1609                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1610                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1611                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1612                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1613                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1614                 ((uint32_t)sAlarm->AlarmMask));
1615     }
1616     else /* Format BCD */
1617     {
1618       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1619       {
1620         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1621         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1622       }
1623       else
1624       {
1625         sAlarm->AlarmTime.TimeFormat = 0x00U;
1626         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1627       }
1628 
1629       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1630       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1631 
1632 #ifdef  USE_FULL_ASSERT
1633       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1634       {
1635         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1636       }
1637       else
1638       {
1639         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1640       }
1641 
1642 #endif /* USE_FULL_ASSERT */
1643       tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1644                 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1645                 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1646                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1647                 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1648                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1649                 ((uint32_t)sAlarm->AlarmMask));
1650 
1651     }
1652   }
1653 
1654   /* Configure the Alarm registers */
1655   if (sAlarm->Alarm == RTC_ALARM_A)
1656   {
1657     /* Disable the Alarm A interrupt */
1658     CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1659 
1660     /* Clear flag alarm A */
1661     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1662 
1663     if (binary_mode == RTC_BINARY_ONLY)
1664     {
1665       RTC->ALRMASSR = sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr;
1666     }
1667     else
1668     {
1669       WRITE_REG(RTC->ALRMAR, tmpreg);
1670 
1671       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1672     }
1673 
1674     WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1675 
1676     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1677     {
1678       /* Configure the  Alarm A output clear */
1679       SET_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1680     }
1681     else
1682     {
1683       /* Disable the Alarm A output clear */
1684       CLEAR_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1685     }
1686 
1687     /* Configure the Alarm interrupt */
1688     SET_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1689   }
1690   else
1691   {
1692     /* Disable the Alarm B interrupt */
1693     CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1694 
1695     /* Clear flag alarm B */
1696     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1697 
1698     if (binary_mode == RTC_BINARY_ONLY)
1699     {
1700       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1701     }
1702     else
1703     {
1704       WRITE_REG(RTC->ALRMBR, tmpreg);
1705 
1706       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1707     }
1708 
1709     WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1710 
1711     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1712     {
1713       /* Configure the  Alarm B Output clear */
1714       SET_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1715     }
1716     else
1717     {
1718       /* Disable the  Alarm B Output clear */
1719       CLEAR_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1720     }
1721 
1722     /* Configure the Alarm interrupt */
1723     SET_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1724   }
1725 
1726   /* Change RTC state */
1727   hrtc->State = HAL_RTC_STATE_READY;
1728 
1729   /* Process Unlocked */
1730   __HAL_UNLOCK(hrtc);
1731 
1732   return HAL_OK;
1733 }
1734 
1735 /**
1736   * @brief  Deactivate the specified RTC Alarm.
1737   * @param  hrtc RTC handle
1738   * @param  Alarm Specifies the Alarm.
1739   *          This parameter can be one of the following values:
1740   *            @arg RTC_ALARM_A:  AlarmA
1741   *            @arg RTC_ALARM_B:  AlarmB
1742   * @retval HAL status
1743   */
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef * hrtc,uint32_t Alarm)1744 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1745 {
1746   /* Check the parameters */
1747   assert_param(IS_RTC_ALARM(Alarm));
1748 
1749   /* Process Locked */
1750   __HAL_LOCK(hrtc);
1751 
1752   /* Change RTC state */
1753   hrtc->State = HAL_RTC_STATE_BUSY;
1754 
1755   /* In case of interrupt mode is used, the interrupt source must disabled */
1756   if (Alarm == RTC_ALARM_A)
1757   {
1758     CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1759 
1760     /* AlarmA, Clear SSCLR */
1761     CLEAR_BIT(RTC->ALRMASSR, RTC_ALRMASSR_SSCLR);
1762   }
1763   else
1764   {
1765     CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1766 
1767     /* AlarmB, Clear SSCLR */
1768     CLEAR_BIT(RTC->ALRMBSSR, RTC_ALRMBSSR_SSCLR);
1769   }
1770 
1771   /* Change RTC state */
1772   hrtc->State = HAL_RTC_STATE_READY;
1773 
1774   /* Process Unlocked */
1775   __HAL_UNLOCK(hrtc);
1776 
1777   return HAL_OK;
1778 }
1779 
1780 /**
1781   * @brief  Get the RTC Alarm value and masks.
1782   * @param  hrtc RTC handle
1783   * @param  sAlarm Pointer to Date structure
1784   * @param  Alarm Specifies the Alarm.
1785   *          This parameter can be one of the following values:
1786   *             @arg RTC_ALARM_A: AlarmA
1787   *             @arg RTC_ALARM_B: AlarmB
1788   * @param  Format Specifies the format of the entered parameters.
1789   *          This parameter can be one of the following values:
1790   *             @arg RTC_FORMAT_BIN: Binary format
1791   *             @arg RTC_FORMAT_BCD: BCD format
1792   * @retval HAL status
1793   */
HAL_RTC_GetAlarm(const RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Alarm,uint32_t Format)1794 HAL_StatusTypeDef HAL_RTC_GetAlarm(const RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm,
1795                                    uint32_t Format)
1796 {
1797   uint32_t tmpreg;
1798   uint32_t subsecondtmpreg;
1799 
1800   /* Prevent unused argument(s) compilation warning */
1801   UNUSED(hrtc);
1802 
1803   /* Check the parameters */
1804   assert_param(IS_RTC_FORMAT(Format));
1805   assert_param(IS_RTC_ALARM(Alarm));
1806 
1807   if (Alarm == RTC_ALARM_A)
1808   {
1809     /* AlarmA */
1810     sAlarm->Alarm = RTC_ALARM_A;
1811 
1812     tmpreg = READ_REG(RTC->ALRMAR);
1813     subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1814 
1815     /* Fill the structure with the read parameters */
1816     sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1817     sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1818     sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1819     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1820     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1821     sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1822     sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1823     sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1824   }
1825   else
1826   {
1827     sAlarm->Alarm = RTC_ALARM_B;
1828 
1829     tmpreg = READ_REG(RTC->ALRMBR);
1830     subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1831 
1832     /* Fill the structure with the read parameters */
1833     sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1834     sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1835     sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1836     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1837     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1838     sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1839     sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1840     sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1841   }
1842 
1843   if (Format == RTC_FORMAT_BIN)
1844   {
1845     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1846     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1847     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1848     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1849   }
1850 
1851   return HAL_OK;
1852 }
1853 
1854 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1855 /**
1856   * @brief  Handle Alarm secure interrupt request.
1857   * @param  hrtc RTC handle
1858   * @retval None
1859   */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)1860 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1861 {
1862   /* Get interrupt status */
1863   uint32_t tmp = READ_REG(RTC->SMISR);
1864 
1865   if ((tmp & RTC_SMISR_ALRAMF) != 0U)
1866   {
1867     /* Clear the AlarmA interrupt pending bit */
1868     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1869 
1870 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1871     /* Call Compare Match registered Callback */
1872     hrtc->AlarmAEventCallback(hrtc);
1873 #else
1874     HAL_RTC_AlarmAEventCallback(hrtc);
1875 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1876   }
1877 
1878   if ((tmp & RTC_SMISR_ALRBMF) != 0U)
1879   {
1880     /* Clear the AlarmB interrupt pending bit */
1881     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1882 
1883 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1884     /* Call Compare Match registered Callback */
1885     hrtc->AlarmBEventCallback(hrtc);
1886 #else
1887     HAL_RTCEx_AlarmBEventCallback(hrtc);
1888 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1889 
1890   }
1891 
1892   /* Change RTC state */
1893   hrtc->State = HAL_RTC_STATE_READY;
1894 }
1895 
1896 #else /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
1897 
1898 /**
1899   * @brief  Handle Alarm non-secure interrupt request.
1900   * @note   Alarm non-secure is available in non-secure driver.
1901   * @param  hrtc RTC handle
1902   * @retval None
1903   */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)1904 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1905 {
1906   /* Get interrupt status */
1907   uint32_t tmp = READ_REG(RTC->MISR);
1908 
1909   if ((tmp & RTC_MISR_ALRAMF) != 0U)
1910   {
1911     /* Clear the AlarmA interrupt pending bit */
1912     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1913 
1914 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1915     /* Call Compare Match registered Callback */
1916     hrtc->AlarmAEventCallback(hrtc);
1917 #else
1918     HAL_RTC_AlarmAEventCallback(hrtc);
1919 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1920   }
1921 
1922   if ((tmp & RTC_MISR_ALRBMF) != 0U)
1923   {
1924     /* Clear the AlarmB interrupt pending bit */
1925     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1926 
1927 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1928     /* Call Compare Match registered Callback */
1929     hrtc->AlarmBEventCallback(hrtc);
1930 #else
1931     HAL_RTCEx_AlarmBEventCallback(hrtc);
1932 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1933   }
1934 
1935   /* Change RTC state */
1936   hrtc->State = HAL_RTC_STATE_READY;
1937 }
1938 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
1939 
1940 /**
1941   * @brief  Alarm A secure callback.
1942   * @param  hrtc RTC handle
1943   * @retval None
1944   */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)1945 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1946 {
1947   /* Prevent unused argument(s) compilation warning */
1948   UNUSED(hrtc);
1949 
1950   /* NOTE : This function should not be modified, when the secure callback is needed,
1951             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1952    */
1953 }
1954 
1955 /**
1956   * @brief  Handle Alarm A Polling request.
1957   * @param  hrtc RTC handle
1958   * @param  Timeout Timeout duration
1959   * @retval HAL status
1960   */
HAL_RTC_PollForAlarmAEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)1961 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1962 {
1963   /* Prevent unused argument(s) compilation warning */
1964   UNUSED(hrtc);
1965 
1966   uint32_t tickstart = HAL_GetTick();
1967 
1968   while (READ_BIT(RTC->SR, RTC_SR_ALRAF) == 0U)
1969   {
1970     if (Timeout != HAL_MAX_DELAY)
1971     {
1972       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1973       {
1974         /* New check to avoid false timeout detection in case of preemption */
1975         if (READ_BIT(RTC->SR, RTC_SR_ALRAF) == 0U)
1976         {
1977           return HAL_TIMEOUT;
1978         }
1979         else
1980         {
1981           break;
1982         }
1983       }
1984     }
1985   }
1986 
1987   /* Clear the Alarm interrupt pending bit */
1988   WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1989 
1990   return HAL_OK;
1991 }
1992 
1993 /**
1994   * @}
1995   */
1996 
1997 /** @addtogroup RTC_Exported_Functions_Group4
1998   *  @brief   Peripheral Control functions
1999   *
2000 @verbatim
2001  ===============================================================================
2002                      ##### Peripheral Control functions #####
2003  ===============================================================================
2004     [..]
2005     This subsection provides functions allowing to
2006       (+) Wait for RTC Time and Date Synchronization
2007 
2008 @endverbatim
2009   * @{
2010   */
2011 
2012 /**
2013   * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
2014   *         synchronized with RTC APB clock.
2015   * @note   The RTC Resynchronization mode is write protected, use the
2016   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2017   * @note   To read the calendar through the shadow registers after Calendar
2018   *         initialization, calendar update or after wakeup from low power modes
2019   *         the software must first clear the RSF flag.
2020   *         The software must then wait until it is set again before reading
2021   *         the calendar, which means that the calendar registers have been
2022   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
2023   * @param  hrtc RTC handle
2024   * @retval HAL status
2025   */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)2026 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
2027 {
2028   uint32_t tickstart;
2029 
2030   /* Clear RSF flag */
2031   CLEAR_BIT(RTC->ICSR, RTC_ICSR_RSF);
2032 
2033   tickstart = HAL_GetTick();
2034 
2035   /* Wait the registers to be synchronised */
2036   while (READ_BIT(RTC->ICSR, RTC_ICSR_RSF) == 0U)
2037   {
2038     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2039     {
2040       /* New check to avoid false timeout detection in case of preemption */
2041       if (READ_BIT(RTC->ICSR, RTC_ICSR_RSF) == 0U)
2042       {
2043         /* Change RTC state */
2044         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2045         return HAL_TIMEOUT;
2046       }
2047       else
2048       {
2049         break;
2050       }
2051     }
2052   }
2053 
2054   return HAL_OK;
2055 }
2056 
2057 /**
2058   * @}
2059   */
2060 
2061 /** @addtogroup RTC_Exported_Functions_Group5
2062   *  @brief   Peripheral State functions
2063   *
2064 @verbatim
2065  ===============================================================================
2066                      ##### Peripheral State functions #####
2067  ===============================================================================
2068     [..]
2069     This subsection provides functions allowing to
2070       (+) Get RTC state
2071 
2072 @endverbatim
2073   * @{
2074   */
2075 /**
2076   * @brief  Return the RTC handle state.
2077   * @param  hrtc RTC handle
2078   * @retval HAL state
2079   */
HAL_RTC_GetState(const RTC_HandleTypeDef * hrtc)2080 HAL_RTCStateTypeDef HAL_RTC_GetState(const RTC_HandleTypeDef *hrtc)
2081 {
2082   /* Return RTC handle state */
2083   return hrtc->State;
2084 }
2085 
2086 /**
2087   * @}
2088   */
2089 /**
2090   * @}
2091   */
2092 
2093 /** @addtogroup RTC_Private_Functions
2094   * @{
2095   */
2096 /**
2097   * @brief  Enter the RTC Initialization mode.
2098   * @note   The RTC Initialization mode is write protected, use the
2099   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2100   * @param  hrtc RTC handle
2101   * @retval HAL status
2102   */
RTC_EnterInitMode(RTC_HandleTypeDef * hrtc)2103 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
2104 {
2105   uint32_t tickstart;
2106   HAL_StatusTypeDef status = HAL_OK;
2107 
2108   /* Check if the Initialization mode is set */
2109   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
2110   {
2111     /* Set the Initialization mode */
2112     SET_BIT(RTC->ICSR, RTC_ICSR_INIT);
2113 
2114     tickstart = HAL_GetTick();
2115     /* Wait till RTC is in INIT state and if Time out is reached exit */
2116     while ((READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
2117     {
2118       if ((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
2119       {
2120         /* New check to avoid false timeout detection in case of preemption */
2121         if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
2122         {
2123           status = HAL_TIMEOUT;
2124 
2125           /* Change RTC state */
2126           hrtc->State = HAL_RTC_STATE_TIMEOUT;
2127         }
2128         else
2129         {
2130           break;
2131         }
2132       }
2133     }
2134   }
2135 
2136   return status;
2137 }
2138 
2139 /**
2140   * @brief  Exit the RTC Initialization mode.
2141   * @param  hrtc RTC handle
2142   * @retval HAL status
2143   */
RTC_ExitInitMode(RTC_HandleTypeDef * hrtc)2144 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
2145 {
2146   HAL_StatusTypeDef status = HAL_OK;
2147 
2148   /* Exit Initialization mode */
2149   CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
2150 
2151   /* If CR_BYPSHAD bit = 0, wait for synchro */
2152   if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
2153   {
2154     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2155     {
2156       /* Change RTC state */
2157       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2158       status = HAL_TIMEOUT;
2159     }
2160   }
2161   else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry. */
2162   {
2163     /* Clear BYPSHAD bit */
2164     CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
2165     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2166     {
2167       /* Change RTC state */
2168       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2169       status = HAL_TIMEOUT;
2170     }
2171     /* Restore BYPSHAD bit */
2172     SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
2173   }
2174   return status;
2175 }
2176 
2177 /**
2178   * @brief  Convert a 2 digit decimal to BCD format.
2179   * @param  Value Byte to be converted
2180   * @retval Converted byte
2181   */
RTC_ByteToBcd2(uint8_t Value)2182 uint8_t RTC_ByteToBcd2(uint8_t Value)
2183 {
2184   uint32_t bcd_high = 0U;
2185   uint8_t tmp_value = Value;
2186 
2187   while (tmp_value >= 10U)
2188   {
2189     bcd_high++;
2190     tmp_value -= 10U;
2191   }
2192 
2193   return ((uint8_t)(bcd_high << 4U) | tmp_value);
2194 }
2195 
2196 /**
2197   * @brief  Convert from 2 digit BCD to Binary.
2198   * @param  Value BCD value to be converted
2199   * @retval Converted word
2200   */
RTC_Bcd2ToByte(uint8_t Value)2201 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2202 {
2203   uint32_t tmp;
2204 
2205   tmp = (((uint32_t)Value & 0xF0U) >> 4) * 10U;
2206 
2207   return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
2208 }
2209 
2210 /**
2211   * @}
2212   */
2213 
2214 #endif /* HAL_RTC_MODULE_ENABLED */
2215 /**
2216   * @}
2217   */
2218 
2219 /**
2220   * @}
2221   */
2222 
2223