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