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