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