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