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