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