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