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