1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_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) 2022 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   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
106   not defined, the callback registration feature is not available and all callbacks
107   are set to the corresponding weak functions. This is the recommended configuration
108   in order to optimize memory/code consumption footprint/performances.
109 
110   The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
111   allows the user to configure dynamically the driver callbacks.
112   Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
113 
114   Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
115     (+) AlarmAEventCallback             : RTC Alarm A Event callback.
116     (+) AlarmBEventCallback             : RTC Alarm B Event callback.
117     (+) TimeStampEventCallback          : RTC TimeStamp Event callback.
118     (+) WakeUpTimerEventCallback        : RTC WakeUpTimer Event callback.
119     (+) SSRUEventCallback               : RTC SSRU Event callback.
120     (+) Tamper1EventCallback            : RTC Tamper 1 Event callback.
121     (+) Tamper2EventCallback            : RTC Tamper 2 Event callback.
122     (+) Tamper3EventCallback            : RTC Tamper 3 Event callback.
123     (+) Tamper4EventCallback            : RTC Tamper 4 Event callback.
124     (+) Tamper5EventCallback            : RTC Tamper 5 Event callback.
125     (+) Tamper6EventCallback            : RTC Tamper 6 Event callback.
126     (+) Tamper7EventCallback            : RTC Tamper 7 Event callback.
127     (+) Tamper8EventCallback            : RTC Tamper 8 Event callback.
128     (+) InternalTamper1EventCallback    : RTC InternalTamper 1 Event callback.
129     (+) InternalTamper2EventCallback    : RTC InternalTamper 2 Event callback.
130     (+) InternalTamper3EventCallback    : RTC InternalTamper 3 Event callback.
131     (+) InternalTamper4EventCallback    : RTC InternalTamper 4 Event callback.
132     (+) InternalTamper5EventCallback    : RTC InternalTamper 5 Event callback.
133     (+) InternalTamper6EventCallback    : RTC InternalTamper 6 Event callback.
134     (+) InternalTamper7EventCallback    : RTC InternalTamper 7 Event callback.
135     (+) InternalTamper8EventCallback    : RTC InternalTamper 8 Event callback.
136     (+) InternalTamper9EventCallback    : RTC InternalTamper 9 Event callback.
137     (+) InternalTamper11EventCallback   : RTC InternalTamper 11 Event callback.
138     (+) InternalTamper12EventCallback   : RTC InternalTamper 12 Event callback.
139     (+) InternalTamper13EventCallback   : RTC InternalTamper 13 Event callback.
140     (+) InternalTamper15EventCallback   : RTC InternalTamper 15 Event callback.
141     (+) MspInitCallback                 : RTC MspInit callback.
142     (+) MspDeInitCallback               : RTC MspDeInit callback.
143   This function takes as parameters the HAL peripheral handle, the Callback ID
144   and a pointer to the user callback function.
145 
146   Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
147   weak function.
148   @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
149   and the Callback ID.
150   This function allows to reset following callbacks:
151     (+) AlarmAEventCallback             : RTC Alarm A Event callback.
152     (+) AlarmBEventCallback             : RTC Alarm B Event callback.
153     (+) TimeStampEventCallback          : RTC TimeStamp Event callback.
154     (+) WakeUpTimerEventCallback        : RTC WakeUpTimer Event callback.
155     (+) SSRUEventCallback               : RTC SSRU Event callback.
156     (+) Tamper1EventCallback            : RTC Tamper 1 Event callback.
157     (+) Tamper2EventCallback            : RTC Tamper 2 Event callback.
158     (+) Tamper3EventCallback            : RTC Tamper 3 Event callback.
159     (+) Tamper4EventCallback            : RTC Tamper 4 Event callback.
160     (+) Tamper5EventCallback            : RTC Tamper 5 Event callback.
161     (+) Tamper6EventCallback            : RTC Tamper 6 Event callback.
162     (+) Tamper7EventCallback            : RTC Tamper 7 Event callback.
163     (+) Tamper8EventCallback            : RTC Tamper 8 Event callback.
164     (+) InternalTamper1EventCallback    : RTC InternalTamper 1 Event callback.
165     (+) InternalTamper2EventCallback    : RTC InternalTamper 2 Event callback.
166     (+) InternalTamper3EventCallback    : RTC InternalTamper 3 Event callback.
167     (+) InternalTamper4EventCallback    : RTC InternalTamper 4 Event callback.
168     (+) InternalTamper5EventCallback    : RTC InternalTamper 5 Event callback.
169     (+) InternalTamper6EventCallback    : RTC InternalTamper 6 Event callback.
170     (+) InternalTamper7EventCallback    : RTC InternalTamper 7 Event callback.
171     (+) InternalTamper8EventCallback    : RTC InternalTamper 8 Event callback.
172     (+) InternalTamper9EventCallback    : RTC InternalTamper 9 Event callback.
173     (+) InternalTamper11EventCallback   : RTC InternalTamper 11 Event callback.
174     (+) InternalTamper12EventCallback   : RTC InternalTamper 12 Event callback.
175     (+) InternalTamper13EventCallback   : RTC InternalTamper 13 Event callback.
176     (+) InternalTamper15EventCallback   : RTC InternalTamper 15 Event callback.
177     (+) MspInitCallback                 : RTC MspInit callback.
178     (+) MspDeInitCallback               : RTC MspDeInit callback.
179 
180   By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
181   all callbacks are set to the corresponding weak functions :
182   examples @ref AlarmAEventCallback(), @ref TimeStampEventCallback().
183   Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
184   in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
185   (not registered beforehand).
186   If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
187   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
188 
189   Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
190   Exception done MspInit/MspDeInit that can be registered/unregistered
191   in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
192   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
193   In that case first register the MspInit/MspDeInit user callbacks
194   using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
195   or @ref HAL_RTC_Init() function.
196 
197   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
198   not defined, the callback registration feature is not available and all callbacks
199   are set to the corresponding weak functions.
200 
201   @endverbatim
202   */
203 
204 /* Includes ------------------------------------------------------------------*/
205 #include "stm32h5xx_hal.h"
206 
207 /** @addtogroup STM32H5xx_HAL_Driver
208   * @{
209   */
210 
211 
212 /** @addtogroup RTC
213   * @brief RTC HAL module driver
214   * @{
215   */
216 
217 #ifdef HAL_RTC_MODULE_ENABLED
218 
219 /* Private typedef -----------------------------------------------------------*/
220 /* Private define ------------------------------------------------------------*/
221 /* Private macro -------------------------------------------------------------*/
222 /* Private variables ---------------------------------------------------------*/
223 /* Private function prototypes -----------------------------------------------*/
224 /* Exported functions --------------------------------------------------------*/
225 
226 /** @addtogroup RTC_Exported_Functions
227   * @{
228   */
229 
230 /** @addtogroup RTC_Exported_Functions_Group1
231   *  @brief    Initialization and Configuration functions
232   *
233 @verbatim
234  ===============================================================================
235               ##### Initialization and de-initialization functions #####
236  ===============================================================================
237    [..] This section provides functions allowing to initialize and configure the
238          RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
239          RTC registers Write protection, enter and exit the RTC initialization mode,
240          RTC registers synchronization check and reference clock detection enable.
241          (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
242              It is split into 2 programmable prescalers to minimize power consumption.
243              (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
244              (++) When both prescalers are used, it is recommended to configure the
245                  asynchronous prescaler to a high value to minimize power consumption.
246          (#) All RTC registers are Write protected. Writing to the RTC registers
247              is enabled by writing a key into the Write Protection register, RTC_WPR.
248          (#) To configure the RTC Calendar, user application should enter
249              initialization mode. In this mode, the calendar counter is stopped
250              and its value can be updated. When the initialization sequence is
251              complete, the calendar restarts counting after 4 RTCCLK cycles.
252          (#) To read the calendar through the shadow registers after Calendar
253              initialization, calendar update or after wakeup from low power modes
254              the software must first clear the RSF flag. The software must then
255              wait until it is set again before reading the calendar, which means
256              that the calendar registers have been correctly copied into the
257              RTC_TR and RTC_DR shadow registers. The HAL_RTC_WaitForSynchro() function
258              implements the above software sequence (RSF clear and RSF check).
259 
260 @endverbatim
261   * @{
262   */
263 
264 /**
265   * @brief  Initialize the RTC peripheral
266   * @param  hrtc RTC handle
267   * @retval HAL status
268   */
HAL_RTC_Init(RTC_HandleTypeDef * hrtc)269 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
270 {
271   HAL_StatusTypeDef status = HAL_ERROR;
272 
273   /* Check the RTC peripheral state */
274   if (hrtc != NULL)
275   {
276     /* Check the parameters */
277     assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
278     assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
279     assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
280     assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
281 #if defined(RTC_CR_OSEL)
282     assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
283     assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
284     assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
285     assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
286 #endif /* RTC_CR_OSEL */
287 #if defined(RTC_CR_OUT2EN)
288     assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
289 #endif /* RTC_CR_OUT2EN */
290     assert_param(IS_RTC_BINARY_MODE(hrtc->Init.BinMode));
291     assert_param(IS_RTC_BINARY_MIX_BCDU(hrtc->Init.BinMixBcdU));
292 
293 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
294     if (hrtc->State == HAL_RTC_STATE_RESET)
295     {
296       /* Allocate lock resource and initialize it */
297       hrtc->Lock = HAL_UNLOCKED;
298 
299       /* Legacy weak AlarmAEventCallback */
300       hrtc->AlarmAEventCallback           = HAL_RTC_AlarmAEventCallback;
301       /* Legacy weak AlarmBEventCallback */
302       hrtc->AlarmBEventCallback           = HAL_RTCEx_AlarmBEventCallback;
303       /* Legacy weak TimeStampEventCallback */
304       hrtc->TimeStampEventCallback        = HAL_RTCEx_TimeStampEventCallback;
305       /* Legacy weak WakeUpTimerEventCallback */
306       hrtc->WakeUpTimerEventCallback      = HAL_RTCEx_WakeUpTimerEventCallback;
307       /* Legacy weak SSRUEventCallback */
308       hrtc->SSRUEventCallback             = HAL_RTCEx_SSRUEventCallback;
309       /* Legacy weak Tamper1EventCallback */
310       hrtc->Tamper1EventCallback          = HAL_RTCEx_Tamper1EventCallback;
311       /* Legacy weak Tamper2EventCallback */
312       hrtc->Tamper2EventCallback          = HAL_RTCEx_Tamper2EventCallback;
313       /* Legacy weak Tamper3EventCallback */
314       hrtc->Tamper3EventCallback          = HAL_RTCEx_Tamper3EventCallback;
315       /* Legacy weak Tamper4EventCallback */
316       hrtc->Tamper4EventCallback          = HAL_RTCEx_Tamper4EventCallback;
317       /* Legacy weak Tamper5EventCallback */
318       hrtc->Tamper5EventCallback          = HAL_RTCEx_Tamper5EventCallback;
319       /* Legacy weak Tamper6EventCallback */
320       hrtc->Tamper6EventCallback          = HAL_RTCEx_Tamper6EventCallback;
321       /* Legacy weak Tamper7EventCallback */
322       hrtc->Tamper7EventCallback          = HAL_RTCEx_Tamper7EventCallback;
323       /* Legacy weak Tamper8EventCallback */
324       hrtc->Tamper8EventCallback          = HAL_RTCEx_Tamper8EventCallback;
325       /* Legacy weak InternalTamper1EventCallback */
326       hrtc->InternalTamper1EventCallback  = HAL_RTCEx_InternalTamper1EventCallback;
327       /* Legacy weak InternalTamper2EventCallback */
328       hrtc->InternalTamper2EventCallback  = HAL_RTCEx_InternalTamper2EventCallback;
329       /* Legacy weak InternalTamper3EventCallback */
330       hrtc->InternalTamper3EventCallback  = HAL_RTCEx_InternalTamper3EventCallback;
331       /* Legacy weak InternalTamper4EventCallback */
332       hrtc->InternalTamper4EventCallback  = HAL_RTCEx_InternalTamper4EventCallback;
333       /* Legacy weak InternalTamper5EventCallback */
334       hrtc->InternalTamper5EventCallback  = HAL_RTCEx_InternalTamper5EventCallback;
335       /* Legacy weak InternalTamper6EventCallback */
336       hrtc->InternalTamper6EventCallback  = HAL_RTCEx_InternalTamper6EventCallback;
337       /* Legacy weak InternalTamper7EventCallback */
338       hrtc->InternalTamper7EventCallback  = HAL_RTCEx_InternalTamper7EventCallback;
339       /* Legacy weak InternalTamper8EventCallback */
340       hrtc->InternalTamper8EventCallback  = HAL_RTCEx_InternalTamper8EventCallback;
341       /* Legacy weak InternalTamper9EventCallback */
342       hrtc->InternalTamper9EventCallback  = HAL_RTCEx_InternalTamper9EventCallback;
343       /* Legacy weak InternalTamper11EventCallback */
344       hrtc->InternalTamper11EventCallback = HAL_RTCEx_InternalTamper11EventCallback;
345       /* Legacy weak InternalTamper12EventCallback */
346       hrtc->InternalTamper12EventCallback = HAL_RTCEx_InternalTamper12EventCallback;
347       /* Legacy weak InternalTamper13EventCallback */
348       hrtc->InternalTamper13EventCallback = HAL_RTCEx_InternalTamper13EventCallback;
349       /* Legacy weak InternalTamper15EventCallback */
350       hrtc->InternalTamper15EventCallback = HAL_RTCEx_InternalTamper15EventCallback;
351 
352       if (hrtc->MspInitCallback == NULL)
353       {
354         hrtc->MspInitCallback = HAL_RTC_MspInit;
355       }
356       /* Init the low level hardware */
357       hrtc->MspInitCallback(hrtc);
358 
359       if (hrtc->MspDeInitCallback == NULL)
360       {
361         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
362       }
363     }
364 #else
365     if (hrtc->State == HAL_RTC_STATE_RESET)
366     {
367       /* Allocate lock resource and initialize it */
368       hrtc->Lock = HAL_UNLOCKED;
369 
370       /* Initialize RTC MSP */
371       HAL_RTC_MspInit(hrtc);
372     }
373 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
374 
375     /* Set RTC state */
376     hrtc->State = HAL_RTC_STATE_BUSY;
377 
378     /* Check if the calendar has been not initialized */
379     if (__HAL_RTC_IS_CALENDAR_INITIALIZED(hrtc) == 0U)
380     {
381       /* Disable the write protection for RTC registers */
382       __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
383 
384       /* Enter Initialization mode */
385       status = RTC_EnterInitMode(hrtc);
386       if (status == HAL_OK)
387       {
388 #if defined(RTC_CR_OSEL)
389         /* Clear RTC_CR FMT, OSEL and POL Bits */
390         CLEAR_BIT(RTC->CR, (RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE));
391 
392         /* Set RTC_CR register */
393         SET_BIT(RTC->CR, (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity));
394 #else
395         /* Clear RTC_CR FMT Bits */
396         CLEAR_BIT(RTC->CR, RTC_CR_FMT);
397 
398         /* Set RTC_CR register */
399         SET_BIT(RTC->CR, hrtc->Init.HourFormat);
400 #endif /* RTC_CR_OSEL */
401 
402         /* Configure the RTC PRER */
403         WRITE_REG(RTC->PRER, ((hrtc->Init.SynchPrediv) | (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos)));
404 
405         /* Configure the Binary mode */
406         MODIFY_REG(RTC->ICSR, RTC_ICSR_BIN | RTC_ICSR_BCDU, hrtc->Init.BinMode | hrtc->Init.BinMixBcdU);
407 
408         /* Exit Initialization mode */
409         status = RTC_ExitInitMode(hrtc);
410 
411 #if defined(RTC_CR_OSEL)
412         if (status == HAL_OK)
413         {
414 #if defined(RTC_CR_OUT2EN)
415           MODIFY_REG(RTC->CR, \
416                      RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN, \
417                      hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
418 #else
419           MODIFY_REG(RTC->CR, \
420                      RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE, \
421                      hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType);
422 #endif /* RTC_CR_OUT2EN */
423         }
424 #endif /* RTC_CR_OSEL */
425       }
426 
427       /* Enable the write protection for RTC registers */
428       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
429 
430     }
431     else
432     {
433       /* Calendar is already initialized */
434       /* Set flag to OK */
435       status = HAL_OK;
436     }
437 
438     if (status == HAL_OK)
439     {
440       /* Change RTC state */
441       hrtc->State = HAL_RTC_STATE_READY;
442     }
443   }
444 
445   return status;
446 }
447 
448 /**
449   * @brief  DeInitialize the RTC peripheral.
450   * @note   This function does not reset the RTC Backup Data registers.
451   * @param  hrtc RTC handle
452   * @retval HAL status
453   */
HAL_RTC_DeInit(RTC_HandleTypeDef * hrtc)454 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
455 {
456   HAL_StatusTypeDef status;
457 
458   /* Set RTC state */
459   hrtc->State = HAL_RTC_STATE_BUSY;
460 
461   /* Disable the write protection for RTC registers */
462   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
463 
464   /* Enter Initialization mode */
465   status = RTC_EnterInitMode(hrtc);
466   if (status == HAL_OK)
467   {
468     /* Reset all RTC CR register bits */
469     CLEAR_REG(RTC->CR);
470     WRITE_REG(RTC->DR, (uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
471     CLEAR_REG(RTC->TR);
472     WRITE_REG(RTC->WUTR, RTC_WUTR_WUT);
473     WRITE_REG(RTC->PRER, ((uint32_t)(RTC_PRER_PREDIV_A | 0xFFU)));
474     CLEAR_REG(RTC->ALRMAR);
475     CLEAR_REG(RTC->ALRMBR);
476     CLEAR_REG(RTC->SHIFTR);
477     CLEAR_REG(RTC->CALR);
478     CLEAR_REG(RTC->ALRMASSR);
479     CLEAR_REG(RTC->ALRMBSSR);
480     CLEAR_BIT(RTC->ICSR, (RTC_ICSR_BCDU_Msk | RTC_ICSR_BIN_Msk));
481     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSOVF | RTC_SCR_CTSF | RTC_SCR_CWUTF | RTC_SCR_CALRBF | \
482               RTC_SCR_CALRAF);
483 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
484     CLEAR_REG(RTC->SECCFGR);
485 #endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
486 #if defined (RTC_PRIVCFGR_ALRAPRIV)
487     CLEAR_REG(RTC->PRIVCFGR);
488 #endif /* RTC_PRIVCFGR_ALRAPRIV */
489 
490     /* Exit initialization mode */
491     status = RTC_ExitInitMode(hrtc);
492     if (status == HAL_OK)
493     {
494       /* Reset TAMP registers */
495       CLEAR_REG(TAMP->CR1);
496       CLEAR_REG(TAMP->CR2);
497       CLEAR_REG(TAMP->CR3);
498       CLEAR_REG(TAMP->FLTCR);
499       WRITE_REG(TAMP->ATCR1, TAMP_ATCR1_ATCKSEL);
500       CLEAR_REG(TAMP->ATOR);
501       CLEAR_REG(TAMP->ATCR2);
502 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
503       CLEAR_REG(TAMP->SECCFGR);
504 #endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
505 #if defined (TAMP_PRIVCFGR_TAMPPRIV)
506       CLEAR_REG(TAMP->PRIVCFGR);
507 #endif /* TAMP_PRIVCFGR_TAMPPRIV */
508     }
509   }
510 
511   /* Enable the write protection for RTC registers */
512   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
513 
514   if (status == HAL_OK)
515   {
516 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
517     if (hrtc->MspDeInitCallback == NULL)
518     {
519       hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
520     }
521 
522     /* DeInit the low level hardware: CLOCK, NVIC.*/
523     hrtc->MspDeInitCallback(hrtc);
524 
525 #else
526     /* De-Initialize RTC MSP */
527     HAL_RTC_MspDeInit(hrtc);
528 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
529 
530     /* Change RTC state */
531     hrtc->State = HAL_RTC_STATE_RESET;
532   }
533 
534   /* Release Lock */
535   __HAL_UNLOCK(hrtc);
536 
537   return status;
538 }
539 
540 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
541 /**
542   * @brief  Register a User RTC Callback
543   *         To be used instead of the weak predefined callback
544   * @param  hrtc RTC handle
545   * @param  CallbackID ID of the callback to be registered
546   *         This parameter can be one of the following values:
547   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID            Alarm A Event Callback ID
548   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID            Alarm B Event Callback ID
549   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID          TimeStamp Event Callback ID
550   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID        WakeUp Timer Event Callback ID
551   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID            Tamper 1 Callback ID
552   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID            Tamper 2 Callback ID
553   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID            Tamper 3 Callback ID
554   *          @arg @ref HAL_RTC_TAMPER4_EVENT_CB_ID            Tamper 4 Callback ID
555   *          @arg @ref HAL_RTC_TAMPER5_EVENT_CB_ID            Tamper 5 Callback ID
556   *          @arg @ref HAL_RTC_TAMPER6_EVENT_CB_ID            Tamper 6 Callback ID
557   *          @arg @ref HAL_RTC_TAMPER7_EVENT_CB_ID            Tamper 7 Callback ID
558   *          @arg @ref HAL_RTC_TAMPER8_EVENT_CB_ID            Tamper 8 Callback ID
559   *          @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID   Internal Tamper 1 Callback ID
560   *          @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID   Internal Tamper 2 Callback ID
561   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID   Internal Tamper 3 Callback ID
562   *          @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID   Internal Tamper 4 Callback ID
563   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID   Internal Tamper 5 Callback ID
564   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID   Internal Tamper 6 Callback ID
565   *          @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID   Internal Tamper 7 Callback ID
566   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID   Internal Tamper 8 Callback ID
567   *          @arg @ref HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID   Internal Tamper 9 Callback ID
568   *          @arg @ref HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID  Internal Tamper 11 Callback ID
569   *          @arg @ref HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID  Internal Tamper 12 Callback ID
570   *          @arg @ref HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID  Internal Tamper 13 Callback ID
571   *          @arg @ref HAL_RTC_INTERNAL_TAMPER15_EVENT_CB_ID  Internal Tamper 15 Callback ID
572   *          @arg @ref HAL_RTC_MSPINIT_CB_ID                  Msp Init callback ID
573   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID                Msp DeInit callback ID
574   * @param  pCallback pointer to the Callback function
575   * @note  The HAL_RTC_RegisterCallback() may be called before HAL_RTC_Init() in HAL_RTC_STATE_RESET
576   *        to register callbacks for HAL_RTC_MSPINIT_CB_ID and HAL_RTC_MSPDEINIT_CB_ID.
577   * @retval HAL status
578   */
HAL_RTC_RegisterCallback(RTC_HandleTypeDef * hrtc,HAL_RTC_CallbackIDTypeDef CallbackID,pRTC_CallbackTypeDef pCallback)579 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID,
580                                            pRTC_CallbackTypeDef pCallback)
581 {
582   HAL_StatusTypeDef status = HAL_OK;
583 
584   if (pCallback == NULL)
585   {
586     return HAL_ERROR;
587   }
588 
589   if (HAL_RTC_STATE_READY == hrtc->State)
590   {
591     switch (CallbackID)
592     {
593       case HAL_RTC_ALARM_A_EVENT_CB_ID :
594         hrtc->AlarmAEventCallback = pCallback;
595         break;
596 
597       case HAL_RTC_ALARM_B_EVENT_CB_ID :
598         hrtc->AlarmBEventCallback = pCallback;
599         break;
600 
601       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
602         hrtc->TimeStampEventCallback = pCallback;
603         break;
604 
605       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
606         hrtc->WakeUpTimerEventCallback = pCallback;
607         break;
608 
609       case HAL_RTC_SSRU_EVENT_CB_ID :
610         hrtc->SSRUEventCallback = pCallback;
611         break;
612 
613       case HAL_RTC_TAMPER1_EVENT_CB_ID :
614         hrtc->Tamper1EventCallback = pCallback;
615         break;
616 
617       case HAL_RTC_TAMPER2_EVENT_CB_ID :
618         hrtc->Tamper2EventCallback = pCallback;
619         break;
620 
621       case HAL_RTC_TAMPER3_EVENT_CB_ID :
622         hrtc->Tamper3EventCallback = pCallback;
623         break;
624 
625       case HAL_RTC_TAMPER4_EVENT_CB_ID :
626         hrtc->Tamper4EventCallback = pCallback;
627         break;
628 
629       case HAL_RTC_TAMPER5_EVENT_CB_ID :
630         hrtc->Tamper5EventCallback = pCallback;
631         break;
632 
633       case HAL_RTC_TAMPER6_EVENT_CB_ID :
634         hrtc->Tamper6EventCallback = pCallback;
635         break;
636 
637       case HAL_RTC_TAMPER7_EVENT_CB_ID :
638         hrtc->Tamper7EventCallback = pCallback;
639         break;
640 
641       case HAL_RTC_TAMPER8_EVENT_CB_ID :
642         hrtc->Tamper8EventCallback = pCallback;
643         break;
644 
645       case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
646         hrtc->InternalTamper1EventCallback = pCallback;
647         break;
648 
649       case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
650         hrtc->InternalTamper2EventCallback = pCallback;
651         break;
652 
653       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
654         hrtc->InternalTamper3EventCallback = pCallback;
655         break;
656 
657       case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
658         hrtc->InternalTamper4EventCallback = pCallback;
659         break;
660 
661       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
662         hrtc->InternalTamper5EventCallback = pCallback;
663         break;
664 
665       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
666         hrtc->InternalTamper6EventCallback = pCallback;
667         break;
668 
669       case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
670         hrtc->InternalTamper7EventCallback = pCallback;
671         break;
672 
673       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
674         hrtc->InternalTamper8EventCallback = pCallback;
675         break;
676 
677       case HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID :
678         hrtc->InternalTamper9EventCallback = pCallback;
679         break;
680 
681       case HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID :
682         hrtc->InternalTamper11EventCallback = pCallback;
683         break;
684 
685       case HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID :
686         hrtc->InternalTamper12EventCallback = pCallback;
687         break;
688 
689       case HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID :
690         hrtc->InternalTamper13EventCallback = pCallback;
691         break;
692 
693       case HAL_RTC_INTERNAL_TAMPER15_EVENT_CB_ID :
694         hrtc->InternalTamper15EventCallback = pCallback;
695         break;
696 
697       case HAL_RTC_MSPINIT_CB_ID :
698         hrtc->MspInitCallback = pCallback;
699         break;
700 
701       case HAL_RTC_MSPDEINIT_CB_ID :
702         hrtc->MspDeInitCallback = pCallback;
703         break;
704 
705       default :
706         /* Return error status */
707         status = HAL_ERROR;
708         break;
709     }
710   }
711   else if (HAL_RTC_STATE_RESET == hrtc->State)
712   {
713     switch (CallbackID)
714     {
715       case HAL_RTC_MSPINIT_CB_ID :
716         hrtc->MspInitCallback = pCallback;
717         break;
718 
719       case HAL_RTC_MSPDEINIT_CB_ID :
720         hrtc->MspDeInitCallback = pCallback;
721         break;
722 
723       default :
724         /* Return error status */
725         status = HAL_ERROR;
726         break;
727     }
728   }
729   else
730   {
731     /* Return error status */
732     status = HAL_ERROR;
733   }
734 
735   return status;
736 }
737 
738 /**
739   * @brief  Unregister an RTC Callback
740   *         RTC callback is redirected to the weak predefined callback
741   * @param  hrtc RTC handle
742   * @param  CallbackID ID of the callback to be unregistered
743   *         This parameter can be one of the following values:
744   *         This parameter can be one of the following values:
745   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID              Alarm A Event Callback ID
746   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID              Alarm B Event Callback ID
747   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID            TimeStamp Event Callback ID
748   *          @arg @ref HAL_RTC_SSRU_EVENT_CB_ID                 SSRU Callback ID
749   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID          WakeUp Timer Event Callback ID
750   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID              Tamper 1 Callback ID
751   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID              Tamper 2 Callback ID
752   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID              Tamper 3 Callback ID
753   *          @arg @ref HAL_RTC_TAMPER4_EVENT_CB_ID              Tamper 4 Callback ID
754   *          @arg @ref HAL_RTC_TAMPER5_EVENT_CB_ID              Tamper 5 Callback ID
755   *          @arg @ref HAL_RTC_TAMPER6_EVENT_CB_ID              Tamper 6 Callback ID
756   *          @arg @ref HAL_RTC_TAMPER7_EVENT_CB_ID              Tamper 7 Callback ID
757   *          @arg @ref HAL_RTC_TAMPER8_EVENT_CB_ID              Tamper 8 Callback ID
758   *          @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID     Internal Tamper 1 Callback ID
759   *          @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID     Internal Tamper 2 Callback ID
760   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID     Internal Tamper 3 Callback ID
761   *          @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID     Internal Tamper 4 Callback ID
762   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID     Internal Tamper 5 Callback ID
763   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID     Internal Tamper 6 Callback ID
764   *          @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID     Internal Tamper 7 Callback ID
765   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID     Internal Tamper 8 Callback ID
766   *          @arg @ref HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID     Internal Tamper 9 Callback ID
767   *          @arg @ref HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID    Internal Tamper 11 Callback ID
768   *          @arg @ref HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID    Internal Tamper 12 Callback ID
769   *          @arg @ref HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID    Internal Tamper 13 Callback ID
770   *          @arg @ref HAL_RTC_INTERNAL_TAMPER15_EVENT_CB_ID    Internal Tamper 15 Callback ID
771   *          @arg @ref HAL_RTC_MSPINIT_CB_ID                    Msp Init callback ID
772   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID                  Msp DeInit callback ID
773   * @note  The HAL_RTC_UnRegisterCallback() may be called before HAL_RTC_Init() in HAL_RTC_STATE_RESET
774   *        to un-register callbacks for HAL_RTC_MSPINIT_CB_ID and HAL_RTC_MSPDEINIT_CB_ID.
775   * @retval HAL status
776   */
HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef * hrtc,HAL_RTC_CallbackIDTypeDef CallbackID)777 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
778 {
779   HAL_StatusTypeDef status = HAL_OK;
780 
781   if (HAL_RTC_STATE_READY == hrtc->State)
782   {
783     switch (CallbackID)
784     {
785       case HAL_RTC_ALARM_A_EVENT_CB_ID :
786         /* Legacy weak AlarmAEventCallback */
787         hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback;
788         break;
789 
790       case HAL_RTC_ALARM_B_EVENT_CB_ID :
791         /* Legacy weak AlarmBEventCallback */
792         hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback;
793         break;
794 
795       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
796         /* Legacy weak TimeStampEventCallback */
797         hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback;
798         break;
799 
800       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
801         /* Legacy weak WakeUpTimerEventCallback */
802         hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback;
803         break;
804 
805       case HAL_RTC_SSRU_EVENT_CB_ID :
806         /* Legacy weak SSRUEventCallback */
807         hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback;
808         break;
809 
810       case HAL_RTC_TAMPER1_EVENT_CB_ID :
811         /* Legacy weak Tamper1EventCallback */
812         hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback;
813         break;
814 
815       case HAL_RTC_TAMPER2_EVENT_CB_ID :
816         /* Legacy weak Tamper2EventCallback */
817         hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback;
818         break;
819 
820       case HAL_RTC_TAMPER3_EVENT_CB_ID :
821         /* Legacy weak Tamper3EventCallback */
822         hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback;
823         break;
824 
825       case HAL_RTC_TAMPER4_EVENT_CB_ID :
826         /* Legacy weak Tamper4EventCallback */
827         hrtc->Tamper4EventCallback = HAL_RTCEx_Tamper4EventCallback;
828         break;
829 
830       case HAL_RTC_TAMPER5_EVENT_CB_ID :
831         /* Legacy weak Tamper5EventCallback */
832         hrtc->Tamper5EventCallback = HAL_RTCEx_Tamper5EventCallback;
833         break;
834 
835       case HAL_RTC_TAMPER6_EVENT_CB_ID :
836         /* Legacy weak Tamper6EventCallback */
837         hrtc->Tamper6EventCallback = HAL_RTCEx_Tamper6EventCallback;
838         break;
839 
840       case HAL_RTC_TAMPER7_EVENT_CB_ID :
841         /* Legacy weak Tamper7EventCallback */
842         hrtc->Tamper7EventCallback = HAL_RTCEx_Tamper7EventCallback;
843         break;
844 
845       case HAL_RTC_TAMPER8_EVENT_CB_ID :
846         /* Legacy weak Tamper8EventCallback */
847         hrtc->Tamper8EventCallback = HAL_RTCEx_Tamper8EventCallback;
848         break;
849 
850       case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
851         /* Legacy weak InternalTamper1EventCallback */
852         hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback;
853         break;
854 
855       case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
856         /* Legacy weak InternalTamper2EventCallback */
857         hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback;
858         break;
859 
860       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
861         /* Legacy weak InternalTamper3EventCallback */
862         hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback;
863         break;
864 
865       case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
866         /* Legacy weak InternalTamper4EventCallback */
867         hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback;
868         break;
869 
870       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
871         /* Legacy weak InternalTamper5EventCallback */
872         hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback;
873         break;
874 
875       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
876         /* Legacy weak InternalTamper6EventCallback */
877         hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback;
878         break;
879 
880       case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
881         /* Legacy weak InternalTamper7EventCallback */
882         hrtc->InternalTamper7EventCallback = HAL_RTCEx_InternalTamper7EventCallback;
883         break;
884 
885       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
886         /* Legacy weak InternalTamper8EventCallback */
887         hrtc->InternalTamper8EventCallback = HAL_RTCEx_InternalTamper8EventCallback;
888         break;
889 
890       case HAL_RTC_INTERNAL_TAMPER9_EVENT_CB_ID :
891         /* Legacy weak InternalTamper9EventCallback */
892         hrtc->InternalTamper9EventCallback = HAL_RTCEx_InternalTamper9EventCallback;
893         break;
894 
895       case HAL_RTC_INTERNAL_TAMPER11_EVENT_CB_ID :
896         /* Legacy weak InternalTamper11EventCallback */
897         hrtc->InternalTamper11EventCallback = HAL_RTCEx_InternalTamper11EventCallback;
898         break;
899 
900       case HAL_RTC_INTERNAL_TAMPER12_EVENT_CB_ID :
901         /* Legacy weak InternalTamper12EventCallback */
902         hrtc->InternalTamper12EventCallback = HAL_RTCEx_InternalTamper12EventCallback;
903         break;
904 
905       case HAL_RTC_INTERNAL_TAMPER13_EVENT_CB_ID :
906         /* Legacy weak InternalTamper13EventCallback */
907         hrtc->InternalTamper13EventCallback = HAL_RTCEx_InternalTamper13EventCallback;
908         break;
909 
910       case HAL_RTC_INTERNAL_TAMPER15_EVENT_CB_ID :
911         /* Legacy weak InternalTamper15EventCallback */
912         hrtc->InternalTamper15EventCallback = HAL_RTCEx_InternalTamper15EventCallback;
913         break;
914 
915       case HAL_RTC_MSPINIT_CB_ID :
916         hrtc->MspInitCallback = HAL_RTC_MspInit;
917         break;
918 
919       case HAL_RTC_MSPDEINIT_CB_ID :
920         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
921         break;
922 
923       default :
924         /* Return error status */
925         status = HAL_ERROR;
926         break;
927     }
928   }
929   else if (HAL_RTC_STATE_RESET == hrtc->State)
930   {
931     switch (CallbackID)
932     {
933       case HAL_RTC_MSPINIT_CB_ID :
934         hrtc->MspInitCallback = HAL_RTC_MspInit;
935         break;
936 
937       case HAL_RTC_MSPDEINIT_CB_ID :
938         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
939         break;
940 
941       default :
942         /* Return error status */
943         status = HAL_ERROR;
944         break;
945     }
946   }
947   else
948   {
949     /* Return error status */
950     status = HAL_ERROR;
951   }
952 
953   return status;
954 }
955 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
956 
957 /**
958   * @brief  Initialize the RTC MSP.
959   * @param  hrtc RTC handle
960   * @retval None
961   */
HAL_RTC_MspInit(RTC_HandleTypeDef * hrtc)962 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
963 {
964   /* Prevent unused argument(s) compilation warning */
965   UNUSED(hrtc);
966 
967   /* NOTE : This function should not be modified, when the callback is needed,
968             the HAL_RTC_MspInit could be implemented in the user file
969    */
970 }
971 
972 /**
973   * @brief  DeInitialize the RTC MSP.
974   * @param  hrtc RTC handle
975   * @retval None
976   */
HAL_RTC_MspDeInit(RTC_HandleTypeDef * hrtc)977 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
978 {
979   /* Prevent unused argument(s) compilation warning */
980   UNUSED(hrtc);
981 
982   /* NOTE : This function should not be modified, when the callback is needed,
983             the HAL_RTC_MspDeInit could be implemented in the user file
984    */
985 }
986 
987 /**
988   * @}
989   */
990 
991 /** @addtogroup RTC_Exported_Functions_Group2
992   *  @brief   RTC Time and Date functions
993   *
994 @verbatim
995  ===============================================================================
996                  ##### RTC Time and Date functions #####
997  ===============================================================================
998 
999  [..] This section provides functions allowing to configure Time and Date features
1000 
1001 @endverbatim
1002   * @{
1003   */
1004 
1005 /**
1006   * @brief  Set RTC current time.
1007   * @param  hrtc RTC handle
1008   * @param  sTime Pointer to Time structure
1009   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used and RTC_SSR will be automatically
1010   *          reset to 0xFFFFFFFF
1011   *          else sTime->SubSeconds is not used and RTC_SSR will be automatically reset to the
1012   *          A 7-bit async prescaler (RTC_PRER_PREDIV_A)
1013   * @param  Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
1014   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1015   *          else this parameter can be one of the following values
1016   *             @arg RTC_FORMAT_BIN: Binary format
1017   *             @arg RTC_FORMAT_BCD: BCD format
1018   * @retval HAL status
1019   */
HAL_RTC_SetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)1020 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1021 {
1022   uint32_t tmpreg;
1023   HAL_StatusTypeDef status;
1024 
1025 #ifdef USE_FULL_ASSERT
1026   /* Check the parameters depending of the Binary mode with 32-bit free-running counter configuration */
1027   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1028   {
1029     /* Check the parameters */
1030     assert_param(IS_RTC_FORMAT(Format));
1031   }
1032 #endif /* USE_FULL_ASSERT */
1033 
1034   /* Process Locked */
1035   __HAL_LOCK(hrtc);
1036 
1037   /* Change RTC state */
1038   hrtc->State = HAL_RTC_STATE_BUSY;
1039 
1040   /* Disable the write protection for RTC registers */
1041   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1042 
1043   /* Enter Initialization mode */
1044   status = RTC_EnterInitMode(hrtc);
1045   if (status == HAL_OK)
1046   {
1047     /* Check Binary mode ((32-bit free-running counter) */
1048     if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
1049     {
1050       if (Format == RTC_FORMAT_BIN)
1051       {
1052         if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1053         {
1054           assert_param(IS_RTC_HOUR12(sTime->Hours));
1055           assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
1056         }
1057         else
1058         {
1059           sTime->TimeFormat = 0x00U;
1060           assert_param(IS_RTC_HOUR24(sTime->Hours));
1061         }
1062         assert_param(IS_RTC_MINUTES(sTime->Minutes));
1063         assert_param(IS_RTC_SECONDS(sTime->Seconds));
1064 
1065         tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
1066                             ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
1067                             ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
1068                             (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
1069       }
1070       else
1071       {
1072         if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1073         {
1074           assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
1075           assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
1076         }
1077         else
1078         {
1079           sTime->TimeFormat = 0x00U;
1080           assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
1081         }
1082         assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
1083         assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
1084         tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
1085                   ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
1086                   ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
1087                   ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
1088       }
1089 
1090       /* Set the RTC_TR register */
1091       WRITE_REG(RTC->TR, (tmpreg & RTC_TR_RESERVED_MASK));
1092 
1093       /* Clear the bits to be configured */
1094       CLEAR_BIT(RTC->CR, RTC_CR_BKP);
1095     }
1096 
1097     /* Exit Initialization mode */
1098     status = RTC_ExitInitMode(hrtc);
1099   }
1100 
1101   /* Enable the write protection for RTC registers */
1102   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1103 
1104   if (status == HAL_OK)
1105   {
1106     /* Change RTC state */
1107     hrtc->State = HAL_RTC_STATE_READY;
1108   }
1109 
1110   /* Process Unlocked */
1111   __HAL_UNLOCK(hrtc);
1112 
1113   return status;
1114 }
1115 
1116 /**
1117   * @brief  Get RTC current time.
1118   * @note  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
1119   *        value in second fraction ratio with time unit following generic formula:
1120   *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
1121   *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
1122   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1123   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1124   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read
1125   *        to ensure consistency between the time and date values.
1126   * @param  hrtc RTC handle
1127   * @param  sTime
1128   *          if Binary mode is RTC_BINARY_ONLY, sTime->SubSeconds only is updated
1129   *          else
1130   *             Pointer to Time structure with Hours, Minutes and Seconds fields returned
1131   *               with input format (BIN or BCD), also SubSeconds field returning the
1132   *               RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
1133   *               factor to be used for second fraction ratio computation.
1134   * @param  Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
1135   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1136   *          else this parameter can be one of the following values:
1137   *            @arg RTC_FORMAT_BIN: Binary format
1138   *            @arg RTC_FORMAT_BCD: BCD format
1139   * @retval HAL status
1140   */
HAL_RTC_GetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)1141 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
1142 {
1143   uint32_t tmpreg;
1144 
1145   /* Prevent unused argument(s) compilation warning */
1146   UNUSED(hrtc);
1147 
1148   /* Get subseconds structure field from the corresponding register */
1149   sTime->SubSeconds = READ_REG(RTC->SSR);
1150 
1151   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
1152   {
1153     /* Check the parameters */
1154     assert_param(IS_RTC_FORMAT(Format));
1155 
1156     /* Get SecondFraction structure field from the corresponding register field */
1157     sTime->SecondFraction = (uint32_t)(READ_REG(RTC->PRER) & RTC_PRER_PREDIV_S);
1158 
1159     /* Get the TR register */
1160     tmpreg = (uint32_t)(READ_REG(RTC->TR) & RTC_TR_RESERVED_MASK);
1161 
1162     /* Fill the structure fields with the read parameters */
1163     sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
1164     sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
1165     sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
1166     sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
1167 
1168     /* Check the input parameters format */
1169     if (Format == RTC_FORMAT_BIN)
1170     {
1171       /* Convert the time structure parameters to Binary format */
1172       sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
1173       sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
1174       sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
1175     }
1176   }
1177 
1178   return HAL_OK;
1179 }
1180 
1181 /**
1182   * @brief  Set RTC current date.
1183   * @param  hrtc RTC handle
1184   * @param  sDate Pointer to date structure
1185   * @param  Format Format of sDate->Year, sDate->Month and sDate->Weekday.
1186   *          This parameter can be one of the following values:
1187   *            @arg RTC_FORMAT_BIN: Binary format
1188   *            @arg RTC_FORMAT_BCD: BCD format
1189   * @retval HAL status
1190   */
HAL_RTC_SetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1191 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1192 {
1193   uint32_t datetmpreg;
1194   HAL_StatusTypeDef status;
1195 
1196   /* Check the parameters */
1197   assert_param(IS_RTC_FORMAT(Format));
1198 
1199   /* Process Locked */
1200   __HAL_LOCK(hrtc);
1201 
1202   /* Change RTC state */
1203   hrtc->State = HAL_RTC_STATE_BUSY;
1204 
1205   if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
1206   {
1207     sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
1208   }
1209 
1210   assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
1211 
1212   if (Format == RTC_FORMAT_BIN)
1213   {
1214     assert_param(IS_RTC_YEAR(sDate->Year));
1215     assert_param(IS_RTC_MONTH(sDate->Month));
1216     assert_param(IS_RTC_DATE(sDate->Date));
1217 
1218     datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
1219                   ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
1220                   ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
1221                   ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
1222   }
1223   else
1224   {
1225     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1226     assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1227     assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1228 
1229     datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
1230                   (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
1231                   (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
1232                   (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1233   }
1234 
1235   /* Disable the write protection for RTC registers */
1236   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1237 
1238   /* Enter Initialization mode */
1239   status = RTC_EnterInitMode(hrtc);
1240   if (status == HAL_OK)
1241   {
1242     /* Set the RTC_DR register */
1243     WRITE_REG(RTC->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK));
1244 
1245     /* Exit Initialization mode */
1246     status = RTC_ExitInitMode(hrtc);
1247   }
1248 
1249   /* Enable the write protection for RTC registers */
1250   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1251 
1252   if (status == HAL_OK)
1253   {
1254     /* Change RTC state */
1255     hrtc->State = HAL_RTC_STATE_READY;
1256   }
1257 
1258   /* Process Unlocked */
1259   __HAL_UNLOCK(hrtc);
1260 
1261   return status;
1262 }
1263 
1264 /**
1265   * @brief  Get RTC current date.
1266   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1267   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1268   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1269   * @param  hrtc RTC handle
1270   * @param  sDate Pointer to Date structure
1271   * @param  Format Format of sDate->Year, sDate->Month and sDate->Weekday.
1272   *          This parameter can be one of the following values:
1273   *            @arg RTC_FORMAT_BIN: Binary format
1274   *            @arg RTC_FORMAT_BCD: BCD format
1275   * @retval HAL status
1276   */
HAL_RTC_GetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)1277 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1278 {
1279   uint32_t datetmpreg;
1280 
1281   /* Prevent unused argument(s) compilation warning */
1282   UNUSED(hrtc);
1283 
1284   /* Check the parameters */
1285   assert_param(IS_RTC_FORMAT(Format));
1286 
1287   /* Get the DR register */
1288   datetmpreg = (uint32_t)(READ_REG(RTC->DR) & RTC_DR_RESERVED_MASK);
1289 
1290   /* Fill the structure fields with the read parameters */
1291   sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1292   sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1293   sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1294   sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
1295 
1296   /* Check the input parameters format */
1297   if (Format == RTC_FORMAT_BIN)
1298   {
1299     /* Convert the date structure parameters to Binary format */
1300     sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1301     sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1302     sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1303   }
1304   return HAL_OK;
1305 }
1306 
1307 /**
1308   * @brief  Daylight Saving Time, Add one hour to the calendar in one single operation
1309   *         without going through the initialization procedure.
1310   * @param  hrtc RTC handle
1311   * @retval None
1312   */
HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef * hrtc)1313 void HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc)
1314 {
1315   /* Prevent unused argument(s) compilation warning */
1316   UNUSED(hrtc);
1317 
1318   /* Disable the write protection for RTC registers */
1319   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1320 
1321   /* Set RTC_CR_ADD1H Bit */
1322   SET_BIT(RTC->CR, RTC_CR_ADD1H);
1323 
1324   /* Enable the write protection for RTC registers */
1325   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1326 }
1327 
1328 /**
1329   * @brief  Daylight Saving Time, Subtract one hour from the calendar in one
1330   *         single operation without going through the initialization procedure.
1331   * @param  hrtc RTC handle
1332   * @retval None
1333   */
HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef * hrtc)1334 void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc)
1335 {
1336   /* Prevent unused argument(s) compilation warning */
1337   UNUSED(hrtc);
1338 
1339   /* Disable the write protection for RTC registers */
1340   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1341 
1342   /* Set RTC_CR_SUB1H Bit */
1343   SET_BIT(RTC->CR, RTC_CR_SUB1H);
1344 
1345   /* Enable the write protection for RTC registers */
1346   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1347 }
1348 
1349 /**
1350   * @brief  Daylight Saving Time, Set the store operation bit.
1351   * @note   It can be used by the software in order to memorize the DST status.
1352   * @param  hrtc RTC handle
1353   * @retval None
1354   */
HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef * hrtc)1355 void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
1356 {
1357   /* Prevent unused argument(s) compilation warning */
1358   UNUSED(hrtc);
1359 
1360   /* Disable the write protection for RTC registers */
1361   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1362 
1363   /* Set RTC_CR_BKP Bit */
1364   SET_BIT(RTC->CR, RTC_CR_BKP);
1365 
1366   /* Enable the write protection for RTC registers */
1367   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1368 }
1369 
1370 /**
1371   * @brief  Daylight Saving Time, Clear the store operation bit.
1372   * @param  hrtc RTC handle
1373   * @retval None
1374   */
HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef * hrtc)1375 void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
1376 {
1377   /* Prevent unused argument(s) compilation warning */
1378   UNUSED(hrtc);
1379 
1380   /* Disable the write protection for RTC registers */
1381   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1382 
1383   /* Clear RTC_CR_BKP Bit */
1384   CLEAR_BIT(RTC->CR, RTC_CR_BKP);
1385 
1386   /* Enable the write protection for RTC registers */
1387   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1388 }
1389 
1390 /**
1391   * @brief  Daylight Saving Time, Read the store operation bit.
1392   * @param  hrtc RTC handle
1393   * @retval operation see RTC_StoreOperation_Definitions
1394   */
HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef * hrtc)1395 uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
1396 {
1397   /* Prevent unused argument(s) compilation warning */
1398   UNUSED(hrtc);
1399 
1400   /* Get RTC_CR_BKP Bit */
1401   return READ_BIT(RTC->CR, RTC_CR_BKP);
1402 }
1403 
1404 
1405 /**
1406   * @}
1407   */
1408 
1409 /** @addtogroup RTC_Exported_Functions_Group3
1410   *  @brief   RTC Alarm functions
1411   *
1412 @verbatim
1413  ===============================================================================
1414                  ##### RTC Alarm functions #####
1415  ===============================================================================
1416 
1417  [..] This section provides functions allowing to configure Alarm feature
1418 
1419 @endverbatim
1420   * @{
1421   */
1422 /**
1423   * @brief  Set the specified RTC Alarm.
1424   * @param  hrtc RTC handle
1425   * @param  sAlarm Pointer to Alarm structure
1426   *          if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1427   *             sAlarm->AlarmTime.SubSeconds
1428   *             sAlarm->AlarmSubSecondMask
1429   *             sAlarm->BinaryAutoClr
1430   * @param  Format of the entered parameters.
1431   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1432   *          else this parameter can be one of the following values
1433   *             @arg RTC_FORMAT_BIN: Binary format
1434   *             @arg RTC_FORMAT_BCD: BCD format
1435   * @retval HAL status
1436   */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1437 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1438 {
1439   uint32_t tmpreg = 0;
1440   uint32_t binaryMode;
1441 
1442   /* Process Locked */
1443   __HAL_LOCK(hrtc);
1444 
1445   /* Change RTC state */
1446   hrtc->State = HAL_RTC_STATE_BUSY;
1447 
1448 #ifdef  USE_FULL_ASSERT
1449   /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration) */
1450   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1451   {
1452     assert_param(IS_RTC_FORMAT(Format));
1453     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1454     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1455     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1456     assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1457     assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1458   }
1459   else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1460   {
1461     assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1462     assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1463   }
1464   else /* RTC_BINARY_MIX */
1465   {
1466     assert_param(IS_RTC_FORMAT(Format));
1467     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1468     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1469     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1470     /* In Binary Mix Mode, the RTC can not generate an alarm on a match
1471        involving all calendar items + the upper SSR bits */
1472     assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <=
1473                  (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1474   }
1475 #endif /* USE_FULL_ASSERT */
1476 
1477   /* Get Binary mode (32-bit free-running counter configuration) */
1478   binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1479 
1480   if (binaryMode != RTC_BINARY_ONLY)
1481   {
1482     if (Format == RTC_FORMAT_BIN)
1483     {
1484       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1485       {
1486         assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1487         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1488       }
1489       else
1490       {
1491         sAlarm->AlarmTime.TimeFormat = 0x00U;
1492         assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1493       }
1494       assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1495       assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1496 
1497       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1498       {
1499         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1500       }
1501       else
1502       {
1503         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1504       }
1505       tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1506                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1507                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1508                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1509                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1510                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1511                 ((uint32_t)sAlarm->AlarmMask));
1512     }
1513     else /* format BCD */
1514     {
1515       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1516       {
1517         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1518         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1519       }
1520       else
1521       {
1522         sAlarm->AlarmTime.TimeFormat = 0x00U;
1523         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1524       }
1525 
1526       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1527       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1528 
1529 #ifdef  USE_FULL_ASSERT
1530       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1531       {
1532         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1533       }
1534       else
1535       {
1536         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1537       }
1538 
1539 #endif /* USE_FULL_ASSERT */
1540       tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1541                 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1542                 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1543                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1544                 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1545                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1546                 ((uint32_t)sAlarm->AlarmMask));
1547     }
1548   }
1549 
1550   /* Disable the write protection for RTC registers */
1551   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1552 
1553   /* Configure the Alarm register */
1554   if (sAlarm->Alarm == RTC_ALARM_A)
1555   {
1556     /* Disable the Alarm A interrupt */
1557 
1558     /* In case of interrupt mode is used, the interrupt source must disabled */
1559     CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
1560 
1561     /* Clear flag alarm A */
1562     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1563 
1564     if (binaryMode == RTC_BINARY_ONLY)
1565     {
1566       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1567     }
1568     else
1569     {
1570       WRITE_REG(RTC->ALRMAR, tmpreg);
1571       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1572     }
1573 
1574     WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1575 
1576     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1577     {
1578       /* Configure the  Alarm A output clear */
1579       SET_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1580     }
1581     else
1582     {
1583       /* Disable the  Alarm A  output clear */
1584       CLEAR_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1585     }
1586     /* Configure the Alarm state: Enable Alarm */
1587     SET_BIT(RTC->CR, RTC_CR_ALRAE);
1588   }
1589   else
1590   {
1591     /* Disable the Alarm B interrupt */
1592 
1593     /* In case of interrupt mode is used, the interrupt source must disabled */
1594     CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
1595 
1596     /* Clear flag alarm B */
1597     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1598 
1599     if (binaryMode == RTC_BINARY_ONLY)
1600     {
1601       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1602     }
1603     else
1604     {
1605       WRITE_REG(RTC->ALRMBR, tmpreg);
1606 
1607       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1608     }
1609 
1610     WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1611 
1612     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1613     {
1614       /* Configure the  Alarm B output clear */
1615       SET_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1616     }
1617     else
1618     {
1619       /* Disable the  Alarm B output clear */
1620       CLEAR_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1621     }
1622 
1623     /* Configure the Alarm state: Enable Alarm */
1624     SET_BIT(RTC->CR, RTC_CR_ALRBE);
1625   }
1626 
1627   /* Enable the write protection for RTC registers */
1628   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1629 
1630   /* Change RTC state */
1631   hrtc->State = HAL_RTC_STATE_READY;
1632 
1633   /* Process Unlocked */
1634   __HAL_UNLOCK(hrtc);
1635 
1636   return HAL_OK;
1637 }
1638 
1639 /**
1640   * @brief  Set the specified RTC Alarm with Interrupt.
1641   * @param  hrtc RTC handle
1642   * @param  sAlarm Pointer to Alarm structure
1643   *          if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
1644   *             sAlarm->AlarmTime.SubSeconds
1645   *             sAlarm->AlarmSubSecondMask
1646   *             sAlarm->BinaryAutoClr
1647   * @param  Format Specifies the format of the entered parameters.
1648   *          if Binary mode is RTC_BINARY_ONLY, this parameter is not used
1649   *          else this parameter can be one of the following values
1650   *             @arg RTC_FORMAT_BIN: Binary format
1651   *             @arg RTC_FORMAT_BCD: BCD format
1652   * @retval HAL status
1653   */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)1654 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1655 {
1656   uint32_t tmpreg = 0;
1657   uint32_t binaryMode;
1658 
1659   /* Process Locked */
1660   __HAL_LOCK(hrtc);
1661 
1662   /* Change RTC state */
1663   hrtc->State = HAL_RTC_STATE_BUSY;
1664 
1665 #ifdef  USE_FULL_ASSERT
1666   /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration) */
1667   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
1668   {
1669     assert_param(IS_RTC_FORMAT(Format));
1670     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1671     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1672     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1673     assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1674     assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1675   }
1676   else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
1677   {
1678     assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
1679     assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
1680   }
1681   else /* RTC_BINARY_MIX */
1682   {
1683     assert_param(IS_RTC_FORMAT(Format));
1684     assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1685     assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1686     assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1687     /* In Binary Mix Mode, the RTC can not generate an alarm on a match
1688       involving all calendar items + the upper SSR bits */
1689     assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <=
1690                  (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
1691   }
1692 #endif /* USE_FULL_ASSERT */
1693 
1694   /* Get Binary mode (32-bit free-running counter configuration) */
1695   binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
1696 
1697   if (binaryMode != RTC_BINARY_ONLY)
1698   {
1699     if (Format == RTC_FORMAT_BIN)
1700     {
1701       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1702       {
1703         assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1704         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1705       }
1706       else
1707       {
1708         sAlarm->AlarmTime.TimeFormat = 0x00U;
1709         assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1710       }
1711       assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1712       assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1713 
1714       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1715       {
1716         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1717       }
1718       else
1719       {
1720         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1721       }
1722       tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1723                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1724                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1725                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1726                 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1727                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1728                 ((uint32_t)sAlarm->AlarmMask));
1729     }
1730     else /* Format BCD */
1731     {
1732       if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
1733       {
1734         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1735         assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1736       }
1737       else
1738       {
1739         sAlarm->AlarmTime.TimeFormat = 0x00U;
1740         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1741       }
1742 
1743       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1744       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1745 
1746 #ifdef  USE_FULL_ASSERT
1747       if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1748       {
1749         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1750       }
1751       else
1752       {
1753         assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1754       }
1755 
1756 #endif /* USE_FULL_ASSERT */
1757       tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1758                 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1759                 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1760                 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1761                 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1762                 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1763                 ((uint32_t)sAlarm->AlarmMask));
1764 
1765     }
1766   }
1767 
1768   /* Disable the write protection for RTC registers */
1769   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1770 
1771   /* Configure the Alarm registers */
1772   if (sAlarm->Alarm == RTC_ALARM_A)
1773   {
1774     /* Disable the Alarm A interrupt */
1775     CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1776 
1777     /* Clear flag alarm A */
1778     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1779 
1780     if (binaryMode == RTC_BINARY_ONLY)
1781     {
1782       RTC->ALRMASSR = sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr;
1783     }
1784     else
1785     {
1786       WRITE_REG(RTC->ALRMAR, tmpreg);
1787 
1788       WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
1789     }
1790 
1791     WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
1792 
1793     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1794     {
1795       /* Configure the  Alarm A output clear */
1796       SET_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1797     }
1798     else
1799     {
1800       /* Disable the Alarm A output clear */
1801       CLEAR_BIT(RTC->CR, RTC_CR_ALRAFCLR);
1802     }
1803 
1804     /* Configure the Alarm interrupt */
1805     SET_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1806   }
1807   else
1808   {
1809     /* Disable the Alarm B interrupt */
1810     CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1811 
1812     /* Clear flag alarm B */
1813     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1814 
1815     if (binaryMode == RTC_BINARY_ONLY)
1816     {
1817       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
1818     }
1819     else
1820     {
1821       WRITE_REG(RTC->ALRMBR, tmpreg);
1822 
1823       WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
1824     }
1825 
1826     WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
1827 
1828     if (sAlarm->FlagAutoClr == ALARM_FLAG_AUTOCLR_ENABLE)
1829     {
1830       /* Configure the  Alarm B Output clear */
1831       SET_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1832     }
1833     else
1834     {
1835       /* Disable the  Alarm B Output clear */
1836       CLEAR_BIT(RTC->CR, RTC_CR_ALRBFCLR);
1837     }
1838 
1839     /* Configure the Alarm interrupt */
1840     SET_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1841   }
1842 
1843   /* Enable the write protection for RTC registers */
1844   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1845 
1846   /* Change RTC state */
1847   hrtc->State = HAL_RTC_STATE_READY;
1848 
1849   /* Process Unlocked */
1850   __HAL_UNLOCK(hrtc);
1851 
1852   return HAL_OK;
1853 }
1854 
1855 /**
1856   * @brief  Deactivate the specified RTC Alarm.
1857   * @param  hrtc RTC handle
1858   * @param  Alarm Specifies the Alarm.
1859   *          This parameter can be one of the following values:
1860   *            @arg RTC_ALARM_A:  AlarmA
1861   *            @arg RTC_ALARM_B:  AlarmB
1862   * @retval HAL status
1863   */
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef * hrtc,uint32_t Alarm)1864 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1865 {
1866   /* Check the parameters */
1867   assert_param(IS_RTC_ALARM(Alarm));
1868 
1869   /* Process Locked */
1870   __HAL_LOCK(hrtc);
1871 
1872   /* Change RTC state */
1873   hrtc->State = HAL_RTC_STATE_BUSY;
1874 
1875   /* Disable the write protection for RTC registers */
1876   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1877 
1878   /* In case of interrupt mode is used, the interrupt source must disabled */
1879   if (Alarm == RTC_ALARM_A)
1880   {
1881     CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
1882 
1883     /* AlarmA, Clear SSCLR */
1884     CLEAR_BIT(RTC->ALRMASSR, RTC_ALRMASSR_SSCLR);
1885   }
1886   else
1887   {
1888     CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
1889 
1890     /* AlarmB, Clear SSCLR */
1891     CLEAR_BIT(RTC->ALRMBSSR, RTC_ALRMBSSR_SSCLR);
1892   }
1893 
1894   /* Enable the write protection for RTC registers */
1895   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1896 
1897   /* Change RTC state */
1898   hrtc->State = HAL_RTC_STATE_READY;
1899 
1900   /* Process Unlocked */
1901   __HAL_UNLOCK(hrtc);
1902 
1903   return HAL_OK;
1904 }
1905 
1906 /**
1907   * @brief  Get the RTC Alarm value and masks.
1908   * @param  hrtc RTC handle
1909   * @param  sAlarm Pointer to Date structure
1910   * @param  Alarm Specifies the Alarm.
1911   *          This parameter can be one of the following values:
1912   *             @arg RTC_ALARM_A: AlarmA
1913   *             @arg RTC_ALARM_B: AlarmB
1914   * @param  Format Specifies the format of the entered parameters.
1915   *          This parameter can be one of the following values:
1916   *             @arg RTC_FORMAT_BIN: Binary format
1917   *             @arg RTC_FORMAT_BCD: BCD format
1918   * @retval HAL status
1919   */
HAL_RTC_GetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Alarm,uint32_t Format)1920 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1921 {
1922   uint32_t tmpreg;
1923   uint32_t subsecondtmpreg;
1924 
1925   /* Prevent unused argument(s) compilation warning */
1926   UNUSED(hrtc);
1927 
1928   /* Check the parameters */
1929   assert_param(IS_RTC_FORMAT(Format));
1930   assert_param(IS_RTC_ALARM(Alarm));
1931 
1932   if (Alarm == RTC_ALARM_A)
1933   {
1934     /* AlarmA */
1935     sAlarm->Alarm = RTC_ALARM_A;
1936 
1937     tmpreg = READ_REG(RTC->ALRMAR);
1938     subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1939 
1940     /* Fill the structure with the read parameters */
1941     sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1942     sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1943     sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1944     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1945     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1946     sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1947     sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1948     sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1949   }
1950   else
1951   {
1952     sAlarm->Alarm = RTC_ALARM_B;
1953 
1954     tmpreg = READ_REG(RTC->ALRMBR);
1955     subsecondtmpreg = (uint32_t)(READ_REG(RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1956 
1957     /* Fill the structure with the read parameters */
1958     sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1959     sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1960     sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1961     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1962     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1963     sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1964     sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1965     sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1966   }
1967 
1968   if (Format == RTC_FORMAT_BIN)
1969   {
1970     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1971     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1972     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1973     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1974   }
1975 
1976   return HAL_OK;
1977 }
1978 
1979 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1980 /**
1981   * @brief  Handle Alarm secure interrupt request.
1982   * @param  hrtc RTC handle
1983   * @retval None
1984   */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)1985 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1986 {
1987   /* Get interrupt status */
1988   uint32_t tmp = READ_REG(RTC->SMISR);
1989 
1990   if ((tmp & RTC_SMISR_ALRAMF) != 0u)
1991   {
1992     /* Clear the AlarmA interrupt pending bit */
1993     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
1994 
1995 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1996     /* Call Compare Match registered Callback */
1997     hrtc->AlarmAEventCallback(hrtc);
1998 #else
1999     HAL_RTC_AlarmAEventCallback(hrtc);
2000 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2001   }
2002 
2003   if ((tmp & RTC_SMISR_ALRBMF) != 0u)
2004   {
2005     /* Clear the AlarmB interrupt pending bit */
2006     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
2007 
2008 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2009     /* Call Compare Match registered Callback */
2010     hrtc->AlarmBEventCallback(hrtc);
2011 #else
2012     HAL_RTCEx_AlarmBEventCallback(hrtc);
2013 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2014 
2015   }
2016 
2017   /* Change RTC state */
2018   hrtc->State = HAL_RTC_STATE_READY;
2019 }
2020 
2021 #else /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2022 
2023 /**
2024   * @brief  Handle Alarm non-secure interrupt request.
2025   * @note   Alarm non-secure is available in non-secure driver.
2026   * @param  hrtc RTC handle
2027   * @retval None
2028   */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)2029 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
2030 {
2031   /* Get interrupt status */
2032   uint32_t tmp = READ_REG(RTC->MISR);
2033 
2034   if ((tmp & RTC_MISR_ALRAMF) != 0U)
2035   {
2036     /* Clear the AlarmA interrupt pending bit */
2037     WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
2038 
2039 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2040     /* Call Compare Match registered Callback */
2041     hrtc->AlarmAEventCallback(hrtc);
2042 #else
2043     HAL_RTC_AlarmAEventCallback(hrtc);
2044 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2045   }
2046 
2047   if ((tmp & RTC_MISR_ALRBMF) != 0U)
2048   {
2049     /* Clear the AlarmB interrupt pending bit */
2050     WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
2051 
2052 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2053     /* Call Compare Match registered Callback */
2054     hrtc->AlarmBEventCallback(hrtc);
2055 #else
2056     HAL_RTCEx_AlarmBEventCallback(hrtc);
2057 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2058   }
2059 
2060   /* Change RTC state */
2061   hrtc->State = HAL_RTC_STATE_READY;
2062 }
2063 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2064 
2065 /**
2066   * @brief  Alarm A secure callback.
2067   * @param  hrtc RTC handle
2068   * @retval None
2069   */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)2070 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
2071 {
2072   /* Prevent unused argument(s) compilation warning */
2073   UNUSED(hrtc);
2074 
2075   /* NOTE : This function should not be modified, when the secure callback is needed,
2076             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
2077    */
2078 }
2079 
2080 /**
2081   * @brief  Handle Alarm A Polling request.
2082   * @param  hrtc RTC handle
2083   * @param  Timeout Timeout duration
2084   * @retval HAL status
2085   */
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)2086 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2087 {
2088   uint32_t tickstart = HAL_GetTick();
2089 
2090   while (READ_BIT(RTC->SR, RTC_SR_ALRAF) == 0U)
2091   {
2092     if (Timeout != HAL_MAX_DELAY)
2093     {
2094       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2095       {
2096         /* Change RTC state */
2097         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2098         return HAL_TIMEOUT;
2099       }
2100     }
2101   }
2102 
2103   /* Clear the Alarm interrupt pending bit */
2104   WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
2105 
2106   /* Change RTC state */
2107   hrtc->State = HAL_RTC_STATE_READY;
2108 
2109   return HAL_OK;
2110 }
2111 
2112 /**
2113   * @}
2114   */
2115 
2116 /** @addtogroup RTC_Exported_Functions_Group4
2117   *  @brief   Peripheral Control functions
2118   *
2119 @verbatim
2120  ===============================================================================
2121                      ##### Peripheral Control functions #####
2122  ===============================================================================
2123     [..]
2124     This subsection provides functions allowing to
2125       (+) Wait for RTC Time and Date Synchronization
2126 
2127 @endverbatim
2128   * @{
2129   */
2130 
2131 /**
2132   * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
2133   *         synchronized with RTC APB clock.
2134   * @note   The RTC Resynchronization mode is write protected, use the
2135   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2136   * @note   To read the calendar through the shadow registers after Calendar
2137   *         initialization, calendar update or after wakeup from low power modes
2138   *         the software must first clear the RSF flag.
2139   *         The software must then wait until it is set again before reading
2140   *         the calendar, which means that the calendar registers have been
2141   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
2142   * @param  hrtc RTC handle
2143   * @retval HAL status
2144   */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)2145 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
2146 {
2147   uint32_t tickstart;
2148 
2149   /* Prevent unused argument(s) compilation warning */
2150   UNUSED(hrtc);
2151 
2152   /* Clear RSF flag */
2153   CLEAR_BIT(RTC->ICSR, RTC_ICSR_RSF);
2154 
2155   tickstart = HAL_GetTick();
2156 
2157   /* Wait the registers to be synchronised */
2158   while (READ_BIT(RTC->ICSR, RTC_ICSR_RSF) == 0U)
2159   {
2160     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2161     {
2162       return HAL_TIMEOUT;
2163     }
2164   }
2165 
2166   return HAL_OK;
2167 }
2168 
2169 /**
2170   * @}
2171   */
2172 
2173 /** @addtogroup RTC_Exported_Functions_Group5
2174   *  @brief   Peripheral State functions
2175   *
2176 @verbatim
2177  ===============================================================================
2178                      ##### Peripheral State functions #####
2179  ===============================================================================
2180     [..]
2181     This subsection provides functions allowing to
2182       (+) Get RTC state
2183 
2184 @endverbatim
2185   * @{
2186   */
2187 /**
2188   * @brief  Return the RTC handle state.
2189   * @param  hrtc RTC handle
2190   * @retval HAL state
2191   */
HAL_RTC_GetState(RTC_HandleTypeDef * hrtc)2192 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
2193 {
2194   /* Return RTC handle state */
2195   return hrtc->State;
2196 }
2197 
2198 /**
2199   * @}
2200   */
2201 /**
2202   * @}
2203   */
2204 
2205 /** @addtogroup RTC_Private_Functions
2206   * @{
2207   */
2208 /**
2209   * @brief  Enter the RTC Initialization mode.
2210   * @note   The RTC Initialization mode is write protected, use the
2211   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
2212   * @param  hrtc RTC handle
2213   * @retval HAL status
2214   */
RTC_EnterInitMode(RTC_HandleTypeDef * hrtc)2215 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
2216 {
2217   uint32_t tickstart;
2218   HAL_StatusTypeDef status = HAL_OK;
2219 
2220   /* Prevent unused argument(s) compilation warning */
2221   UNUSED(hrtc);
2222 
2223   /* Check if the Initialization mode is set */
2224   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
2225   {
2226     /* Set the Initialization mode */
2227     SET_BIT(RTC->ICSR, RTC_ICSR_INIT);
2228 
2229     tickstart = HAL_GetTick();
2230     /* Wait till RTC is in INIT state and if Time out is reached exit */
2231     while ((READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
2232     {
2233       if ((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
2234       {
2235         status = HAL_TIMEOUT;
2236 
2237         /* Change RTC state */
2238         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2239       }
2240     }
2241   }
2242 
2243   return status;
2244 }
2245 
2246 /**
2247   * @brief  Exit the RTC Initialization mode.
2248   * @param  hrtc RTC handle
2249   * @retval HAL status
2250   */
RTC_ExitInitMode(RTC_HandleTypeDef * hrtc)2251 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
2252 {
2253   HAL_StatusTypeDef status = HAL_OK;
2254 
2255   /* Exit Initialization mode */
2256   CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
2257 
2258   /* If CR_BYPSHAD bit = 0, wait for synchro */
2259   if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
2260   {
2261     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2262     {
2263       /* Change RTC state */
2264       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2265       status = HAL_TIMEOUT;
2266     }
2267   }
2268   else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry. */
2269   {
2270     /* Clear BYPSHAD bit */
2271     CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
2272     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2273     {
2274       /* Change RTC state */
2275       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2276       status = HAL_TIMEOUT;
2277     }
2278     /* Restore BYPSHAD bit */
2279     SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
2280   }
2281   return status;
2282 }
2283 
2284 /**
2285   * @brief  Convert a 2 digit decimal to BCD format.
2286   * @param  Value Byte to be converted
2287   * @retval Converted byte
2288   */
RTC_ByteToBcd2(uint8_t Value)2289 uint8_t RTC_ByteToBcd2(uint8_t Value)
2290 {
2291   uint32_t bcd_high = 0U;
2292   uint8_t tmp_value = Value;
2293 
2294   while (tmp_value >= 10U)
2295   {
2296     bcd_high++;
2297     tmp_value -= 10U;
2298   }
2299 
2300   return ((uint8_t)(bcd_high << 4U) | tmp_value);
2301 }
2302 
2303 /**
2304   * @brief  Convert from 2 digit BCD to Binary.
2305   * @param  Value BCD value to be converted
2306   * @retval Converted word
2307   */
RTC_Bcd2ToByte(uint8_t Value)2308 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2309 {
2310   uint32_t tmp;
2311 
2312   tmp = (((uint32_t)Value & 0xF0U) >> 4) * 10U;
2313 
2314   return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
2315 }
2316 
2317 /**
2318   * @}
2319   */
2320 
2321 #endif /* HAL_RTC_MODULE_ENABLED */
2322 /**
2323   * @}
2324   */
2325 
2326 /**
2327   * @}
2328   */
2329 
2330