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   uint32_t tickstart;
660 
661   /* Process Locked */
662   __HAL_LOCK(hrtc);
663 
664   /* Change RTC state */
665   hrtc->State = HAL_RTC_STATE_BUSY;
666 
667 
668   /* Disable the Wakeup Timer */
669   /* In case of interrupt mode is used, the interrupt source must disabled */
670   CLEAR_BIT(RTC->CR, (RTC_CR_WUTE | RTC_CR_WUTIE));
671 
672   tickstart = HAL_GetTick();
673 
674   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
675   while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
676   {
677     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
678     {
679       /* New check to avoid false timeout detection in case of preemption */
680       if (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
681       {
682 
683         /* Change RTC state */
684         hrtc->State = HAL_RTC_STATE_TIMEOUT;
685 
686         /* Process Unlocked */
687         __HAL_UNLOCK(hrtc);
688 
689         return HAL_TIMEOUT;
690       }
691       else
692       {
693         break;
694       }
695     }
696   }
697 
698   /* Enable the write protection for RTC registers */
699   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
700 
701   /* Change RTC state */
702   hrtc->State = HAL_RTC_STATE_READY;
703 
704   /* Process Unlocked */
705   __HAL_UNLOCK(hrtc);
706 
707   return HAL_OK;
708 }
709 
710 /**
711   * @brief  Get wake up timer counter.
712   * @param  hrtc RTC handle
713   * @retval Counter value
714   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)715 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
716 {
717   /* Prevent unused argument(s) compilation warning */
718   UNUSED(hrtc);
719   /* Get the counter value */
720   return (uint32_t)(READ_BIT(RTC->WUTR, RTC_WUTR_WUT));
721 }
722 
723 /**
724   * @brief  Handle Wake Up Timer interrupt request.
725   * @param  hrtc RTC handle
726   * @retval None
727   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)728 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
729 {
730   /* Get the pending status of the Wake-Up Timer Interrupt */
731 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
732   if (READ_BIT(RTC->SMISR, RTC_SMISR_WUTMF) != 0U)
733 #else
734   if (READ_BIT(RTC->MISR, RTC_MISR_WUTMF) != 0U)
735 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
736   {
737     /* Clear the WAKEUPTIMER interrupt pending bit */
738     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
739 
740 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
741     /* Call WakeUpTimerEvent registered Callback */
742     hrtc->WakeUpTimerEventCallback(hrtc);
743 #else
744     /* WAKEUPTIMER callback */
745     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
746 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
747   }
748 
749   /* Change RTC state */
750   hrtc->State = HAL_RTC_STATE_READY;
751 }
752 
753 /**
754   * @brief  Wake Up Timer callback.
755   * @param  hrtc RTC handle
756   * @retval None
757   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)758 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
759 {
760   /* Prevent unused argument(s) compilation warning */
761   UNUSED(hrtc);
762 
763   /* NOTE : This function should not be modified, when the callback is needed,
764             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
765    */
766 }
767 
768 /**
769   * @brief  Handle Wake Up Timer Polling.
770   * @param  hrtc RTC handle
771   * @param  Timeout Timeout duration
772   * @retval HAL status
773   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)774 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
775 {
776   /* Prevent unused argument(s) compilation warning */
777   UNUSED(hrtc);
778 
779   uint32_t tickstart = HAL_GetTick();
780 
781   while (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
782   {
783     if (Timeout != HAL_MAX_DELAY)
784     {
785       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
786       {
787         /* New check to avoid false timeout detection in case of preemption */
788         if (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
789         {
790           return HAL_TIMEOUT;
791         }
792         else
793         {
794           break;
795         }
796       }
797     }
798   }
799 
800   /* Clear the WAKEUPTIMER Flag */
801   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
802 
803   /* Change RTC state */
804   hrtc->State = HAL_RTC_STATE_READY;
805 
806   return HAL_OK;
807 }
808 
809 /**
810   * @}
811   */
812 
813 
814 /** @addtogroup RTCEx_Exported_Functions_Group3
815   * @brief    Extended Peripheral Control functions
816   *
817 @verbatim
818  ===============================================================================
819               ##### Extended Peripheral Control functions #####
820  ===============================================================================
821     [..]
822     This subsection provides functions allowing to
823       (+) Write a data in a specified RTC Backup data register
824       (+) Read a data in a specified RTC Backup data register
825       (+) Set the Coarse calibration parameters.
826       (+) Deactivate the Coarse calibration parameters
827       (+) Set the Smooth calibration parameters.
828       (+) Set Low Power calibration parameter.
829       (+) Configure the Synchronization Shift Control Settings.
830       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
831       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
832       (+) Enable the RTC reference clock detection.
833       (+) Disable the RTC reference clock detection.
834       (+) Enable the Bypass Shadow feature.
835       (+) Disable the Bypass Shadow feature.
836 
837 @endverbatim
838   * @{
839   */
840 
841 /**
842   * @brief  Set the Smooth calibration parameters.
843   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
844   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
845   *         SmoothCalibMinusPulsesValue must be equal to 0.
846   * @param  hrtc RTC handle
847   * @param  SmoothCalibPeriod  Select the Smooth Calibration Period.
848   *          This parameter can be one of the following values :
849   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
850   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
851   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
852   * @param  SmoothCalibPlusPulses  Select to Set or reset the CALP bit.
853   *          This parameter can be one of the following values:
854   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
855   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
856   * @param  SmoothCalibMinusPulsesValue  Select the value of CALM[8:0] bits.
857   *          This parameter can be one any value from 0 to 0x000001FF.
858   * @retval HAL status
859   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)860 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod,
861                                            uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
862 {
863   uint32_t tickstart;
864 
865   /* Check the parameters */
866   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
867   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
868   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
869 
870   /* Process Locked */
871   __HAL_LOCK(hrtc);
872 
873   /* Change RTC state */
874   hrtc->State = HAL_RTC_STATE_BUSY;
875 
876 
877   tickstart = HAL_GetTick();
878 
879   /* check if a calibration is pending */
880   while (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
881   {
882     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
883     {
884       /* New check to avoid false timeout detection in case of preemption */
885       if (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
886       {
887 
888         /* Change RTC state */
889         hrtc->State = HAL_RTC_STATE_TIMEOUT;
890 
891         /* Process Unlocked */
892         __HAL_UNLOCK(hrtc);
893 
894         return HAL_TIMEOUT;
895       }
896       else
897       {
898         break;
899       }
900     }
901   }
902   /* Disable the write protection for RTC registers */
903   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
904 
905   /* Configure the Smooth calibration settings */
906   MODIFY_REG(RTC->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM),
907              (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
908 
909   /* Enable the write protection for RTC registers */
910   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
911 
912   /* Change RTC state */
913   hrtc->State = HAL_RTC_STATE_READY;
914 
915   /* Process Unlocked */
916   __HAL_UNLOCK(hrtc);
917 
918   return HAL_OK;
919 }
920 
921 /**
922   * @brief  Select the low power Calibration mode.
923   * @param  hrtc: RTC handle
924   * @param  LowPowerCalib: Low power Calibration mode.
925   *          This parameter can be can be one of the following values :
926   *             @arg RTC_LPCAL_SET: Low power mode.
927   *             @arg RTC_LPCAL_RESET: High consumption mode.
928   * @retval HAL status
929   */
HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef * hrtc,uint32_t LowPowerCalib)930 HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
931 {
932   /* Check the parameters */
933   assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
934 
935   /* Process Locked */
936   __HAL_LOCK(hrtc);
937 
938   /* Change RTC state */
939   hrtc->State = HAL_RTC_STATE_BUSY;
940 
941   /* Disable the write protection for RTC registers */
942   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
943 
944   /* Configure the Smooth calibration settings */
945   MODIFY_REG(RTC->CALR, RTC_CALR_LPCAL, LowPowerCalib);
946 
947   /* Enable the write protection for RTC registers */
948   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
949 
950   /* Change RTC state */
951   hrtc->State = HAL_RTC_STATE_READY;
952 
953   /* Process Unlocked */
954   __HAL_UNLOCK(hrtc);
955 
956   return HAL_OK;
957 }
958 
959 /**
960   * @brief  Configure the Synchronization Shift Control Settings.
961   * @note   When REFCKON is set, firmware must not write to Shift control register.
962   * @param  hrtc RTC handle
963   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
964   *          This parameter can be one of the following values:
965   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
966   *             @arg RTC_SHIFTADD1S_RESET: No effect.
967   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
968   *          This parameter can be one any value from 0 to 0x7FFF.
969   * @retval HAL status
970   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)971 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
972 {
973   uint32_t tickstart;
974 
975   /* Check the parameters */
976   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
977   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
978 
979   /* Process Locked */
980   __HAL_LOCK(hrtc);
981 
982   /* Change RTC state */
983   hrtc->State = HAL_RTC_STATE_BUSY;
984 
985 
986   tickstart = HAL_GetTick();
987 
988   /* Wait until the shift is completed */
989   while (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
990   {
991     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
992     {
993       /* New check to avoid false timeout detection in case of preemption */
994       if (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
995       {
996 
997         /* Change RTC state */
998         hrtc->State = HAL_RTC_STATE_TIMEOUT;
999 
1000         /* Process Unlocked */
1001         __HAL_UNLOCK(hrtc);
1002 
1003         return HAL_TIMEOUT;
1004       }
1005       else
1006       {
1007         break;
1008       }
1009     }
1010   }
1011 
1012   /* Disable the write protection for RTC registers */
1013   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1014 
1015   /* Check if the reference clock detection is disabled */
1016   if (READ_BIT(RTC->CR, RTC_CR_REFCKON) == 0U)
1017   {
1018     /* Configure the Shift settings */
1019     MODIFY_REG(RTC->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
1020 
1021     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1022     if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1023     {
1024       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
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_ERROR;
1031 
1032         /* Process Unlocked */
1033         __HAL_UNLOCK(hrtc);
1034 
1035         return HAL_ERROR;
1036       }
1037     }
1038   }
1039   else
1040   {
1041     /* Enable the write protection for RTC registers */
1042     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1043 
1044     /* Change RTC state */
1045     hrtc->State = HAL_RTC_STATE_ERROR;
1046 
1047     /* Process Unlocked */
1048     __HAL_UNLOCK(hrtc);
1049 
1050     return HAL_ERROR;
1051   }
1052 
1053   /* Enable the write protection for RTC registers */
1054   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1055 
1056   /* Change RTC state */
1057   hrtc->State = HAL_RTC_STATE_READY;
1058 
1059   /* Process Unlocked */
1060   __HAL_UNLOCK(hrtc);
1061 
1062   return HAL_OK;
1063 }
1064 
1065 /**
1066   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1067   * @param  hrtc RTC handle
1068   * @param  CalibOutput Select the Calibration output Selection .
1069   *          This parameter can be one of the following values:
1070   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1071   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1072   * @retval HAL status
1073   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1074 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1075 {
1076   /* Check the parameters */
1077   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1078 
1079   /* Process Locked */
1080   __HAL_LOCK(hrtc);
1081 
1082   /* Change RTC state */
1083   hrtc->State = HAL_RTC_STATE_BUSY;
1084 
1085 
1086   /* Configure the RTC_CR register */
1087   MODIFY_REG(RTC->CR, RTC_CR_COSEL, CalibOutput);
1088 
1089   /* Enable calibration output */
1090   SET_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  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1104   * @param  hrtc RTC handle
1105   * @retval HAL status
1106   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1107 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1108 {
1109   /* Process Locked */
1110   __HAL_LOCK(hrtc);
1111 
1112   /* Change RTC state */
1113   hrtc->State = HAL_RTC_STATE_BUSY;
1114 
1115 
1116   /* Disable calibration output */
1117   CLEAR_BIT(RTC->CR, RTC_CR_COE);
1118 
1119 
1120   /* Change RTC state */
1121   hrtc->State = HAL_RTC_STATE_READY;
1122 
1123   /* Process Unlocked */
1124   __HAL_UNLOCK(hrtc);
1125 
1126   return HAL_OK;
1127 }
1128 
1129 /**
1130   * @brief  Enable the RTC reference clock detection.
1131   * @param  hrtc RTC handle
1132   * @retval HAL status
1133   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1134 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1135 {
1136   HAL_StatusTypeDef status;
1137 
1138   /* Process Locked */
1139   __HAL_LOCK(hrtc);
1140 
1141   /* Change RTC state */
1142   hrtc->State = HAL_RTC_STATE_BUSY;
1143 
1144   /* Disable the write protection for RTC registers */
1145   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1146 
1147   /* Enter Initialization mode */
1148   status = RTC_EnterInitMode(hrtc);
1149   if (status == HAL_OK)
1150   {
1151     /* Enable clockref detection */
1152     SET_BIT(RTC->CR, RTC_CR_REFCKON);
1153 
1154     /* Exit Initialization mode */
1155     status = RTC_ExitInitMode(hrtc);
1156   }
1157 
1158   /* Enable the write protection for RTC registers */
1159   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1160 
1161   if (status == HAL_OK)
1162   {
1163     /* Change RTC state */
1164     hrtc->State = HAL_RTC_STATE_READY;
1165   }
1166 
1167   /* Process Unlocked */
1168   __HAL_UNLOCK(hrtc);
1169 
1170   return status;
1171 }
1172 
1173 /**
1174   * @brief  Disable the RTC reference clock detection.
1175   * @param  hrtc RTC handle
1176   * @retval HAL status
1177   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1178 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1179 {
1180   HAL_StatusTypeDef status;
1181 
1182   /* Process Locked */
1183   __HAL_LOCK(hrtc);
1184 
1185   /* Change RTC state */
1186   hrtc->State = HAL_RTC_STATE_BUSY;
1187 
1188   /* Disable the write protection for RTC registers */
1189   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1190 
1191   /* Enter Initialization mode */
1192   status = RTC_EnterInitMode(hrtc);
1193   if (status == HAL_OK)
1194   {
1195     /* Disable clockref detection */
1196     CLEAR_BIT(RTC->CR, RTC_CR_REFCKON);
1197 
1198     /* Exit Initialization mode */
1199     status = RTC_ExitInitMode(hrtc);
1200   }
1201 
1202   /* Enable the write protection for RTC registers */
1203   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1204 
1205   if (status == HAL_OK)
1206   {
1207     /* Change RTC state */
1208     hrtc->State = HAL_RTC_STATE_READY;
1209   }
1210 
1211   /* Process Unlocked */
1212   __HAL_UNLOCK(hrtc);
1213 
1214   return status;
1215 }
1216 
1217 /**
1218   * @brief  Enable the Bypass Shadow feature.
1219   * @note   When the Bypass Shadow is enabled the calendar value are taken
1220   *         directly from the Calendar counter.
1221   * @param  hrtc RTC handle
1222   * @retval HAL status
1223   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1224 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1225 {
1226   /* Process Locked */
1227   __HAL_LOCK(hrtc);
1228 
1229   /* Change RTC state */
1230   hrtc->State = HAL_RTC_STATE_BUSY;
1231 
1232 
1233   /* Set the BYPSHAD bit */
1234   SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1235 
1236 
1237   /* Change RTC state */
1238   hrtc->State = HAL_RTC_STATE_READY;
1239 
1240   /* Process Unlocked */
1241   __HAL_UNLOCK(hrtc);
1242 
1243   return HAL_OK;
1244 }
1245 
1246 /**
1247   * @brief  Disable the Bypass Shadow feature.
1248   * @note   When the Bypass Shadow is enabled the calendar value are taken
1249   *         directly from the Calendar counter.
1250   * @param  hrtc RTC handle
1251   * @retval HAL status
1252   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1253 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1254 {
1255   /* Process Locked */
1256   __HAL_LOCK(hrtc);
1257 
1258   /* Change RTC state */
1259   hrtc->State = HAL_RTC_STATE_BUSY;
1260 
1261 
1262   /* Reset the BYPSHAD bit */
1263   CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1264 
1265 
1266   /* Change RTC state */
1267   hrtc->State = HAL_RTC_STATE_READY;
1268 
1269   /* Process Unlocked */
1270   __HAL_UNLOCK(hrtc);
1271 
1272   return HAL_OK;
1273 }
1274 
1275 /**
1276   * @brief  Increment Monotonic counter.
1277   * @param  hrtc RTC handle
1278   * @param  Instance  Monotonic counter Instance
1279   *         This parameter can be can be one of the following values :
1280   *           @arg RTC_MONOTONIC_COUNTER_1
1281   * @retval HAL status
1282   */
HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef * hrtc,uint32_t Instance)1283 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef *hrtc, uint32_t Instance)
1284 {
1285   /* Prevent unused argument(s) compilation warning */
1286   UNUSED(hrtc);
1287   UNUSED(Instance);
1288   /* This register is read-only only and is incremented by one when a write access is done to this
1289      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1290   CLEAR_REG(TAMP->COUNTR);
1291 
1292   return HAL_OK;
1293 }
1294 
1295 /**
1296   * @brief  Monotonic counter incrementation.
1297   * @param  hrtc RTC handle
1298   * @param  Instance  Monotonic counter Instance
1299   *         This parameter can be can be one of the following values :
1300   *           @arg RTC_MONOTONIC_COUNTER_1
1301   * @param  Value Pointer to the counter monotonic counter value
1302   * @retval HAL status
1303   */
HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef * hrtc,uint32_t Instance,uint32_t * Value)1304 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef *hrtc, uint32_t Instance, uint32_t *Value)
1305 {
1306   /* Prevent unused argument(s) compilation warning */
1307   UNUSED(hrtc);
1308   UNUSED(Instance);
1309 
1310   /* This register is read-only only and is incremented by one when a write access is done to this
1311      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1312   *Value = READ_REG(TAMP->COUNTR);
1313 
1314   return HAL_OK;
1315 }
1316 
1317 /**
1318   * @brief  Set SSR Underflow detection with Interrupt.
1319   * @param  hrtc RTC handle
1320   * @retval HAL status
1321   */
HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef * hrtc)1322 HAL_StatusTypeDef HAL_RTCEx_SetSSRU_IT(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   /* Enable IT SSRU */
1332   __HAL_RTC_SSRU_ENABLE_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  Deactivate SSR Underflow.
1346   * @param  hrtc RTC handle
1347   * @retval HAL status
1348   */
HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef * hrtc)1349 HAL_StatusTypeDef HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef *hrtc)
1350 {
1351   /* Process Locked */
1352   __HAL_LOCK(hrtc);
1353 
1354   /* Change RTC state */
1355   hrtc->State = HAL_RTC_STATE_BUSY;
1356 
1357 
1358   /* In case of interrupt mode is used, the interrupt source must disabled */
1359   __HAL_RTC_SSRU_DISABLE_IT(hrtc, RTC_IT_SSRU);
1360 
1361 
1362   /* Change RTC state */
1363   hrtc->State = HAL_RTC_STATE_READY;
1364 
1365   /* Process Unlocked */
1366   __HAL_UNLOCK(hrtc);
1367 
1368   return HAL_OK;
1369 }
1370 
1371 /**
1372   * @brief  Handle SSR underflow interrupt request.
1373   * @param  hrtc RTC handle
1374   * @retval None
1375   */
HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef * hrtc)1376 void HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef *hrtc)
1377 {
1378   /* Get the pending status of the SSR Underflow Interrupt */
1379 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1380   if (READ_BIT(RTC->SMISR, RTC_SMISR_SSRUMF) != 0U)
1381 #else
1382   if (READ_BIT(RTC->MISR, RTC_MISR_SSRUMF) != 0U)
1383 #endif /* (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
1384   {
1385     /* Immediately clear SSR underflow flag */
1386     WRITE_REG(RTC->SCR, RTC_SCR_CSSRUF);
1387 
1388     /* SSRU callback */
1389 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1390     /* Call SSRUEvent registered Callback */
1391     hrtc->SSRUEventCallback(hrtc);
1392 #else
1393     HAL_RTCEx_SSRUEventCallback(hrtc);
1394 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1395 
1396   }
1397 
1398   /* Change RTC state */
1399   hrtc->State = HAL_RTC_STATE_READY;
1400 }
1401 
1402 /**
1403   * @brief  SSR underflow callback.
1404   * @param  hrtc RTC handle
1405   * @retval None
1406   */
HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef * hrtc)1407 __weak void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
1408 {
1409   /* Prevent unused argument(s) compilation warning */
1410   UNUSED(hrtc);
1411 
1412   /* NOTE : This function should not be modified, when the callback is needed,
1413             the HAL_RTCEx_SSRUEventCallback could be implemented in the user file
1414    */
1415 }
1416 
1417 /**
1418   * @}
1419   */
1420 
1421 /** @addtogroup RTCEx_Exported_Functions_Group4
1422   * @brief    Extended features functions
1423   *
1424 @verbatim
1425  ===============================================================================
1426                  ##### Extended features functions #####
1427  ===============================================================================
1428     [..]  This section provides functions allowing to:
1429       (+) RTC Alarm B callback
1430       (+) RTC Poll for Alarm B request
1431 
1432 @endverbatim
1433   * @{
1434   */
1435 
1436 /**
1437   * @brief  Alarm B callback.
1438   * @param  hrtc RTC handle
1439   * @retval None
1440   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1441 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1442 {
1443   /* Prevent unused argument(s) compilation warning */
1444   UNUSED(hrtc);
1445 
1446   /* NOTE : This function should not be modified, when the callback is needed,
1447             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1448    */
1449 }
1450 
1451 /**
1452   * @brief  Handle Alarm B Polling request.
1453   * @param  hrtc RTC handle
1454   * @param  Timeout Timeout duration
1455   * @retval HAL status
1456   */
HAL_RTCEx_PollForAlarmBEvent(const RTC_HandleTypeDef * hrtc,uint32_t Timeout)1457 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(const RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1458 {
1459   /* Prevent unused argument(s) compilation warning */
1460   UNUSED(hrtc);
1461 
1462   uint32_t tickstart = HAL_GetTick();
1463 
1464   while (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1465   {
1466     if (Timeout != HAL_MAX_DELAY)
1467     {
1468       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1469       {
1470         /* New check to avoid false timeout detection in case of preemption */
1471         if (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1472         {
1473           return HAL_TIMEOUT;
1474         }
1475         else
1476         {
1477           break;
1478         }
1479       }
1480     }
1481   }
1482 
1483   /* Clear the Alarm Flag */
1484   WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1485 
1486   return HAL_OK;
1487 }
1488 
1489 /**
1490   * @}
1491   */
1492 
1493 /** @addtogroup RTCEx_Exported_Functions_Group5
1494   * @brief      Extended RTC Tamper functions
1495   *
1496 @verbatim
1497   ==============================================================================
1498                          ##### Tamper functions #####
1499   ==============================================================================
1500   [..]
1501    (+) Before calling any tamper or internal tamper function, you have to call first
1502        HAL_RTC_Init() function.
1503    (+) In that ine you can select to output tamper event on RTC pin.
1504   [..]
1505    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1506        or Level according to the Tamper filter (if equal to 0 Edge else Level)
1507        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1508        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1509        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1510    (+) The default configuration of the Tamper erases the backup registers. To avoid
1511        erase, enable the NoErase field on the TAMP_TAMPCR register.
1512   [..]
1513    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1514        the HAL_RTCEx_SetInternalTamper() function.
1515 
1516 @endverbatim
1517   * @{
1518   */
1519 
1520 
1521 /**
1522   * @brief  Set Tamper
1523   * @param  hrtc RTC handle
1524   * @param  sTamper Pointer to Tamper Structure.
1525   * @retval HAL status
1526   */
HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1527 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
1528 {
1529   uint32_t tmpreg;
1530 
1531   /* Prevent unused argument(s) compilation warning */
1532   UNUSED(hrtc);
1533 
1534   /* Check the parameters */
1535   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1536   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1537   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1538   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1539   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1540   /* Mask flag only supported by TAMPER 1, 2 and 3 */
1541   assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_3)));
1542   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1543   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1544   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1545   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1546   /* Trigger and Filter have exclusive configurations */
1547   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && \
1548                 ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || \
1549                  (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL))) \
1550                || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE)  && \
1551                    ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || \
1552                     (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1553 
1554   /* Configuration register 2 */
1555   tmpreg = READ_REG(TAMP->CR2);
1556   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1557               (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1558 
1559   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1560   {
1561     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1562   }
1563 
1564   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1565   {
1566     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1567   }
1568 
1569   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1570   {
1571     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1572   }
1573   WRITE_REG(TAMP->CR2, tmpreg);
1574 
1575   /* Filter control register */
1576   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | \
1577             sTamper->TamperPullUp);
1578 
1579   /* Timestamp on tamper */
1580   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1581   {
1582     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1583   }
1584 
1585   /* Control register 1 */
1586   SET_BIT(TAMP->CR1, sTamper->Tamper);
1587 
1588   return HAL_OK;
1589 }
1590 
1591 
1592 /**
1593   * @brief  Set Tamper in IT mode
1594   * @param  hrtc RTC handle
1595   * @param  sTamper Pointer to Tamper Structure.
1596   * @retval HAL status
1597   */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1598 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
1599 {
1600   uint32_t tmpreg;
1601 
1602   /* Prevent unused argument(s) compilation warning */
1603   UNUSED(hrtc);
1604 
1605   /* Check the parameters */
1606   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1607   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1608   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1609   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1610   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1611   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1612   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1613   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1614   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1615 
1616   /* Configuration register 2 */
1617   tmpreg = READ_REG(TAMP->CR2);
1618   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1619               (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1620 
1621   if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1622   {
1623     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1624   }
1625 
1626   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1627   {
1628     /* Feature only supported by TAMPER 1, 2 and 3 */
1629     if (sTamper->Tamper < RTC_TAMPER_4)
1630     {
1631       tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1632     }
1633     else
1634     {
1635       return HAL_ERROR;
1636     }
1637   }
1638 
1639   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1640   {
1641     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1642   }
1643   WRITE_REG(TAMP->CR2, tmpreg);
1644 
1645   /* Filter control register */
1646   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | \
1647             sTamper->TamperPullUp);
1648 
1649   /* Timestamp on tamper */
1650   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1651   {
1652     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1653   }
1654 
1655   /* Interrupt enable register */
1656   SET_BIT(TAMP->IER, sTamper->Tamper);
1657 
1658   /* Control register 1 */
1659   SET_BIT(TAMP->CR1, sTamper->Tamper);
1660 
1661   return HAL_OK;
1662 }
1663 
1664 /**
1665   * @brief  Deactivate Tamper.
1666   * @param  hrtc RTC handle
1667   * @param  Tamper Selected tamper pin.
1668   *         This parameter can be a combination of the following values:
1669   *         @arg RTC_TAMPER_1
1670   *         @arg RTC_TAMPER_2
1671   *         @arg RTC_TAMPER_3
1672   *         @arg RTC_TAMPER_4
1673   *         @arg RTC_TAMPER_5
1674   *         @arg RTC_TAMPER_6
1675   *         @arg RTC_TAMPER_7
1676   *         @arg RTC_TAMPER_8
1677   * @retval HAL status
1678   */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)1679 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1680 {
1681   UNUSED(hrtc);
1682   assert_param(IS_RTC_TAMPER(Tamper));
1683 
1684   /* Disable the selected Tamper pin */
1685   CLEAR_BIT(TAMP->CR1, Tamper);
1686 
1687   /* Clear tamper mask/noerase/trigger configuration */
1688   CLEAR_BIT(TAMP->CR2, (Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | \
1689             (Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1690 
1691   /* Clear tamper interrupt mode configuration */
1692   CLEAR_BIT(TAMP->IER, Tamper);
1693 
1694   /* Clear tamper interrupt and event flags (WO register) */
1695   WRITE_REG(TAMP->SCR, Tamper);
1696 
1697   return HAL_OK;
1698 }
1699 
1700 /**
1701   * @brief  Set all active Tampers at the same time.
1702   * @param  hrtc RTC handle
1703   * @param  sAllTamper Pointer to active Tamper Structure.
1704   * @retval HAL status
1705   */
HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef * hrtc,RTC_ActiveTampersTypeDef * sAllTamper)1706 HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, RTC_ActiveTampersTypeDef *sAllTamper)
1707 {
1708   uint32_t IER;
1709   uint32_t CR1;
1710   uint32_t CR2;
1711   uint32_t ATCR1;
1712   uint32_t ATCR2;
1713   uint32_t CR;
1714   uint32_t i;
1715   uint32_t tickstart;
1716 
1717 #ifdef  USE_FULL_ASSERT
1718   for (i = 0; i < RTC_TAMP_NB; i++)
1719   {
1720     assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
1721     assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
1722     /* Mask flag only supported by TAMPER 1, 2 and 3 */
1723     assert_param(!((sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (i > RTC_TAMPER_3)));
1724   }
1725   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
1726 #endif /* USE_FULL_ASSERT */
1727 
1728   /* Active Tampers must not be already enabled */
1729   if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
1730   {
1731     /* Disable all actives tampers with HAL_RTCEx_DeactivateActiveTampers.
1732        No need to check return value because it returns always HAL_OK */
1733     (void) HAL_RTCEx_DeactivateActiveTampers(hrtc);
1734   }
1735 
1736   /* Set TimeStamp on tamper detection */
1737   CR = READ_REG(RTC->CR);
1738   if ((CR & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
1739   {
1740     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
1741   }
1742 
1743   CR1 = READ_REG(TAMP->CR1);
1744   CR2 = READ_REG(TAMP->CR2);
1745   ATCR2 = 0U;
1746   IER = READ_REG(TAMP->IER);
1747 
1748   /* Set common parameters */
1749   ATCR1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) | \
1750            sAllTamper->ActiveAsyncPrescaler);
1751 
1752   /* Set specific parameters for each active tamper inputs if enable */
1753   for (i = 0; i < RTC_TAMP_NB; i++)
1754   {
1755     if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
1756     {
1757       CR1 |= (TAMP_CR1_TAMP1E << i);
1758       ATCR1 |= (TAMP_ATCR1_TAMP1AM << i);
1759 
1760       if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
1761       {
1762         /* Interrupt enable register */
1763         IER |= (TAMP_IER_TAMP1IE << i);
1764       }
1765 
1766       if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1767       {
1768         CR2 |= (TAMP_CR2_TAMP1MSK << i);
1769       }
1770 
1771       if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1772       {
1773         CR2 |= (TAMP_CR2_TAMP1NOERASE << i);
1774       }
1775 
1776       /* Configure ATOSELx[] in case of output sharing */
1777       ATCR2 |= sAllTamper->TampInput[i].Output << ((3U * i) + TAMP_ATCR2_ATOSEL1_Pos);
1778 
1779       if (i != sAllTamper->TampInput[i].Output)
1780       {
1781         ATCR1 |= TAMP_ATCR1_ATOSHARE;
1782       }
1783     }
1784   }
1785 
1786   WRITE_REG(TAMP->IER, IER);
1787   WRITE_REG(TAMP->IER, IER);
1788   WRITE_REG(TAMP->ATCR1, ATCR1);
1789   WRITE_REG(TAMP->ATCR2, ATCR2);
1790   WRITE_REG(TAMP->CR2, CR2);
1791   WRITE_REG(TAMP->CR1, CR1);
1792 
1793   /* Write seed */
1794   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1795   {
1796     WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
1797   }
1798 
1799   /* Wait till RTC SEEDF flag is cleared and if Time out is reached exit */
1800   tickstart = HAL_GetTick();
1801   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0U)
1802   {
1803     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1804     {
1805       /* New check to avoid false timeout detection in case of preemption */
1806       if (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1807       {
1808         /* Change RTC state */
1809         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1810 
1811         return HAL_TIMEOUT;
1812       }
1813       else
1814       {
1815         break;
1816       }
1817     }
1818   }
1819 
1820   return HAL_OK;
1821 }
1822 
1823 /**
1824   * @brief  Get active Tampers configuration.
1825   * @param  sAllTamper Pointer to active Tamper Structure.
1826   * @retval none
1827   */
HAL_RTCEx_GetActiveTampers(RTC_ActiveTampersTypeDef * sAllTamper)1828 void HAL_RTCEx_GetActiveTampers(RTC_ActiveTampersTypeDef *sAllTamper)
1829 {
1830   uint32_t i ;
1831 
1832   sAllTamper->ActiveFilter = (uint32_t)(TAMP->ATCR1 & TAMP_ATCR1_FLTEN);
1833   sAllTamper->ActiveOutputChangePeriod = (uint32_t)((TAMP->ATCR1 & TAMP_ATCR1_ATPER) >> TAMP_ATCR1_ATPER_Pos);
1834   sAllTamper->ActiveAsyncPrescaler = (uint32_t)(TAMP->ATCR1 & TAMP_ATCR1_ATCKSEL);
1835   sAllTamper->TimeStampOnTamperDetection = (uint32_t)(RTC->CR & RTC_CR_TAMPTS);
1836   /* Set specific parameters for each active tamper inputs if enable */
1837   for (i = 0; i < RTC_TAMP_NB; i++)
1838   {
1839     sAllTamper->TampInput[i].Enable = (uint32_t)(((TAMP->CR1 & (TAMP_CR1_TAMP1E << i))) >> i);
1840     sAllTamper->TampInput[i].Interrupt = (uint32_t)(((TAMP->IER & (TAMP_IER_TAMP1IE << i))) >> i);
1841     sAllTamper->TampInput[i].MaskFlag = (uint32_t)(((TAMP->CR2 & (TAMP_CR2_TAMP1MSK << i))) >> i);
1842     sAllTamper->TampInput[i].NoErase = (uint32_t)(((TAMP->CR2 & (TAMP_CR2_TAMP1NOERASE << i))) >> i);
1843     sAllTamper->TampInput[i].Output = (uint32_t)(((TAMP->ATCR2 & (TAMP_ATCR2_ATOSEL1 << ((3u * i)))) \
1844                                                   >> ((3u * i) + TAMP_ATCR2_ATOSEL1_Pos)));
1845   }
1846 }
1847 
1848 /**
1849   * @brief  Write a new seed. Active tamper must be enabled.
1850   * @param  hrtc RTC handle
1851   * @param  pSeed Pointer to active tamper seed values.
1852   * @retval HAL status
1853   */
HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef * hrtc,uint32_t * pSeed)1854 HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, uint32_t *pSeed)
1855 {
1856   uint32_t i;
1857   uint32_t tickstart;
1858 
1859   /* Active Tampers must be enabled */
1860   if (READ_BIT(TAMP->ATOR,  TAMP_ATOR_INITS) == 0U)
1861   {
1862     return HAL_ERROR;
1863   }
1864 
1865   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1866   {
1867     WRITE_REG(TAMP->ATSEEDR, pSeed[i]);
1868   }
1869 
1870   /* Wait till RTC SEEDF flag is cleared and if Time out is reached exit */
1871   tickstart = HAL_GetTick();
1872   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0U)
1873   {
1874     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1875     {
1876       /* New check to avoid false timeout detection in case of preemption */
1877       if (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
1878       {
1879         /* Change RTC state */
1880         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1881 
1882         return HAL_TIMEOUT;
1883       }
1884       else
1885       {
1886         break;
1887       }
1888     }
1889   }
1890 
1891   return HAL_OK;
1892 }
1893 
1894 /**
1895   * @brief  Lock the Boot hardware Key
1896   * @param  hrtc RTC handle
1897   * @note   The backup registers from TAMP_BKP0R to TAMP_BKP7R cannot be accessed neither in
1898   *         read nor in write (they are read as 0 and write ignore).
1899   * @retval HAL status
1900   */
HAL_RTCEx_SetBoothardwareKey(RTC_HandleTypeDef * hrtc)1901 HAL_StatusTypeDef HAL_RTCEx_SetBoothardwareKey(RTC_HandleTypeDef *hrtc)
1902 {
1903   /* Prevent unused argument(s) compilation warning */
1904   UNUSED(hrtc);
1905   WRITE_REG(TAMP->SECCFGR, TAMP_SECCFGR_BHKLOCK);
1906 
1907   return HAL_OK;
1908 }
1909 
1910 /**
1911   * @brief  Deactivate all Active Tampers at the same time.
1912   * @param  hrtc RTC handle
1913   * @retval HAL status
1914   */
HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef * hrtc)1915 HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef *hrtc)
1916 {
1917   /* Get Active tampers */
1918   uint32_t ATamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
1919 
1920   UNUSED(hrtc);
1921   /* Disable all actives tampers but not passives tampers */
1922   CLEAR_BIT(TAMP->CR1, ATamp_mask);
1923   /* Disable no erase and mask */
1924   CLEAR_BIT(TAMP->CR2, (ATamp_mask | ((ATamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM))\
1925                                       << TAMP_CR2_TAMP1MSK_Pos)));
1926 
1927   /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
1928   WRITE_REG(TAMP->SCR, ATamp_mask);
1929 
1930   /* Clear all active tampers interrupt mode configuration but not passives tampers */
1931   CLEAR_BIT(TAMP->IER, ATamp_mask);
1932 
1933   CLEAR_BIT(TAMP->ATCR1, TAMP_ALL | TAMP_ATCR1_ATCKSEL | TAMP_ATCR1_ATPER | \
1934             TAMP_ATCR1_ATOSHARE | TAMP_ATCR1_FLTEN);
1935 
1936   CLEAR_BIT(TAMP->ATCR2, TAMP_ATCR2_ATOSEL1 | TAMP_ATCR2_ATOSEL2 | TAMP_ATCR2_ATOSEL3 | TAMP_ATCR2_ATOSEL4 |
1937             TAMP_ATCR2_ATOSEL5 | TAMP_ATCR2_ATOSEL6 | TAMP_ATCR2_ATOSEL7 | TAMP_ATCR2_ATOSEL8);
1938 
1939   return HAL_OK;
1940 }
1941 
1942 
1943 /**
1944   * @brief  Tamper event polling.
1945   * @param  hrtc RTC handle
1946   * @param  Tamper Selected tamper pin.
1947   *         This parameter can be a combination of the following values:
1948   *         @arg RTC_TAMPER_1
1949   *         @arg RTC_TAMPER_2
1950   *         @arg RTC_TAMPER_3
1951   *         @arg RTC_TAMPER_4
1952   *         @arg RTC_TAMPER_5
1953   *         @arg RTC_TAMPER_6
1954   *         @arg RTC_TAMPER_7
1955   *         @arg RTC_TAMPER_8
1956   * @param  Timeout Timeout duration
1957   * @retval HAL status
1958   */
HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t Tamper,uint32_t Timeout)1959 HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
1960 {
1961   /* Prevent unused argument(s) compilation warning */
1962   UNUSED(hrtc);
1963 
1964   assert_param(IS_RTC_TAMPER(Tamper));
1965 
1966   uint32_t tickstart = HAL_GetTick();
1967 
1968   /* Get the status of the Interrupt */
1969   while (READ_BIT(TAMP->SR, Tamper) != Tamper)
1970   {
1971     if (Timeout != HAL_MAX_DELAY)
1972     {
1973       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1974       {
1975         /* New check to avoid false timeout detection in case of preemption */
1976         if (READ_BIT(TAMP->SR, Tamper) != Tamper)
1977         {
1978           return HAL_TIMEOUT;
1979         }
1980         else
1981         {
1982           break;
1983         }
1984       }
1985     }
1986   }
1987 
1988   /* Clear the Tamper Flag */
1989   WRITE_REG(TAMP->SCR, Tamper);
1990 
1991   return HAL_OK;
1992 }
1993 
1994 
1995 /**
1996   * @brief  Set Internal Tamper
1997   * @param  hrtc RTC handle
1998   * @param  sIntTamper Pointer to Internal Tamper Structure.
1999   * @retval HAL status
2000   */
HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef * hrtc,RTC_InternalTamperTypeDef * sIntTamper)2001 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
2002 {
2003   /* Prevent unused argument(s) compilation warning */
2004   UNUSED(hrtc);
2005 
2006   /* Check the parameters */
2007   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
2008   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
2009   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
2010 
2011   /* timestamp on internal tamper */
2012   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
2013   {
2014     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
2015 
2016   }
2017 
2018   /* No Erase Backup register enable for Internal Tamper */
2019   if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
2020   {
2021     /* Control register 3 */
2022     SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2023   }
2024   else
2025   {
2026     CLEAR_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2027   }
2028 
2029   /* Control register 1 */
2030   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
2031 
2032   return HAL_OK;
2033 }
2034 
2035 /**
2036   * @brief  Get Internal Tamper Configuration
2037   * @param  sIntTamper Pointer to Internal Tamper Structure.
2038   * @retval HAL status
2039   */
HAL_RTCEx_GetInternalTampers(RTC_InternalTamperTypeDef * sIntTamper)2040 void HAL_RTCEx_GetInternalTampers(RTC_InternalTamperTypeDef *sIntTamper)
2041 {
2042   sIntTamper->IntTamper = (uint32_t)(TAMP->CR1 & (RTC_INT_TAMPER_ALL));
2043   sIntTamper->TimeStampOnTamperDetection = (uint32_t)(RTC->CR & RTC_CR_TAMPTS);
2044   if ((uint32_t)(TAMP->CR3 & (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos))) != 0U)
2045   {
2046     sIntTamper->NoErase = RTC_TAMPER_ERASE_BACKUP_DISABLE;
2047   }
2048   else
2049   {
2050     sIntTamper->NoErase = RTC_TAMPER_ERASE_BACKUP_ENABLE;
2051   }
2052 }
2053 
2054 /**
2055   * @brief  Set Internal Tamper in interrupt mode
2056   * @param  hrtc RTC handle
2057   * @param  sIntTamper Pointer to Internal Tamper Structure.
2058   * @retval HAL status
2059   */
HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef * hrtc,RTC_InternalTamperTypeDef * sIntTamper)2060 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
2061 {
2062   /* Prevent unused argument(s) compilation warning */
2063   UNUSED(hrtc);
2064 
2065   /* Check the parameters */
2066   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
2067   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
2068   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
2069 
2070   /* timestamp on internal tamper */
2071   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
2072   {
2073     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
2074   }
2075 
2076   /* Interrupt enable register */
2077   SET_BIT(TAMP->IER, sIntTamper->IntTamper);
2078 
2079   if (sIntTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
2080   {
2081     /* Control register 3 */
2082     SET_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2083   }
2084   else
2085   {
2086     CLEAR_BIT(TAMP->CR3, (sIntTamper->IntTamper >> (TAMP_CR1_ITAMP1E_Pos - TAMP_CR3_ITAMP1NOER_Pos)));
2087   }
2088 
2089   /* Control register 1 */
2090   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
2091   return HAL_OK;
2092 }
2093 
2094 /**
2095   * @brief  Deactivate Internal Tamper.
2096   * @param  hrtc RTC handle
2097   * @param  IntTamper Selected internal tamper event.
2098   *          This parameter can be any combination of existing internal tampers.
2099   * @retval HAL status
2100   */
HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef * hrtc,uint32_t IntTamper)2101 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
2102 {
2103   /* Prevent unused argument(s) compilation warning */
2104   UNUSED(hrtc);
2105 
2106   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
2107 
2108   /* Disable the selected Tamper pin */
2109   CLEAR_BIT(TAMP->CR1, IntTamper);
2110 
2111   /* Clear internal tamper interrupt mode configuration */
2112   CLEAR_BIT(TAMP->IER, IntTamper);
2113 
2114   /* Clear internal tamper interrupt */
2115   WRITE_REG(TAMP->SCR, IntTamper);
2116 
2117   return HAL_OK;
2118 }
2119 
2120 
2121 /**
2122   * @brief  Internal Tamper event polling.
2123   * @param  hrtc RTC handle
2124   * @param  IntTamper selected tamper.
2125   *          This parameter can be any combination of existing internal tampers.
2126   * @param  Timeout Timeout duration
2127   * @retval HAL status
2128   */
HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t IntTamper,uint32_t Timeout)2129 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
2130 {
2131   /* Prevent unused argument(s) compilation warning */
2132   UNUSED(hrtc);
2133 
2134   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
2135 
2136   uint32_t tickstart = HAL_GetTick();
2137 
2138   /* Get the status of the Interrupt */
2139   while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
2140   {
2141     if (Timeout != HAL_MAX_DELAY)
2142     {
2143       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2144       {
2145         /* New check to avoid false timeout detection in case of preemption */
2146         if (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
2147         {
2148           return HAL_TIMEOUT;
2149         }
2150         else
2151         {
2152           break;
2153         }
2154       }
2155     }
2156   }
2157 
2158   /* Clear the Tamper Flag */
2159   WRITE_REG(TAMP->SCR, IntTamper);
2160 
2161   return HAL_OK;
2162 }
2163 
2164 /**
2165   * @brief  Handle Tamper interrupt request.
2166   * @param  hrtc RTC handle
2167   * @retval None
2168   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)2169 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
2170 {
2171   /* Get the pending status of the Tampers Interrupt */
2172 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
2173   uint32_t tmp = READ_REG(TAMP->SMISR);
2174 #else
2175   uint32_t tmp = READ_REG(TAMP->MISR);
2176 #endif /* defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2177 
2178   /* Check Tamper1 status */
2179   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
2180   {
2181 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2182     /* Call Tamper 1 Event registered secure Callback */
2183     hrtc->Tamper1EventCallback(hrtc);
2184 #else
2185     /* Tamper1 secure callback */
2186     HAL_RTCEx_Tamper1EventCallback(hrtc);
2187 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2188   }
2189 
2190   /* Check Tamper2 status */
2191   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
2192   {
2193 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2194     /* Call Tamper 2 Event registered secure Callback */
2195     hrtc->Tamper2EventCallback(hrtc);
2196 #else
2197     /* Tamper2 secure callback */
2198     HAL_RTCEx_Tamper2EventCallback(hrtc);
2199 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2200   }
2201 
2202   /* Check Tamper3 status */
2203   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
2204   {
2205 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2206     /* Call Tamper 3 Event registered secure Callback */
2207     hrtc->Tamper3EventCallback(hrtc);
2208 #else
2209     /* Tamper3 secure callback */
2210     HAL_RTCEx_Tamper3EventCallback(hrtc);
2211 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2212   }
2213 
2214   /* Check Tamper4 status */
2215   if ((tmp & RTC_TAMPER_4) == RTC_TAMPER_4)
2216   {
2217 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2218     /* Call Tamper 4 Event registered secure Callback */
2219     hrtc->Tamper4EventCallback(hrtc);
2220 #else
2221     /* Tamper4 secure callback */
2222     HAL_RTCEx_Tamper4EventCallback(hrtc);
2223 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2224   }
2225 
2226   /* Check Tamper5 status */
2227   if ((tmp & RTC_TAMPER_5) == RTC_TAMPER_5)
2228   {
2229 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2230     /* Call Tamper 5 Event registered secure Callback */
2231     hrtc->Tamper5EventCallback(hrtc);
2232 #else
2233     /* Tamper5 secure callback */
2234     HAL_RTCEx_Tamper5EventCallback(hrtc);
2235 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2236   }
2237 
2238   /* Check Tamper6 status */
2239   if ((tmp & RTC_TAMPER_6) == RTC_TAMPER_6)
2240   {
2241 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2242     /* Call Tamper 6 Event registered secure Callback */
2243     hrtc->Tamper6EventCallback(hrtc);
2244 #else
2245     /* Tamper6 secure callback */
2246     HAL_RTCEx_Tamper6EventCallback(hrtc);
2247 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2248   }
2249 
2250   /* Check Tamper7 status */
2251   if ((tmp & RTC_TAMPER_7) == RTC_TAMPER_7)
2252   {
2253 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2254     /* Call Tamper 7 Event registered secure Callback */
2255     hrtc->Tamper7EventCallback(hrtc);
2256 #else
2257     /* Tamper7 secure callback */
2258     HAL_RTCEx_Tamper7EventCallback(hrtc);
2259 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2260   }
2261 
2262   /* Check Tamper8 status */
2263   if ((tmp & RTC_TAMPER_8) == RTC_TAMPER_8)
2264   {
2265 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2266     /* Call Tamper 8 Event registered secure Callback */
2267     hrtc->Tamper8EventCallback(hrtc);
2268 #else
2269     /* Tamper8 secure callback */
2270     HAL_RTCEx_Tamper8EventCallback(hrtc);
2271 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2272   }
2273 
2274   /* Check Internal Tamper1 status */
2275   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
2276   {
2277 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2278     /* Call Internal Tamper 1 Event registered secure Callback */
2279     hrtc->InternalTamper1EventCallback(hrtc);
2280 #else
2281     /* Internal Tamper1 secure callback */
2282     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
2283 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2284   }
2285 
2286   /* Check Internal Tamper2 status */
2287   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
2288   {
2289 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2290     /* Call Internal Tamper 2 Event registered secure Callback */
2291     hrtc->InternalTamper2EventCallback(hrtc);
2292 #else
2293     /* Internal Tamper2 secure callback */
2294     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
2295 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2296   }
2297 
2298   /* Check Internal Tamper3 status */
2299   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
2300   {
2301 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2302     /* Call Internal Tamper 3 Event registered secure Callback */
2303     hrtc->InternalTamper3EventCallback(hrtc);
2304 #else
2305     /* Internal Tamper3 secure callback */
2306     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2307 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2308   }
2309 
2310   /* Check Internal Tamper5 status */
2311   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2312   {
2313 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2314     /* Call Internal Tamper 5 Event registered secure Callback */
2315     hrtc->InternalTamper5EventCallback(hrtc);
2316 #else
2317     /* Internal Tamper5 secure callback */
2318     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2319 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2320   }
2321   /* Check Internal Tamper6 status */
2322   if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
2323   {
2324 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2325     /* Call Internal Tamper 6 Event registered secure Callback */
2326     hrtc->InternalTamper6EventCallback(hrtc);
2327 #else
2328     /* Internal Tamper6 secure callback */
2329     HAL_RTCEx_InternalTamper6EventCallback(hrtc);
2330 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2331   }
2332 
2333   /* Check Internal Tamper7 status */
2334   if ((tmp & RTC_INT_TAMPER_7) == RTC_INT_TAMPER_7)
2335   {
2336 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2337     /* Call Internal Tamper 7 Event registered secure Callback */
2338     hrtc->InternalTamper7EventCallback(hrtc);
2339 #else
2340     /* Internal Tamper7 secure callback */
2341     HAL_RTCEx_InternalTamper7EventCallback(hrtc);
2342 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2343   }
2344 
2345   /* Check Internal Tamper8 status */
2346   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2347   {
2348 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2349     /* Call Internal Tamper 8 Event registered secure Callback */
2350     hrtc->InternalTamper8EventCallback(hrtc);
2351 #else
2352     /* Internal Tamper8 secure callback */
2353     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2354 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2355   }
2356 
2357   /* Check Internal Tamper9 status */
2358   if ((tmp & RTC_INT_TAMPER_9) == RTC_INT_TAMPER_9)
2359   {
2360 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2361     /* Call Internal Tamper 9 Event registered secure Callback */
2362     hrtc->InternalTamper9EventCallback(hrtc);
2363 #else
2364     /* Internal Tamper9 secure callback */
2365     HAL_RTCEx_InternalTamper9EventCallback(hrtc);
2366 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS == 1 */
2367   }
2368 
2369   /* Check Internal Tamper11 status */
2370   if ((tmp & RTC_INT_TAMPER_11) == RTC_INT_TAMPER_11)
2371   {
2372 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2373     /* Call Internal Tamper 11 Event registered secure Callback */
2374     hrtc->InternalTamper11EventCallback(hrtc);
2375 #else
2376     /* Internal Tamper11 secure callback */
2377     HAL_RTCEx_InternalTamper11EventCallback(hrtc);
2378 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2379   }
2380 
2381   /* Check Internal Tamper12 status */
2382   if ((tmp & RTC_INT_TAMPER_12) == RTC_INT_TAMPER_12)
2383   {
2384 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2385     /* Call Internal Tamper 12 Event registered secure Callback */
2386     hrtc->InternalTamper12EventCallback(hrtc);
2387 #else
2388     /* Internal Tamper 12 secure callback */
2389     HAL_RTCEx_InternalTamper12EventCallback(hrtc);
2390 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2391   }
2392 
2393   /* Check Internal Tamper13 status */
2394   if ((tmp & RTC_INT_TAMPER_13) == RTC_INT_TAMPER_13)
2395   {
2396 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2397     /* Call Internal Tamper 13 Event registered secure Callback */
2398     hrtc->InternalTamper13EventCallback(hrtc);
2399 #else
2400     /* Internal Tamper 13 secure callback */
2401     HAL_RTCEx_InternalTamper13EventCallback(hrtc);
2402 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2403   }
2404 
2405   /* Clear flags after treatment to allow the potential tamper feature */
2406   WRITE_REG(TAMP->SCR, tmp);
2407 }
2408 
2409 /**
2410   * @brief  Tamper 1 callback.
2411   * @param  hrtc RTC handle
2412   * @retval None
2413   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)2414 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
2415 {
2416   /* Prevent unused argument(s) compilation warning */
2417   UNUSED(hrtc);
2418 
2419   /* NOTE : This function should not be modified, when the callback is needed,
2420             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
2421    */
2422 }
2423 
2424 /**
2425   * @brief  Tamper 2 callback.
2426   * @param  hrtc RTC handle
2427   * @retval None
2428   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)2429 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
2430 {
2431   /* Prevent unused argument(s) compilation warning */
2432   UNUSED(hrtc);
2433 
2434   /* NOTE : This function should not be modified, when the callback is needed,
2435             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
2436    */
2437 }
2438 
2439 /**
2440   * @brief  Tamper 3 callback.
2441   * @param  hrtc RTC handle
2442   * @retval None
2443   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)2444 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
2445 {
2446   /* Prevent unused argument(s) compilation warning */
2447   UNUSED(hrtc);
2448 
2449   /* NOTE : This function should not be modified, when the callback is needed,
2450             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
2451    */
2452 }
2453 
2454 /**
2455   * @brief  Tamper 4 callback.
2456   * @param  hrtc RTC handle
2457   * @retval None
2458   */
HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef * hrtc)2459 __weak void HAL_RTCEx_Tamper4EventCallback(RTC_HandleTypeDef *hrtc)
2460 {
2461   /* Prevent unused argument(s) compilation warning */
2462   UNUSED(hrtc);
2463 
2464   /* NOTE : This function should not be modified, when the callback is needed,
2465             the HAL_RTCEx_Tamper4EventCallback could be implemented in the user file
2466    */
2467 }
2468 
2469 /**
2470   * @brief  Tamper 5 callback.
2471   * @param  hrtc RTC handle
2472   * @retval None
2473   */
HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef * hrtc)2474 __weak void HAL_RTCEx_Tamper5EventCallback(RTC_HandleTypeDef *hrtc)
2475 {
2476   /* Prevent unused argument(s) compilation warning */
2477   UNUSED(hrtc);
2478 
2479   /* NOTE : This function should not be modified, when the callback is needed,
2480             the HAL_RTCEx_Tamper5EventCallback could be implemented in the user file
2481    */
2482 }
2483 
2484 /**
2485   * @brief  Tamper 6 callback.
2486   * @param  hrtc RTC handle
2487   * @retval None
2488   */
HAL_RTCEx_Tamper6EventCallback(RTC_HandleTypeDef * hrtc)2489 __weak void HAL_RTCEx_Tamper6EventCallback(RTC_HandleTypeDef *hrtc)
2490 {
2491   /* Prevent unused argument(s) compilation warning */
2492   UNUSED(hrtc);
2493 
2494   /* NOTE : This function should not be modified, when the callback is needed,
2495             the HAL_RTCEx_Tamper6EventCallback could be implemented in the user file
2496    */
2497 }
2498 
2499 /**
2500   * @brief  Tamper 7 callback.
2501   * @param  hrtc RTC handle
2502   * @retval None
2503   */
HAL_RTCEx_Tamper7EventCallback(RTC_HandleTypeDef * hrtc)2504 __weak void HAL_RTCEx_Tamper7EventCallback(RTC_HandleTypeDef *hrtc)
2505 {
2506   /* Prevent unused argument(s) compilation warning */
2507   UNUSED(hrtc);
2508 
2509   /* NOTE : This function should not be modified, when the callback is needed,
2510             the HAL_RTCEx_Tamper7EventCallback could be implemented in the user file
2511    */
2512 }
2513 
2514 /**
2515   * @brief  Tamper 8 callback.
2516   * @param  hrtc RTC handle
2517   * @retval None
2518   */
HAL_RTCEx_Tamper8EventCallback(RTC_HandleTypeDef * hrtc)2519 __weak void HAL_RTCEx_Tamper8EventCallback(RTC_HandleTypeDef *hrtc)
2520 {
2521   /* Prevent unused argument(s) compilation warning */
2522   UNUSED(hrtc);
2523 
2524   /* NOTE : This function should not be modified, when the callback is needed,
2525             the HAL_RTCEx_Tamper8EventCallback could be implemented in the user file
2526    */
2527 }
2528 
2529 /**
2530   * @brief  Internal Tamper 1 callback.
2531   * @param  hrtc RTC handle
2532   * @retval None
2533   */
HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef * hrtc)2534 __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
2535 {
2536   /* Prevent unused argument(s) compilation warning */
2537   UNUSED(hrtc);
2538 
2539   /* NOTE : This function should not be modified, when the callback is needed,
2540             the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
2541    */
2542 }
2543 
2544 /**
2545   * @brief  Internal Tamper 2 callback.
2546   * @param  hrtc RTC handle
2547   * @retval None
2548   */
HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef * hrtc)2549 __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
2550 {
2551   /* Prevent unused argument(s) compilation warning */
2552   UNUSED(hrtc);
2553 
2554   /* NOTE : This function should not be modified, when the callback is needed,
2555             the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
2556    */
2557 }
2558 
2559 /**
2560   * @brief  Internal Tamper 3 callback.
2561   * @param  hrtc RTC handle
2562   * @retval None
2563   */
HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef * hrtc)2564 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
2565 {
2566   /* Prevent unused argument(s) compilation warning */
2567   UNUSED(hrtc);
2568 
2569   /* NOTE : This function should not be modified, when the callback is needed,
2570             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2571    */
2572 }
2573 
2574 
2575 /**
2576   * @brief  Internal Tamper 5 callback.
2577   * @param  hrtc RTC handle
2578   * @retval None
2579   */
HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef * hrtc)2580 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
2581 {
2582   /* Prevent unused argument(s) compilation warning */
2583   UNUSED(hrtc);
2584 
2585   /* NOTE : This function should not be modified, when the callback is needed,
2586             the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
2587    */
2588 }
2589 
2590 /**
2591   * @brief  Internal Tamper 6 callback.
2592   * @param  hrtc RTC handle
2593   * @retval None
2594   */
HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef * hrtc)2595 __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
2596 {
2597   /* Prevent unused argument(s) compilation warning */
2598   UNUSED(hrtc);
2599 
2600   /* NOTE : This function should not be modified, when the callback is needed,
2601             the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
2602    */
2603 }
2604 
2605 /**
2606   * @brief  Internal Tamper 7 callback.
2607   * @param  hrtc RTC handle
2608   * @retval None
2609   */
HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef * hrtc)2610 __weak void HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef *hrtc)
2611 {
2612   /* Prevent unused argument(s) compilation warning */
2613   UNUSED(hrtc);
2614 
2615   /* NOTE : This function should not be modified, when the callback is needed,
2616             the HAL_RTCEx_InternalTamper7EventCallback could be implemented in the user file
2617    */
2618 }
2619 
2620 /**
2621   * @brief  Internal Tamper 8 callback.
2622   * @param  hrtc RTC handle
2623   * @retval None
2624   */
HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef * hrtc)2625 __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
2626 {
2627   /* Prevent unused argument(s) compilation warning */
2628   UNUSED(hrtc);
2629 
2630   /* NOTE : This function should not be modified, when the callback is needed,
2631             the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
2632    */
2633 }
2634 
2635 /**
2636   * @brief  Internal Tamper 9 callback.
2637   * @param  hrtc RTC handle
2638   * @retval None
2639   */
HAL_RTCEx_InternalTamper9EventCallback(RTC_HandleTypeDef * hrtc)2640 __weak void HAL_RTCEx_InternalTamper9EventCallback(RTC_HandleTypeDef *hrtc)
2641 {
2642   /* Prevent unused argument(s) compilation warning */
2643   UNUSED(hrtc);
2644 
2645   /* NOTE : This function should not be modified, when the callback is needed,
2646             the HAL_RTCEx_InternalTamper9EventCallback could be implemented in the user file
2647    */
2648 }
2649 
2650 /**
2651   * @brief  Internal Tamper 11 callback.
2652   * @param  hrtc RTC handle
2653   * @retval None
2654   */
HAL_RTCEx_InternalTamper11EventCallback(RTC_HandleTypeDef * hrtc)2655 __weak void HAL_RTCEx_InternalTamper11EventCallback(RTC_HandleTypeDef *hrtc)
2656 {
2657   /* Prevent unused argument(s) compilation warning */
2658   UNUSED(hrtc);
2659 
2660   /* NOTE : This function should not be modified, when the callback is needed,
2661             the HAL_RTCEx_InternalTamper11EventCallback could be implemented in the user file
2662    */
2663 }
2664 
2665 /**
2666   * @brief  Internal Tamper 12 callback.
2667   * @param  hrtc RTC handle
2668   * @retval None
2669   */
HAL_RTCEx_InternalTamper12EventCallback(RTC_HandleTypeDef * hrtc)2670 __weak void HAL_RTCEx_InternalTamper12EventCallback(RTC_HandleTypeDef *hrtc)
2671 {
2672   /* Prevent unused argument(s) compilation warning */
2673   UNUSED(hrtc);
2674 
2675   /* NOTE : This function should not be modified, when the callback is needed,
2676             the HAL_RTCEx_InternalTamper12EventCallback could be implemented in the user file
2677    */
2678 }
2679 
2680 /**
2681   * @brief  Internal Tamper 13 callback.
2682   * @param  hrtc RTC handle
2683   * @retval None
2684   */
HAL_RTCEx_InternalTamper13EventCallback(RTC_HandleTypeDef * hrtc)2685 __weak void HAL_RTCEx_InternalTamper13EventCallback(RTC_HandleTypeDef *hrtc)
2686 {
2687   /* Prevent unused argument(s) compilation warning */
2688   UNUSED(hrtc);
2689 
2690   /* NOTE : This function should not be modified, when the callback is needed,
2691             the HAL_RTCEx_InternalTamper13EventCallback could be implemented in the user file
2692    */
2693 }
2694 /**
2695   * @}
2696   */
2697 
2698 
2699 /** @addtogroup RTCEx_Exported_Functions_Group6
2700   * @brief      Extended RTC Backup register functions
2701   *
2702 @verbatim
2703   ===============================================================================
2704              ##### Extended RTC Backup register functions #####
2705   ===============================================================================
2706   [..]
2707    (+) Before calling any tamper or internal tamper function, you have to call first
2708        HAL_RTC_Init() function.
2709    (+) In that one you can select to output tamper event on RTC pin.
2710   [..]
2711    This subsection provides functions allowing to
2712    (+) Write a data in a specified RTC Backup data register
2713    (+) Read a data in a specified RTC Backup data register
2714 @endverbatim
2715   * @{
2716   */
2717 
2718 
2719 /**
2720   * @brief  Write a data in a specified RTC Backup data register.
2721   * @param  hrtc RTC handle
2722   * @param  BackupRegister RTC Backup data Register number.
2723   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2724   * @param  Data Data to be written in the specified Backup data register.
2725   * @retval None
2726   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)2727 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2728 {
2729   uint32_t tmp;
2730 
2731   /* Prevent unused argument(s) compilation warning */
2732   UNUSED(hrtc);
2733 
2734   /* Check the parameters */
2735   assert_param(IS_RTC_BKP(BackupRegister));
2736 
2737   tmp = (uint32_t) &(TAMP->BKP0R);
2738   tmp += (BackupRegister * 4U);
2739 
2740   /* Write the specified register */
2741   *(__IO uint32_t *)tmp = (uint32_t)Data;
2742 }
2743 
2744 
2745 /**
2746   * @brief  Reads data from the specified RTC Backup data Register.
2747   * @param  hrtc RTC handle
2748   * @param  BackupRegister RTC Backup data Register number.
2749   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2750   * @retval Read value
2751   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)2752 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2753 {
2754   uint32_t tmp;
2755 
2756   /* Prevent unused argument(s) compilation warning */
2757   UNUSED(hrtc);
2758   /* Check the parameters */
2759   assert_param(IS_RTC_BKP(BackupRegister));
2760 
2761   tmp = (uint32_t) &(TAMP->BKP0R);
2762   tmp += (BackupRegister * 4U);
2763 
2764   /* Read the specified register */
2765   return (*(__IO uint32_t *)tmp);
2766 }
2767 
2768 /**
2769   * @brief  Reset the RTC Backup data Register and the device secrets.
2770   * @param  hrtc RTC handle
2771   * @retval None
2772   */
HAL_RTCEx_BKUPErase(RTC_HandleTypeDef * hrtc)2773 void  HAL_RTCEx_BKUPErase(RTC_HandleTypeDef *hrtc)
2774 {
2775   UNUSED(hrtc);
2776   WRITE_REG(TAMP->CR2, TAMP_CR2_BKERASE);
2777 }
2778 
2779 /**
2780   * @brief  Block the access to the RTC Backup data Register and the device secrets.
2781   * @param  hrtc RTC handle
2782   * @retval None
2783   */
HAL_RTCEx_BKUPBlock_Enable(RTC_HandleTypeDef * hrtc)2784 void  HAL_RTCEx_BKUPBlock_Enable(RTC_HandleTypeDef *hrtc)
2785 {
2786   UNUSED(hrtc);
2787   WRITE_REG(TAMP->CR2, TAMP_CR2_BKBLOCK);
2788 }
2789 
2790 /**
2791   * @brief  Disable the Block to the access to the RTC Backup data Register and the device secrets.
2792   * @param  hrtc RTC handle
2793   * @retval None
2794   */
HAL_RTCEx_BKUPBlock_Disable(RTC_HandleTypeDef * hrtc)2795 void  HAL_RTCEx_BKUPBlock_Disable(RTC_HandleTypeDef *hrtc)
2796 {
2797   UNUSED(hrtc);
2798   CLEAR_BIT(TAMP->CR2, TAMP_CR2_BKBLOCK);
2799 }
2800 
2801 /**
2802   * @brief  Enable and Disable the erase of the configurable Device Secerts
2803   * @note   This API must be called before enabling the Tamper.
2804   * @param  hrtc RTC handle
2805   * @param  SecretDeviceConf Specifies the configuration of the Secrets Devices
2806   *          This parameter can be one of the following values:
2807   *             @arg TAMP_SECRETDEVICE_ERASE_ENABLE: Configurable device secrets are is included in the device secrets
2808   *              protected by TAMP peripheral.
2809   *             @arg TAMP_SECRETDEVICE_ERASE_DISABLE: Configurable device secrets are not included in the device
2810   *             secrets protected by TAMP peripheral.
2811   * @retval None
2812   */
HAL_RTCEx_Erase_SecretDev_Conf(RTC_HandleTypeDef * hrtc,uint32_t SecretDeviceConf)2813 void  HAL_RTCEx_Erase_SecretDev_Conf(RTC_HandleTypeDef *hrtc, uint32_t SecretDeviceConf)
2814 {
2815   UNUSED(hrtc);
2816 
2817   if (SecretDeviceConf != TAMP_SECRETDEVICE_ERASE_ENABLE)
2818   {
2819     CLEAR_BIT(TAMP->ERCFGR, TAMP_ERCFGR0);
2820   }
2821   else
2822   {
2823     SET_BIT(TAMP->ERCFGR, TAMP_ERCFGR0);
2824   }
2825 }
2826 
2827 /**
2828   * @brief  Get the erase configuration of the Device Secerts
2829   * @retval RTCEx_TAMP_Secret_Device_Conf_Erase
2830   */
HAL_RTCEx_Get_Erase_SecretDev_Conf(void)2831 uint32_t HAL_RTCEx_Get_Erase_SecretDev_Conf(void)
2832 {
2833   if (READ_BIT(TAMP->ERCFGR, TAMP_ERCFGR0) == TAMP_ERCFGR0)
2834   {
2835     return TAMP_SECRETDEVICE_ERASE_ENABLE;
2836   }
2837   else
2838   {
2839     return TAMP_SECRETDEVICE_ERASE_DISABLE;
2840   }
2841 }
2842 /**
2843   * @}
2844   */
2845 
2846 
2847 /** @addtogroup RTCEx_Exported_Functions_Group7
2848   * @brief      Extended RTC security functions
2849   *
2850 @verbatim
2851   ===============================================================================
2852              ##### Extended RTC security functions #####
2853   ===============================================================================
2854   [..]
2855    (+) Before calling security function, you have to call first
2856        HAL_RTC_Init() function.
2857 @endverbatim
2858   * @{
2859   */
2860 
2861 /**
2862   * @brief  Get the security level of the RTC.
2863   *         To set the secure level please call HAL_RTCEx_SecureModeSet.
2864   * @param  hrtc RTC handle
2865   * @param  secureState  Secure state
2866   * @retval HAL_StatusTypeDef
2867   */
HAL_RTCEx_SecureModeGet(RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2868 HAL_StatusTypeDef HAL_RTCEx_SecureModeGet(RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef  *secureState)
2869 {
2870   /* Prevent unused argument(s) compilation warning */
2871   UNUSED(hrtc);
2872   /* Read registers */
2873   uint32_t rtc_seccfgr = READ_REG(RTC->SECCFGR);
2874   uint32_t tamp_seccfgr = READ_REG(TAMP->SECCFGR);
2875 
2876   /* RTC */
2877   secureState->rtcSecureFull = READ_BIT(rtc_seccfgr, RTC_SECCFGR_SEC);
2878 
2879   /* Warning, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2880   secureState->rtcNonSecureFeatures = READ_BIT(rtc_seccfgr, RTC_NONSECURE_FEATURE_NONE);
2881 
2882   /* TAMP */
2883   secureState->tampSecureFull = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_TAMPSEC);
2884 
2885   /* Monotonic Counter */
2886   secureState->MonotonicCounterSecure = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_CNT1SEC);
2887 
2888   /* Backup register start zones
2889      Warning : Backup register start zones are shared with privilege configuration */
2890   secureState->backupRegisterStartZone2 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPRWSEC) >> TAMP_SECCFGR_BKPRWSEC_Pos;
2891   secureState->backupRegisterStartZone3 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPWSEC) >> TAMP_SECCFGR_BKPWSEC_Pos;
2892 
2893   return HAL_OK;
2894 }
2895 
2896 
2897 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
2898 /**
2899   * @brief  Set the security level of the RTC/TAMP/Backup registers.
2900   *         To get the current security level call HAL_RTCEx_SecureModeGet.
2901   * @param  hrtc RTC handle
2902   * @param  secureState  Secure state
2903   * @retval HAL_StatusTypeDef
2904   */
HAL_RTCEx_SecureModeSet(RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2905 HAL_StatusTypeDef HAL_RTCEx_SecureModeSet(RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef  *secureState)
2906 {
2907   /* Prevent unused argument(s) compilation warning */
2908   UNUSED(hrtc);
2909   assert_param(IS_RTC_SECURE_FULL(secureState->rtcSecureFull));
2910   assert_param(IS_RTC_NONSECURE_FEATURES(secureState->rtcNonSecureFeatures));
2911   assert_param(IS_TAMP_SECURE_FULL(secureState->tampSecureFull));
2912   assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone2));
2913   assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone3));
2914   assert_param(IS_TAMP_MONOTONIC_CNT_SECURE(secureState->MonotonicCounterSecure));
2915 
2916   /* RTC, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2917   WRITE_REG(RTC->SECCFGR, secureState->rtcSecureFull | secureState->rtcNonSecureFeatures);
2918 
2919   /* Tamper + Backup register + Monotonic counter
2920      Warning : Backup register start zone are Shared with privilege configuration */
2921   WRITE_REG(TAMP->SECCFGR,
2922             secureState->tampSecureFull | secureState->MonotonicCounterSecure |
2923             (TAMP_SECCFGR_BKPRWSEC & (secureState->backupRegisterStartZone2 << TAMP_SECCFGR_BKPRWSEC_Pos)) |
2924             (TAMP_SECCFGR_BKPWSEC & (secureState->backupRegisterStartZone3 << TAMP_SECCFGR_BKPWSEC_Pos)));
2925 
2926   return HAL_OK;
2927 }
2928 
2929 
2930 #endif /* #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
2931 
2932 /**
2933   * @}
2934   */
2935 
2936 /** @addtogroup RTCEx_Exported_Functions_Group8
2937   * @brief      Extended RTC privilege functions
2938   *
2939 @verbatim
2940   ===============================================================================
2941              ##### Extended RTC privilege functions #####
2942   ===============================================================================
2943   [..]
2944    (+) Before calling privilege function, you have to call first
2945        HAL_RTC_Init() function.
2946 @endverbatim
2947   * @{
2948   */
2949 
2950 
2951 /**
2952   * @brief  Set the privilege level of the RTC/TAMP registers.
2953   *         To get the current privilege level call HAL_RTCEx_PrivilegeModeGet.
2954   * @param  hrtc RTC handle
2955   * @param  privilegeState  Privilege state
2956   * @retval HAL_StatusTypeDef
2957   */
HAL_RTCEx_PrivilegeModeSet(RTC_HandleTypeDef * hrtc,RTC_PrivilegeStateTypeDef * privilegeState)2958 HAL_StatusTypeDef HAL_RTCEx_PrivilegeModeSet(RTC_HandleTypeDef *hrtc, RTC_PrivilegeStateTypeDef *privilegeState)
2959 {
2960   /* Prevent unused argument(s) compilation warning */
2961   UNUSED(hrtc);
2962   assert_param(IS_RTC_PRIVILEGE_FULL(privilegeState->rtcPrivilegeFull));
2963   assert_param(IS_RTC_PRIVILEGE_FEATURES(privilegeState->rtcPrivilegeFeatures));
2964   assert_param(IS_TAMP_PRIVILEGE_FULL(privilegeState->tampPrivilegeFull));
2965   assert_param(IS_TAMP_MONOTONIC_CNT_PRIVILEGE(privilegeState->MonotonicCounterPrivilege));
2966   assert_param(IS_RTC_PRIVILEGE_BKUP_ZONE(privilegeState->backupRegisterPrivZone));
2967   assert_param(IS_RTC_BKP(privilegeState->backupRegisterStartZone2));
2968   assert_param(IS_RTC_BKP(privilegeState->backupRegisterStartZone3));
2969 
2970   /* RTC privilege configuration */
2971   WRITE_REG(RTC->PRIVCFGR, privilegeState->rtcPrivilegeFull | privilegeState->rtcPrivilegeFeatures);
2972 
2973   /* TAMP, Monotonic counter and Backup registers privilege configuration
2974      Warning : privilegeState->backupRegisterPrivZone is only writable in secure mode or if trustzone is disabled.
2975                In non secure mode, a notification is generated through a flag/interrupt in the TZIC
2976          (TrustZone interrupt controller). The bits are not written. */
2977   WRITE_REG(TAMP->PRIVCFGR, privilegeState->tampPrivilegeFull | privilegeState->backupRegisterPrivZone | \
2978             privilegeState->MonotonicCounterPrivilege);
2979 
2980   /* Backup register start zone
2981      Warning : This parameter is only writable in secure mode or if trustzone is disabled.
2982                In non secure mode, a notification is generated through a flag/interrupt in the TZIC
2983          (TrustZone interrupt controller). The bits are not written.
2984      Warning : Backup register start zones are shared with secure configuration */
2985   MODIFY_REG(TAMP->SECCFGR,
2986              (TAMP_SECCFGR_BKPRWSEC | TAMP_SECCFGR_BKPWSEC),
2987              ((privilegeState->backupRegisterStartZone2 << TAMP_SECCFGR_BKPRWSEC_Pos) | \
2988               (privilegeState->backupRegisterStartZone3 << TAMP_SECCFGR_BKPWSEC_Pos)));
2989 
2990   return HAL_OK;
2991 }
2992 
2993 /**
2994   * @brief  Get the privilege level of the RTC.
2995   *         To set the privilege level please call HAL_RTCEx_PrivilegeModeSet.
2996   * @param  hrtc RTC handle
2997   * @param  privilegeState  Privilege state
2998   * @retval HAL_StatusTypeDef
2999   */
HAL_RTCEx_PrivilegeModeGet(RTC_HandleTypeDef * hrtc,RTC_PrivilegeStateTypeDef * privilegeState)3000 HAL_StatusTypeDef HAL_RTCEx_PrivilegeModeGet(RTC_HandleTypeDef *hrtc, RTC_PrivilegeStateTypeDef *privilegeState)
3001 {
3002   /* Prevent unused argument(s) compilation warning */
3003   UNUSED(hrtc);
3004 
3005   /* Read registers */
3006   uint32_t rtc_privcfgr = READ_REG(RTC->PRIVCFGR);
3007   uint32_t tamp_privcfgr = READ_REG(TAMP->PRIVCFGR);
3008   uint32_t tamp_seccfgr = READ_REG(TAMP->SECCFGR);
3009 
3010   /* RTC privilege configuration */
3011   privilegeState->rtcPrivilegeFull = READ_BIT(rtc_privcfgr, RTC_PRIVCFGR_PRIV);
3012 
3013   /* Warning, rtcPrivilegeFeatures is only relevant if privilegeState->rtcPrivilegeFull == RTC_PRIVILEGE_FULL_NO */
3014   privilegeState->rtcPrivilegeFeatures = READ_BIT(rtc_privcfgr, RTC_PRIVILEGE_FEATURE_ALL);
3015 
3016   /* TAMP and Backup registers privilege configuration */
3017   privilegeState->tampPrivilegeFull = READ_BIT(tamp_privcfgr, TAMP_PRIVCFGR_TAMPPRIV);
3018 
3019   /* Monotonic registers privilege configuration */
3020   privilegeState->MonotonicCounterPrivilege = READ_BIT(tamp_privcfgr, TAMP_PRIVCFGR_CNT1PRIV);
3021 
3022   /* Backup registers Zones */
3023   privilegeState->backupRegisterPrivZone = READ_BIT(tamp_privcfgr, (TAMP_PRIVCFGR_BKPWPRIV | TAMP_PRIVCFGR_BKPRWPRIV));
3024 
3025   /* Backup register start zones
3026      Warning : Shared with secure configuration */
3027   privilegeState->backupRegisterStartZone2 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPRWSEC) >> TAMP_SECCFGR_BKPRWSEC_Pos;
3028   privilegeState->backupRegisterStartZone3 = READ_BIT(tamp_seccfgr, TAMP_SECCFGR_BKPWSEC) >> TAMP_SECCFGR_BKPWSEC_Pos;
3029 
3030   return HAL_OK;
3031 }
3032 
3033 /**
3034   * @}
3035   */
3036 
3037 /**
3038   * @}
3039   */
3040 
3041 #endif /* HAL_RTC_MODULE_ENABLED */
3042 
3043 /**
3044   * @}
3045   */
3046 
3047 
3048 /**
3049   * @}
3050   */
3051