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