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