1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_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) 2022 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 outputs pins (RTC_OUT1 & RTC_OUT2).
46         To configure the outputs, use the HAL_RTC_Init() function.
47     (+) RTC_OUT1 and RTC_OUT2 which select one of the following two outputs:
48     (+) CALIB: 512Hz or 1Hz clock output (with an LSE frequency of 32.768kHz).
49         To enable the CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
50     (+) TAMPALRM: This output is the OR between rtc_tamp_evt and ALARM signals.
51         ALARM is enabled by configuring the OSEL[1:0] bits in the RTC_CR register
52         which select the alarm A, alarm B or wakeup outputs.
53         rtc_tamp_evt is enabled by setting the TAMPOE bit
54         in the RTC_CR register which selects the tamper event outputs.
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   *** RTC synchronization ***
64   ================================================
65   [..]
66     (+) The RTC can be finely adjusted using HAL_RTCEx_SetSynchroShift() function.
67         Writing to RTC_SHIFTR can shift (either delay or advance) the clock with
68         a resolution of 1 ck_apre period.
69         The shift operation consists in adding the SUBFS[14:0] value to the synchronous
70         prescaler counter SS[15:0]: this delays the clock.
71 
72   *** Bypass shadow registers ***
73   ================================================
74   [..]
75     (+) Enable bypass shadow registers using the HAL_RTCEx_EnableBypassShadow().
76         When the Bypass Shadow is enabled the calendar value are taken directly
77         from the Calendar counter.
78         Thus eliminating the need to wait for the RSF bit to be set.
79         This is especially useful after exiting from low-power modes (Stop or Standby),
80         since the shadow registers are not updated during these modes.
81 
82   *** RTC ultra-low-power mode ***
83   ================================================
84   [..]
85     (+) Configure the RTC ultra-low-power mode using HAL_RTCEx_SetLowPowerCalib() function.
86         In this case, the calibration mechanism is applied on ck_apre instead of RTCCLK.
87         The resulting accuracy is the same, but the calibration is performed during a
88         calibration cycle of about 220 x PREDIV_A x RTCCLK pulses instead of 220 RTCCLK pulses.
89 
90   *** RTC subsecond register underflow interruption ***
91   ================================================
92   [..]
93     (+) Enable the RTC SSRU interruption mode using HAL_RTCEx_SetSSRU_IT() function.
94         In this case, when the SSR rolls under 0, an SSRU interruption is triggered.
95         Disable the RTC SSRU interruption mode using HAL_RTCEx_DeactivateSSRU() function.
96 
97   *** TimeStamp configuration ***
98   ===============================
99   [..]
100     (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
101         You can also configure the RTC TimeStamp with interrupt mode using the
102         HAL_RTCEx_SetTimeStamp_IT() function.
103     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
104         function.
105 
106   *** Internal TimeStamp configuration ***
107   ===============================
108   [..]
109     (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
110         User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
111     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
112         function.
113 
114    *** Tamper configuration ***
115    ============================
116    [..]
117      (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
118          or Level according to the Tamper filter (if equal to 0 Edge else Level)
119          value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and
120          Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
121          with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
122      (+) The default configuration of the Tamper erases the backup registers. To avoid
123          erase, enable the TAMPxPOM field on the TAMP_CR2 register.
124      (+) With new RTC tamper configuration, you have to call HAL_RTC_Init() in order to
125          perform TAMP base address offset calculation.
126      (+) Enable Internal tamper using HAL_RTCEx_SetInternalTamper. IT mode can be chosen using
127          setting Interrupt field.
128 
129    *** Backup Data Registers and Device Secrets configuration ***
130    ===========================================
131    [..]
132      (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
133          function.
134      (+) To read the RTC Backup registers, use the HAL_RTCEx_BKUPRead()
135          function.
136      (+) To reset the RTC Backup registers and erase the device secrets,
137          use HAL_RTCEx_BKUPErase() function.
138      (+) Enable the lock of the Boot hardware Key using the HAL_RTCEx_LockBootHardwareKey()
139          function.
140          The backup registers from TAMP_BKP0R to TAMP_BKP7R cannot be accessed neither in
141          read nor in write (they are read as 0 and write ignore).
142      (+) Configure the erase of the Device Secrets using HAL_RTCEx_ConfigEraseDeviceSecrets()
143          function.
144      (+) Block the access to the RTC Backup registers and all the device secrets
145          using HAL_RTCEx_BKUPBlock() function.
146 
147   *** Monotonic counter ***
148   ================================================
149   [..]
150      (+) To increment the Monotonic counter, use the HAL_RTCEx_MonotonicCounterIncrement()
151          function.
152      (+) To get the current value of the Monotonic counter, use the HAL_RTCEx_MonotonicCounterGet()
153          function.
154 
155   *** RTC & TAMP privilege protection modes ***
156   ================================================
157   [..]
158      (+) Set the privilege level of the RTC/TAMP/Backup registers using HAL_RTCEx_PrivilegeModeSet()
159          function.
160       +) Get the privilege level of the RTC/TAMP/Backup registers using HAL_RTCEx_PrivilegeModeGet()
161          function.
162 
163   @endverbatim
164   */
165 
166 /* Includes ------------------------------------------------------------------*/
167 #include "stm32h7rsxx_hal.h"
168 
169 /** @addtogroup STM32H7RSxx_HAL_Driver
170   * @{
171   */
172 
173 /** @addtogroup RTCEx
174   * @brief RTC Extended HAL module driver
175   * @{
176   */
177 
178 #ifdef HAL_RTC_MODULE_ENABLED
179 
180 /* Private typedef -----------------------------------------------------------*/
181 /* Private define ------------------------------------------------------------*/
182 #define TAMP_ALL RTC_TAMPER_ALL
183 
184 /* Private macro -------------------------------------------------------------*/
185 /* Private variables ---------------------------------------------------------*/
186 /* Private function prototypes -----------------------------------------------*/
187 /* Exported functions --------------------------------------------------------*/
188 
189 /** @addtogroup RTCEx_Exported_Functions
190   * @{
191   */
192 
193 
194 /** @addtogroup RTCEx_Exported_Functions_Group1
195   *  @brief   RTC TimeStamp and Tamper functions
196   *
197 @verbatim
198  ===============================================================================
199                  ##### RTC TimeStamp and Tamper functions #####
200  ===============================================================================
201 
202  [..] This section provides functions allowing to configure TimeStamp feature
203 
204 @endverbatim
205   * @{
206   */
207 
208 #ifdef RTC_CR_TSE
209 /**
210   * @brief  Set TimeStamp.
211   * @note   This API must be called before enabling the TimeStamp feature.
212   * @param  hrtc RTC handle
213   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
214   *         activated.
215   *          This parameter can be one of the following values:
216   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
217   *                                        rising edge of the related pin.
218   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
219   *                                         falling edge of the related pin.
220   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
221   *          This parameter can be one of the following values:
222   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
223   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
224   *               compatibility, this parameter is required.
225   * @retval HAL status
226   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)227 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
228 {
229   /* Check the parameters */
230 #if defined(RTC_CR_TSEDGE)
231   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
232 #endif /* RTC_CR_TSEDGE */
233   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
234   UNUSED(RTC_TimeStampPin);
235 
236   /* Process Locked */
237   __HAL_LOCK(hrtc);
238 
239   /* Change RTC state */
240   hrtc->State = HAL_RTC_STATE_BUSY;
241 
242   /* Get the RTC_CR register and clear the bits to be configured */
243 #if defined(RTC_CR_TSEDGE)
244   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
245 #else
246   CLEAR_BIT(RTC->CR, RTC_CR_TSE);
247 #endif /* RTC_CR_TSEDGE */
248 
249   /* Configure the Time Stamp TSEDGE and Enable bits */
250   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge | RTC_CR_TSE);
251 
252   /* Change RTC state */
253   hrtc->State = HAL_RTC_STATE_READY;
254 
255   /* Process Unlocked */
256   __HAL_UNLOCK(hrtc);
257 
258   return HAL_OK;
259 }
260 
261 /**
262   * @brief  Set TimeStamp with Interrupt.
263   * @note   This API must be called before enabling the TimeStamp feature.
264   * @note   The application must ensure that the EXTI RTC interrupt line is configured and enabled.
265   * @param  hrtc RTC handle
266   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
267   *         activated.
268   *          This parameter can be one of the following values:
269   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
270   *                                        rising edge of the related pin.
271   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
272   *                                         falling edge of the related pin.
273   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
274   *          This parameter can be one of the following values:
275   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
276   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
277   *               compatibility, this parameter is required.
278   * @retval HAL status
279   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)280 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
281 {
282   /* Check the parameters */
283 #if defined(RTC_CR_TSEDGE)
284   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
285 #endif /* RTC_CR_TSEDGE */
286   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
287   UNUSED(RTC_TimeStampPin);
288 
289   /* Process Locked */
290   __HAL_LOCK(hrtc);
291 
292   /* Change RTC state */
293   hrtc->State = HAL_RTC_STATE_BUSY;
294 
295   /* Get the RTC_CR register and clear the bits to be configured */
296 #if defined(RTC_CR_TSEDGE)
297   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
298 #else
299   CLEAR_BIT(RTC->CR, (RTC_CR_TSE | RTC_CR_TSIE));
300 #endif /* RTC_CR_TSEDGE */
301 
302   /* Configure the Time Stamp TSEDGE before Enable bit to avoid unwanted TSF setting. */
303   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge);
304 
305   /* Enable timestamp and IT */
306   SET_BIT(RTC->CR, RTC_CR_TSE | RTC_CR_TSIE);
307 
308   /* Change RTC state */
309   hrtc->State = HAL_RTC_STATE_READY;
310 
311   /* Process Unlocked */
312   __HAL_UNLOCK(hrtc);
313 
314   return HAL_OK;
315 }
316 
317 /**
318   * @brief  Deactivate TimeStamp.
319   * @param  hrtc RTC handle
320   * @retval HAL status
321   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)322 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
323 {
324   /* Process Locked */
325   __HAL_LOCK(hrtc);
326 
327   /* Change RTC state */
328   hrtc->State = HAL_RTC_STATE_BUSY;
329 
330   /* In case of interrupt mode is used, the interrupt source must disabled */
331 #if defined(RTC_CR_TSEDGE)
332   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
333 #else
334   CLEAR_BIT(RTC->CR, (RTC_CR_TSE | RTC_CR_TSIE));
335 #endif /* RTC_CR_TSEDGE */
336 
337   /* Change RTC state */
338   hrtc->State = HAL_RTC_STATE_READY;
339 
340   /* Process Unlocked */
341   __HAL_UNLOCK(hrtc);
342 
343   return HAL_OK;
344 }
345 #endif /* RTC_CR_TSE */
346 
347 /**
348   * @brief  Set Internal TimeStamp.
349   * @note   This API must be called before enabling the internal TimeStamp feature.
350   * @param  hrtc RTC handle
351   * @retval HAL status
352   */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)353 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
354 {
355   /* Process Locked */
356   __HAL_LOCK(hrtc);
357 
358   /* Change RTC state */
359   hrtc->State = HAL_RTC_STATE_BUSY;
360 
361   /* Configure the internal Time Stamp Enable bits */
362   SET_BIT(RTC->CR, RTC_CR_ITSE);
363 
364   /* Change RTC state */
365   hrtc->State = HAL_RTC_STATE_READY;
366 
367   /* Process Unlocked */
368   __HAL_UNLOCK(hrtc);
369 
370   return HAL_OK;
371 }
372 
373 /**
374   * @brief  Deactivate Internal TimeStamp.
375   * @param  hrtc RTC handle
376   * @retval HAL status
377   */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)378 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
379 {
380   /* Process Locked */
381   __HAL_LOCK(hrtc);
382 
383   /* Change RTC state */
384   hrtc->State = HAL_RTC_STATE_BUSY;
385 
386   /* Configure the internal Time Stamp Enable bits */
387   CLEAR_BIT(RTC->CR, RTC_CR_ITSE);
388 
389   hrtc->State = HAL_RTC_STATE_READY;
390 
391   /* Process Unlocked */
392   __HAL_UNLOCK(hrtc);
393 
394   return HAL_OK;
395 }
396 
397 /**
398   * @brief  Get the RTC TimeStamp value.
399   * @param  hrtc RTC handle
400   * @param  sTimeStamp Pointer to Time structure
401   * @param  sTimeStampDate Pointer to Date structure
402   * @param  Format specifies the format of the entered parameters.
403   *          This parameter can be one of the following values:
404   *             @arg RTC_FORMAT_BIN: Binary data format
405   *             @arg RTC_FORMAT_BCD: BCD data format
406   * @retval HAL status
407   */
HAL_RTCEx_GetTimeStamp(const RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)408 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(const RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp,
409                                          RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
410 {
411   uint32_t tmptime;
412   uint32_t tmpdate;
413 
414   /* Prevent unused argument(s) compilation warning */
415   UNUSED(hrtc);
416 
417   /* Check the parameters */
418   assert_param(IS_RTC_FORMAT(Format));
419 
420   /* Get the TimeStamp time and date registers values */
421   tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
422   tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
423 
424   /* Fill the Time structure fields with the read parameters */
425   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
426   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
427   sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
428   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
429   sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
430 
431   /* Fill the Date structure fields with the read parameters */
432   sTimeStampDate->Year = 0U;
433   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
434   sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
435   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
436 
437   /* Check the input parameters format */
438   if (Format == RTC_FORMAT_BIN)
439   {
440     /* Convert the TimeStamp structure parameters to Binary format */
441     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
442     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
443     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
444 
445     /* Convert the DateTimeStamp structure parameters to Binary format */
446     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
447     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
448     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
449   }
450 
451   /* Check if TIMESTAMP flag is set */
452   if (READ_BIT(RTC->SR, RTC_SR_TSF) != 0U)
453   {
454     /* Clear the TIMESTAMP Flags */
455     WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
456 
457     /* Check if TIMESTAMP OverRun flag is set */
458     if (READ_BIT(RTC->SR, RTC_SR_TSOVF) != 0U)
459     {
460       /* Clear the TIMESTAMP OverRun Flag */
461       WRITE_REG(RTC->SCR, RTC_SCR_CTSOVF);
462 
463       return HAL_ERROR;
464     }
465   }
466 
467   return HAL_OK;
468 }
469 
470 /**
471   * @brief  Handle TimeStamp interrupt request.
472   * @param  hrtc RTC handle
473   * @retval None
474   */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)475 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
476 {
477   /* Get the pending status of the TimeStamp Interrupt */
478   if (READ_BIT(RTC->MISR, RTC_MISR_TSMF) != 0U)
479   {
480 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
481     /* Call TimeStampEvent registered Callback */
482     hrtc->TimeStampEventCallback(hrtc);
483 #else
484     HAL_RTCEx_TimeStampEventCallback(hrtc);
485 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
486 
487     /* Clearing flags after the Callback because the content of RTC_TSTR and RTC_TSDR are cleared when
488        TSF bit is reset.*/
489     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
490   }
491 
492   /* Change RTC state */
493   hrtc->State = HAL_RTC_STATE_READY;
494 }
495 
496 /**
497   * @brief  TimeStamp callback.
498   * @param  hrtc RTC handle
499   * @retval None
500   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)501 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
502 {
503   /* Prevent unused argument(s) compilation warning */
504   UNUSED(hrtc);
505 
506   /* NOTE : This function should not be modified, when the callback is needed,
507             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
508   */
509 }
510 
511 /**
512   * @brief  Handle TimeStamp polling request.
513   * @param  hrtc RTC handle
514   * @param  Timeout Timeout duration
515   * @retval HAL status
516   */
HAL_RTCEx_PollForTimeStampEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)517 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
518 {
519   /* Prevent unused argument(s) compilation warning */
520   UNUSED(hrtc);
521 
522   uint32_t tickstart = HAL_GetTick();
523 
524   while (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
525   {
526     if (Timeout != HAL_MAX_DELAY)
527     {
528       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
529       {
530         /* New check to avoid false timeout detection in case of preemption */
531         if (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
532         {
533           return HAL_TIMEOUT;
534         }
535         else
536         {
537           break;
538         }
539       }
540     }
541   }
542 
543   return HAL_OK;
544 }
545 
546 /**
547   * @}
548   */
549 
550 /** @addtogroup RTCEx_Exported_Functions_Group2
551   * @brief    RTC Wake-up functions
552   *
553 @verbatim
554  ===============================================================================
555                         ##### RTC Wake-up functions #####
556  ===============================================================================
557 
558  [..] This section provides functions allowing to configure Wake-up feature
559 
560 @endverbatim
561   * @{
562   */
563 
564 /**
565   * @brief  Set wake up timer.
566   * @param  hrtc RTC handle
567   * @param  WakeUpCounter Wake up counter
568   * @param  WakeUpClock Wake up clock
569   * @retval HAL status
570   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)571 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
572 {
573   uint32_t tickstart;
574 
575   /* Check the parameters */
576   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
577   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
578 
579   /* Process Locked */
580   __HAL_LOCK(hrtc);
581 
582   /* Change RTC state */
583   hrtc->State = HAL_RTC_STATE_BUSY;
584 
585   /* Clear WUTE in RTC_CR to disable the wakeup timer */
586   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
587 
588   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
589      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
590      calendar initialization mode. */
591   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
592   {
593     tickstart = HAL_GetTick();
594 
595     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
596     {
597       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
598       {
599         /* New check to avoid false timeout detection in case of preemption */
600         if (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
601         {
602           /* Change RTC state */
603           hrtc->State = HAL_RTC_STATE_TIMEOUT;
604 
605           /* Process Unlocked */
606           __HAL_UNLOCK(hrtc);
607 
608           return HAL_TIMEOUT;
609         }
610         else
611         {
612           break;
613         }
614       }
615     }
616   }
617 
618   /* Configure the clock source */
619   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
620 
621   /* Configure the Wakeup Timer counter */
622   WRITE_REG(RTC->WUTR, (uint32_t)WakeUpCounter);
623 
624   /* Enable the Wakeup Timer */
625   SET_BIT(RTC->CR, RTC_CR_WUTE);
626 
627   /* Change RTC state */
628   hrtc->State = HAL_RTC_STATE_READY;
629 
630   /* Process Unlocked */
631   __HAL_UNLOCK(hrtc);
632 
633   return HAL_OK;
634 }
635 
636 /**
637   * @brief  Set wake up timer with interrupt.
638   * @note   The application must ensure that the EXTI RTC interrupt line is configured and enabled.
639   * @param  hrtc RTC handle
640   * @param  WakeUpCounter Wake up counter
641   * @param  WakeUpClock Wake up clock
642   * @param  WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
643   *                       - No effect if WakeUpAutoClr is set to zero
644   *                       - This feature is meaningful in case of Low power mode to avoid any RTC software execution
645   *                         after Wake Up.
646   * @retval HAL status
647   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock,uint32_t WakeUpAutoClr)648 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock,
649                                               uint32_t WakeUpAutoClr)
650 {
651   uint32_t tickstart;
652 
653   /* Check the parameters */
654   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
655   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
656   /* (0x0000<=WUTOCLR<=WUT) */
657   assert_param(WakeUpAutoClr <= WakeUpCounter);
658 
659   /* Process Locked */
660   __HAL_LOCK(hrtc);
661 
662   /* Change RTC state */
663   hrtc->State = HAL_RTC_STATE_BUSY;
664 
665   /* Clear WUTE in RTC_CR to disable the wakeup timer */
666   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
667 
668   /* Clear flag Wake-Up */
669   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
670 
671   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
672      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
673      calendar initialization mode. */
674   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
675   {
676     tickstart = HAL_GetTick();
677     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
678     {
679       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
680       {
681         /* New check to avoid false timeout detection in case of preemption */
682         if (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
683         {
684           /* Change RTC state */
685           hrtc->State = HAL_RTC_STATE_TIMEOUT;
686 
687           /* Process Unlocked */
688           __HAL_UNLOCK(hrtc);
689 
690           return HAL_TIMEOUT;
691         }
692         else
693         {
694           break;
695         }
696       }
697     }
698   }
699 
700   /* Configure the Wakeup Timer counter and auto clear value */
701   WRITE_REG(RTC->WUTR, (uint32_t)(WakeUpCounter | (WakeUpAutoClr << RTC_WUTR_WUTOCLR_Pos)));
702 
703   /* Configure the clock source */
704   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
705 
706   /* Configure the Interrupt in the RTC_CR register and Enable the Wakeup Timer*/
707   SET_BIT(RTC->CR, (RTC_CR_WUTIE | RTC_CR_WUTE));
708 
709   /* Change RTC state */
710   hrtc->State = HAL_RTC_STATE_READY;
711 
712   /* Process Unlocked */
713   __HAL_UNLOCK(hrtc);
714 
715   return HAL_OK;
716 }
717 
718 /**
719   * @brief  Deactivate wake up timer counter.
720   * @param  hrtc RTC handle
721   * @retval HAL status
722   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)723 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
724 {
725   /* Process Locked */
726   __HAL_LOCK(hrtc);
727 
728   /* Change RTC state */
729   hrtc->State = HAL_RTC_STATE_BUSY;
730 
731   /* Disable the Wakeup Timer */
732   /* In case of interrupt mode is used, the interrupt source must disabled */
733   CLEAR_BIT(RTC->CR, (RTC_CR_WUTE | RTC_CR_WUTIE));
734 
735   /* Change RTC state */
736   hrtc->State = HAL_RTC_STATE_READY;
737 
738   /* Process Unlocked */
739   __HAL_UNLOCK(hrtc);
740 
741   return HAL_OK;
742 }
743 
744 /**
745   * @brief  Get wake up timer counter.
746   * @param  hrtc RTC handle
747   * @retval Counter value
748   */
HAL_RTCEx_GetWakeUpTimer(const RTC_HandleTypeDef * hrtc)749 uint32_t HAL_RTCEx_GetWakeUpTimer(const RTC_HandleTypeDef *hrtc)
750 {
751   /* Prevent unused argument(s) compilation warning */
752   UNUSED(hrtc);
753 
754   /* Get the counter value */
755   return (uint32_t)(READ_BIT(RTC->WUTR, RTC_WUTR_WUT));
756 }
757 
758 /**
759   * @brief  Handle Wake Up Timer interrupt request.
760   * @param  hrtc RTC handle
761   * @retval None
762   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)763 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
764 {
765   /* Get the pending status of the Wake-Up Timer Interrupt */
766   if (READ_BIT(RTC->MISR, RTC_MISR_WUTMF) != 0U)
767   {
768     /* Immediately clear flags */
769     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
770 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
771     /* Call wake up timer registered Callback */
772     hrtc->WakeUpTimerEventCallback(hrtc);
773 #else
774     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
775 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
776   }
777 
778   /* Change RTC state */
779   hrtc->State = HAL_RTC_STATE_READY;
780 }
781 
782 /**
783   * @brief  Wake Up Timer callback.
784   * @param  hrtc RTC handle
785   * @retval None
786   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)787 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
788 {
789   /* Prevent unused argument(s) compilation warning */
790   UNUSED(hrtc);
791 
792   /* NOTE : This function should not be modified, when the callback is needed,
793             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
794    */
795 }
796 
797 /**
798   * @brief  Handle Wake Up Timer Polling.
799   * @param  hrtc RTC handle
800   * @param  Timeout Timeout duration
801   * @retval HAL status
802   */
HAL_RTCEx_PollForWakeUpTimerEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)803 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
804 {
805   /* Prevent unused argument(s) compilation warning */
806   UNUSED(hrtc);
807 
808   uint32_t tickstart = HAL_GetTick();
809 
810   while (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
811   {
812     if (Timeout != HAL_MAX_DELAY)
813     {
814       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
815       {
816         /* New check to avoid false timeout detection in case of preemption */
817         if (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
818         {
819           return HAL_TIMEOUT;
820         }
821         else
822         {
823           break;
824         }
825       }
826     }
827   }
828 
829   /* Clear the WAKEUPTIMER Flag */
830   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
831 
832   return HAL_OK;
833 }
834 
835 /**
836   * @}
837   */
838 
839 /** @addtogroup RTCEx_Exported_Functions_Group3
840   * @brief    Extended Peripheral Control functions
841   *
842 @verbatim
843  ===============================================================================
844               ##### Extended Peripheral Control functions #####
845  ===============================================================================
846     [..]
847     This subsection provides functions allowing to
848       (+) Write a data in a specified RTC Backup data register
849       (+) Read a data in a specified RTC Backup data register
850       (+) Set the Coarse calibration parameters.
851       (+) Deactivate the Coarse calibration parameters
852       (+) Set the Smooth calibration parameters.
853       (+) Set Low Power calibration parameter.
854       (+) Configure the Synchronization Shift Control Settings.
855       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
856       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
857       (+) Enable the RTC reference clock detection.
858       (+) Disable the RTC reference clock detection.
859       (+) Enable the Bypass Shadow feature.
860       (+) Disable the Bypass Shadow feature.
861 
862 @endverbatim
863   * @{
864   */
865 
866 /**
867   * @brief  Set the Smooth calibration parameters.
868   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
869   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
870   *         SmoothCalibMinusPulsesValue must be equal to 0.
871   * @param  hrtc RTC handle
872   * @param  SmoothCalibPeriod  Select the Smooth Calibration Period.
873   *          This parameter can be one of the following values :
874   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
875   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
876   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
877   * @param  SmoothCalibPlusPulses  Select to Set or reset the CALP bit.
878   *          This parameter can be one of the following values:
879   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
880   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
881   * @param  SmoothCalibMinusPulsesValue  Select the value of CALM[8:0] bits.
882   *          This parameter can be one any value from 0 to 0x000001FF.
883   * @retval HAL status
884   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)885 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod,
886                                            uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
887 {
888   uint32_t tickstart;
889 
890   /* Check the parameters */
891   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
892   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
893   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
894 
895   /* Process Locked */
896   __HAL_LOCK(hrtc);
897 
898   /* Change RTC state */
899   hrtc->State = HAL_RTC_STATE_BUSY;
900 
901   tickstart = HAL_GetTick();
902 
903   /* check if a calibration is pending */
904   while (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
905   {
906     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
907     {
908       /* New check to avoid false timeout detection in case of preemption */
909       if (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
910       {
911 
912         /* Change RTC state */
913         hrtc->State = HAL_RTC_STATE_TIMEOUT;
914 
915         /* Process Unlocked */
916         __HAL_UNLOCK(hrtc);
917 
918         return HAL_TIMEOUT;
919       }
920       else
921       {
922         break;
923       }
924     }
925   }
926 
927   /* Disable the write protection for RTC registers */
928   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
929 
930   /* Configure the Smooth calibration settings */
931   MODIFY_REG(RTC->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM),
932              (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
933 
934   /* Enable the write protection for RTC registers */
935   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
936 
937   /* Change RTC state */
938   hrtc->State = HAL_RTC_STATE_READY;
939 
940   /* Process Unlocked */
941   __HAL_UNLOCK(hrtc);
942 
943   return HAL_OK;
944 }
945 
946 /**
947   * @brief  Select the low power Calibration mode.
948   * @param  hrtc: RTC handle
949   * @param  LowPowerCalib: Low power Calibration mode.
950   *          This parameter can be one of the following values :
951   *             @arg RTC_LPCAL_SET: Low power mode.
952   *             @arg RTC_LPCAL_RESET: High consumption mode.
953   * @retval HAL status
954   */
HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef * hrtc,uint32_t LowPowerCalib)955 HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
956 {
957   /* Check the parameters */
958   assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
959 
960   /* Process Locked */
961   __HAL_LOCK(hrtc);
962 
963   /* Change RTC state */
964   hrtc->State = HAL_RTC_STATE_BUSY;
965 
966   /* Disable the write protection for RTC registers */
967   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
968 
969   /* Configure the Smooth calibration settings */
970   MODIFY_REG(RTC->CALR, RTC_CALR_LPCAL, LowPowerCalib);
971 
972   /* Enable the write protection for RTC registers */
973   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
974 
975   /* Change RTC state */
976   hrtc->State = HAL_RTC_STATE_READY;
977 
978   /* Process Unlocked */
979   __HAL_UNLOCK(hrtc);
980 
981   return HAL_OK;
982 }
983 
984 /**
985   * @brief  Configure the Synchronization Shift Control Settings.
986   * @note   When REFCKON is set, firmware must not write to Shift control register.
987   * @param  hrtc RTC handle
988   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
989   *          This parameter can be one of the following values:
990   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
991   *             @arg RTC_SHIFTADD1S_RESET: No effect.
992   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
993   *          This parameter can be one any value from 0 to 0x7FFF.
994   * @retval HAL status
995   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)996 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
997 {
998   uint32_t tickstart;
999 
1000   /* Check the parameters */
1001   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1002   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1003 
1004   /* Process Locked */
1005   __HAL_LOCK(hrtc);
1006 
1007   /* Change RTC state */
1008   hrtc->State = HAL_RTC_STATE_BUSY;
1009 
1010   tickstart = HAL_GetTick();
1011 
1012   /* Wait until the shift is completed */
1013   while (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
1014   {
1015     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1016     {
1017       /* New check to avoid false timeout detection in case of preemption */
1018       if (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
1019       {
1020         /* Change RTC state */
1021         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1022 
1023         /* Process Unlocked */
1024         __HAL_UNLOCK(hrtc);
1025 
1026         return HAL_TIMEOUT;
1027       }
1028       else
1029       {
1030         break;
1031       }
1032     }
1033   }
1034 
1035   /* Disable the write protection for RTC registers */
1036   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1037 
1038 #if defined(RTC_CR_REFCKON)
1039   /* Check if the reference clock detection is disabled */
1040   if (READ_BIT(RTC->CR, RTC_CR_REFCKON) == 0U)
1041   {
1042 #endif /* RTC_CR_REFCKON */
1043     /* Configure the Shift settings */
1044     MODIFY_REG(RTC->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
1045 
1046     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1047     if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1048     {
1049       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1050       {
1051         /* Enable the write protection for RTC registers */
1052         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1053 
1054         /* Change RTC state */
1055         hrtc->State = HAL_RTC_STATE_ERROR;
1056 
1057         /* Process Unlocked */
1058         __HAL_UNLOCK(hrtc);
1059 
1060         return HAL_ERROR;
1061       }
1062     }
1063 #if defined(RTC_CR_REFCKON)
1064   }
1065   else
1066   {
1067     /* Enable the write protection for RTC registers */
1068     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1069 
1070     /* Change RTC state */
1071     hrtc->State = HAL_RTC_STATE_ERROR;
1072 
1073     /* Process Unlocked */
1074     __HAL_UNLOCK(hrtc);
1075 
1076     return HAL_ERROR;
1077   }
1078 #endif /* RTC_CR_REFCKON */
1079 
1080   /* Enable the write protection for RTC registers */
1081   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1082 
1083   /* Change RTC state */
1084   hrtc->State = HAL_RTC_STATE_READY;
1085 
1086   /* Process Unlocked */
1087   __HAL_UNLOCK(hrtc);
1088 
1089   return HAL_OK;
1090 }
1091 
1092 #if defined(RTC_CR_COSEL)
1093 /**
1094   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1095   * @param  hrtc RTC handle
1096   * @param  CalibOutput Select the Calibration output Selection .
1097   *          This parameter can be one of the following values:
1098   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1099   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1100   * @retval HAL status
1101   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1102 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1103 {
1104   /* Check the parameters */
1105   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1106 
1107   /* Process Locked */
1108   __HAL_LOCK(hrtc);
1109 
1110   /* Change RTC state */
1111   hrtc->State = HAL_RTC_STATE_BUSY;
1112 
1113   /* Configure the RTC_CR register */
1114   MODIFY_REG(RTC->CR, RTC_CR_COSEL, CalibOutput);
1115 
1116   /* Enable calibration output */
1117   SET_BIT(RTC->CR, RTC_CR_COE);
1118 
1119   /* Change RTC state */
1120   hrtc->State = HAL_RTC_STATE_READY;
1121 
1122   /* Process Unlocked */
1123   __HAL_UNLOCK(hrtc);
1124 
1125   return HAL_OK;
1126 }
1127 
1128 /**
1129   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1130   * @param  hrtc RTC handle
1131   * @retval HAL status
1132   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1133 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1134 {
1135   /* Process Locked */
1136   __HAL_LOCK(hrtc);
1137 
1138   /* Change RTC state */
1139   hrtc->State = HAL_RTC_STATE_BUSY;
1140 
1141   /* Disable calibration output */
1142   CLEAR_BIT(RTC->CR, RTC_CR_COE);
1143 
1144   /* Change RTC state */
1145   hrtc->State = HAL_RTC_STATE_READY;
1146 
1147   /* Process Unlocked */
1148   __HAL_UNLOCK(hrtc);
1149 
1150   return HAL_OK;
1151 }
1152 #endif /* RTC_CR_COSEL */
1153 
1154 #if defined(RTC_CR_REFCKON)
1155 /**
1156   * @brief  Enable the RTC reference clock detection.
1157   * @param  hrtc RTC handle
1158   * @retval HAL status
1159   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1160 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1161 {
1162   HAL_StatusTypeDef status;
1163 
1164   /* Process Locked */
1165   __HAL_LOCK(hrtc);
1166 
1167   /* Change RTC state */
1168   hrtc->State = HAL_RTC_STATE_BUSY;
1169 
1170   /* Disable the write protection for RTC registers */
1171   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1172 
1173   /* Enter Initialization mode */
1174   status = RTC_EnterInitMode(hrtc);
1175   if (status == HAL_OK)
1176   {
1177     /* Enable clockref detection */
1178     SET_BIT(RTC->CR, RTC_CR_REFCKON);
1179 
1180     /* Exit Initialization mode */
1181     status = RTC_ExitInitMode(hrtc);
1182   }
1183 
1184   /* Enable the write protection for RTC registers */
1185   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1186 
1187   if (status == HAL_OK)
1188   {
1189     /* Change RTC state */
1190     hrtc->State = HAL_RTC_STATE_READY;
1191   }
1192 
1193   /* Process Unlocked */
1194   __HAL_UNLOCK(hrtc);
1195 
1196   return status;
1197 }
1198 
1199 /**
1200   * @brief  Disable the RTC reference clock detection.
1201   * @param  hrtc RTC handle
1202   * @retval HAL status
1203   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1204 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1205 {
1206   HAL_StatusTypeDef status;
1207 
1208   /* Process Locked */
1209   __HAL_LOCK(hrtc);
1210 
1211   /* Change RTC state */
1212   hrtc->State = HAL_RTC_STATE_BUSY;
1213 
1214   /* Disable the write protection for RTC registers */
1215   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1216 
1217   /* Enter Initialization mode */
1218   status = RTC_EnterInitMode(hrtc);
1219   if (status == HAL_OK)
1220   {
1221     /* Disable clockref detection */
1222     CLEAR_BIT(RTC->CR, RTC_CR_REFCKON);
1223 
1224     /* Exit Initialization mode */
1225     status = RTC_ExitInitMode(hrtc);
1226   }
1227 
1228   /* Enable the write protection for RTC registers */
1229   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1230 
1231   if (status == HAL_OK)
1232   {
1233     /* Change RTC state */
1234     hrtc->State = HAL_RTC_STATE_READY;
1235   }
1236 
1237   /* Process Unlocked */
1238   __HAL_UNLOCK(hrtc);
1239 
1240   return status;
1241 }
1242 #endif /* RTC_CR_REFCKON */
1243 
1244 /**
1245   * @brief  Enable the Bypass Shadow feature.
1246   * @note   When the Bypass Shadow is enabled the calendar value are taken
1247   *         directly from the Calendar counter.
1248   * @param  hrtc RTC handle
1249   * @retval HAL status
1250   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1251 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1252 {
1253   /* Process Locked */
1254   __HAL_LOCK(hrtc);
1255 
1256   /* Change RTC state */
1257   hrtc->State = HAL_RTC_STATE_BUSY;
1258 
1259   /* Set the BYPSHAD bit */
1260   SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1261 
1262   /* Change RTC state */
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  Disable the Bypass Shadow feature.
1273   * @note   When the Bypass Shadow is enabled the calendar value are taken
1274   *         directly from the Calendar counter.
1275   * @param  hrtc RTC handle
1276   * @retval HAL status
1277   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1278 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1279 {
1280   /* Process Locked */
1281   __HAL_LOCK(hrtc);
1282 
1283   /* Change RTC state */
1284   hrtc->State = HAL_RTC_STATE_BUSY;
1285 
1286   /* Reset the BYPSHAD bit */
1287   CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1288 
1289   /* Change RTC state */
1290   hrtc->State = HAL_RTC_STATE_READY;
1291 
1292   /* Process Unlocked */
1293   __HAL_UNLOCK(hrtc);
1294 
1295   return HAL_OK;
1296 }
1297 
1298 /**
1299   * @brief  Increment Monotonic counter.
1300   * @param  hrtc RTC handle
1301   * @param  Instance  Monotonic counter Instance
1302   *         This parameter can be one of the following values :
1303   *           @arg RTC_MONOTONIC_COUNTER_1
1304   * @retval HAL status
1305   */
HAL_RTCEx_MonotonicCounterIncrement(const RTC_HandleTypeDef * hrtc,uint32_t Instance)1306 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(const RTC_HandleTypeDef *hrtc, uint32_t Instance)
1307 {
1308   /* Prevent unused argument(s) compilation warning */
1309   UNUSED(hrtc);
1310   UNUSED(Instance);
1311 
1312   /* This register is read-only only and is incremented by one when a write access is done to this
1313      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1314   CLEAR_REG(TAMP->COUNT1R);
1315 
1316   return HAL_OK;
1317 }
1318 
1319 /**
1320   * @brief  Monotonic counter.
1321   * @param  hrtc RTC handle
1322   * @param  Instance  Monotonic counter Instance
1323   *         This parameter can be one of the following values :
1324   *           @arg RTC_MONOTONIC_COUNTER_1
1325   * @param  pValue Pointer to the counter monotonic counter value
1326   * @retval HAL status
1327   */
HAL_RTCEx_MonotonicCounterGet(const RTC_HandleTypeDef * hrtc,uint32_t Instance,uint32_t * pValue)1328 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(const RTC_HandleTypeDef *hrtc, uint32_t Instance, uint32_t *pValue)
1329 {
1330   /* Prevent unused argument(s) compilation warning */
1331   UNUSED(hrtc);
1332   UNUSED(Instance);
1333 
1334   /* This register is read-only only and is incremented by one when a write access is done to this
1335      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1336   *pValue = READ_REG(TAMP->COUNT1R);
1337 
1338   return HAL_OK;
1339 }
1340 
1341 /**
1342   * @brief  Set SSR Underflow detection with Interrupt.
1343   * @note   The application must ensure that the EXTI RTC interrupt line is configured and enabled.
1344   * @param  hrtc RTC handle
1345   * @retval HAL status
1346   */
HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef * hrtc)1347 HAL_StatusTypeDef HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef *hrtc)
1348 {
1349   /* Process Locked */
1350   __HAL_LOCK(hrtc);
1351 
1352   /* Change RTC state */
1353   hrtc->State = HAL_RTC_STATE_BUSY;
1354 
1355   /* Enable IT SSRU */
1356   __HAL_RTC_SSRU_ENABLE_IT(hrtc, RTC_IT_SSRU);
1357 
1358   /* Change RTC state */
1359   hrtc->State = HAL_RTC_STATE_READY;
1360 
1361   /* Process Unlocked */
1362   __HAL_UNLOCK(hrtc);
1363 
1364   return HAL_OK;
1365 }
1366 
1367 /**
1368   * @brief  Deactivate SSR Underflow.
1369   * @param  hrtc RTC handle
1370   * @retval HAL status
1371   */
HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef * hrtc)1372 HAL_StatusTypeDef HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef *hrtc)
1373 {
1374   /* Process Locked */
1375   __HAL_LOCK(hrtc);
1376 
1377   /* Change RTC state */
1378   hrtc->State = HAL_RTC_STATE_BUSY;
1379 
1380   /* In case of interrupt mode is used, the interrupt source must disabled */
1381   __HAL_RTC_SSRU_DISABLE_IT(hrtc, RTC_IT_SSRU);
1382 
1383   /* Change RTC state */
1384   hrtc->State = HAL_RTC_STATE_READY;
1385 
1386   /* Process Unlocked */
1387   __HAL_UNLOCK(hrtc);
1388 
1389   return HAL_OK;
1390 }
1391 
1392 /**
1393   * @brief  Handle SSR underflow interrupt request.
1394   * @param  hrtc RTC handle
1395   * @retval None
1396   */
HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef * hrtc)1397 void HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef *hrtc)
1398 {
1399   /* Get the pending status of the SSR Underflow Interrupt */
1400   if (READ_BIT(RTC->MISR, RTC_MISR_SSRUMF) != 0U)
1401   {
1402     /* Immediately clear SSR underflow flag */
1403     WRITE_REG(RTC->SCR, RTC_SCR_CSSRUF);
1404 
1405     /* SSRU callback */
1406 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1407     /* Call SSRUEvent registered Callback */
1408     hrtc->SSRUEventCallback(hrtc);
1409 #else
1410     HAL_RTCEx_SSRUEventCallback(hrtc);
1411 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1412 
1413   }
1414 
1415   /* Change RTC state */
1416   hrtc->State = HAL_RTC_STATE_READY;
1417 }
1418 
1419 /**
1420   * @brief  SSR underflow callback.
1421   * @param  hrtc RTC handle
1422   * @retval None
1423   */
HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef * hrtc)1424 __weak void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
1425 {
1426   /* Prevent unused argument(s) compilation warning */
1427   UNUSED(hrtc);
1428 
1429   /* NOTE : This function should not be modified, when the callback is needed,
1430             the HAL_RTCEx_SSRUEventCallback could be implemented in the user file
1431    */
1432 }
1433 
1434 /**
1435   * @}
1436   */
1437 
1438 /** @addtogroup RTCEx_Exported_Functions_Group4
1439   * @brief    Extended features functions
1440   *
1441 @verbatim
1442  ===============================================================================
1443                  ##### Extended features functions #####
1444  ===============================================================================
1445     [..]  This section provides functions allowing to:
1446       (+) RTC Alarm B callback
1447       (+) RTC Poll for Alarm B request
1448 
1449 @endverbatim
1450   * @{
1451   */
1452 
1453 /**
1454   * @brief  Alarm B callback.
1455   * @param  hrtc RTC handle
1456   * @retval None
1457   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1458 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1459 {
1460   /* Prevent unused argument(s) compilation warning */
1461   UNUSED(hrtc);
1462 
1463   /* NOTE : This function should not be modified, when the callback is needed,
1464             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1465    */
1466 }
1467 
1468 /**
1469   * @brief  Handle Alarm B Polling request.
1470   * @param  hrtc RTC handle
1471   * @param  Timeout Timeout duration
1472   * @retval HAL status
1473   */
HAL_RTCEx_PollForAlarmBEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)1474 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1475 {
1476   /* Prevent unused argument(s) compilation warning */
1477   UNUSED(hrtc);
1478 
1479   uint32_t tickstart = HAL_GetTick();
1480 
1481   while (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1482   {
1483     if (Timeout != HAL_MAX_DELAY)
1484     {
1485       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1486       {
1487         /* New check to avoid false timeout detection in case of preemption */
1488         if (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1489         {
1490           return HAL_TIMEOUT;
1491         }
1492         else
1493         {
1494           break;
1495         }
1496       }
1497     }
1498   }
1499 
1500   /* Clear the Alarm Flag */
1501   WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1502 
1503   return HAL_OK;
1504 }
1505 
1506 /**
1507   * @}
1508   */
1509 
1510 /** @addtogroup RTCEx_Exported_Functions_Group5
1511   * @brief      Extended RTC Tamper functions
1512   *
1513 @verbatim
1514   ==============================================================================
1515                          ##### Tamper functions #####
1516   ==============================================================================
1517   [..]
1518    (+) Before calling any tamper or internal tamper function, you have to call first
1519        HAL_RTC_Init() function.
1520    (+) In that one you can select to output tamper event on RTC pin.
1521   [..]
1522    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1523        or Level according to the Tamper filter (if equal to 0 Edge else Level)
1524        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1525        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1526        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1527    (+) The default configuration of the Tamper erases the backup registers. To avoid
1528        erase, enable the NoErase field on the TAMP_TAMPCR register.
1529   [..]
1530    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1531        the HAL_RTCEx_SetInternalTamper() function.
1532 
1533 @endverbatim
1534   * @{
1535   */
1536 
1537 
1538 /**
1539   * @brief  Set Tamper
1540   * @param  hrtc RTC handle
1541   * @param  sTamper Pointer to Tamper Structure.
1542   * @retval HAL status
1543   */
HAL_RTCEx_SetTamper(const RTC_HandleTypeDef * hrtc,const RTC_TamperTypeDef * sTamper)1544 HAL_StatusTypeDef HAL_RTCEx_SetTamper(const RTC_HandleTypeDef *hrtc, const RTC_TamperTypeDef *sTamper)
1545 {
1546   uint32_t tmpreg;
1547 
1548   /* Prevent unused argument(s) compilation warning */
1549   UNUSED(hrtc);
1550 
1551   /* Check the parameters */
1552   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1553   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1554   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1555   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1556   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1557 #if (RTC_TAMP_NB > 2U)
1558   /* Mask flag only supported by TAMPER 1, 2 and 3 */
1559   assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_3)));
1560 #else
1561   assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_2)));
1562 #endif /* (RTC_TAMP_NB > 2U) */
1563   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1564   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1565   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1566   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1567   /* Trigger and Filter have exclusive configurations */
1568   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) &&
1569                 ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) ||
1570                  (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL))) ||
1571                ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) &&
1572                 ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) ||
1573                  (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1574 
1575   /* Configuration register 2 */
1576   tmpreg = READ_REG(TAMP->CR2);
1577   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) |
1578               (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos));
1579 
1580   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1581   {
1582     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1583   }
1584 
1585   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1586   {
1587     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1588   }
1589 
1590   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1591   {
1592     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos);
1593   }
1594   WRITE_REG(TAMP->CR2, tmpreg);
1595 
1596   /* Filter control register */
1597   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration |
1598             sTamper->TamperPullUp);
1599 
1600   /* Timestamp on tamper */
1601   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1602   {
1603     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1604   }
1605 
1606   /* Control register 1 */
1607   SET_BIT(TAMP->CR1, sTamper->Tamper);
1608 
1609   return HAL_OK;
1610 }
1611 
1612 
1613 /**
1614   * @brief  Set Tamper in IT mode
1615   * @note   The application must ensure that the EXTI TAMP interrupt line is configured and enabled.
1616   * @param  hrtc RTC handle
1617   * @param  sTamper Pointer to Tamper Structure.
1618   * @retval HAL status
1619   */
HAL_RTCEx_SetTamper_IT(const RTC_HandleTypeDef * hrtc,const RTC_TamperTypeDef * sTamper)1620 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(const RTC_HandleTypeDef *hrtc, const RTC_TamperTypeDef *sTamper)
1621 {
1622   uint32_t tmpreg;
1623 
1624   /* Prevent unused argument(s) compilation warning */
1625   UNUSED(hrtc);
1626 
1627   /* Check the parameters */
1628   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1629   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1630   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1631   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1632   /* The interrupt must not be enabled when TAMPxMSK is set. */
1633   assert_param(sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_DISABLE);
1634   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1635   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1636   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1637   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1638   /* Trigger and Filter have exclusive configurations */
1639   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) &&
1640                 ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) ||
1641                  (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL))) ||
1642                ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) &&
1643                 ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) ||
1644                  (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1645 
1646   /* Configuration register 2 */
1647   tmpreg = READ_REG(TAMP->CR2);
1648   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) |
1649               (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos));
1650 
1651   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1652   {
1653     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1654   }
1655 
1656   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1657   {
1658     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1POM_Pos);
1659   }
1660   WRITE_REG(TAMP->CR2, tmpreg);
1661 
1662   /* Filter control register */
1663   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration |
1664             sTamper->TamperPullUp);
1665 
1666   /* Timestamp on tamper */
1667   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1668   {
1669     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1670   }
1671 
1672   /* Interrupt enable register */
1673   SET_BIT(TAMP->IER, sTamper->Tamper);
1674 
1675   /* Control register 1 */
1676   SET_BIT(TAMP->CR1, sTamper->Tamper);
1677 
1678   return HAL_OK;
1679 }
1680 
1681 /**
1682   * @brief  Deactivate Tamper.
1683   * @param  hrtc RTC handle
1684   * @param  Tamper Selected tamper pin.
1685   *         This parameter can be a combination of the following values:
1686   *         @arg RTC_TAMPER_1
1687   *         @arg RTC_TAMPER_2
1688   *         @arg RTC_TAMPER_3
1689   *         @arg RTC_TAMPER_4
1690   *         @arg RTC_TAMPER_5
1691   *         @arg RTC_TAMPER_6
1692   *         @arg RTC_TAMPER_7
1693   *         @arg RTC_TAMPER_8
1694   * @retval HAL status
1695   */
HAL_RTCEx_DeactivateTamper(const RTC_HandleTypeDef * hrtc,uint32_t Tamper)1696 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(const RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1697 {
1698   /* Prevent unused argument(s) compilation warning */
1699   UNUSED(hrtc);
1700 
1701   assert_param(IS_RTC_TAMPER(Tamper));
1702 
1703   /* Disable the selected Tamper pin */
1704   CLEAR_BIT(TAMP->CR1, Tamper);
1705 
1706   /* Clear tamper interrupt and event flags (WO register) */
1707   WRITE_REG(TAMP->SCR, Tamper);
1708 
1709   /* Clear tamper mask/noerase/trigger configuration */
1710   CLEAR_BIT(TAMP->CR2, (Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1711             (Tamper << TAMP_CR2_TAMP1POM_Pos));
1712 
1713   /* Clear tamper interrupt mode configuration */
1714   CLEAR_BIT(TAMP->IER, Tamper);
1715 
1716   return HAL_OK;
1717 }
1718 
1719 /**
1720   * @brief  Set all active Tampers at the same time.
1721   * @note   For interrupt mode, the application must ensure that the EXTI RTC interrupt line is configured and enabled.
1722   * @param  hrtc RTC handle
1723   * @param  sAllTamper Pointer to active Tamper Structure.
1724   * @retval HAL status
1725   */
HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef * hrtc,const RTC_ActiveTampersTypeDef * sAllTamper)1726 HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, const RTC_ActiveTampersTypeDef *sAllTamper)
1727 {
1728   uint32_t tmp_ier;
1729   uint32_t tmp_cr1;
1730   uint32_t tmp_cr2;
1731   uint32_t tmp_atcr1;
1732   uint32_t tmp_atcr2;
1733   uint32_t tmp_cr;
1734   uint32_t i;
1735   uint32_t tickstart;
1736 
1737 #ifdef  USE_FULL_ASSERT
1738   for (i = 0; i < RTC_TAMP_NB; i++)
1739   {
1740     assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
1741     assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
1742     /* Mask flag only supported by TAMPER 1, 2 and 3 */
1743     assert_param(!((sAllTamper->TampInput[i].MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE) &&
1744                    (i >= RTC_TAMPER_MASKABLE_NB)));
1745   }
1746   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
1747   assert_param(IS_RTC_ATAMPER_FILTER(sAllTamper->ActiveFilter));
1748   assert_param(IS_RTC_ATAMPER_OUTPUT_CHANGE_PERIOD(sAllTamper->ActiveOutputChangePeriod));
1749   assert_param(IS_RTC_ATAMPER_ASYNCPRES_RTCCLK(sAllTamper->ActiveAsyncPrescaler));
1750 #endif /* USE_FULL_ASSERT */
1751 
1752   /* Active Tampers must not be already enabled */
1753   if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
1754   {
1755     /* Disable all actives tampers with HAL_RTCEx_DeactivateActiveTampers.
1756        No need to check return value because it returns always HAL_OK */
1757     (void) HAL_RTCEx_DeactivateActiveTampers(hrtc);
1758   }
1759 
1760   /* Set TimeStamp on tamper detection */
1761   tmp_cr = READ_REG(RTC->CR);
1762   if ((tmp_cr & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
1763   {
1764     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
1765   }
1766 
1767   tmp_cr1 = READ_REG(TAMP->CR1);
1768   tmp_cr2 = READ_REG(TAMP->CR2);
1769   tmp_atcr2 = 0U;
1770   tmp_ier = READ_REG(TAMP->IER);
1771 
1772   /* Set common parameters */
1773   tmp_atcr1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) |
1774                sAllTamper->ActiveAsyncPrescaler);
1775 
1776   /* Set specific parameters for each active tamper inputs if enable */
1777   for (i = 0; i < RTC_TAMP_NB; i++)
1778   {
1779     if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
1780     {
1781       tmp_cr1 |= (TAMP_CR1_TAMP1E << i);
1782       tmp_atcr1 |= (TAMP_ATCR1_TAMP1AM << i);
1783 
1784       if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
1785       {
1786         /* Interrupt enable register */
1787         tmp_ier |= (TAMP_IER_TAMP1IE << i);
1788       }
1789 
1790       if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1791       {
1792         tmp_cr2 |= (TAMP_CR2_TAMP1MSK << i);
1793       }
1794 
1795       if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1796       {
1797         tmp_cr2 |= (TAMP_CR2_TAMP1POM << i);
1798       }
1799 
1800       /* Configure ATOSELx[] in case of output sharing */
1801       tmp_atcr2 |= sAllTamper->TampInput[i].Output << ((3U * i) + TAMP_ATCR2_ATOSEL1_Pos);
1802 
1803       if (i != sAllTamper->TampInput[i].Output)
1804       {
1805         tmp_atcr1 |= TAMP_ATCR1_ATOSHARE;
1806       }
1807     }
1808   }
1809 
1810   WRITE_REG(TAMP->IER, tmp_ier);
1811   WRITE_REG(TAMP->ATCR1, tmp_atcr1);
1812   WRITE_REG(TAMP->ATCR2, tmp_atcr2);
1813   WRITE_REG(TAMP->CR2, tmp_cr2);
1814   WRITE_REG(TAMP->CR1, tmp_cr1);
1815 
1816   /* Write seed */
1817   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1818   {
1819     WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
1820   }
1821 
1822   /* Wait till RTC SEEDF flag is cleared and if Time out is reached exit */
1823   tickstart = HAL_GetTick();
1824   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0U)
1825   {
1826     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1827     {
1828       /* New check to avoid false timeout detection in case of preemption */
1829       if (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1830       {
1831         /* Change RTC state */
1832         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1833 
1834         return HAL_TIMEOUT;
1835       }
1836       else
1837       {
1838         break;
1839       }
1840     }
1841   }
1842 
1843   return HAL_OK;
1844 }
1845 
1846 /**
1847   * @brief  Write a new seed. Active tamper must be enabled.
1848   * @param  hrtc RTC handle
1849   * @param  pSeed Pointer to active tamper seed values.
1850   * @retval HAL status
1851   */
HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef * hrtc,const uint32_t * pSeed)1852 HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, const uint32_t *pSeed)
1853 {
1854   uint32_t i;
1855   uint32_t tickstart;
1856 
1857   /* Active Tampers must be enabled */
1858   if (READ_BIT(TAMP->ATOR,  TAMP_ATOR_INITS) == 0U)
1859   {
1860     return HAL_ERROR;
1861   }
1862 
1863   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1864   {
1865     WRITE_REG(TAMP->ATSEEDR, pSeed[i]);
1866   }
1867 
1868   /* Wait till RTC SEEDF flag is cleared and if Time out is reached exit */
1869   tickstart = HAL_GetTick();
1870   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0U)
1871   {
1872     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1873     {
1874       /* New check to avoid false timeout detection in case of preemption */
1875       if (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1876       {
1877         /* Change RTC state */
1878         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1879 
1880         return HAL_TIMEOUT;
1881       }
1882       else
1883       {
1884         break;
1885       }
1886     }
1887   }
1888 
1889   return HAL_OK;
1890 }
1891 
1892 #if defined(TAMP_CFGR_BHKLOCK)
1893 /**
1894   * @brief  Lock the Boot hardware Key
1895   * @param  hrtc RTC handle
1896   * @note   The backup registers from TAMP_BKP0R to TAMP_BKP7R cannot be accessed neither in
1897   *         read nor in write (they are read as 0 and write ignore).
1898   * @retval HAL status
1899   */
HAL_RTCEx_LockBootHardwareKey(const RTC_HandleTypeDef * hrtc)1900 HAL_StatusTypeDef HAL_RTCEx_LockBootHardwareKey(const RTC_HandleTypeDef *hrtc)
1901 {
1902   /* Prevent unused argument(s) compilation warning */
1903   UNUSED(hrtc);
1904 
1905   WRITE_REG(TAMP->CFGR, TAMP_CFGR_BHKLOCK);
1906 
1907   return HAL_OK;
1908 }
1909 #endif /* TAMP_CFGR_BHKLOCK */
1910 
1911 /**
1912   * @brief  Deactivate all Active Tampers at the same time.
1913   * @param  hrtc RTC handle
1914   * @retval HAL status
1915   */
HAL_RTCEx_DeactivateActiveTampers(const RTC_HandleTypeDef * hrtc)1916 HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(const RTC_HandleTypeDef *hrtc)
1917 {
1918   /* Prevent unused argument(s) compilation warning */
1919   UNUSED(hrtc);
1920 
1921   /* Get Active tampers */
1922   uint32_t atamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
1923 
1924   /* Disable all actives tampers but not passives tampers */
1925   CLEAR_BIT(TAMP->CR1, atamp_mask);
1926 
1927   /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
1928   WRITE_REG(TAMP->SCR, atamp_mask);
1929 
1930   /* Disable no erase and mask */
1931   CLEAR_BIT(TAMP->CR2, (atamp_mask | ((atamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM)) <<
1932                                       TAMP_CR2_TAMP1MSK_Pos)));
1933 
1934   /* Clear all active tampers interrupt mode configuration but not passives tampers */
1935   CLEAR_BIT(TAMP->IER, atamp_mask);
1936 
1937   /* Set reset value for active tamper control register 1 */
1938   WRITE_REG(TAMP->ATCR1, TAMP_ATCR1_ATCKSEL_0 | TAMP_ATCR1_ATCKSEL_1 | TAMP_ATCR1_ATCKSEL_2);
1939 
1940   /* Set reset value for active tamper control register 2 */
1941   CLEAR_REG(TAMP->ATCR2);
1942 
1943   return HAL_OK;
1944 }
1945 
1946 /**
1947   * @brief  Tamper event polling.
1948   * @param  hrtc RTC handle
1949   * @param  Tamper Selected tamper pin.
1950   *         This parameter can be a combination of the following values:
1951   *         @arg RTC_TAMPER_1
1952   *         @arg RTC_TAMPER_2
1953   *         @arg RTC_TAMPER_3
1954   *         @arg RTC_TAMPER_4
1955   *         @arg RTC_TAMPER_5
1956   *         @arg RTC_TAMPER_6
1957   *         @arg RTC_TAMPER_7
1958   *         @arg RTC_TAMPER_8
1959   * @param  Timeout Timeout duration
1960   * @retval HAL status
1961   */
HAL_RTCEx_PollForTamperEvent(const RTC_HandleTypeDef * hrtc,uint32_t Tamper,uint32_t Timeout)1962 HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(const RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
1963 {
1964   /* Prevent unused argument(s) compilation warning */
1965   UNUSED(hrtc);
1966 
1967   assert_param(IS_RTC_TAMPER(Tamper));
1968 
1969   uint32_t tickstart = HAL_GetTick();
1970 
1971   /* Get the status of the Interrupt */
1972   while (READ_BIT(TAMP->SR, Tamper) != Tamper)
1973   {
1974     if (Timeout != HAL_MAX_DELAY)
1975     {
1976       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1977       {
1978         /* New check to avoid false timeout detection in case of preemption */
1979         if (READ_BIT(TAMP->SR, Tamper) != Tamper)
1980         {
1981           return HAL_TIMEOUT;
1982         }
1983         else
1984         {
1985           break;
1986         }
1987       }
1988     }
1989   }
1990 
1991   /* Clear the Tamper Flag */
1992   WRITE_REG(TAMP->SCR, Tamper);
1993 
1994   return HAL_OK;
1995 }
1996 
1997 
1998 /**
1999   * @brief  Set Internal Tamper
2000   * @param  hrtc RTC handle
2001   * @param  sIntTamper Pointer to Internal Tamper Structure.
2002   * @retval HAL status
2003   */
HAL_RTCEx_SetInternalTamper(const RTC_HandleTypeDef * hrtc,const RTC_InternalTamperTypeDef * sIntTamper)2004 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(const RTC_HandleTypeDef *hrtc,
2005                                               const RTC_InternalTamperTypeDef *sIntTamper)
2006 {
2007   /* Prevent unused argument(s) compilation warning */
2008   UNUSED(hrtc);
2009 
2010   /* Check the parameters */
2011   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
2012   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
2013   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
2014 
2015   /* Timestamp enable on internal tamper */
2016   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
2017   {
2018     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
2019   }
2020 
2021   /* No Erase Backup register enable for Internal Tamper */
2022   if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
2023   {
2024     SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1POM_Pos)));
2025   }
2026   else
2027   {
2028     CLEAR_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1POM_Pos)));
2029   }
2030 
2031   /* Enable Internal Tamper */
2032   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
2033 
2034   return HAL_OK;
2035 }
2036 
2037 
2038 /**
2039   * @brief  Set Internal Tamper in interrupt mode
2040   * @note   The application must ensure that the EXTI RTC tamper interrupt line is configured and enabled.
2041   * @param  hrtc RTC handle
2042   * @param  sIntTamper Pointer to Internal Tamper Structure.
2043   * @retval HAL status
2044   */
HAL_RTCEx_SetInternalTamper_IT(const RTC_HandleTypeDef * hrtc,const RTC_InternalTamperTypeDef * sIntTamper)2045 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(const RTC_HandleTypeDef *hrtc,
2046                                                  const RTC_InternalTamperTypeDef *sIntTamper)
2047 {
2048   /* Prevent unused argument(s) compilation warning */
2049   UNUSED(hrtc);
2050 
2051   /* Check the parameters */
2052   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
2053   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
2054   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
2055 
2056   /* Timestamp enable on internal tamper */
2057   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
2058   {
2059     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
2060   }
2061 
2062   /* Interrupt enable register */
2063   SET_BIT(TAMP->IER, sIntTamper->IntTamper);
2064 
2065   /* No Erase Backup register enable for Internal Tamper */
2066   if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
2067   {
2068     SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1POM_Pos)));
2069   }
2070   else
2071   {
2072     CLEAR_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1POM_Pos)));
2073   }
2074 
2075   /* Enable Internal Tamper */
2076   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
2077 
2078   return HAL_OK;
2079 }
2080 
2081 /**
2082   * @brief  Deactivate Internal Tamper.
2083   * @param  hrtc RTC handle
2084   * @param  IntTamper Selected internal tamper event.
2085   *          This parameter can be any combination of existing internal tampers.
2086   * @retval HAL status
2087   */
HAL_RTCEx_DeactivateInternalTamper(const RTC_HandleTypeDef * hrtc,uint32_t IntTamper)2088 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(const RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
2089 {
2090   /* Prevent unused argument(s) compilation warning */
2091   UNUSED(hrtc);
2092 
2093   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
2094 
2095   /* Disable the selected Tamper pin */
2096   CLEAR_BIT(TAMP->CR1, IntTamper);
2097 
2098   /* Clear internal tamper interrupt mode configuration */
2099   CLEAR_BIT(TAMP->IER, IntTamper);
2100 
2101   /* Clear internal tamper interrupt */
2102   WRITE_REG(TAMP->SCR, IntTamper);
2103 
2104   return HAL_OK;
2105 }
2106 
2107 /**
2108   * @brief  Internal Tamper event polling.
2109   * @param  hrtc RTC handle
2110   * @param  IntTamper selected tamper.
2111   *          This parameter can be any combination of existing internal tampers.
2112   * @param  Timeout Timeout duration
2113   * @retval HAL status
2114   */
HAL_RTCEx_PollForInternalTamperEvent(const RTC_HandleTypeDef * hrtc,uint32_t IntTamper,uint32_t Timeout)2115 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(const RTC_HandleTypeDef *hrtc, uint32_t IntTamper,
2116                                                        uint32_t Timeout)
2117 {
2118   /* Prevent unused argument(s) compilation warning */
2119   UNUSED(hrtc);
2120 
2121   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
2122 
2123   uint32_t tickstart = HAL_GetTick();
2124 
2125   /* Get the status of the Interrupt */
2126   while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
2127   {
2128     if (Timeout != HAL_MAX_DELAY)
2129     {
2130       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2131       {
2132         /* New check to avoid false timeout detection in case of preemption */
2133         if (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
2134         {
2135           return HAL_TIMEOUT;
2136         }
2137         else
2138         {
2139           break;
2140         }
2141       }
2142     }
2143   }
2144 
2145   /* Clear the Tamper Flag */
2146   WRITE_REG(TAMP->SCR, IntTamper);
2147 
2148   return HAL_OK;
2149 }
2150 
2151 /**
2152   * @brief  Handle Tamper interrupt request.
2153   * @param  hrtc RTC handle
2154   * @retval None
2155   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)2156 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
2157 {
2158   /* Get the pending status of the Tampers Interrupt */
2159   uint32_t tmp = READ_REG(TAMP->MISR);
2160 
2161   /* Check Tamper1 status */
2162   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
2163   {
2164 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2165     /* Call Tamper 1 Event registered Callback */
2166     hrtc->Tamper1EventCallback(hrtc);
2167 #else
2168     /* Tamper1 callback */
2169     HAL_RTCEx_Tamper1EventCallback(hrtc);
2170 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2171   }
2172 
2173   /* Check Tamper2 status */
2174   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
2175   {
2176 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2177     /* Call Tamper 2 Event registered Callback */
2178     hrtc->Tamper2EventCallback(hrtc);
2179 #else
2180     /* Tamper2 callback */
2181     HAL_RTCEx_Tamper2EventCallback(hrtc);
2182 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2183   }
2184 
2185 #if (RTC_TAMP_NB > 2U)
2186 
2187   /* Check Tamper3 status */
2188   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
2189   {
2190 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2191     /* Call Tamper 3 Event registered Callback */
2192     hrtc->Tamper3EventCallback(hrtc);
2193 #else
2194     /* Tamper3 callback */
2195     HAL_RTCEx_Tamper3EventCallback(hrtc);
2196 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2197   }
2198 
2199   /* Check Tamper4 status */
2200   if ((tmp & RTC_TAMPER_4) == RTC_TAMPER_4)
2201   {
2202 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2203     /* Call Tamper 4 Event registered Callback */
2204     hrtc->Tamper4EventCallback(hrtc);
2205 #else
2206     /* Tamper4 callback */
2207     HAL_RTCEx_Tamper4EventCallback(hrtc);
2208 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2209   }
2210 
2211   /* Check Tamper5 status */
2212   if ((tmp & RTC_TAMPER_5) == RTC_TAMPER_5)
2213   {
2214 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2215     /* Call Tamper 5 Event registered Callback */
2216     hrtc->Tamper5EventCallback(hrtc);
2217 #else
2218     /* Tamper5 callback */
2219     HAL_RTCEx_Tamper5EventCallback(hrtc);
2220 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2221   }
2222 
2223   /* Check Tamper6 status */
2224   if ((tmp & RTC_TAMPER_6) == RTC_TAMPER_6)
2225   {
2226 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2227     /* Call Tamper 6 Event registered Callback */
2228     hrtc->Tamper6EventCallback(hrtc);
2229 #else
2230     /* Tamper6 callback */
2231     HAL_RTCEx_Tamper6EventCallback(hrtc);
2232 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2233   }
2234 
2235 #endif /* (RTC_TAMP_NB > 2U) */
2236 #if (RTC_TAMP_NB > 6U)
2237 
2238   /* Check Tamper7 status */
2239   if ((tmp & RTC_TAMPER_7) == RTC_TAMPER_7)
2240   {
2241 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2242     /* Call Tamper 7 Event registered Callback */
2243     hrtc->Tamper7EventCallback(hrtc);
2244 #else
2245     /* Tamper7 callback */
2246     HAL_RTCEx_Tamper7EventCallback(hrtc);
2247 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2248   }
2249 
2250   /* Check Tamper8 status */
2251   if ((tmp & RTC_TAMPER_8) == RTC_TAMPER_8)
2252   {
2253 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2254     /* Call Tamper 8 Event registered Callback */
2255     hrtc->Tamper8EventCallback(hrtc);
2256 #else
2257     /* Tamper8 callback */
2258     HAL_RTCEx_Tamper8EventCallback(hrtc);
2259 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2260   }
2261 
2262 #endif /* (RTC_TAMP_NB > 6U) */
2263 
2264   /* Check Internal Tamper1 status */
2265   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
2266   {
2267 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2268     /* Call Internal Tamper 1 Event registered Callback */
2269     hrtc->InternalTamper1EventCallback(hrtc);
2270 #else
2271     /* Internal Tamper1 callback */
2272     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
2273 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2274   }
2275 
2276   /* Check Internal Tamper2 status */
2277   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
2278   {
2279 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2280     /* Call Internal Tamper 2 Event registered Callback */
2281     hrtc->InternalTamper2EventCallback(hrtc);
2282 #else
2283     /* Internal Tamper2 callback */
2284     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
2285 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2286   }
2287 
2288   /* Check Internal Tamper3 status */
2289   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
2290   {
2291 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2292     /* Call Internal Tamper 3 Event registered Callback */
2293     hrtc->InternalTamper3EventCallback(hrtc);
2294 #else
2295     /* Internal Tamper3 callback */
2296     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2297 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2298   }
2299 
2300   /* Check Internal Tamper4 status */
2301   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
2302   {
2303 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2304     /* Call Internal Tamper 4 Event registered Callback */
2305     hrtc->InternalTamper4EventCallback(hrtc);
2306 #else
2307     /* Internal Tamper4 callback */
2308     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
2309 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2310   }
2311 
2312   /* Check Internal Tamper5 status */
2313   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2314   {
2315 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2316     /* Call Internal Tamper 5 Event registered Callback */
2317     hrtc->InternalTamper5EventCallback(hrtc);
2318 #else
2319     /* Internal Tamper5 callback */
2320     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2321 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2322   }
2323 
2324   /* Check Internal Tamper6 status */
2325   if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
2326   {
2327 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2328     /* Call Internal Tamper 6 Event registered Callback */
2329     hrtc->InternalTamper6EventCallback(hrtc);
2330 #else
2331     /* Internal Tamper6 callback */
2332     HAL_RTCEx_InternalTamper6EventCallback(hrtc);
2333 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2334   }
2335 
2336   /* Check Internal Tamper7 status */
2337   if ((tmp & RTC_INT_TAMPER_7) == RTC_INT_TAMPER_7)
2338   {
2339 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2340     /* Call Internal Tamper 7 Event registered Callback */
2341     hrtc->InternalTamper7EventCallback(hrtc);
2342 #else
2343     /* Internal Tamper7 callback */
2344     HAL_RTCEx_InternalTamper7EventCallback(hrtc);
2345 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2346   }
2347 
2348   /* Check Internal Tamper8 status */
2349   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2350   {
2351 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2352     /* Call Internal Tamper 8 Event registered Callback */
2353     hrtc->InternalTamper8EventCallback(hrtc);
2354 #else
2355     /* Internal Tamper8 callback */
2356     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2357 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2358   }
2359 
2360   /* Check Internal Tamper9 status */
2361   if ((tmp & RTC_INT_TAMPER_9) == RTC_INT_TAMPER_9)
2362   {
2363 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2364     /* Call Internal Tamper 9 Event registered Callback */
2365     hrtc->InternalTamper9EventCallback(hrtc);
2366 #else
2367     /* Internal Tamper9 callback */
2368     HAL_RTCEx_InternalTamper9EventCallback(hrtc);
2369 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2370   }
2371 
2372   /* Check Internal Tamper11 status */
2373   if ((tmp & RTC_INT_TAMPER_11) == RTC_INT_TAMPER_11)
2374   {
2375 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2376     /* Call Internal Tamper 11 Event registered Callback */
2377     hrtc->InternalTamper11EventCallback(hrtc);
2378 #else
2379     /* Internal Tamper11 callback */
2380     HAL_RTCEx_InternalTamper11EventCallback(hrtc);
2381 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2382   }
2383 
2384   /* Check Internal Tamper15 status */
2385   if ((tmp & RTC_INT_TAMPER_15) == RTC_INT_TAMPER_15)
2386   {
2387 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2388     /* Call Internal Tamper 15 Event registered Callback */
2389     hrtc->InternalTamper15EventCallback(hrtc);
2390 #else
2391     /* Internal Tamper15 callback */
2392     HAL_RTCEx_InternalTamper15EventCallback(hrtc);
2393 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2394   }
2395 
2396   /* Clear flags after treatment to allow the potential tamper feature */
2397   WRITE_REG(TAMP->SCR, tmp);
2398 }
2399 
2400 /**
2401   * @brief  Tamper 1 callback.
2402   * @param  hrtc RTC handle
2403   * @retval None
2404   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)2405 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
2406 {
2407   /* Prevent unused argument(s) compilation warning */
2408   UNUSED(hrtc);
2409 
2410   /* NOTE : This function should not be modified, when the callback is needed,
2411             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
2412    */
2413 }
2414 
2415 /**
2416   * @brief  Tamper 2 callback.
2417   * @param  hrtc RTC handle
2418   * @retval None
2419   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)2420 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
2421 {
2422   /* Prevent unused argument(s) compilation warning */
2423   UNUSED(hrtc);
2424 
2425   /* NOTE : This function should not be modified, when the callback is needed,
2426             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
2427    */
2428 }
2429 
2430 /**
2431   * @brief  Tamper 3 callback.
2432   * @param  hrtc RTC handle
2433   * @retval None
2434   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)2435 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
2436 {
2437   /* Prevent unused argument(s) compilation warning */
2438   UNUSED(hrtc);
2439 
2440   /* NOTE : This function should not be modified, when the callback is needed,
2441             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
2442    */
2443 }
2444 
2445 /**
2446   * @brief  Tamper 4 callback.
2447   * @param  hrtc RTC handle
2448   * @retval None
2449   */
HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef * hrtc)2450 __weak void HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef *hrtc)
2451 {
2452   /* Prevent unused argument(s) compilation warning */
2453   UNUSED(hrtc);
2454 
2455   /* NOTE : This function should not be modified, when the callback is needed,
2456             the HAL_RTCEx_Tamper4EventCallback could be implemented in the user file
2457    */
2458 }
2459 
2460 /**
2461   * @brief  Tamper 5 callback.
2462   * @param  hrtc RTC handle
2463   * @retval None
2464   */
HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef * hrtc)2465 __weak void HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef *hrtc)
2466 {
2467   /* Prevent unused argument(s) compilation warning */
2468   UNUSED(hrtc);
2469 
2470   /* NOTE : This function should not be modified, when the callback is needed,
2471             the HAL_RTCEx_Tamper5EventCallback could be implemented in the user file
2472    */
2473 }
2474 
2475 /**
2476   * @brief  Tamper 6 callback.
2477   * @param  hrtc RTC handle
2478   * @retval None
2479   */
HAL_RTCEx_Tamper6EventCallback(RTC_HandleTypeDef * hrtc)2480 __weak void HAL_RTCEx_Tamper6EventCallback(RTC_HandleTypeDef *hrtc)
2481 {
2482   /* Prevent unused argument(s) compilation warning */
2483   UNUSED(hrtc);
2484 
2485   /* NOTE : This function should not be modified, when the callback is needed,
2486             the HAL_RTCEx_Tamper6EventCallback could be implemented in the user file
2487    */
2488 }
2489 
2490 /**
2491   * @brief  Tamper 7 callback.
2492   * @param  hrtc RTC handle
2493   * @retval None
2494   */
HAL_RTCEx_Tamper7EventCallback(RTC_HandleTypeDef * hrtc)2495 __weak void HAL_RTCEx_Tamper7EventCallback(RTC_HandleTypeDef *hrtc)
2496 {
2497   /* Prevent unused argument(s) compilation warning */
2498   UNUSED(hrtc);
2499 
2500   /* NOTE : This function should not be modified, when the callback is needed,
2501             the HAL_RTCEx_Tamper7EventCallback could be implemented in the user file
2502    */
2503 }
2504 
2505 /**
2506   * @brief  Tamper 8 callback.
2507   * @param  hrtc RTC handle
2508   * @retval None
2509   */
HAL_RTCEx_Tamper8EventCallback(RTC_HandleTypeDef * hrtc)2510 __weak void HAL_RTCEx_Tamper8EventCallback(RTC_HandleTypeDef *hrtc)
2511 {
2512   /* Prevent unused argument(s) compilation warning */
2513   UNUSED(hrtc);
2514 
2515   /* NOTE : This function should not be modified, when the callback is needed,
2516             the HAL_RTCEx_Tamper8EventCallback could be implemented in the user file
2517    */
2518 }
2519 
2520 /**
2521   * @brief  Internal Tamper 1 callback.
2522   * @param  hrtc RTC handle
2523   * @retval None
2524   */
HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef * hrtc)2525 __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
2526 {
2527   /* Prevent unused argument(s) compilation warning */
2528   UNUSED(hrtc);
2529 
2530   /* NOTE : This function should not be modified, when the callback is needed,
2531             the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
2532    */
2533 }
2534 
2535 /**
2536   * @brief  Internal Tamper 2 callback.
2537   * @param  hrtc RTC handle
2538   * @retval None
2539   */
HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef * hrtc)2540 __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
2541 {
2542   /* Prevent unused argument(s) compilation warning */
2543   UNUSED(hrtc);
2544 
2545   /* NOTE : This function should not be modified, when the callback is needed,
2546             the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
2547    */
2548 }
2549 
2550 /**
2551   * @brief  Internal Tamper 3 callback.
2552   * @param  hrtc RTC handle
2553   * @retval None
2554   */
HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef * hrtc)2555 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
2556 {
2557   /* Prevent unused argument(s) compilation warning */
2558   UNUSED(hrtc);
2559 
2560   /* NOTE : This function should not be modified, when the callback is needed,
2561             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2562    */
2563 }
2564 
2565 /**
2566   * @brief  Internal Tamper 4 callback.
2567   * @param  hrtc RTC handle
2568   * @retval None
2569   */
HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef * hrtc)2570 __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
2571 {
2572   /* Prevent unused argument(s) compilation warning */
2573   UNUSED(hrtc);
2574 
2575   /* NOTE : This function should not be modified, when the callback is needed,
2576             the HAL_RTCEx_InternalTamper4EventCallback could be implemented in the user file
2577    */
2578 }
2579 
2580 /**
2581   * @brief  Internal Tamper 5 callback.
2582   * @param  hrtc RTC handle
2583   * @retval None
2584   */
HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef * hrtc)2585 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
2586 {
2587   /* Prevent unused argument(s) compilation warning */
2588   UNUSED(hrtc);
2589 
2590   /* NOTE : This function should not be modified, when the callback is needed,
2591             the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
2592    */
2593 }
2594 
2595 /**
2596   * @brief  Internal Tamper 6 callback.
2597   * @param  hrtc RTC handle
2598   * @retval None
2599   */
HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef * hrtc)2600 __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
2601 {
2602   /* Prevent unused argument(s) compilation warning */
2603   UNUSED(hrtc);
2604 
2605   /* NOTE : This function should not be modified, when the callback is needed,
2606             the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
2607    */
2608 }
2609 
2610 /**
2611   * @brief  Internal Tamper 7 callback.
2612   * @param  hrtc RTC handle
2613   * @retval None
2614   */
HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef * hrtc)2615 __weak void HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef *hrtc)
2616 {
2617   /* Prevent unused argument(s) compilation warning */
2618   UNUSED(hrtc);
2619 
2620   /* NOTE : This function should not be modified, when the callback is needed,
2621             the HAL_RTCEx_InternalTamper7EventCallback could be implemented in the user file
2622    */
2623 }
2624 
2625 /**
2626   * @brief  Internal Tamper 8 callback.
2627   * @param  hrtc RTC handle
2628   * @retval None
2629   */
HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef * hrtc)2630 __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
2631 {
2632   /* Prevent unused argument(s) compilation warning */
2633   UNUSED(hrtc);
2634 
2635   /* NOTE : This function should not be modified, when the callback is needed,
2636             the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
2637    */
2638 }
2639 
2640 /**
2641   * @brief  Internal Tamper 9 callback.
2642   * @param  hrtc RTC handle
2643   * @retval None
2644   */
HAL_RTCEx_InternalTamper9EventCallback(RTC_HandleTypeDef * hrtc)2645 __weak void HAL_RTCEx_InternalTamper9EventCallback(RTC_HandleTypeDef *hrtc)
2646 {
2647   /* Prevent unused argument(s) compilation warning */
2648   UNUSED(hrtc);
2649 
2650   /* NOTE : This function should not be modified, when the callback is needed,
2651             the HAL_RTCEx_InternalTamper9EventCallback could be implemented in the user file
2652    */
2653 }
2654 
2655 /**
2656   * @brief  Internal Tamper 11 callback.
2657   * @param  hrtc RTC handle
2658   * @retval None
2659   */
HAL_RTCEx_InternalTamper11EventCallback(RTC_HandleTypeDef * hrtc)2660 __weak void HAL_RTCEx_InternalTamper11EventCallback(RTC_HandleTypeDef *hrtc)
2661 {
2662   /* Prevent unused argument(s) compilation warning */
2663   UNUSED(hrtc);
2664 
2665   /* NOTE : This function should not be modified, when the callback is needed,
2666             the HAL_RTCEx_InternalTamper11EventCallback could be implemented in the user file
2667    */
2668 }
2669 
2670 /**
2671   * @brief  Internal Tamper 15 callback.
2672   * @param  hrtc RTC handle
2673   * @retval None
2674   */
HAL_RTCEx_InternalTamper15EventCallback(RTC_HandleTypeDef * hrtc)2675 __weak void HAL_RTCEx_InternalTamper15EventCallback(RTC_HandleTypeDef *hrtc)
2676 {
2677   /* Prevent unused argument(s) compilation warning */
2678   UNUSED(hrtc);
2679 
2680   /* NOTE : This function should not be modified, when the callback is needed,
2681             the HAL_RTCEx_InternalTamper15EventCallback could be implemented in the user file
2682    */
2683 }
2684 /**
2685   * @}
2686   */
2687 
2688 
2689 /** @addtogroup RTCEx_Exported_Functions_Group6
2690   * @brief      Extended RTC Backup register functions
2691   *
2692 @verbatim
2693   ===============================================================================
2694              ##### Extended RTC Backup register functions #####
2695   ===============================================================================
2696   [..]
2697    (+) Before calling any tamper or internal tamper function, you have to call first
2698        HAL_RTC_Init() function.
2699    (+) In that one you can select to output tamper event on RTC pin.
2700   [..]
2701    This subsection provides functions allowing to
2702    (+) Write a data in a specified RTC Backup data register
2703    (+) Read a data in a specified RTC Backup data register
2704 @endverbatim
2705   * @{
2706   */
2707 
2708 
2709 /**
2710   * @brief  Write a data in a specified RTC Backup data register.
2711   * @param  hrtc RTC handle
2712   * @param  BackupRegister RTC Backup data Register number.
2713   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2714   * @param  Data Data to be written in the specified Backup data register.
2715   * @retval None
2716   */
HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)2717 void HAL_RTCEx_BKUPWrite(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2718 {
2719   uint32_t tmp;
2720 
2721   /* Prevent unused argument(s) compilation warning */
2722   UNUSED(hrtc);
2723 
2724   /* Check the parameters */
2725   assert_param(IS_RTC_BKP(BackupRegister));
2726 
2727   /* Determine address of the specified Backup register */
2728   tmp = (uint32_t)(&(TAMP->BKP0R));
2729   tmp += (BackupRegister * 4U);
2730 
2731   /* Write data in the specified register Backup register */
2732   *(__IO uint32_t *)tmp = (uint32_t)Data;
2733 }
2734 
2735 
2736 /**
2737   * @brief  Reads data from the specified RTC Backup data Register.
2738   * @param  hrtc RTC handle
2739   * @param  BackupRegister RTC Backup data Register number.
2740   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2741   * @retval Read value
2742   */
HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)2743 uint32_t HAL_RTCEx_BKUPRead(const RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2744 {
2745   uint32_t tmp;
2746 
2747   /* Prevent unused argument(s) compilation warning */
2748   UNUSED(hrtc);
2749 
2750   /* Check the parameters */
2751   assert_param(IS_RTC_BKP(BackupRegister));
2752 
2753   /* Determine address of the specified Backup register */
2754   tmp = (uint32_t)(&(TAMP->BKP0R));
2755   tmp += (BackupRegister * 4U);
2756 
2757   /* Read the data from the specified register */
2758   return (*(__IO uint32_t *)tmp);
2759 }
2760 
2761 /**
2762   * @brief  Reset the RTC Backup data Registers and the device secrets.
2763   * @param  hrtc RTC handle
2764   * @retval None
2765   */
HAL_RTCEx_BKUPErase(const RTC_HandleTypeDef * hrtc)2766 void  HAL_RTCEx_BKUPErase(const RTC_HandleTypeDef *hrtc)
2767 {
2768   /* Prevent unused argument(s) compilation warning */
2769   UNUSED(hrtc);
2770 
2771   SET_BIT(TAMP->CR2, TAMP_CR2_BKERASE);
2772 }
2773 
2774 /**
2775   * @brief  Block the access to the RTC Backup data Register and all the device secrets.
2776   * @param  hrtc RTC handle
2777   * @retval None
2778   */
HAL_RTCEx_BKUPBlock(const RTC_HandleTypeDef * hrtc)2779 void  HAL_RTCEx_BKUPBlock(const RTC_HandleTypeDef *hrtc)
2780 {
2781   /* Prevent unused argument(s) compilation warning */
2782   UNUSED(hrtc);
2783 
2784   WRITE_REG(TAMP->CR2, TAMP_CR2_BKBLOCK);
2785 }
2786 
2787 /**
2788   * @brief  Disable the Block to the access to the RTC Backup data Register and the device secrets.
2789   * @param  hrtc RTC handle
2790   * @retval None
2791   */
HAL_RTCEx_BKUPUnblock(const RTC_HandleTypeDef * hrtc)2792 void  HAL_RTCEx_BKUPUnblock(const RTC_HandleTypeDef *hrtc)
2793 {
2794   /* Prevent unused argument(s) compilation warning */
2795   UNUSED(hrtc);
2796 
2797   CLEAR_BIT(TAMP->CR2, TAMP_CR2_BKBLOCK);
2798 }
2799 
2800 #ifdef TAMP_RPCFGR_RPCFG0
2801 /**
2802   * @brief  Enable and Disable the erase of the configurable Device Secrets
2803   * @note   This API must be called before enabling the Tamper.
2804   * @param  hrtc RTC handle
2805   * @param  DeviceSecretConf Specifies the configuration of the Device Secrets
2806   *         This parameter can be a combination of the following values:
2807   *         @arg TAMP_DEVICESECRETS_ERASE_NONE
2808   *         @arg TAMP_DEVICESECRETS_ERASE_BKPSRAM
2809   *
2810   * @retval None
2811   */
HAL_RTCEx_ConfigEraseDeviceSecrets(const RTC_HandleTypeDef * hrtc,uint32_t DeviceSecretConf)2812 void  HAL_RTCEx_ConfigEraseDeviceSecrets(const RTC_HandleTypeDef *hrtc, uint32_t DeviceSecretConf)
2813 {
2814   /* Prevent unused argument(s) compilation warning */
2815   UNUSED(hrtc);
2816 
2817   MODIFY_REG(TAMP->RPCFGR, TAMP_RPCFGR_RPCFG0, DeviceSecretConf);
2818 }
2819 #endif /* TAMP_RPCFGR_RPCFG0 */
2820 
2821 /**
2822   * @}
2823   */
2824 
2825 #if defined(TAMP_PRIVCFGR_TAMPPRIV)
2826 /** @addtogroup RTCEx_Exported_Functions_Group8
2827   * @brief      Extended RTC privilege functions
2828   *
2829 @verbatim
2830   ===============================================================================
2831              ##### Extended RTC privilege functions #####
2832   ===============================================================================
2833   [..]
2834    (+) Before calling privilege function, you have to call first
2835        HAL_RTC_Init() function.
2836 @endverbatim
2837   * @{
2838   */
2839 
2840 /**
2841   * @brief  Set the privilege level of the RTC/TAMP/Backup registers.
2842   *         To get the current privilege level call HAL_RTCEx_PrivilegeModeGet.
2843   * @param  hrtc RTC handle
2844   * @param  privilegeState  Privilege state
2845   * @retval HAL_StatusTypeDef
2846   */
HAL_RTCEx_PrivilegeModeSet(const RTC_HandleTypeDef * hrtc,const RTC_PrivilegeStateTypeDef * privilegeState)2847 HAL_StatusTypeDef HAL_RTCEx_PrivilegeModeSet(const RTC_HandleTypeDef *hrtc,
2848                                              const RTC_PrivilegeStateTypeDef *privilegeState)
2849 {
2850   /* Prevent unused argument(s) compilation warning */
2851   UNUSED(hrtc);
2852 
2853   assert_param(IS_RTC_PRIVILEGE_FULL(privilegeState->rtcPrivilegeFull));
2854   assert_param(IS_RTC_PRIVILEGE_FEATURES(privilegeState->rtcPrivilegeFeatures));
2855   assert_param(IS_TAMP_PRIVILEGE_FULL(privilegeState->tampPrivilegeFull));
2856   assert_param(IS_TAMP_MONOTONIC_CNT_PRIVILEGE(privilegeState->MonotonicCounterPrivilege));
2857   assert_param(IS_RTC_PRIVILEGE_BKUP_ZONE(privilegeState->backupRegisterPrivZone));
2858   assert_param(IS_RTC_BKP(privilegeState->backupRegisterStartZone2));
2859   assert_param(IS_RTC_BKP(privilegeState->backupRegisterStartZone3));
2860 
2861   /* RTC privilege configuration */
2862   WRITE_REG(RTC->PRIVCFGR, privilegeState->rtcPrivilegeFull | privilegeState->rtcPrivilegeFeatures);
2863 
2864   /* TAMP, Monotonic counter and Backup registers privilege configuration */
2865   WRITE_REG(TAMP->PRIVCFGR, privilegeState->tampPrivilegeFull | privilegeState->backupRegisterPrivZone | \
2866             privilegeState->MonotonicCounterPrivilege);
2867 
2868   /* Backup register start zone */
2869   MODIFY_REG(TAMP->CFGR,
2870              (TAMP_CFGR_BKPRW | TAMP_CFGR_BKPW),
2871              ((privilegeState->backupRegisterStartZone2 << TAMP_CFGR_BKPRW_Pos) | (privilegeState->backupRegisterStartZone3 << TAMP_CFGR_BKPW_Pos)));
2872 
2873   return HAL_OK;
2874 }
2875 
2876 /**
2877   * @brief  Get the privilege level of the RTC/TAMP/Backup registers.
2878   *         To set the privilege level please call HAL_RTCEx_PrivilegeModeSet.
2879   * @param  hrtc RTC handle
2880   * @param  privilegeState  Privilege state
2881   * @retval HAL_StatusTypeDef
2882   */
HAL_RTCEx_PrivilegeModeGet(const RTC_HandleTypeDef * hrtc,RTC_PrivilegeStateTypeDef * privilegeState)2883 HAL_StatusTypeDef HAL_RTCEx_PrivilegeModeGet(const RTC_HandleTypeDef *hrtc, RTC_PrivilegeStateTypeDef *privilegeState)
2884 {
2885   /* Prevent unused argument(s) compilation warning */
2886   UNUSED(hrtc);
2887 
2888   /* Read registers */
2889   uint32_t rtc_privcfgr = READ_REG(RTC->PRIVCFGR);
2890   uint32_t tamp_privcfgr = READ_REG(TAMP->PRIVCFGR);
2891   uint32_t tamp_cfgr = READ_REG(TAMP->CFGR);
2892 
2893   /* RTC privilege configuration */
2894   privilegeState->rtcPrivilegeFull = READ_BIT(rtc_privcfgr, RTC_PRIVCFGR_PRIV);
2895 
2896   /* Warning, rtcPrivilegeFeatures is only relevant if privilegeState->rtcPrivilegeFull == RTC_PRIVILEGE_FULL_NO */
2897   privilegeState->rtcPrivilegeFeatures = READ_BIT(rtc_privcfgr, RTC_PRIVILEGE_FEATURE_ALL);
2898 
2899   /* TAMP and Backup registers privilege configuration */
2900   privilegeState->tampPrivilegeFull = READ_BIT(tamp_privcfgr, TAMP_PRIVCFGR_TAMPPRIV);
2901 
2902   /* Monotonic registers privilege configuration */
2903   privilegeState->MonotonicCounterPrivilege = READ_BIT(tamp_privcfgr, TAMP_PRIVCFGR_CNT1PRIV);
2904 
2905   /* Backup registers Zones */
2906   privilegeState->backupRegisterPrivZone = READ_BIT(tamp_privcfgr, (TAMP_PRIVCFGR_BKPWPRIV | TAMP_PRIVCFGR_BKPRWPRIV));
2907 
2908   /* Backup registers start zones */
2909   privilegeState->backupRegisterStartZone2 = READ_BIT(tamp_cfgr, TAMP_CFGR_BKPRW) >> TAMP_CFGR_BKPRW_Pos;
2910   privilegeState->backupRegisterStartZone3 = READ_BIT(tamp_cfgr, TAMP_CFGR_BKPW) >> TAMP_CFGR_BKPW_Pos;
2911 
2912   return HAL_OK;
2913 }
2914 
2915 /**
2916   * @}
2917   */
2918 #endif /* TAMP_PRIVCFGR_TAMPPRIV */
2919 
2920 /**
2921   * @}
2922   */
2923 
2924 #endif /* HAL_RTC_MODULE_ENABLED */
2925 
2926 /**
2927   * @}
2928   */
2929 
2930 
2931 /**
2932   * @}
2933   */
2934