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