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