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