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