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