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