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