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