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