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