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