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) 2022 STMicroelectronics.
22   * All rights reserved.
23   *
24   * This software is licensed under terms that can be found in the LICENSE file
25   * in the root directory of this software component.
26   * If no LICENSE file comes with this software, it is provided AS-IS.
27   *
28   ******************************************************************************
29   @verbatim
30  ===============================================================================
31                           ##### RTC Operating Condition #####
32  ===============================================================================
33   [..] The real-time clock (RTC) and the RTC backup registers can be powered
34        from the VBAT voltage when the main VDD supply is powered off.
35        To retain the content of the RTC backup registers and supply the RTC
36        when VDD is turned off, VBAT pin can be connected to an optional
37        standby voltage supplied by a battery or by another source.
38 
39                    ##### Backup Domain Reset #####
40  ===============================================================================
41   [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
42        to their reset values.
43        A backup domain reset is generated when one of the following events occurs:
44     (#) Software reset, triggered by setting the BDRST bit in the
45         RCC Backup domain control register (RCC_BDCR).
46     (#) VDD or VBAT power on, if both supplies have previously been powered off.
47     (#) Tamper detection event resets all data backup registers.
48 
49                    ##### Backup Domain Access #####
50   ==================================================================
51   [..] After reset, the backup domain (RTC registers and RTC backup data registers)
52        is protected against possible unwanted write accesses.
53   [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
54     (+) Enable 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 @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
107 
108   Function @ref 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 @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
141   weak function.
142   @ref 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 @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
175   all callbacks are set to the corresponding weak functions :
176   examples @ref AlarmAEventCallback(), @ref TimeStampEventCallback().
177   Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
178   in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
179   (not registered beforehand).
180   If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref 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 @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
189   or @ref 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(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)1133 HAL_StatusTypeDef HAL_RTC_GetTime(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(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1269 HAL_StatusTypeDef HAL_RTC_GetDate(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(RTC_HandleTypeDef * hrtc)1305 void HAL_RTC_DST_Add1Hour(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(RTC_HandleTypeDef * hrtc)1326 void HAL_RTC_DST_Sub1Hour(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(RTC_HandleTypeDef * hrtc)1347 void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
1348 {
1349   /* Prevent unused argument(s) compilation warning */
1350   UNUSED(hrtc);
1351 
1352   /* Disable the write protection for RTC registers */
1353   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1354 
1355   /* Set RTC_CR_BKP Bit */
1356   SET_BIT(RTC->CR, RTC_CR_BKP);
1357 
1358   /* Enable the write protection for RTC registers */
1359   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1360 }
1361 
1362 /**
1363   * @brief  Daylight Saving Time, Clear the store operation bit.
1364   * @param  hrtc RTC handle
1365   * @retval None
1366   */
HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef * hrtc)1367 void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
1368 {
1369   /* Prevent unused argument(s) compilation warning */
1370   UNUSED(hrtc);
1371 
1372   /* Disable the write protection for RTC registers */
1373   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1374 
1375   /* Clear RTC_CR_BKP Bit */
1376   CLEAR_BIT(RTC->CR, RTC_CR_BKP);
1377 
1378   /* Enable the write protection for RTC registers */
1379   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1380 }
1381 
1382 /**
1383   * @brief  Daylight Saving Time, Read the store operation bit.
1384   * @param  hrtc RTC handle
1385   * @retval operation see RTC_StoreOperation_Definitions
1386   */
HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef * hrtc)1387 uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
1388 {
1389   /* Prevent unused argument(s) compilation warning */
1390   UNUSED(hrtc);
1391 
1392   /* Get RTC_CR_BKP Bit */
1393   return READ_BIT(RTC->CR, RTC_CR_BKP);
1394 }
1395 
1396 
1397 /**
1398   * @}
1399   */
1400 
1401 /** @addtogroup RTC_Exported_Functions_Group3
1402   *  @brief   RTC Alarm functions
1403   *
1404 @verbatim
1405  ===============================================================================
1406                  ##### RTC Alarm functions #####
1407  ===============================================================================
1408 
1409  [..] This section provides functions allowing to configure Alarm feature
1410 
1411 @endverbatim
1412   * @{
1413   */
1414 /**
1415   * @brief  Set the specified RTC Alarm.
1416   * @param  hrtc RTC handle
1417   * @param  sAlarm Pointer to Alarm structure
1418   *          if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1419   *             sAlarm->AlarmTime.SubSeconds
1420   *             sAlarm->AlarmSubSecondMask
1421   *             sAlarm->BinaryAutoClr
1422   * @param  Format of the entered parameters.
1423   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1424   *          else this parameter can be one of the following values
1425   *             @arg RTC_FORMAT_BIN: Binary format
1426   *             @arg RTC_FORMAT_BCD: BCD format
1427   * @retval HAL status
1428   */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1429 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1430 {
1431   uint32_t tmpreg = 0;
1432   uint32_t binaryMode;
1433 
1434   /* Process Locked */
1435   __HAL_LOCK(hrtc);
1436 
1437   /* Change RTC state */
1438   hrtc->State = HAL_RTC_STATE_BUSY;
1439 
1440 #ifdef  USE_FULL_ASSERT
1441   /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration) */
1442   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1443   {
1444     assert_param(IS_RTC_FORMAT(Format));
1445     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1446     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1447     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1448     assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1449     assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1450   }
1451   else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1452   {
1453     assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1454     assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1455   }
1456   else /* RTC_BINARY_MIX */
1457   {
1458     assert_param(IS_RTC_FORMAT(Format));
1459     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1460     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1461     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1462     /* In Binary Mix Mode, the RTC can not generate an alarm on a match
1463        involving all calendar items + the upper SSR bits */
1464     assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <=
1465                  (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1466   }
1467 #endif /* USE_FULL_ASSERT */
1468 
1469   /* Get Binary mode (32-bit free-running counter configuration) */
1470   binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1471 
1472   if (binaryMode != RTC_BINARY_ONLY)
1473   {
1474     if (Format == RTC_FORMAT_BIN)
1475     {
1476       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1477       {
1478         assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1479         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1480       }
1481       else
1482       {
1483         sAlarm->AlarmTime.TimeFormat = 0x00U;
1484         assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1485       }
1486       assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1487       assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1488 
1489       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1490       {
1491         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1492       }
1493       else
1494       {
1495         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1496       }
1497       tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1498                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1499                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1500                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1501                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1502                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1503                 ((uint32_t)sAlarm->AlarmMask));
1504     }
1505     else /* format BCD */
1506     {
1507       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1508       {
1509         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1510         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1511       }
1512       else
1513       {
1514         sAlarm->AlarmTime.TimeFormat = 0x00U;
1515         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1516       }
1517 
1518       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1519       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1520 
1521 #ifdef  USE_FULL_ASSERT
1522       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1523       {
1524         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1525       }
1526       else
1527       {
1528         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1529       }
1530 
1531 #endif /* USE_FULL_ASSERT */
1532       tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1533                 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1534                 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1535                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1536                 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1537                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1538                 ((uint32_t)sAlarm->AlarmMask));
1539     }
1540   }
1541 
1542   /* Disable the write protection for RTC registers */
1543   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1544 
1545   /* Configure the Alarm register */
1546   if (sAlarm->Alarm == RTC_ALARM_A)
1547   {
1548     /* Disable the Alarm A interrupt */
1549 
1550     /* In case of interrupt mode is used, the interrupt source must disabled */
1551     CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
1552 
1553     /* Clear flag alarm A */
1554     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1555 
1556     if (binaryMode == RTC_BINARY_ONLY)
1557     {
1558       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1559     }
1560     else
1561     {
1562       WRITE_REG(RTC->ALRMAR, tmpreg);
1563       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1564     }
1565 
1566     WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1567 
1568     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1569     {
1570       /* Configure the  Alarm A output clear */
1571       SET_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1572     }
1573     else
1574     {
1575       /* Disable the  Alarm A  output clear */
1576       CLEAR_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1577     }
1578     /* Configure the Alarm state: Enable Alarm */
1579     SET_BIT(RTC->CR, RTC_CR_ALRAE);
1580   }
1581   else
1582   {
1583     /* Disable the Alarm B interrupt */
1584 
1585     /* In case of interrupt mode is used, the interrupt source must disabled */
1586     CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
1587 
1588     /* Clear flag alarm B */
1589     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1590 
1591     if (binaryMode == RTC_BINARY_ONLY)
1592     {
1593       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1594     }
1595     else
1596     {
1597       WRITE_REG(RTC->ALRMBR, tmpreg);
1598 
1599       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1600     }
1601 
1602     WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1603 
1604     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1605     {
1606       /* Configure the  Alarm B output clear */
1607       SET_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1608     }
1609     else
1610     {
1611       /* Disable the  Alarm B output clear */
1612       CLEAR_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1613     }
1614 
1615     /* Configure the Alarm state: Enable Alarm */
1616     SET_BIT(RTC->CR, RTC_CR_ALRBE);
1617   }
1618 
1619   /* Enable the write protection for RTC registers */
1620   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1621 
1622   /* Change RTC state */
1623   hrtc->State = HAL_RTC_STATE_READY;
1624 
1625   /* Process Unlocked */
1626   __HAL_UNLOCK(hrtc);
1627 
1628   return HAL_OK;
1629 }
1630 
1631 /**
1632   * @brief  Set the specified RTC Alarm with Interrupt.
1633   * @param  hrtc RTC handle
1634   * @param  sAlarm Pointer to Alarm structure
1635   *          if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1636   *             sAlarm->AlarmTime.SubSeconds
1637   *             sAlarm->AlarmSubSecondMask
1638   *             sAlarm->BinaryAutoClr
1639   * @param  Format Specifies the format of the entered parameters.
1640   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1641   *          else this parameter can be one of the following values
1642   *             @arg RTC_FORMAT_BIN: Binary format
1643   *             @arg RTC_FORMAT_BCD: BCD format
1644   * @retval HAL status
1645   */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1646 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1647 {
1648   uint32_t tmpreg = 0;
1649   uint32_t binaryMode;
1650 
1651   /* Process Locked */
1652   __HAL_LOCK(hrtc);
1653 
1654   /* Change RTC state */
1655   hrtc->State = HAL_RTC_STATE_BUSY;
1656 
1657 #ifdef  USE_FULL_ASSERT
1658   /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration) */
1659   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1660   {
1661     assert_param(IS_RTC_FORMAT(Format));
1662     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1663     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1664     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1665     assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1666     assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1667   }
1668   else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1669   {
1670     assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1671     assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1672   }
1673   else /* RTC_BINARY_MIX */
1674   {
1675     assert_param(IS_RTC_FORMAT(Format));
1676     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1677     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1678     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1679     /* In Binary Mix Mode, the RTC can not generate an alarm on a match
1680       involving all calendar items + the upper SSR bits */
1681     assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <=
1682                  (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1683   }
1684 #endif /* USE_FULL_ASSERT */
1685 
1686   /* Get Binary mode (32-bit free-running counter configuration) */
1687   binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1688 
1689   if (binaryMode != RTC_BINARY_ONLY)
1690   {
1691     if (Format == RTC_FORMAT_BIN)
1692     {
1693       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1694       {
1695         assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1696         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1697       }
1698       else
1699       {
1700         sAlarm->AlarmTime.TimeFormat = 0x00U;
1701         assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1702       }
1703       assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1704       assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1705 
1706       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1707       {
1708         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1709       }
1710       else
1711       {
1712         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1713       }
1714       tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1715                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1716                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1717                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1718                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1719                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1720                 ((uint32_t)sAlarm->AlarmMask));
1721     }
1722     else /* Format BCD */
1723     {
1724       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1725       {
1726         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1727         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1728       }
1729       else
1730       {
1731         sAlarm->AlarmTime.TimeFormat = 0x00U;
1732         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1733       }
1734 
1735       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1736       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1737 
1738 #ifdef  USE_FULL_ASSERT
1739       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1740       {
1741         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1742       }
1743       else
1744       {
1745         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1746       }
1747 
1748 #endif /* USE_FULL_ASSERT */
1749       tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1750                 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1751                 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1752                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1753                 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1754                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1755                 ((uint32_t)sAlarm->AlarmMask));
1756 
1757     }
1758   }
1759 
1760   /* Disable the write protection for RTC registers */
1761   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1762 
1763   /* Configure the Alarm registers */
1764   if (sAlarm->Alarm == RTC_ALARM_A)
1765   {
1766     /* Disable the Alarm A interrupt */
1767     CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1768 
1769     /* Clear flag alarm A */
1770     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1771 
1772     if (binaryMode == RTC_BINARY_ONLY)
1773     {
1774       RTC->ALRMASSR = sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr;
1775     }
1776     else
1777     {
1778       WRITE_REG(RTC->ALRMAR, tmpreg);
1779 
1780       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1781     }
1782 
1783     WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1784 
1785     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1786     {
1787       /* Configure the  Alarm A output clear */
1788       SET_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1789     }
1790     else
1791     {
1792       /* Disable the Alarm A output clear */
1793       CLEAR_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1794     }
1795 
1796     /* Configure the Alarm interrupt */
1797     SET_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1798   }
1799   else
1800   {
1801     /* Disable the Alarm B interrupt */
1802     CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1803 
1804     /* Clear flag alarm B */
1805     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1806 
1807     if (binaryMode == RTC_BINARY_ONLY)
1808     {
1809       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1810     }
1811     else
1812     {
1813       WRITE_REG(RTC->ALRMBR, tmpreg);
1814 
1815       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1816     }
1817 
1818     WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1819 
1820     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1821     {
1822       /* Configure the  Alarm B Output clear */
1823       SET_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1824     }
1825     else
1826     {
1827       /* Disable the  Alarm B Output clear */
1828       CLEAR_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1829     }
1830 
1831     /* Configure the Alarm interrupt */
1832     SET_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1833   }
1834 
1835   /* Enable the write protection for RTC registers */
1836   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1837 
1838   /* Change RTC state */
1839   hrtc->State = HAL_RTC_STATE_READY;
1840 
1841   /* Process Unlocked */
1842   __HAL_UNLOCK(hrtc);
1843 
1844   return HAL_OK;
1845 }
1846 
1847 /**
1848   * @brief  Deactivate the specified RTC Alarm.
1849   * @param  hrtc RTC handle
1850   * @param  Alarm Specifies the Alarm.
1851   *          This parameter can be one of the following values:
1852   *            @arg RTC_ALARM_A:  AlarmA
1853   *            @arg RTC_ALARM_B:  AlarmB
1854   * @retval HAL status
1855   */
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef * hrtc,uint32_t Alarm)1856 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1857 {
1858   /* Check the parameters */
1859   assert_param(IS_RTC_ALARM(Alarm));
1860 
1861   /* Process Locked */
1862   __HAL_LOCK(hrtc);
1863 
1864   /* Change RTC state */
1865   hrtc->State = HAL_RTC_STATE_BUSY;
1866 
1867   /* Disable the write protection for RTC registers */
1868   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1869 
1870   /* In case of interrupt mode is used, the interrupt source must disabled */
1871   if (Alarm == RTC_ALARM_A)
1872   {
1873     CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1874 
1875     /* AlarmA, Clear SSCLR */
1876     CLEAR_BIT(RTC->ALRMASSR, RTC_ALRMASSR_SSCLR);
1877   }
1878   else
1879   {
1880     CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1881 
1882     /* AlarmB, Clear SSCLR */
1883     CLEAR_BIT(RTC->ALRMBSSR, RTC_ALRMBSSR_SSCLR);
1884   }
1885 
1886   /* Enable the write protection for RTC registers */
1887   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1888 
1889   /* Change RTC state */
1890   hrtc->State = HAL_RTC_STATE_READY;
1891 
1892   /* Process Unlocked */
1893   __HAL_UNLOCK(hrtc);
1894 
1895   return HAL_OK;
1896 }
1897 
1898 /**
1899   * @brief  Get the RTC Alarm value and masks.
1900   * @param  hrtc RTC handle
1901   * @param  sAlarm Pointer to Date structure
1902   * @param  Alarm Specifies the Alarm.
1903   *          This parameter can be one of the following values:
1904   *             @arg RTC_ALARM_A: AlarmA
1905   *             @arg RTC_ALARM_B: AlarmB
1906   * @param  Format Specifies the format of the entered parameters.
1907   *          This parameter can be one of the following values:
1908   *             @arg RTC_FORMAT_BIN: Binary format
1909   *             @arg RTC_FORMAT_BCD: BCD format
1910   * @retval HAL status
1911   */
HAL_RTC_GetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Alarm,uint32_t Format)1912 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1913 {
1914   uint32_t tmpreg;
1915   uint32_t subsecondtmpreg;
1916 
1917   /* Prevent unused argument(s) compilation warning */
1918   UNUSED(hrtc);
1919 
1920   /* Check the parameters */
1921   assert_param(IS_RTC_FORMAT(Format));
1922   assert_param(IS_RTC_ALARM(Alarm));
1923 
1924   if (Alarm == RTC_ALARM_A)
1925   {
1926     /* AlarmA */
1927     sAlarm->Alarm = RTC_ALARM_A;
1928 
1929     tmpreg = READ_REG(RTC->ALRMAR);
1930     subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1931 
1932     /* Fill the structure with the read parameters */
1933     sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1934     sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1935     sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1936     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1937     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1938     sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1939     sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1940     sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1941   }
1942   else
1943   {
1944     sAlarm->Alarm = RTC_ALARM_B;
1945 
1946     tmpreg = READ_REG(RTC->ALRMBR);
1947     subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1948 
1949     /* Fill the structure with the read parameters */
1950     sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1951     sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1952     sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1953     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1954     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1955     sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1956     sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1957     sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1958   }
1959 
1960   if (Format == RTC_FORMAT_BIN)
1961   {
1962     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1963     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1964     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1965     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1966   }
1967 
1968   return HAL_OK;
1969 }
1970 
1971 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1972 /**
1973   * @brief  Handle Alarm secure interrupt request.
1974   * @param  hrtc RTC handle
1975   * @retval None
1976   */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)1977 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1978 {
1979   /* Get interrupt status */
1980   uint32_t tmp = READ_REG(RTC->SMISR);
1981 
1982   if ((tmp & RTC_SMISR_ALRAMF) != 0u)
1983   {
1984     /* Clear the AlarmA interrupt pending bit */
1985     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1986 
1987 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1988     /* Call Compare Match registered Callback */
1989     hrtc->AlarmAEventCallback(hrtc);
1990 #else
1991     HAL_RTC_AlarmAEventCallback(hrtc);
1992 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1993   }
1994 
1995   if ((tmp & RTC_SMISR_ALRBMF) != 0u)
1996   {
1997     /* Clear the AlarmB interrupt pending bit */
1998     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1999 
2000 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2001     /* Call Compare Match registered Callback */
2002     hrtc->AlarmBEventCallback(hrtc);
2003 #else
2004     HAL_RTCEx_AlarmBEventCallback(hrtc);
2005 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2006 
2007   }
2008 
2009   /* Change RTC state */
2010   hrtc->State = HAL_RTC_STATE_READY;
2011 }
2012 
2013 #else /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2014 
2015 /**
2016   * @brief  Handle Alarm non-secure interrupt request.
2017   * @note   Alarm non-secure is available in non-secure driver.
2018   * @param  hrtc RTC handle
2019   * @retval None
2020   */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)2021 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
2022 {
2023   /* Get interrupt status */
2024   uint32_t tmp = READ_REG(RTC->MISR);
2025 
2026   if ((tmp & RTC_MISR_ALRAMF) != 0U)
2027   {
2028     /* Clear the AlarmA interrupt pending bit */
2029     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
2030 
2031 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2032     /* Call Compare Match registered Callback */
2033     hrtc->AlarmAEventCallback(hrtc);
2034 #else
2035     HAL_RTC_AlarmAEventCallback(hrtc);
2036 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2037   }
2038 
2039   if ((tmp & RTC_MISR_ALRBMF) != 0U)
2040   {
2041     /* Clear the AlarmB interrupt pending bit */
2042     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
2043 
2044 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2045     /* Call Compare Match registered Callback */
2046     hrtc->AlarmBEventCallback(hrtc);
2047 #else
2048     HAL_RTCEx_AlarmBEventCallback(hrtc);
2049 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2050   }
2051 
2052   /* Change RTC state */
2053   hrtc->State = HAL_RTC_STATE_READY;
2054 }
2055 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2056 
2057 /**
2058   * @brief  Alarm A secure callback.
2059   * @param  hrtc RTC handle
2060   * @retval None
2061   */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)2062 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
2063 {
2064   /* Prevent unused argument(s) compilation warning */
2065   UNUSED(hrtc);
2066 
2067   /* NOTE : This function should not be modified, when the secure callback is needed,
2068             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
2069    */
2070 }
2071 
2072 /**
2073   * @brief  Handle Alarm A Polling request.
2074   * @param  hrtc RTC handle
2075   * @param  Timeout Timeout duration
2076   * @retval HAL status
2077   */
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)2078 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2079 {
2080   uint32_t tickstart = HAL_GetTick();
2081 
2082   while (READ_BIT(RTC->SR, RTC_SR_ALRAF) == 0U)
2083   {
2084     if (Timeout != HAL_MAX_DELAY)
2085     {
2086       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2087       {
2088         /* Change RTC state */
2089         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2090         return HAL_TIMEOUT;
2091       }
2092     }
2093   }
2094 
2095   /* Clear the Alarm interrupt pending bit */
2096   WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
2097 
2098   /* Change RTC state */
2099   hrtc->State = HAL_RTC_STATE_READY;
2100 
2101   return HAL_OK;
2102 }
2103 
2104 /**
2105   * @}
2106   */
2107 
2108 /** @addtogroup RTC_Exported_Functions_Group4
2109   *  @brief   Peripheral Control functions
2110   *
2111 @verbatim
2112  ===============================================================================
2113                      ##### Peripheral Control functions #####
2114  ===============================================================================
2115     [..]
2116     This subsection provides functions allowing to
2117       (+) Wait for RTC Time and Date Synchronization
2118 
2119 @endverbatim
2120   * @{
2121   */
2122 
2123 /**
2124   * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
2125   *         synchronized with RTC APB clock.
2126   * @note   The RTC Resynchronization mode is write protected, use the
2127   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2128   * @note   To read the calendar through the shadow registers after Calendar
2129   *         initialization, calendar update or after wakeup from low power modes
2130   *         the software must first clear the RSF flag.
2131   *         The software must then wait until it is set again before reading
2132   *         the calendar, which means that the calendar registers have been
2133   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
2134   * @param  hrtc RTC handle
2135   * @retval HAL status
2136   */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)2137 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
2138 {
2139   uint32_t tickstart;
2140 
2141   /* Prevent unused argument(s) compilation warning */
2142   UNUSED(hrtc);
2143 
2144   /* Clear RSF flag */
2145   CLEAR_BIT(RTC->ICSR, RTC_ICSR_RSF);
2146 
2147   tickstart = HAL_GetTick();
2148 
2149   /* Wait the registers to be synchronised */
2150   while (READ_BIT(RTC->ICSR, RTC_ICSR_RSF) == 0U)
2151   {
2152     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2153     {
2154       return HAL_TIMEOUT;
2155     }
2156   }
2157 
2158   return HAL_OK;
2159 }
2160 
2161 /**
2162   * @}
2163   */
2164 
2165 /** @addtogroup RTC_Exported_Functions_Group5
2166   *  @brief   Peripheral State functions
2167   *
2168 @verbatim
2169  ===============================================================================
2170                      ##### Peripheral State functions #####
2171  ===============================================================================
2172     [..]
2173     This subsection provides functions allowing to
2174       (+) Get RTC state
2175 
2176 @endverbatim
2177   * @{
2178   */
2179 /**
2180   * @brief  Return the RTC handle state.
2181   * @param  hrtc RTC handle
2182   * @retval HAL state
2183   */
HAL_RTC_GetState(RTC_HandleTypeDef * hrtc)2184 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
2185 {
2186   /* Return RTC handle state */
2187   return hrtc->State;
2188 }
2189 
2190 /**
2191   * @}
2192   */
2193 /**
2194   * @}
2195   */
2196 
2197 /** @addtogroup RTC_Private_Functions
2198   * @{
2199   */
2200 /**
2201   * @brief  Enter the RTC Initialization mode.
2202   * @note   The RTC Initialization mode is write protected, use the
2203   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2204   * @param  hrtc RTC handle
2205   * @retval HAL status
2206   */
RTC_EnterInitMode(RTC_HandleTypeDef * hrtc)2207 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
2208 {
2209   uint32_t tickstart;
2210   HAL_StatusTypeDef status = HAL_OK;
2211 
2212   /* Check if the Initialization mode is set */
2213   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
2214   {
2215     /* Set the Initialization mode */
2216     SET_BIT(RTC->ICSR, RTC_ICSR_INIT);
2217 
2218     tickstart = HAL_GetTick();
2219     /* Wait till RTC is in INIT state and if Time out is reached exit */
2220     while ((READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
2221     {
2222       if ((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
2223       {
2224         status = HAL_TIMEOUT;
2225 
2226         /* Change RTC state */
2227         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2228       }
2229     }
2230   }
2231 
2232   return status;
2233 }
2234 
2235 /**
2236   * @brief  Exit the RTC Initialization mode.
2237   * @param  hrtc RTC handle
2238   * @retval HAL status
2239   */
RTC_ExitInitMode(RTC_HandleTypeDef * hrtc)2240 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
2241 {
2242   HAL_StatusTypeDef status = HAL_OK;
2243 
2244   /* Exit Initialization mode */
2245   CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
2246 
2247   /* If CR_BYPSHAD bit = 0, wait for synchro */
2248   if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
2249   {
2250     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2251     {
2252       /* Change RTC state */
2253       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2254       status = HAL_TIMEOUT;
2255     }
2256   }
2257   else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry. */
2258   {
2259     /* Clear BYPSHAD bit */
2260     CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
2261     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2262     {
2263       /* Change RTC state */
2264       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2265       status = HAL_TIMEOUT;
2266     }
2267     /* Restore BYPSHAD bit */
2268     SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
2269   }
2270   return status;
2271 }
2272 
2273 /**
2274   * @brief  Convert a 2 digit decimal to BCD format.
2275   * @param  Value Byte to be converted
2276   * @retval Converted byte
2277   */
RTC_ByteToBcd2(uint8_t Value)2278 uint8_t RTC_ByteToBcd2(uint8_t Value)
2279 {
2280   uint32_t bcd_high = 0U;
2281   uint8_t tmp_value = Value;
2282 
2283   while (tmp_value >= 10U)
2284   {
2285     bcd_high++;
2286     tmp_value -= 10U;
2287   }
2288 
2289   return ((uint8_t)(bcd_high << 4U) | tmp_value);
2290 }
2291 
2292 /**
2293   * @brief  Convert from 2 digit BCD to Binary.
2294   * @param  Value BCD value to be converted
2295   * @retval Converted word
2296   */
RTC_Bcd2ToByte(uint8_t Value)2297 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2298 {
2299   uint32_t tmp;
2300 
2301   tmp = (((uint32_t)Value & 0xF0U) >> 4) * 10U;
2302 
2303   return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
2304 }
2305 
2306 /**
2307   * @}
2308   */
2309 
2310 #endif /* HAL_RTC_MODULE_ENABLED */
2311 /**
2312   * @}
2313   */
2314 
2315 /**
2316   * @}
2317   */
2318