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