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