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