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