1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_hal_rtc_ex.c
4   * @author  GPM Application Team
5   * @brief   Extended RTC HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Real Time Clock (RTC) Extended peripheral:
8   *           + RTC Time Stamp functions
9   *           + RTC Tamper functions
10   *           + RTC Wake-up functions
11   *           + Extended Control functions
12   *           + Extended RTC features functions
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2023 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ==============================================================================
27                   ##### How to use this driver #####
28   ==============================================================================
29   [..]
30     (+) Enable the RTC domain access.
31     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
32         format using the HAL_RTC_Init() function.
33 
34   *** RTC Wakeup configuration ***
35   ================================
36   [..]
37     (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
38         function. You can also configure the RTC Wakeup timer with interrupt mode
39         using the HAL_RTCEx_SetWakeUpTimer_IT() function.
40     (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
41         function.
42 
43   *** Outputs configuration ***
44   =============================
45   [..]  The RTC has 2 different outputs:
46     (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
47         and WaKeUp signals.
48         To output the selected RTC signal, use the HAL_RTC_Init() function.
49     (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
50         To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
51     (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB2) managed on
52         the RTC_OR register.
53     (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
54         automatically configured in output alternate function.
55 
56   *** Smooth digital Calibration configuration ***
57   ================================================
58   [..]
59     (+) Configure the RTC Original Digital Calibration Value and the corresponding
60         calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
61         function.
62 
63   *** TimeStamp configuration ***
64   ===============================
65   [..]
66     (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
67         You can also configure the RTC TimeStamp with interrupt mode using the
68         HAL_RTCEx_SetTimeStamp_IT() function.
69     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
70         function.
71 
72   *** Internal TimeStamp configuration ***
73   ===============================
74   [..]
75     (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
76         User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
77     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
78         function.
79 
80    *** Tamper configuration ***
81    ============================
82    [..]
83      (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
84          or Level according to the Tamper filter (if equal to 0 Edge else Level)
85          value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and
86          Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
87          with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
88      (+) The default configuration of the Tamper erases the backup registers. To avoid
89          erase, enable the NoErase field on the RTC_TAMPCR register.
90      (+) With new RTC tamper configuration, you have to call HAL_RTC_Init() in order to
91          perform TAMP base address offset calculation.
92      (+) If you do not intend to have tamper using RTC clock, you can bypass its initialization
93          by setting ClockEnable inti field to RTC_CLOCK_DISABLE.
94      (+) Enable Internal tamper using HAL_RTCEx_SetInternalTamper. IT mode can be chosen using
95          setting Interrupt field.
96 
97    *** Backup Data Registers configuration ***
98    ===========================================
99    [..]
100      (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
101          function.
102      (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
103          function.
104      (+) Before calling these functions you have to call HAL_RTC_Init() in order to
105          perform TAMP base address offset calculation.
106 
107    @endverbatim
108   */
109 
110 /* Includes ------------------------------------------------------------------*/
111 #include "stm32u0xx_hal.h"
112 
113 /** @addtogroup STM32U0xx_HAL_Driver
114   * @{
115   */
116 
117 /** @addtogroup RTCEx
118   * @brief RTC Extended HAL module driver
119   * @{
120   */
121 
122 #ifdef HAL_RTC_MODULE_ENABLED
123 
124 /* Private typedef -----------------------------------------------------------*/
125 /* Private define ------------------------------------------------------------*/
126 #define TAMP_ALL (TAMP_CR1_TAMP1E | TAMP_CR1_TAMP2E | TAMP_CR1_TAMP3E | \
127                   TAMP_CR1_TAMP4E | TAMP_CR1_TAMP5E )
128 
129 
130 /* Private macro -------------------------------------------------------------*/
131 /* Private variables ---------------------------------------------------------*/
132 /* Private function prototypes -----------------------------------------------*/
133 /* Exported functions --------------------------------------------------------*/
134 
135 /** @addtogroup RTCEx_Exported_Functions
136   * @{
137   */
138 
139 
140 /** @addtogroup RTCEx_Exported_Functions_Group1
141   * @brief   RTC TimeStamp and Tamper functions
142   *
143 @verbatim
144  ===============================================================================
145                  ##### RTC TimeStamp and Tamper functions #####
146  ===============================================================================
147 
148  [..] This section provides functions allowing to configure TimeStamp feature
149 
150 @endverbatim
151   * @{
152   */
153 
154 /**
155   * @brief  Set TimeStamp.
156   * @note   This API must be called before enabling the TimeStamp feature.
157   * @param  hrtc RTC handle
158   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
159   *         activated.
160   *          This parameter can be one of the following values:
161   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
162   *                                        rising edge of the related pin.
163   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
164   *                                         falling edge of the related pin.
165   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
166   *          This parameter can be one of the following values:
167   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
168   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
169   *               compatibility, this parameter is required.
170   * @retval HAL status
171   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)172 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
173 {
174   /* Check the parameters */
175   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
176   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
177   UNUSED(RTC_TimeStampPin);
178 
179   /* Process Locked */
180   __HAL_LOCK(hrtc);
181 
182   hrtc->State = HAL_RTC_STATE_BUSY;
183 
184   /* Get the RTC_CR register and clear the bits to be configured */
185   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
186 
187   /* Configure the Time Stamp TSEDGE and Enable bits */
188   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge | RTC_CR_TSE);
189 
190   /* Change RTC state */
191   hrtc->State = HAL_RTC_STATE_READY;
192 
193   /* Process Unlocked */
194   __HAL_UNLOCK(hrtc);
195 
196   return HAL_OK;
197 }
198 
199 /**
200   * @brief  Set TimeStamp with Interrupt.
201   * @note   This API must be called before enabling the TimeStamp feature.
202   * @param  hrtc RTC handle
203   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
204   *         activated.
205   *          This parameter can be one of the following values:
206   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
207   *                                        rising edge of the related pin.
208   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
209   *                                         falling edge of the related pin.
210   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
211   *          This parameter can be one of the following values:
212   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
213   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
214   *               compatibility, this parameter is required.
215   * @retval HAL status
216   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)217 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
218 {
219   /* Check the parameters */
220   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
221   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
222   UNUSED(RTC_TimeStampPin);
223 
224   /* Process Locked */
225   __HAL_LOCK(hrtc);
226 
227   hrtc->State = HAL_RTC_STATE_BUSY;
228 
229   /* RTC timestamp Interrupt Configuration: EXTI configuration */
230   __HAL_RTC_TIMESTAMP_EXTI_ENABLE_IT();
231 
232   /* Get the RTC_CR register and clear the bits to be configured */
233   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
234 
235   /* Configure the Time Stamp TSEDGE before Enable bit to avoid unwanted TSF setting. */
236   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge);
237 
238   /* Enable timestamp and IT */
239   SET_BIT(RTC->CR, RTC_CR_TSE | RTC_CR_TSIE);
240 
241   hrtc->State = HAL_RTC_STATE_READY;
242 
243   /* Process Unlocked */
244   __HAL_UNLOCK(hrtc);
245 
246   return HAL_OK;
247 }
248 
249 /**
250   * @brief  Deactivate TimeStamp.
251   * @param  hrtc RTC handle
252   * @retval HAL status
253   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)254 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
255 {
256   /* Process Locked */
257   __HAL_LOCK(hrtc);
258 
259   hrtc->State = HAL_RTC_STATE_BUSY;
260 
261   /* In case of interrupt mode is used, the interrupt source must disabled */
262   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
263 
264   hrtc->State = HAL_RTC_STATE_READY;
265 
266   /* Process Unlocked */
267   __HAL_UNLOCK(hrtc);
268 
269   return HAL_OK;
270 }
271 
272 /**
273   * @brief  Set Internal TimeStamp.
274   * @note   This API must be called before enabling the internal TimeStamp feature.
275   * @param  hrtc RTC handle
276   * @retval HAL status
277   */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)278 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
279 {
280   /* Process Locked */
281   __HAL_LOCK(hrtc);
282 
283   hrtc->State = HAL_RTC_STATE_BUSY;
284 
285   /* Configure the internal Time Stamp Enable bits */
286   SET_BIT(RTC->CR, RTC_CR_ITSE);
287 
288   /* Change RTC state */
289   hrtc->State = HAL_RTC_STATE_READY;
290 
291   /* Process Unlocked */
292   __HAL_UNLOCK(hrtc);
293 
294   return HAL_OK;
295 }
296 
297 /**
298   * @brief  Deactivate Internal TimeStamp.
299   * @param  hrtc RTC handle
300   * @retval HAL status
301   */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)302 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
303 {
304   /* Process Locked */
305   __HAL_LOCK(hrtc);
306 
307   hrtc->State = HAL_RTC_STATE_BUSY;
308 
309   /* Configure the internal Time Stamp Enable bits */
310   CLEAR_BIT(RTC->CR, RTC_CR_ITSE);
311 
312   hrtc->State = HAL_RTC_STATE_READY;
313 
314   /* Process Unlocked */
315   __HAL_UNLOCK(hrtc);
316 
317   return HAL_OK;
318 }
319 
320 /**
321   * @brief  Get the RTC TimeStamp value.
322   * @param  hrtc RTC handle
323   * @param  sTimeStamp Pointer to Time structure
324   *          if BinMode = RTC_BINARY_ONLY, sTimeStamp->SubSeconds only is used
325   * @param  sTimeStampDate Pointer to Date structure
326   *          if BinMode = RTC_BINARY_ONLY, this parameter is not used.
327   * @param  Format specifies the format of the entered parameters.
328   *          if BinMode = RTC_BINARY_ONLY, this parameter is not used
329   *          else this parameter can be one of the following values
330   *             @arg RTC_FORMAT_BIN: Binary data format
331   *             @arg RTC_FORMAT_BCD: BCD data format
332   * @retval HAL status
333   */
HAL_RTCEx_GetTimeStamp(const RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)334 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(const RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp,
335                                          RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
336 {
337   uint32_t tmptime;
338   uint32_t tmpdate;
339   UNUSED(hrtc);
340 
341   sTimeStamp->SubSeconds = READ_REG(RTC->TSSSR);
342   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
343   {
344     /* Check the parameters */
345     assert_param(IS_RTC_FORMAT(Format));
346 
347     /* Get the TimeStamp time and date registers values */
348     tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
349     tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
350 
351     /* Fill the Time structure fields with the read parameters */
352     sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
353     sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
354     sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
355     sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
356     sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
357 
358     /* Fill the Date structure fields with the read parameters */
359     sTimeStampDate->Year = 0U;
360     sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
361     sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
362     sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
363 
364     /* Check the input parameters format */
365     if (Format == RTC_FORMAT_BIN)
366     {
367       /* Convert the TimeStamp structure parameters to Binary format */
368       sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
369       sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
370       sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
371 
372       /* Convert the DateTimeStamp structure parameters to Binary format */
373       sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
374       sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
375       sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
376     }
377   }
378 
379   /* Clear the TIMESTAMP Flags */
380   WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
381 
382   return HAL_OK;
383 }
384 
385 /**
386   * @brief  Handle TimeStamp interrupt request.
387   * @param  hrtc RTC handle
388   * @retval None
389   */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)390 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
391 {
392   if (READ_BIT(RTC->MISR, RTC_MISR_TSMF) != 0U)
393   {
394 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
395     /* Call TimeStampEvent registered Callback */
396     hrtc->TimeStampEventCallback(hrtc);
397 #else
398     HAL_RTCEx_TimeStampEventCallback(hrtc);
399 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
400     /* Clearing flags after the Callback because the content
401        of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset. */
402     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
403   }
404 
405   /* Change RTC state */
406   hrtc->State = HAL_RTC_STATE_READY;
407 }
408 
409 
410 /**
411   * @brief  TimeStamp callback.
412   * @param  hrtc RTC handle
413   * @retval None
414   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)415 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
416 {
417   /* Prevent unused argument(s) compilation warning */
418   UNUSED(hrtc);
419 
420   /* NOTE : This function should not be modified, when the callback is needed,
421             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
422   */
423 }
424 
425 
426 /**
427   * @brief  Handle TimeStamp polling request.
428   * @param  hrtc RTC handle
429   * @param  Timeout Timeout duration
430   * @retval HAL status
431   */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)432 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
433 {
434   uint32_t tickstart = HAL_GetTick();
435 
436   while (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
437   {
438     if (READ_BIT(RTC->SR, RTC_SR_TSOVF) != 0U)
439     {
440       /* Clear the TIMESTAMP OverRun Flag */
441       WRITE_REG(RTC->SCR, RTC_SCR_CTSOVF);
442 
443       /* Change TIMESTAMP state */
444       hrtc->State = HAL_RTC_STATE_ERROR;
445 
446       return HAL_ERROR;
447     }
448 
449     if (Timeout != HAL_MAX_DELAY)
450     {
451       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
452       {
453         hrtc->State = HAL_RTC_STATE_TIMEOUT;
454         return HAL_TIMEOUT;
455       }
456     }
457   }
458 
459   /* Change RTC state */
460   hrtc->State = HAL_RTC_STATE_READY;
461 
462   return HAL_OK;
463 }
464 
465 /**
466   * @}
467   */
468 
469 /** @addtogroup RTCEx_Exported_Functions_Group2
470   * @brief    RTC Wake-up functions
471   *
472 @verbatim
473  ===============================================================================
474                         ##### RTC Wake-up functions #####
475  ===============================================================================
476 
477  [..] This section provides functions allowing to configure Wake-up feature
478 
479 @endverbatim
480   * @{
481   */
482 
483 /**
484   * @brief  Set wake up timer.
485   * @param  hrtc RTC handle
486   * @param  WakeUpCounter Wake up counter
487   * @param  WakeUpClock Wake up clock
488   * @retval HAL status
489   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)490 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
491 {
492   uint32_t tickstart;
493 
494   /* Check the parameters */
495   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
496   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
497 
498   /* Process Locked */
499   __HAL_LOCK(hrtc);
500 
501   hrtc->State = HAL_RTC_STATE_BUSY;
502 
503   /* Check RTC WUTWF flag is reset only when wake up timer enabled*/
504   if (READ_BIT(RTC->CR, RTC_CR_WUTE) != 0U)
505   {
506     tickstart = HAL_GetTick();
507 
508     /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
509     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) != 0U)
510     {
511       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
512       {
513         hrtc->State = HAL_RTC_STATE_TIMEOUT;
514 
515         /* Process Unlocked */
516         __HAL_UNLOCK(hrtc);
517 
518         return HAL_TIMEOUT;
519       }
520     }
521   }
522 
523   /* Disable Wake Up timer */
524   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
525 
526   tickstart = HAL_GetTick();
527 
528   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
529   while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
530   {
531     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
532     {
533       hrtc->State = HAL_RTC_STATE_TIMEOUT;
534 
535       /* Process Unlocked */
536       __HAL_UNLOCK(hrtc);
537 
538       return HAL_TIMEOUT;
539     }
540   }
541 
542   /* Configure the clock source */
543   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
544 
545   /* Configure the Wakeup Timer counter */
546   WRITE_REG(RTC->WUTR, (uint32_t)WakeUpCounter);
547 
548   /* Enable the Wakeup Timer */
549   SET_BIT(RTC->CR, RTC_CR_WUTE);
550 
551   /* Enable the write protection for RTC registers */
552   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
553 
554   hrtc->State = HAL_RTC_STATE_READY;
555 
556   /* Process Unlocked */
557   __HAL_UNLOCK(hrtc);
558 
559   return HAL_OK;
560 }
561 
562 /**
563   * @brief  Set wake up timer with interrupt.
564   * @param  hrtc RTC handle
565   * @param  WakeUpCounter Wake up counter
566   * @param  WakeUpClock Wake up clock
567   * @param  WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
568   *                       - No effect if WakeUpAutoClr is set to zero
569   *                       - This feature is meaningful in case of Low power mode to avoid
570   *                         any RTC software execution after Wake Up.
571   *                         That is why when WakeUpAutoClr is set,
572   *                         EXTI is configured as EVENT instead of Interrupt to avoid useless IRQ handler execution.
573   * @retval HAL status
574   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock,uint32_t WakeUpAutoClr)575 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock,
576                                               uint32_t WakeUpAutoClr)
577 {
578   uint32_t tickstart;
579 
580   /* Check the parameters */
581   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
582   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
583   /* (0x0000<=WUTOCLR<=WUT) */
584   assert_param(WakeUpAutoClr <= WakeUpCounter);
585 
586   /* Process Locked */
587   __HAL_LOCK(hrtc);
588 
589   hrtc->State = HAL_RTC_STATE_BUSY;
590 
591   /* Check RTC WUTWF flag is reset only when wake up timer enabled*/
592   if (READ_BIT(RTC->CR, RTC_CR_WUTE) != 0U)
593   {
594     tickstart = HAL_GetTick();
595 
596     /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
597     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) != 0U)
598     {
599       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
600       {
601         hrtc->State = HAL_RTC_STATE_TIMEOUT;
602 
603         /* Process Unlocked */
604         __HAL_UNLOCK(hrtc);
605 
606         return HAL_TIMEOUT;
607       }
608     }
609   }
610   /* Disable the Wake-Up timer */
611   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
612 
613   /* Clear flag Wake-Up */
614   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
615 
616   tickstart = HAL_GetTick();
617 
618   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
619   while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
620   {
621     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
622     {
623       /* Enable the write protection for RTC registers */
624       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
625 
626       hrtc->State = HAL_RTC_STATE_TIMEOUT;
627 
628       /* Process Unlocked */
629       __HAL_UNLOCK(hrtc);
630 
631       return HAL_TIMEOUT;
632     }
633   }
634 
635   /* Configure the Wakeup Timer counter and auto clear value */
636   WRITE_REG(RTC->WUTR, (uint32_t)(WakeUpCounter | (WakeUpAutoClr << RTC_WUTR_WUTOCLR_Pos)));
637 
638   /* Configure the clock source */
639   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
640 
641   /* In case of WUT autoclr, the IRQ handler should not be called */
642   if (WakeUpAutoClr != 0U)
643   {
644     /* RTC WakeUpTimer EXTI Configuration: Event configuration */
645     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
646   }
647   else
648   {
649     /* RTC WakeUpTimer EXTI Configuration: Interrupt configuration */
650     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
651   }
652 
653   /* Configure the Interrupt in the RTC_CR register and Enable the Wakeup Timer*/
654   SET_BIT(RTC->CR, (RTC_CR_WUTIE | RTC_CR_WUTE));
655 
656   hrtc->State = HAL_RTC_STATE_READY;
657 
658   /* Process Unlocked */
659   __HAL_UNLOCK(hrtc);
660 
661   return HAL_OK;
662 }
663 
664 /**
665   * @brief  Deactivate wake up timer counter.
666   * @param  hrtc RTC handle
667   * @retval HAL status
668   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)669 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
670 {
671 
672   /* Process Locked */
673   __HAL_LOCK(hrtc);
674 
675   /* Change RTC state */
676   hrtc->State = HAL_RTC_STATE_BUSY;
677 
678   /* Disable the Wakeup Timer */
679   /* In case of interrupt mode is used, the interrupt source must disabled */
680   CLEAR_BIT(RTC->CR, (RTC_CR_WUTE | RTC_CR_WUTIE));
681 
682   /* Change RTC state */
683   hrtc->State = HAL_RTC_STATE_READY;
684 
685   /* Process Unlocked */
686   __HAL_UNLOCK(hrtc);
687 
688   return HAL_OK;
689 }
690 
691 /**
692   * @brief  Get wake up timer counter.
693   * @param  hrtc RTC handle
694   * @retval Counter value
695   */
HAL_RTCEx_GetWakeUpTimer(const RTC_HandleTypeDef * hrtc)696 uint32_t HAL_RTCEx_GetWakeUpTimer(const RTC_HandleTypeDef *hrtc)
697 {
698   UNUSED(hrtc);
699   /* Get the counter value */
700   return (uint32_t)(READ_BIT(RTC->WUTR, RTC_WUTR_WUT));
701 }
702 
703 /**
704   * @brief  Handle Wake Up Timer interrupt request.
705   * @param  hrtc RTC handle
706   * @retval None
707   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)708 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
709 {
710   /* Get the pending status of the WAKEUPTIMER Interrupt */
711   if (READ_BIT(RTC->MISR, RTC_MISR_WUTMF) != 0U)
712   {
713     /* Clear the WAKEUPTIMER interrupt pending bit */
714     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
715 
716 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
717     /* Call WakeUpTimerEvent registered Callback */
718     hrtc->WakeUpTimerEventCallback(hrtc);
719 #else
720     /* WAKEUPTIMER callback */
721     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
722 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
723   }
724 
725   /* Change RTC state */
726   hrtc->State = HAL_RTC_STATE_READY;
727 }
728 
729 /**
730   * @brief  Wake Up Timer callback.
731   * @param  hrtc RTC handle
732   * @retval None
733   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)734 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
735 {
736   /* Prevent unused argument(s) compilation warning */
737   UNUSED(hrtc);
738 
739   /* NOTE : This function should not be modified, when the callback is needed,
740             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
741    */
742 }
743 
744 
745 /**
746   * @brief  Handle Wake Up Timer Polling.
747   * @param  hrtc RTC handle
748   * @param  Timeout Timeout duration
749   * @retval HAL status
750   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)751 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
752 {
753   uint32_t tickstart = HAL_GetTick();
754 
755   while (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
756   {
757     if (Timeout != HAL_MAX_DELAY)
758     {
759       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
760       {
761         hrtc->State = HAL_RTC_STATE_TIMEOUT;
762         return HAL_TIMEOUT;
763       }
764     }
765   }
766 
767   /* Clear the WAKEUPTIMER Flag */
768   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
769 
770   /* Change RTC state */
771   hrtc->State = HAL_RTC_STATE_READY;
772 
773   return HAL_OK;
774 }
775 
776 /**
777   * @}
778   */
779 
780 
781 /** @addtogroup RTCEx_Exported_Functions_Group3
782   * @brief    Extended Peripheral Control functions
783   *
784 @verbatim
785  ===============================================================================
786               ##### Extended Peripheral Control functions #####
787  ===============================================================================
788     [..]
789     This subsection provides functions allowing to
790       (+) Write a data in a specified RTC Backup data register
791       (+) Read a data in a specified RTC Backup data register
792       (+) Set the Coarse calibration parameters.
793       (+) Deactivate the Coarse calibration parameters
794       (+) Set the Smooth calibration parameters.
795       (+) Set Low Power calibration parameter.
796       (+) Configure the Synchronization Shift Control Settings.
797       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
798       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
799       (+) Enable the RTC reference clock detection.
800       (+) Disable the RTC reference clock detection.
801       (+) Enable the Bypass Shadow feature.
802       (+) Disable the Bypass Shadow feature.
803 
804 @endverbatim
805   * @{
806   */
807 
808 
809 /**
810   * @brief  Set the Smooth calibration parameters.
811   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
812   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
813   *         SmoothCalibMinusPulsesValue must be equal to 0.
814   * @param  hrtc RTC handle
815   * @param  SmoothCalibPeriod  Select the Smooth Calibration Period.
816   *          This parameter can be one of the following values :
817   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
818   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
819   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
820   * @param  SmoothCalibPlusPulses  Select to Set or reset the CALP bit.
821   *          This parameter can be one of the following values:
822   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
823   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
824   * @param  SmoothCalibMinusPulsesValue  Select the value of CALM[8:0] bits.
825   *          This parameter can be one any value from 0 to 0x000001FF.
826   * @retval HAL status
827   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)828 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod,
829                                            uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
830 {
831   uint32_t tickstart;
832 
833   /* Check the parameters */
834   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
835   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
836   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
837 
838   /* Process Locked */
839   __HAL_LOCK(hrtc);
840 
841   hrtc->State = HAL_RTC_STATE_BUSY;
842 
843   /* check if a calibration is pending*/
844   if (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
845   {
846     tickstart = HAL_GetTick();
847 
848     /* check if a calibration is pending*/
849     while (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
850     {
851       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
852       {
853         /* Change RTC state */
854         hrtc->State = HAL_RTC_STATE_TIMEOUT;
855 
856         /* Process Unlocked */
857         __HAL_UNLOCK(hrtc);
858 
859         return HAL_TIMEOUT;
860       }
861     }
862   }
863 
864   /* Disable the write protection for RTC registers */
865   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
866 
867   /* Configure the Smooth calibration settings */
868   MODIFY_REG(RTC->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM),
869              (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
870 
871   /* Enable the write protection for RTC registers */
872   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
873 
874   /* Change RTC state */
875   hrtc->State = HAL_RTC_STATE_READY;
876 
877   /* Process Unlocked */
878   __HAL_UNLOCK(hrtc);
879 
880   return HAL_OK;
881 }
882 
883 /**
884   * @brief  Select the low power Calibration mode.
885   * @param  hrtc: RTC handle
886   * @param  LowPowerCalib: Low power Calibration mode.
887   *          This parameter can be one of the following values :
888   *             @arg RTC_LPCAL_SET: Low power mode.
889   *             @arg RTC_LPCAL_RESET: High consumption mode.
890   * @retval HAL status
891   */
HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef * hrtc,uint32_t LowPowerCalib)892 HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
893 {
894   /* Check the parameters */
895   assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
896 
897   /* Process Locked */
898   __HAL_LOCK(hrtc);
899 
900   hrtc->State = HAL_RTC_STATE_BUSY;
901 
902   /* Disable the write protection for RTC registers */
903   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
904 
905   /* Configure the Smooth calibration settings */
906   MODIFY_REG(RTC->CALR, RTC_CALR_LPCAL, LowPowerCalib);
907 
908   /* Enable the write protection for RTC registers */
909   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
910 
911   /* Change RTC state */
912   hrtc->State = HAL_RTC_STATE_READY;
913 
914   /* Process Unlocked */
915   __HAL_UNLOCK(hrtc);
916 
917   return HAL_OK;
918 }
919 
920 /**
921   * @brief  Configure the Synchronization Shift Control Settings.
922   * @note   When REFCKON is set, firmware must not write to Shift control register.
923   * @param  hrtc RTC handle
924   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
925   *          This parameter can be one of the following values:
926   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
927   *             @arg RTC_SHIFTADD1S_RESET: No effect.
928   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
929   *          This parameter can be one any value from 0 to 0x7FFF.
930   * @retval HAL status
931   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)932 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
933 {
934   uint32_t tickstart;
935 
936   /* Check the parameters */
937   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
938   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
939 
940   /* Process Locked */
941   __HAL_LOCK(hrtc);
942 
943   hrtc->State = HAL_RTC_STATE_BUSY;
944 
945   tickstart = HAL_GetTick();
946 
947   /* Wait until the shift is completed*/
948   while (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
949   {
950     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
951     {
952       hrtc->State = HAL_RTC_STATE_TIMEOUT;
953 
954       /* Process Unlocked */
955       __HAL_UNLOCK(hrtc);
956 
957       return HAL_TIMEOUT;
958     }
959   }
960 
961   /* Disable the write protection for RTC registers */
962   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
963   /* Check if the reference clock detection is disabled */
964   if (READ_BIT(RTC->CR, RTC_CR_REFCKON) == 0U)
965   {
966     /* Configure the Shift settings */
967     MODIFY_REG(RTC->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
968 
969     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
970     if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
971     {
972       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
973       {
974         /* Enable the write protection for RTC registers */
975         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
976 
977         hrtc->State = HAL_RTC_STATE_ERROR;
978 
979         /* Process Unlocked */
980         __HAL_UNLOCK(hrtc);
981 
982         return HAL_ERROR;
983       }
984     }
985   }
986   else
987   {
988     /* Enable the write protection for RTC registers */
989     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
990 
991     /* Change RTC state */
992     hrtc->State = HAL_RTC_STATE_ERROR;
993 
994     /* Process Unlocked */
995     __HAL_UNLOCK(hrtc);
996 
997     return HAL_ERROR;
998   }
999 
1000   /* Enable the write protection for RTC registers */
1001   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1002 
1003   /* Change RTC state */
1004   hrtc->State = HAL_RTC_STATE_READY;
1005 
1006   /* Process Unlocked */
1007   __HAL_UNLOCK(hrtc);
1008 
1009   return HAL_OK;
1010 }
1011 
1012 /**
1013   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1014   * @param  hrtc RTC handle
1015   * @param  CalibOutput Select the Calibration output Selection .
1016   *          This parameter can be one of the following values:
1017   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1018   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1019   * @retval HAL status
1020   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1021 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1022 {
1023   /* Check the parameters */
1024   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1025 
1026   /* Process Locked */
1027   __HAL_LOCK(hrtc);
1028 
1029   hrtc->State = HAL_RTC_STATE_BUSY;
1030 
1031   /* Configure the RTC_CR register */
1032   MODIFY_REG(RTC->CR, RTC_CR_COSEL, CalibOutput);
1033 
1034   /* Enable calibration output */
1035   SET_BIT(RTC->CR, RTC_CR_COE);
1036 
1037   /* Change RTC state */
1038   hrtc->State = HAL_RTC_STATE_READY;
1039 
1040   /* Process Unlocked */
1041   __HAL_UNLOCK(hrtc);
1042 
1043   return HAL_OK;
1044 }
1045 
1046 /**
1047   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1048   * @param  hrtc RTC handle
1049   * @retval HAL status
1050   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1051 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1052 {
1053   /* Process Locked */
1054   __HAL_LOCK(hrtc);
1055 
1056   hrtc->State = HAL_RTC_STATE_BUSY;
1057 
1058   /* Disable calibration output */
1059   CLEAR_BIT(RTC->CR, RTC_CR_COE);
1060 
1061   /* Change RTC state */
1062   hrtc->State = HAL_RTC_STATE_READY;
1063 
1064   /* Process Unlocked */
1065   __HAL_UNLOCK(hrtc);
1066 
1067   return HAL_OK;
1068 }
1069 
1070 /**
1071   * @brief  Enable the RTC reference clock detection.
1072   * @param  hrtc RTC handle
1073   * @retval HAL status
1074   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1075 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1076 {
1077   /* Process Locked */
1078   __HAL_LOCK(hrtc);
1079 
1080   hrtc->State = HAL_RTC_STATE_BUSY;
1081 
1082   /* Disable the write protection for RTC registers */
1083   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1084 
1085   /* Set Initialization mode */
1086   if (RTC_EnterInitMode(hrtc) != HAL_OK)
1087   {
1088     /* Enable the write protection for RTC registers */
1089     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1090 
1091     /* Set RTC state*/
1092     hrtc->State = HAL_RTC_STATE_ERROR;
1093 
1094     /* Process Unlocked */
1095     __HAL_UNLOCK(hrtc);
1096 
1097     return HAL_ERROR;
1098   }
1099   else
1100   {
1101     /* Enable clockref detection */
1102     SET_BIT(RTC->CR, RTC_CR_REFCKON);
1103 
1104     /* Exit Initialization mode */
1105     CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1106   }
1107 
1108   /* Enable the write protection for RTC registers */
1109   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1110 
1111   /* Change RTC state */
1112   hrtc->State = HAL_RTC_STATE_READY;
1113 
1114   /* Process Unlocked */
1115   __HAL_UNLOCK(hrtc);
1116 
1117   return HAL_OK;
1118 }
1119 
1120 /**
1121   * @brief  Disable the RTC reference clock detection.
1122   * @param  hrtc RTC handle
1123   * @retval HAL status
1124   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1125 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1126 {
1127   /* Process Locked */
1128   __HAL_LOCK(hrtc);
1129 
1130   hrtc->State = HAL_RTC_STATE_BUSY;
1131 
1132   /* Disable the write protection for RTC registers */
1133   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1134 
1135   /* Set Initialization mode */
1136   if (RTC_EnterInitMode(hrtc) != HAL_OK)
1137   {
1138     /* Enable the write protection for RTC registers */
1139     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1140 
1141     /* Set RTC state*/
1142     hrtc->State = HAL_RTC_STATE_ERROR;
1143 
1144     /* Process Unlocked */
1145     __HAL_UNLOCK(hrtc);
1146 
1147     return HAL_ERROR;
1148   }
1149   else
1150   {
1151     /* Disable clockref detection */
1152     CLEAR_BIT(RTC->CR, RTC_CR_REFCKON);
1153 
1154     /* Exit Initialization mode */
1155     CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1156   }
1157 
1158   /* Enable the write protection for RTC registers */
1159   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1160 
1161   /* Change RTC state */
1162   hrtc->State = HAL_RTC_STATE_READY;
1163 
1164   /* Process Unlocked */
1165   __HAL_UNLOCK(hrtc);
1166 
1167   return HAL_OK;
1168 }
1169 
1170 /**
1171   * @brief  Enable the Bypass Shadow feature.
1172   * @note   When the Bypass Shadow is enabled the calendar value are taken
1173   *         directly from the Calendar counter.
1174   * @param  hrtc RTC handle
1175   * @retval HAL status
1176   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1177 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1178 {
1179   /* Process Locked */
1180   __HAL_LOCK(hrtc);
1181 
1182   hrtc->State = HAL_RTC_STATE_BUSY;
1183 
1184   /* Set the BYPSHAD bit */
1185   SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1186 
1187   /* Change RTC state */
1188   hrtc->State = HAL_RTC_STATE_READY;
1189 
1190   /* Process Unlocked */
1191   __HAL_UNLOCK(hrtc);
1192 
1193   return HAL_OK;
1194 }
1195 
1196 /**
1197   * @brief  Disable the Bypass Shadow feature.
1198   * @note   When the Bypass Shadow is enabled the calendar value are taken
1199   *         directly from the Calendar counter.
1200   * @param  hrtc RTC handle
1201   * @retval HAL status
1202   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1203 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1204 {
1205   /* Process Locked */
1206   __HAL_LOCK(hrtc);
1207 
1208   hrtc->State = HAL_RTC_STATE_BUSY;
1209 
1210   /* Reset the BYPSHAD bit */
1211   CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1212 
1213   /* Change RTC state */
1214   hrtc->State = HAL_RTC_STATE_READY;
1215 
1216   /* Process Unlocked */
1217   __HAL_UNLOCK(hrtc);
1218 
1219   return HAL_OK;
1220 }
1221 
1222 /**
1223   * @brief  Set SSR Underflow detection with Interrupt.
1224   * @param  hrtc RTC handle
1225   * @retval HAL status
1226   */
HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef * hrtc)1227 HAL_StatusTypeDef HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef *hrtc)
1228 {
1229   /* Process Locked */
1230   __HAL_LOCK(hrtc);
1231 
1232   hrtc->State = HAL_RTC_STATE_BUSY;
1233 
1234   /* Enable IT SSRU */
1235   __HAL_RTC_SSRU_ENABLE_IT(hrtc, RTC_IT_SSRU);
1236 
1237   /* RTC SSRU Interrupt Configuration: EXTI configuration */
1238   __HAL_RTC_SSRU_EXTI_ENABLE_IT();
1239 
1240   hrtc->State = HAL_RTC_STATE_READY;
1241 
1242   /* Process Unlocked */
1243   __HAL_UNLOCK(hrtc);
1244 
1245   return HAL_OK;
1246 }
1247 
1248 /**
1249   * @brief  Deactivate SSR Underflow.
1250   * @param  hrtc RTC handle
1251   * @retval HAL status
1252   */
HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef * hrtc)1253 HAL_StatusTypeDef HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef *hrtc)
1254 {
1255   /* Process Locked */
1256   __HAL_LOCK(hrtc);
1257 
1258   hrtc->State = HAL_RTC_STATE_BUSY;
1259 
1260   /* In case of interrupt mode is used, the interrupt source must disabled */
1261   __HAL_RTC_SSRU_DISABLE_IT(hrtc, RTC_IT_SSRU);
1262 
1263   hrtc->State = HAL_RTC_STATE_READY;
1264 
1265   /* Process Unlocked */
1266   __HAL_UNLOCK(hrtc);
1267 
1268   return HAL_OK;
1269 }
1270 
1271 /**
1272   * @brief  Handle SSR underflow interrupt request.
1273   * @param  hrtc RTC handle
1274   * @retval None
1275   */
HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef * hrtc)1276 void HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef *hrtc)
1277 {
1278   if ((RTC->MISR & RTC_MISR_SSRUMF) != 0u)
1279   {
1280     /* Immediately clear flags */
1281     RTC->SCR = RTC_SCR_CSSRUF;
1282 
1283     /* SSRU callback */
1284 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1285     /* Call SSRUEvent registered Callback */
1286     hrtc->SSRUEventCallback(hrtc);
1287 #else
1288     HAL_RTCEx_SSRUEventCallback(hrtc);
1289 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1290   }
1291 
1292   /* Change RTC state */
1293   hrtc->State = HAL_RTC_STATE_READY;
1294 }
1295 
1296 /**
1297   * @brief  SSR underflow callback.
1298   * @param  hrtc RTC handle
1299   * @retval None
1300   */
HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef * hrtc)1301 __weak void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
1302 {
1303   /* Prevent unused argument(s) compilation warning */
1304   UNUSED(hrtc);
1305 
1306   /* NOTE : This function should not be modified, when the callback is needed,
1307             the HAL_RTCEx_SSRUEventCallback could be implemented in the user file
1308    */
1309 }
1310 
1311 /**
1312   * @}
1313   */
1314 
1315 /** @addtogroup RTCEx_Exported_Functions_Group4
1316   * @brief    Extended features functions
1317   *
1318 @verbatim
1319  ===============================================================================
1320                  ##### Extended features functions #####
1321  ===============================================================================
1322     [..]  This section provides functions allowing to:
1323       (+) RTC Alarm B callback
1324       (+) RTC Poll for Alarm B request
1325 
1326 @endverbatim
1327   * @{
1328   */
1329 
1330 /**
1331   * @brief  Alarm B callback.
1332   * @param  hrtc RTC handle
1333   * @retval None
1334   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1335 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1336 {
1337   /* Prevent unused argument(s) compilation warning */
1338   UNUSED(hrtc);
1339 
1340   /* NOTE : This function should not be modified, when the callback is needed,
1341             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1342    */
1343 }
1344 
1345 /**
1346   * @brief  Handle Alarm B Polling request.
1347   * @param  hrtc RTC handle
1348   * @param  Timeout Timeout duration
1349   * @retval HAL status
1350   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1351 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1352 {
1353   uint32_t tickstart = HAL_GetTick();
1354 
1355   while (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1356   {
1357     if (Timeout != HAL_MAX_DELAY)
1358     {
1359       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1360       {
1361         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1362         return HAL_TIMEOUT;
1363       }
1364     }
1365   }
1366 
1367   /* Clear the Alarm Flag */
1368   WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1369 
1370   /* Change RTC state */
1371   hrtc->State = HAL_RTC_STATE_READY;
1372 
1373   return HAL_OK;
1374 }
1375 
1376 /**
1377   * @}
1378   */
1379 
1380 /** @addtogroup RTCEx_Exported_Functions_Group5
1381   * @brief      Extended RTC Tamper functions
1382   *
1383 @verbatim
1384   ==============================================================================
1385                          ##### Tamper functions #####
1386   ==============================================================================
1387   [..]
1388    (+) Before calling any tamper or internal tamper function, you have to call first
1389        HAL_RTC_Init() function.
1390    (+) In that one you can select to output tamper event on RTC pin.
1391   [..]
1392    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1393        or Level according to the Tamper filter (if equal to 0 Edge else Level)
1394        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1395        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1396        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1397    (+) The default configuration of the Tamper erases the backup registers. To avoid
1398        erase, enable the NoErase field on the TAMP_TAMPCR register.
1399   [..]
1400    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1401        the HAL_RTCEx_SetInternalTamper() function.
1402 
1403 @endverbatim
1404   * @{
1405   */
1406 
1407 
1408 /**
1409   * @brief  Set Tamper
1410   * @param  hrtc RTC handle
1411   * @param  sTamper Pointer to Tamper Structure.
1412   * @retval HAL status
1413   */
HAL_RTCEx_SetTamper(const RTC_HandleTypeDef * hrtc,const RTC_TamperTypeDef * sTamper)1414 HAL_StatusTypeDef HAL_RTCEx_SetTamper(const RTC_HandleTypeDef *hrtc, const RTC_TamperTypeDef *sTamper)
1415 {
1416   uint32_t tmpreg;
1417 
1418   /* Prevent unused argument(s) compilation warning */
1419   UNUSED(hrtc);
1420 
1421   /* Check the parameters */
1422   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1423   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1424   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1425   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1426   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1427   /* Mask flag only supported by TAMPER 1, 2 and 3 */
1428   assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_3)));
1429   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1430   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1431   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1432   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1433   /* Trigger and Filter have exclusive configurations */
1434   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || \
1435                                                                   (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
1436                || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && \
1437                    ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || \
1438                     (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1439 
1440   /* Configuration register 2 */
1441   tmpreg = READ_REG(TAMP->CR2);
1442   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1443               (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos));
1444 
1445   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1446   {
1447     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1448   }
1449 
1450   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1451   {
1452     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1453   }
1454 
1455   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1456   {
1457     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos);
1458   }
1459   WRITE_REG(TAMP->CR2, tmpreg);
1460 
1461   /* Filter control register */
1462   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | \
1463             sTamper->TamperPullUp);
1464 
1465   /* Timestamp on tamper */
1466   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1467   {
1468     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1469   }
1470 
1471   /* Control register 1 */
1472   SET_BIT(TAMP->CR1, sTamper->Tamper);
1473 
1474   return HAL_OK;
1475 }
1476 
1477 
1478 /**
1479   * @brief  Set Tamper in IT mode
1480   * @param  hrtc RTC handle
1481   * @param  sTamper Pointer to Tamper Structure.
1482   * @retval HAL status
1483   */
HAL_RTCEx_SetTamper_IT(const RTC_HandleTypeDef * hrtc,const RTC_TamperTypeDef * sTamper)1484 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(const RTC_HandleTypeDef *hrtc, const RTC_TamperTypeDef *sTamper)
1485 {
1486   uint32_t tmpreg;
1487 
1488   /* Prevent unused argument(s) compilation warning */
1489   UNUSED(hrtc);
1490 
1491   /* Check the parameters */
1492   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1493   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1494   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1495   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1496   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1497   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1498   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1499   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1500   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1501 
1502   /* Configuration register 2 */
1503   tmpreg = READ_REG(TAMP->CR2);
1504   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1505               (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos));
1506 
1507   if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1508   {
1509     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1510   }
1511 
1512   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1513   {
1514     /* Feature only supported by TAMPER 1, 2 and 3 */
1515     if (sTamper->Tamper < RTC_TAMPER_4)
1516     {
1517       tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1518     }
1519     else
1520     {
1521       return HAL_ERROR;
1522     }
1523   }
1524 
1525   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1526   {
1527     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos);
1528   }
1529   WRITE_REG(TAMP->CR2, tmpreg);
1530 
1531   /* Filter control register */
1532   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | \
1533             sTamper->TamperPullUp);
1534 
1535   /* Timestamp on tamper */
1536   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1537   {
1538     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1539   }
1540 
1541   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1542   __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1543 
1544   /* Interrupt enable register */
1545   SET_BIT(TAMP->IER, sTamper->Tamper);
1546 
1547   /* Control register 1 */
1548   SET_BIT(TAMP->CR1, sTamper->Tamper);
1549 
1550   return HAL_OK;
1551 }
1552 
1553 /**
1554   * @brief  Deactivate Tamper.
1555   * @param  hrtc RTC handle
1556   * @param  Tamper Selected tamper pin.
1557   *         This parameter can be a combination of the following values:
1558   *         @arg RTC_TAMPER_1
1559   *         @arg RTC_TAMPER_2
1560   *         @arg RTC_TAMPER_3
1561   *         @arg RTC_TAMPER_4
1562   *         @arg RTC_TAMPER_5
1563   * @retval HAL status
1564   */
HAL_RTCEx_DeactivateTamper(const RTC_HandleTypeDef * hrtc,uint32_t Tamper)1565 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(const RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1566 {
1567   UNUSED(hrtc);
1568   assert_param(IS_RTC_TAMPER(Tamper));
1569 
1570   /* Disable the selected Tamper pin */
1571   CLEAR_BIT(TAMP->CR1, Tamper);
1572 
1573   /* Clear tamper mask/noerase/trigger configuration */
1574   CLEAR_BIT(TAMP->CR2, (Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1575             (Tamper << TAMP_CR2_TAMP1POM_Pos));
1576 
1577   /* Clear tamper interrupt mode configuration */
1578   CLEAR_BIT(TAMP->IER, Tamper);
1579 
1580   /* Clear tamper interrupt and event flags (WO register) */
1581   WRITE_REG(TAMP->SCR, Tamper);
1582 
1583   return HAL_OK;
1584 }
1585 
1586 /**
1587   * @brief  Tamper event polling.
1588   * @param  hrtc RTC handle
1589   * @param  Tamper Selected tamper pin.
1590   *         This parameter can be a combination of the following values:
1591   *         @arg RTC_TAMPER_1
1592   *         @arg RTC_TAMPER_2
1593   *         @arg RTC_TAMPER_3
1594   *         @arg RTC_TAMPER_4
1595   *         @arg RTC_TAMPER_5
1596   * @param  Timeout Timeout duration
1597   * @retval HAL status
1598   */
HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t Tamper,uint32_t Timeout)1599 HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
1600 {
1601   UNUSED(hrtc);
1602   assert_param(IS_RTC_TAMPER(Tamper));
1603 
1604   uint32_t tickstart = HAL_GetTick();
1605 
1606   /* Get the status of the Interrupt */
1607   while (READ_BIT(TAMP->SR, Tamper) != Tamper)
1608   {
1609     if (Timeout != HAL_MAX_DELAY)
1610     {
1611       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1612       {
1613         return HAL_TIMEOUT;
1614       }
1615     }
1616   }
1617 
1618   /* Clear the Tamper Flag */
1619   WRITE_REG(TAMP->SCR, Tamper);
1620 
1621   return HAL_OK;
1622 }
1623 
1624 /**
1625   * @brief  Set Internal Tamper
1626   * @param  hrtc RTC handle
1627   * @param  sIntTamper Pointer to Internal Tamper Structure.
1628   * @retval HAL status
1629   */
HAL_RTCEx_SetInternalTamper(const RTC_HandleTypeDef * hrtc,const RTC_InternalTamperTypeDef * sIntTamper)1630 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(const RTC_HandleTypeDef *hrtc,
1631                                               const RTC_InternalTamperTypeDef *sIntTamper)
1632 {
1633   /* Prevent unused argument(s) compilation warning */
1634   UNUSED(hrtc);
1635 
1636   /* Check the parameters */
1637   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1638   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1639   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
1640 
1641   /* timestamp on internal tamper */
1642   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1643   {
1644     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1645   }
1646 
1647   /* Control register 1 */
1648   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1649 
1650   if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_DISABLE)
1651   {
1652     /* Control register 3 */
1653     SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP3E_Pos - TAMP_CR3_ITAMP3POM_Pos)));
1654   }
1655   return HAL_OK;
1656 }
1657 
1658 
1659 /**
1660   * @brief  Set Internal Tamper in interrupt mode.
1661   * @param  hrtc RTC handle
1662   * @param  sIntTamper Pointer to Internal Tamper Structure.
1663   * @retval HAL status
1664   */
HAL_RTCEx_SetInternalTamper_IT(const RTC_HandleTypeDef * hrtc,const RTC_InternalTamperTypeDef * sIntTamper)1665 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(const RTC_HandleTypeDef *hrtc,
1666                                                  const RTC_InternalTamperTypeDef *sIntTamper)
1667 {
1668   /* Prevent unused argument(s) compilation warning */
1669   UNUSED(hrtc);
1670 
1671   /* Check the parameters */
1672   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1673   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1674   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
1675 
1676   /* timestamp on internal tamper */
1677   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1678   {
1679     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1680   }
1681 
1682   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1683   __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1684 
1685   /* Interrupt enable register */
1686   SET_BIT(TAMP->IER, sIntTamper->IntTamper);
1687 
1688   /* Control register 1 */
1689   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1690 
1691   if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_DISABLE)
1692   {
1693     /* Control register 3 */
1694     SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP3E_Pos - TAMP_CR3_ITAMP3POM_Pos)));
1695   }
1696   return HAL_OK;
1697 }
1698 
1699 /**
1700   * @brief  Deactivate Internal Tamper.
1701   * @param  hrtc RTC handle
1702   * @param  IntTamper Selected internal tamper event.
1703   *          This parameter can be any combination of existing internal tampers.
1704   * @retval HAL status
1705   */
HAL_RTCEx_DeactivateInternalTamper(const RTC_HandleTypeDef * hrtc,uint32_t IntTamper)1706 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(const RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
1707 {
1708   UNUSED(hrtc);
1709   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1710 
1711   /* Disable the selected Tamper pin */
1712   CLEAR_BIT(TAMP->CR1, IntTamper);
1713 
1714   /* Clear internal tamper interrupt mode configuration */
1715   CLEAR_BIT(TAMP->IER, IntTamper);
1716 
1717   /* Clear internal tamper interrupt */
1718   WRITE_REG(TAMP->SCR, IntTamper);
1719 
1720   return HAL_OK;
1721 }
1722 
1723 
1724 /**
1725   * @brief  Internal Tamper event polling.
1726   * @param  hrtc RTC handle
1727   * @param  IntTamper selected tamper.
1728   *          This parameter can be any combination of existing internal tampers.
1729   * @param  Timeout Timeout duration
1730   * @retval HAL status
1731   */
HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t IntTamper,uint32_t Timeout)1732 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
1733 {
1734   UNUSED(hrtc);
1735   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1736 
1737   uint32_t tickstart = HAL_GetTick();
1738 
1739   /* Get the status of the Interrupt */
1740   while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
1741   {
1742     if (Timeout != HAL_MAX_DELAY)
1743     {
1744       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1745       {
1746         return HAL_TIMEOUT;
1747       }
1748     }
1749   }
1750 
1751   /* Clear the Tamper Flag */
1752   WRITE_REG(TAMP->SCR, IntTamper);
1753 
1754   return HAL_OK;
1755 }
1756 
1757 /**
1758   * @brief  Handle Tamper interrupt request.
1759   * @param  hrtc RTC handle
1760   * @retval None
1761   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)1762 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
1763 {
1764   /* Get interrupt status */
1765   uint32_t tmp = READ_REG(TAMP->MISR);
1766 
1767   /* Immediately clear flags */
1768   WRITE_REG(TAMP->SCR, tmp);
1769 
1770   /* Check Tamper1 status */
1771   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
1772   {
1773 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1774     /* Call Tamper 1 Event registered Callback */
1775     hrtc->Tamper1EventCallback(hrtc);
1776 #else
1777     /* Tamper1 callback */
1778     HAL_RTCEx_Tamper1EventCallback(hrtc);
1779 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1780   }
1781 
1782   /* Check Tamper2 status */
1783   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
1784   {
1785 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1786     /* Call Tamper 2 Event registered Callback */
1787     hrtc->Tamper2EventCallback(hrtc);
1788 #else
1789     /* Tamper2 callback */
1790     HAL_RTCEx_Tamper2EventCallback(hrtc);
1791 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1792   }
1793 
1794   /* Check Tamper3 status */
1795   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
1796   {
1797 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1798     /* Call Tamper 3 Event registered Callback */
1799     hrtc->Tamper3EventCallback(hrtc);
1800 #else
1801     /* Tamper3 callback */
1802     HAL_RTCEx_Tamper3EventCallback(hrtc);
1803 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1804   }
1805 
1806   /* Check Tamper4 status */
1807   if ((tmp & RTC_TAMPER_4) == RTC_TAMPER_4)
1808   {
1809 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1810     /* Call Tamper 4 Event registered Callback */
1811     hrtc->Tamper4EventCallback(hrtc);
1812 #else
1813     /* Tamper4 callback */
1814     HAL_RTCEx_Tamper4EventCallback(hrtc);
1815 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1816   }
1817 
1818   /* Check Tamper5 status */
1819   if ((tmp & RTC_TAMPER_5) == RTC_TAMPER_5)
1820   {
1821 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1822     /* Call Tamper 5 Event registered Callback */
1823     hrtc->Tamper5EventCallback(hrtc);
1824 #else
1825     /* Tamper5 callback */
1826     HAL_RTCEx_Tamper5EventCallback(hrtc);
1827 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1828   }
1829 
1830   /* Check Internal Tamper3 status */
1831   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
1832   {
1833 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1834     /* Call Internal Tamper 3 Event registered Callback */
1835     hrtc->InternalTamper3EventCallback(hrtc);
1836 #else
1837     /* Internal Tamper3 callback */
1838     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
1839 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1840   }
1841 
1842   /* Check Internal Tamper4 status */
1843   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
1844   {
1845 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1846     /* Call Internal Tamper 4 Event registered Callback */
1847     hrtc->InternalTamper4EventCallback(hrtc);
1848 #else
1849     /* Internal Tamper4 callback */
1850     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
1851 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1852   }
1853 
1854   /* Check Internal Tamper5 status */
1855   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
1856   {
1857 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1858     /* Call Internal Tamper 5 Event registered Callback */
1859     hrtc->InternalTamper5EventCallback(hrtc);
1860 #else
1861     /* Internal Tamper5 callback */
1862     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
1863 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1864   }
1865   /* Check Internal Tamper6 status */
1866   if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
1867   {
1868 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1869     /* Call Internal Tamper 6 Event registered Callback */
1870     hrtc->InternalTamper6EventCallback(hrtc);
1871 #else
1872     /* Internal Tamper6 callback */
1873     HAL_RTCEx_InternalTamper6EventCallback(hrtc);
1874 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1875   }
1876 }
1877 
1878 /**
1879   * @brief  Tamper 1 callback.
1880   * @param  hrtc RTC handle
1881   * @retval None
1882   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)1883 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
1884 {
1885   /* Prevent unused argument(s) compilation warning */
1886   UNUSED(hrtc);
1887 
1888   /* NOTE : This function should not be modified, when the callback is needed,
1889             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
1890    */
1891 }
1892 
1893 
1894 /**
1895   * @brief  Tamper 2 callback.
1896   * @param  hrtc RTC handle
1897   * @retval None
1898   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)1899 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
1900 {
1901   /* Prevent unused argument(s) compilation warning */
1902   UNUSED(hrtc);
1903 
1904   /* NOTE : This function should not be modified, when the callback is needed,
1905             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
1906    */
1907 }
1908 
1909 /**
1910   * @brief  Tamper 3 callback.
1911   * @param  hrtc RTC handle
1912   * @retval None
1913   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)1914 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
1915 {
1916   /* Prevent unused argument(s) compilation warning */
1917   UNUSED(hrtc);
1918 
1919   /* NOTE : This function should not be modified, when the callback is needed,
1920             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
1921    */
1922 }
1923 
1924 /**
1925   * @brief  Tamper 4 callback.
1926   * @param  hrtc RTC handle
1927   * @retval None
1928   */
HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef * hrtc)1929 __weak void HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef *hrtc)
1930 {
1931   /* Prevent unused argument(s) compilation warning */
1932   UNUSED(hrtc);
1933 
1934   /* NOTE : This function should not be modified, when the callback is needed,
1935             the HAL_RTCEx_Tamper4EventCallback could be implemented in the user file
1936    */
1937 }
1938 
1939 /**
1940   * @brief  Tamper 5 callback.
1941   * @param  hrtc RTC handle
1942   * @retval None
1943   */
HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef * hrtc)1944 __weak void HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef *hrtc)
1945 {
1946   /* Prevent unused argument(s) compilation warning */
1947   UNUSED(hrtc);
1948 
1949   /* NOTE : This function should not be modified, when the callback is needed,
1950             the HAL_RTCEx_Tamper5EventCallback could be implemented in the user file
1951    */
1952 }
1953 
1954 /**
1955   * @brief  Internal Tamper 3 callback.
1956   * @param  hrtc RTC handle
1957   * @retval None
1958   */
HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef * hrtc)1959 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
1960 {
1961   /* Prevent unused argument(s) compilation warning */
1962   UNUSED(hrtc);
1963 
1964   /* NOTE : This function should not be modified, when the callback is needed,
1965             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
1966    */
1967 }
1968 
1969 /**
1970   * @brief  Internal Tamper 4 callback.
1971   * @param  hrtc RTC handle
1972   * @retval None
1973   */
HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef * hrtc)1974 __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
1975 {
1976   /* Prevent unused argument(s) compilation warning */
1977   UNUSED(hrtc);
1978 
1979   /* NOTE : This function should not be modified, when the callback is needed,
1980             the HAL_RTCEx_InternalTamperEventCallback could be implemented in the user file
1981    */
1982 }
1983 
1984 /**
1985   * @brief  Internal Tamper 5 callback.
1986   * @param  hrtc RTC handle
1987   * @retval None
1988   */
HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef * hrtc)1989 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
1990 {
1991   /* Prevent unused argument(s) compilation warning */
1992   UNUSED(hrtc);
1993 
1994   /* NOTE : This function should not be modified, when the callback is needed,
1995             the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
1996    */
1997 }
1998 
1999 /**
2000   * @brief  Internal Tamper 6 callback.
2001   * @param  hrtc RTC handle
2002   * @retval None
2003   */
HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef * hrtc)2004 __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
2005 {
2006   /* Prevent unused argument(s) compilation warning */
2007   UNUSED(hrtc);
2008 
2009   /* NOTE : This function should not be modified, when the callback is needed,
2010             the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
2011    */
2012 }
2013 
2014 /**
2015   * @}
2016   */
2017 
2018 
2019 /** @addtogroup RTCEx_Exported_Functions_Group6
2020   * @brief      Extended RTC Backup register functions
2021   *
2022 @verbatim
2023   ===============================================================================
2024              ##### Extended RTC Backup register functions #####
2025   ===============================================================================
2026   [..]
2027    (+) Before calling any tamper or internal tamper function, you have to call first
2028        HAL_RTC_Init() function.
2029    (+) In that ine you can select to output tamper event on RTC pin.
2030   [..]
2031    This subsection provides functions allowing to
2032    (+) Write a data in a specified RTC Backup data register
2033    (+) Read a data in a specified RTC Backup data register
2034 @endverbatim
2035   * @{
2036   */
2037 
2038 
2039 /**
2040   * @brief  Write a data in a specified RTC Backup data register.
2041   * @param  hrtc RTC handle
2042   * @param  BackupRegister RTC Backup data Register number.
2043   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2044   * @param  Data Data to be written in the specified Backup data register.
2045   * @retval None
2046   */
HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)2047 void HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2048 {
2049   uint32_t tmp;
2050 
2051   UNUSED(hrtc);
2052   /* Check the parameters */
2053   assert_param(IS_RTC_BKP(BackupRegister));
2054 
2055   tmp = (uint32_t) &(TAMP->BKP0R);
2056   tmp += (BackupRegister * 4U);
2057 
2058   /* Write the specified register */
2059   *(__IO uint32_t *)tmp = (uint32_t)Data;
2060 }
2061 
2062 
2063 /**
2064   * @brief  Reads data from the specified RTC Backup data Register.
2065   * @param  hrtc RTC handle
2066   * @param  BackupRegister RTC Backup data Register number.
2067   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2068   * @retval Read value
2069   */
HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)2070 uint32_t HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2071 {
2072   uint32_t tmp;
2073 
2074   UNUSED(hrtc);
2075   /* Check the parameters */
2076   assert_param(IS_RTC_BKP(BackupRegister));
2077 
2078   tmp = (uint32_t) &(TAMP->BKP0R);
2079   tmp += (BackupRegister * 4U);
2080 
2081   /* Read the specified register */
2082   return (*(__IO uint32_t *)tmp);
2083 }
2084 
2085 /**
2086   * @brief  Reset the RTC Backup data Register and the device secrets.
2087   * @param  hrtc RTC handle
2088   * @retval None
2089   */
HAL_RTCEx_BKUPErase(const RTC_HandleTypeDef * hrtc)2090 void  HAL_RTCEx_BKUPErase(const RTC_HandleTypeDef *hrtc)
2091 {
2092   UNUSED(hrtc);
2093   WRITE_REG(TAMP->CR2, TAMP_CR2_BKERASE);
2094 }
2095 /**
2096   * @}
2097   */
2098 
2099 /**
2100   * @}
2101   */
2102 
2103 #endif /* HAL_RTC_MODULE_ENABLED */
2104 
2105 /**
2106   * @}
2107   */
2108 
2109 
2110 /**
2111   * @}
2112   */
2113