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