1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_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 "stm32mp1xx_hal.h"
113 
114 /** @addtogroup STM32MP1xx_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)
128 
129 
130 /* Private macro -------------------------------------------------------------*/
131 /* Private variables ---------------------------------------------------------*/
132 /* Private function prototypes -----------------------------------------------*/
133 /* Exported functions --------------------------------------------------------*/
134 
135 /** @addtogroup RTCEx_Exported_Functions
136   * @{
137   */
138 
139 
140 /** @addtogroup RTCEx_Exported_Functions_Group1
141  *  @brief   RTC TimeStamp and Tamper functions
142   *
143 @verbatim
144  ===============================================================================
145                  ##### RTC TimeStamp and Tamper functions #####
146  ===============================================================================
147 
148  [..] This section provides functions allowing to configure TimeStamp feature
149 
150 @endverbatim
151   * @{
152   */
153 
154 /**
155   * @brief  Set TimeStamp.
156   * @note   This API must be called before enabling the TimeStamp feature.
157   * @param  hrtc RTC handle
158   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
159   *         activated.
160   *          This parameter can be one of the following values:
161   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
162   *                                        rising edge of the related pin.
163   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
164   *                                         falling edge of the related pin.
165   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
166   *          This parameter can be one of the following values:
167   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
168   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
169   *               compatibility, this parameter is required.
170   * @retval HAL status
171   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)172 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
173 {
174   /* Check the parameters */
175   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
176   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
177   UNUSED(RTC_TimeStampPin);
178 
179   /* Process Locked */
180   __HAL_LOCK(hrtc);
181 
182   hrtc->State = HAL_RTC_STATE_BUSY;
183 
184   /* Get the RTC_CR register and clear the bits to be configured */
185   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
186 
187   /* Disable the write protection for RTC registers */
188   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
189 
190   /* Configure the Time Stamp TSEDGE and Enable bits */
191   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge | RTC_CR_TSE);
192 
193   /* Enable the write protection for RTC registers */
194   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
195 
196   /* Change RTC state */
197   hrtc->State = HAL_RTC_STATE_READY;
198 
199   /* Process Unlocked */
200   __HAL_UNLOCK(hrtc);
201 
202   return HAL_OK;
203 }
204 
205 /**
206   * @brief  Set TimeStamp with Interrupt.
207   * @note   This API must be called before enabling the TimeStamp feature.
208   * @param  hrtc RTC handle
209   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
210   *         activated.
211   *          This parameter can be one of the following values:
212   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
213   *                                        rising edge of the related pin.
214   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
215   *                                         falling edge of the related pin.
216   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
217   *          This parameter can be one of the following values:
218   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
219   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
220   *               compatibility, this parameter is required.
221   * @retval HAL status
222   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)223 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
224 {
225   /* Check the parameters */
226   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
227   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
228   UNUSED(RTC_TimeStampPin);
229 
230   /* Process Locked */
231   __HAL_LOCK(hrtc);
232 
233   hrtc->State = HAL_RTC_STATE_BUSY;
234 
235   /* RTC timestamp Interrupt Configuration: EXTI configuration */
236   __HAL_RTC_TIMESTAMP_EXTI_ENABLE_IT();
237 
238   /* Get the RTC_CR register and clear the bits to be configured */
239   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
240 
241   /* Disable the write protection for RTC registers */
242   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
243 
244   /* Configure the Time Stamp TSEDGE before Enable bit to avoid unwanted TSF setting. */
245   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge);
246 
247   /* Enable timestamp and IT */
248   SET_BIT(RTC->CR, RTC_CR_TSE | RTC_CR_TSIE);
249 
250   /* Enable the write protection for RTC registers */
251   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
252 
253   hrtc->State = HAL_RTC_STATE_READY;
254 
255   /* Process Unlocked */
256   __HAL_UNLOCK(hrtc);
257 
258   return HAL_OK;
259 }
260 
261 /**
262   * @brief  Deactivate TimeStamp.
263   * @param  hrtc RTC handle
264   * @retval HAL status
265   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)266 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
267 {
268   /* Process Locked */
269   __HAL_LOCK(hrtc);
270 
271   hrtc->State = HAL_RTC_STATE_BUSY;
272 
273   /* Disable the write protection for RTC registers */
274   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
275 
276   /* In case of interrupt mode is used, the interrupt source must disabled */
277   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
278 
279   /* Enable the write protection for RTC registers */
280   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
281 
282   hrtc->State = HAL_RTC_STATE_READY;
283 
284   /* Process Unlocked */
285   __HAL_UNLOCK(hrtc);
286 
287   return HAL_OK;
288 }
289 
290 /**
291   * @brief  Set Internal TimeStamp.
292   * @note   This API must be called before enabling the internal TimeStamp feature.
293   * @param  hrtc RTC handle
294   * @retval HAL status
295   */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)296 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
297 {
298   /* Process Locked */
299   __HAL_LOCK(hrtc);
300 
301   hrtc->State = HAL_RTC_STATE_BUSY;
302 
303   /* Disable the write protection for RTC registers */
304   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
305 
306   /* Configure the internal Time Stamp Enable bits */
307   SET_BIT(RTC->CR, RTC_CR_ITSE);
308 
309   /* Enable the write protection for RTC registers */
310   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
311 
312   /* Change RTC state */
313   hrtc->State = HAL_RTC_STATE_READY;
314 
315   /* Process Unlocked */
316   __HAL_UNLOCK(hrtc);
317 
318   return HAL_OK;
319 }
320 
321 /**
322   * @brief  Deactivate Internal TimeStamp.
323   * @param  hrtc RTC handle
324   * @retval HAL status
325   */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)326 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
327 {
328   /* Process Locked */
329   __HAL_LOCK(hrtc);
330 
331   hrtc->State = HAL_RTC_STATE_BUSY;
332 
333   /* Disable the write protection for RTC registers */
334   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
335 
336   /* Configure the internal Time Stamp Enable bits */
337   CLEAR_BIT(RTC->CR, RTC_CR_ITSE);
338 
339   /* Enable the write protection for RTC registers */
340   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
341 
342   hrtc->State = HAL_RTC_STATE_READY;
343 
344   /* Process Unlocked */
345   __HAL_UNLOCK(hrtc);
346 
347   return HAL_OK;
348 }
349 
350 /**
351   * @brief  Get the RTC TimeStamp value.
352   * @param  hrtc RTC handle
353   * @param  sTimeStamp Pointer to Time structure
354   * @param  sTimeStampDate Pointer to Date structure
355   * @param  Format specifies the format of the entered parameters.
356   *          This parameter can be one of the following values:
357   *             @arg RTC_FORMAT_BIN: Binary data format
358   *             @arg RTC_FORMAT_BCD: BCD data format
359   * @retval HAL status
360   */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)361 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
362 {
363   uint32_t tmptime, tmpdate;
364 
365   UNUSED(hrtc);
366   /* Check the parameters */
367   assert_param(IS_RTC_FORMAT(Format));
368 
369   /* Get the TimeStamp time and date registers values */
370   tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
371   tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
372 
373   /* Fill the Time structure fields with the read parameters */
374   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
375   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
376   sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
377   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
378   sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
379 
380   /* Fill the Date structure fields with the read parameters */
381   sTimeStampDate->Year = 0U;
382   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
383   sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
384   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
385 
386   /* Check the input parameters format */
387   if (Format == RTC_FORMAT_BIN)
388   {
389     /* Convert the TimeStamp structure parameters to Binary format */
390     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
391     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
392     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
393 
394     /* Convert the DateTimeStamp structure parameters to Binary format */
395     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
396     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
397     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
398   }
399 
400   /* Clear the TIMESTAMP Flags */
401   WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
402 
403   return HAL_OK;
404 }
405 
406 #if defined (CORTEX_IN_SECURE_STATE)
407 /**
408   * @brief  Handle TimeStamp secure interrupt request.
409   * @param  hrtc RTC handle
410   * @retval None
411   */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)412 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
413 {
414   if (READ_BIT(RTC->SMISR, RTC_SMISR_TSMF) != 0U)
415   {
416 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
417     /* Call TimeStampEvent registered Callback */
418     hrtc->TimeStampEventCallback(hrtc);
419 #else
420     HAL_RTCEx_TimeStampEventCallback(hrtc);
421 #endif
422     /* Clearing flags after the Callback because the content of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset.*/
423     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
424   }
425 
426   /* Change RTC state */
427   hrtc->State = HAL_RTC_STATE_READY;
428 }
429 
430 #else /* #if defined (CORTEX_IN_SECURE_STATE) */
431 
432 /**
433   * @brief  Handle TimeStamp non-secure interrupt request.
434   * @param  hrtc RTC handle
435   * @retval None
436   */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)437 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
438 {
439   if (READ_BIT(RTC->MISR, RTC_MISR_TSMF) != 0U)
440   {
441 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
442     /* Call TimeStampEvent registered Callback */
443     hrtc->TimeStampEventCallback(hrtc);
444 #else
445     HAL_RTCEx_TimeStampEventCallback(hrtc);
446 #endif
447     /* Clearing flags after the Callback because the content of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset.*/
448     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
449   }
450 
451   /* Change RTC state */
452   hrtc->State = HAL_RTC_STATE_READY;
453 }
454 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
455 
456 /**
457   * @brief  TimeStamp callback.
458   * @param  hrtc RTC handle
459   * @retval None
460   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)461 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
462 {
463   /* Prevent unused argument(s) compilation warning */
464   UNUSED(hrtc);
465 
466   /* NOTE : This function should not be modified, when the callback is needed,
467             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
468   */
469 }
470 
471 /**
472   * @brief  Handle TimeStamp polling request.
473   * @param  hrtc RTC handle
474   * @param  Timeout Timeout duration
475   * @retval HAL status
476   */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)477 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
478 {
479   uint32_t tickstart = HAL_GetTick();
480 
481   while (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
482   {
483     if (READ_BIT(RTC->SR, RTC_SR_TSOVF) != 0U)
484     {
485       /* Clear the TIMESTAMP OverRun Flag */
486       WRITE_REG(RTC->SCR, RTC_SCR_CTSOVF);
487 
488       /* Change TIMESTAMP state */
489       hrtc->State = HAL_RTC_STATE_ERROR;
490 
491       return HAL_ERROR;
492     }
493 
494     if (Timeout != HAL_MAX_DELAY)
495     {
496       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
497       {
498         hrtc->State = HAL_RTC_STATE_TIMEOUT;
499         return HAL_TIMEOUT;
500       }
501     }
502   }
503 
504   /* Change RTC state */
505   hrtc->State = HAL_RTC_STATE_READY;
506 
507   return HAL_OK;
508 }
509 
510 /**
511   * @}
512   */
513 
514 /** @addtogroup RTCEx_Exported_Functions_Group2
515   * @brief    RTC Wake-up functions
516   *
517 @verbatim
518  ===============================================================================
519                         ##### RTC Wake-up functions #####
520  ===============================================================================
521 
522  [..] This section provides functions allowing to configure Wake-up feature
523 
524 @endverbatim
525   * @{
526   */
527 
528 /**
529   * @brief  Set wake up timer.
530   * @param  hrtc RTC handle
531   * @param  WakeUpCounter Wake up counter
532   * @param  WakeUpClock Wake up clock
533   * @retval HAL status
534   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)535 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
536 {
537   uint32_t tickstart;
538 
539   /* Check the parameters */
540   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
541   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
542 
543   /* Process Locked */
544   __HAL_LOCK(hrtc);
545 
546   hrtc->State = HAL_RTC_STATE_BUSY;
547 
548   /* Disable the write protection for RTC registers */
549   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
550 
551   /* Clear WUTE in RTC_CR to disable the wakeup timer */
552   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
553 
554   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
555      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
556      calendar initialization mode. */
557   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
558   {
559     tickstart = HAL_GetTick();
560     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
561     {
562       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
563       {
564         /* Enable the write protection for RTC registers */
565         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
566 
567         hrtc->State = HAL_RTC_STATE_TIMEOUT;
568 
569         /* Process Unlocked */
570         __HAL_UNLOCK(hrtc);
571 
572         return HAL_TIMEOUT;
573       }
574     }
575   }
576 
577   /* Configure the clock source */
578   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
579 
580   /* Configure the Wakeup Timer counter */
581   WRITE_REG(RTC->WUTR, (uint32_t)WakeUpCounter);
582 
583   /* Enable the Wakeup Timer */
584   SET_BIT(RTC->CR, RTC_CR_WUTE);
585 
586   /* Enable the write protection for RTC registers */
587   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
588 
589   hrtc->State = HAL_RTC_STATE_READY;
590 
591   /* Process Unlocked */
592   __HAL_UNLOCK(hrtc);
593 
594   return HAL_OK;
595 }
596 
597 /**
598   * @brief  Set wake up timer with interrupt.
599   * @param  hrtc RTC handle
600   * @param  WakeUpCounter Wake up counter
601   * @param  WakeUpClock Wake up clock
602   * @param  WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
603   *                       - No effect if WakeUpAutoClr is set to zero
604   *                       - This feature is meaningful in case of Low power mode to avoid any RTC software execution after Wake Up.
605   *                         That is why when WakeUpAutoClr is set, EXTI is configured as EVENT instead of Interrupt to avoid useless IRQ handler execution.
606   * @retval HAL status
607   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock,uint32_t WakeUpAutoClr)608 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock, uint32_t WakeUpAutoClr)
609 {
610   uint32_t tickstart;
611 
612   /* Check the parameters */
613   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
614   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
615   /* (0x0000<=WUTOCLR<=WUT) */
616   assert_param(WakeUpAutoClr <= WakeUpCounter);
617 
618   /* Process Locked */
619   __HAL_LOCK(hrtc);
620 
621   hrtc->State = HAL_RTC_STATE_BUSY;
622 
623   /* Disable the write protection for RTC registers */
624   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
625 
626   /* Clear WUTE in RTC_CR to disable the wakeup timer */
627   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
628 
629   /* Clear flag Wake-Up */
630   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
631 
632   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
633      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
634      calendar initialization mode. */
635   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
636   {
637     tickstart = HAL_GetTick();
638     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
639     {
640       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
641       {
642         /* Enable the write protection for RTC registers */
643         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
644 
645         hrtc->State = HAL_RTC_STATE_TIMEOUT;
646 
647         /* Process Unlocked */
648         __HAL_UNLOCK(hrtc);
649 
650         return HAL_TIMEOUT;
651       }
652     }
653   }
654 
655   /* Configure the Wakeup Timer counter and auto clear value */
656   WRITE_REG(RTC->WUTR, (uint32_t)(WakeUpCounter));
657 
658   /* Configure the clock source */
659   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
660 
661   /* In case of WUT autoclr, the IRQ handler should not be called */
662   if (WakeUpAutoClr != 0U)
663   {
664     /* RTC WakeUpTimer EXTI Configuration: Event configuration */
665     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
666   }
667   else
668   {
669     /* RTC WakeUpTimer EXTI Configuration: Interrupt configuration */
670     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
671   }
672 
673   /* Configure the Interrupt in the RTC_CR register and Enable the Wakeup Timer*/
674   SET_BIT(RTC->CR, (RTC_CR_WUTIE | RTC_CR_WUTE));
675 
676   /* Enable the write protection for RTC registers */
677   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
678 
679   hrtc->State = HAL_RTC_STATE_READY;
680 
681   /* Process Unlocked */
682   __HAL_UNLOCK(hrtc);
683 
684   return HAL_OK;
685 }
686 
687 /**
688   * @brief  Deactivate wake up timer counter.
689   * @param  hrtc RTC handle
690   * @retval HAL status
691   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)692 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
693 {
694   uint32_t tickstart;
695 
696   /* Process Locked */
697   __HAL_LOCK(hrtc);
698 
699   hrtc->State = HAL_RTC_STATE_BUSY;
700 
701   /* Disable the write protection for RTC registers */
702   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
703 
704   /* Disable the Wakeup Timer */
705   /* In case of interrupt mode is used, the interrupt source must disabled */
706   CLEAR_BIT(RTC->CR, (RTC_CR_WUTE | RTC_CR_WUTIE));
707 
708   tickstart = HAL_GetTick();
709   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
710   while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
711   {
712     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
713     {
714       /* Enable the write protection for RTC registers */
715       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
716 
717       hrtc->State = HAL_RTC_STATE_TIMEOUT;
718 
719       /* Process Unlocked */
720       __HAL_UNLOCK(hrtc);
721 
722       return HAL_TIMEOUT;
723     }
724   }
725 
726   /* Enable the write protection for RTC registers */
727   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
728 
729   hrtc->State = HAL_RTC_STATE_READY;
730 
731   /* Process Unlocked */
732   __HAL_UNLOCK(hrtc);
733 
734   return HAL_OK;
735 }
736 
737 /**
738   * @brief  Get wake up timer counter.
739   * @param  hrtc RTC handle
740   * @retval Counter value
741   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)742 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
743 {
744   UNUSED(hrtc);
745   /* Get the counter value */
746   return (uint32_t)(READ_BIT(RTC->WUTR, RTC_WUTR_WUT));
747 }
748 
749 #if defined (CORTEX_IN_SECURE_STATE)
750 /**
751   * @brief  Handle Wake Up Timer secure interrupt request.
752   * @param  hrtc RTC handle
753   * @retval None
754   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)755 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
756 {
757   if ((RTC->SMISR & RTC_SMISR_WUTMF) != 0u)
758   {
759     /* Immediatly clear flags */
760     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
761 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
762     /* Call wake up timer registered Callback */
763     hrtc->WakeUpTimerEventCallback(hrtc);
764 #else
765     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
766 #endif
767   }
768 
769   /* Change RTC state */
770   hrtc->State = HAL_RTC_STATE_READY;
771 }
772 
773 #else /* #if defined (CORTEX_IN_SECURE_STATE) */
774 
775 /**
776   * @brief  Handle Wake Up Timer non-secure interrupt request.
777   * @param  hrtc RTC handle
778   * @retval None
779   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)780 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
781 {
782   /* Get the pending status of the WAKEUPTIMER Interrupt */
783   if (READ_BIT(RTC->MISR, RTC_MISR_WUTMF) != 0U)
784   {
785     /* Clear the WAKEUPTIMER interrupt pending bit */
786     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
787 
788 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
789     /* Call WakeUpTimerEvent registered Callback */
790     hrtc->WakeUpTimerEventCallback(hrtc);
791 #else
792     /* WAKEUPTIMER callback */
793     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
794 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
795   }
796 
797   /* Change RTC state */
798   hrtc->State = HAL_RTC_STATE_READY;
799 }
800 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
801 
802 /**
803   * @brief  Wake Up Timer callback.
804   * @param  hrtc RTC handle
805   * @retval None
806   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)807 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
808 {
809   /* Prevent unused argument(s) compilation warning */
810   UNUSED(hrtc);
811 
812   /* NOTE : This function should not be modified, when the callback is needed,
813             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
814    */
815 }
816 
817 /**
818   * @brief  Handle Wake Up Timer Polling.
819   * @param  hrtc RTC handle
820   * @param  Timeout Timeout duration
821   * @retval HAL status
822   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)823 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
824 {
825   uint32_t tickstart = HAL_GetTick();
826 
827   while (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
828   {
829     if (Timeout != HAL_MAX_DELAY)
830     {
831       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
832       {
833         hrtc->State = HAL_RTC_STATE_TIMEOUT;
834         return HAL_TIMEOUT;
835       }
836     }
837   }
838 
839   /* Clear the WAKEUPTIMER Flag */
840   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
841 
842   /* Change RTC state */
843   hrtc->State = HAL_RTC_STATE_READY;
844 
845   return HAL_OK;
846 }
847 
848 /**
849   * @}
850   */
851 
852 /** @addtogroup RTCEx_Exported_Functions_Group3
853   * @brief    Extended Peripheral Control functions
854   *
855 @verbatim
856  ===============================================================================
857               ##### Extended Peripheral Control functions #####
858  ===============================================================================
859     [..]
860     This subsection provides functions allowing to
861       (+) Write a data in a specified RTC Backup data register
862       (+) Read a data in a specified RTC Backup data register
863       (+) Set the Coarse calibration parameters.
864       (+) Deactivate the Coarse calibration parameters
865       (+) Set the Smooth calibration parameters.
866       (+) Set Low Power calibration parameter.
867       (+) Configure the Synchronization Shift Control Settings.
868       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
869       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
870       (+) Enable the RTC reference clock detection.
871       (+) Disable the RTC reference clock detection.
872       (+) Enable the Bypass Shadow feature.
873       (+) Disable the Bypass Shadow feature.
874 
875 @endverbatim
876   * @{
877   */
878 
879 /**
880   * @brief  Set the Smooth calibration parameters.
881   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
882   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
883   *         SmoothCalibMinusPulsesValue must be equal to 0.
884   * @param  hrtc RTC handle
885   * @param  SmoothCalibPeriod  Select the Smooth Calibration Period.
886   *          This parameter can be can be one of the following values :
887   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
888   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
889   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
890   * @param  SmoothCalibPlusPulses  Select to Set or reset the CALP bit.
891   *          This parameter can be one of the following values:
892   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
893   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
894   * @param  SmoothCalibMinusPulsesValue  Select the value of CALM[8:0] bits.
895   *          This parameter can be one any value from 0 to 0x000001FF.
896   * @retval HAL status
897   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)898 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
899 {
900   uint32_t tickstart;
901 
902   /* Check the parameters */
903   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
904   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
905   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
906 
907   /* Process Locked */
908   __HAL_LOCK(hrtc);
909 
910   hrtc->State = HAL_RTC_STATE_BUSY;
911 
912   /* Disable the write protection for RTC registers */
913   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
914 
915   /* check if a calibration is pending*/
916   if (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
917   {
918     tickstart = HAL_GetTick();
919 
920     /* check if a calibration is pending*/
921     while (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
922     {
923       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
924       {
925         /* Enable the write protection for RTC registers */
926         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
927 
928         /* Change RTC state */
929         hrtc->State = HAL_RTC_STATE_TIMEOUT;
930 
931         /* Process Unlocked */
932         __HAL_UNLOCK(hrtc);
933 
934         return HAL_TIMEOUT;
935       }
936     }
937   }
938 
939   /* Configure the Smooth calibration settings */
940   MODIFY_REG(RTC->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
941 
942   /* Enable the write protection for RTC registers */
943   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
944 
945   /* Change RTC state */
946   hrtc->State = HAL_RTC_STATE_READY;
947 
948   /* Process Unlocked */
949   __HAL_UNLOCK(hrtc);
950 
951   return HAL_OK;
952 }
953 
954 /**
955   * @brief  Configure the Synchronization Shift Control Settings.
956   * @note   When REFCKON is set, firmware must not write to Shift control register.
957   * @param  hrtc RTC handle
958   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
959   *          This parameter can be one of the following values:
960   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
961   *             @arg RTC_SHIFTADD1S_RESET: No effect.
962   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
963   *          This parameter can be one any value from 0 to 0x7FFF.
964   * @retval HAL status
965   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)966 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
967 {
968   uint32_t tickstart;
969 
970   /* Check the parameters */
971   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
972   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
973 
974   /* Process Locked */
975   __HAL_LOCK(hrtc);
976 
977   hrtc->State = HAL_RTC_STATE_BUSY;
978 
979   /* Disable the write protection for RTC registers */
980   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
981 
982   tickstart = HAL_GetTick();
983 
984   /* Wait until the shift is completed*/
985   while (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
986   {
987     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
988     {
989       /* Enable the write protection for RTC registers */
990       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
991 
992       hrtc->State = HAL_RTC_STATE_TIMEOUT;
993 
994       /* Process Unlocked */
995       __HAL_UNLOCK(hrtc);
996 
997       return HAL_TIMEOUT;
998     }
999   }
1000 
1001   /* Check if the reference clock detection is disabled */
1002   if (READ_BIT(RTC->CR, RTC_CR_REFCKON) == 0U)
1003   {
1004     /* Configure the Shift settings */
1005     MODIFY_REG(RTC->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
1006 
1007     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1008     if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1009     {
1010       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1011       {
1012         /* Enable the write protection for RTC registers */
1013         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1014 
1015         hrtc->State = HAL_RTC_STATE_ERROR;
1016 
1017         /* Process Unlocked */
1018         __HAL_UNLOCK(hrtc);
1019 
1020         return HAL_ERROR;
1021       }
1022     }
1023   }
1024   else
1025   {
1026     /* Enable the write protection for RTC registers */
1027     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1028 
1029     /* Change RTC state */
1030     hrtc->State = HAL_RTC_STATE_ERROR;
1031 
1032     /* Process Unlocked */
1033     __HAL_UNLOCK(hrtc);
1034 
1035     return HAL_ERROR;
1036   }
1037 
1038   /* Enable the write protection for RTC registers */
1039   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1040 
1041   /* Change RTC state */
1042   hrtc->State = HAL_RTC_STATE_READY;
1043 
1044   /* Process Unlocked */
1045   __HAL_UNLOCK(hrtc);
1046 
1047   return HAL_OK;
1048 }
1049 
1050 /**
1051   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1052   * @param  hrtc RTC handle
1053   * @param  CalibOutput Select the Calibration output Selection .
1054   *          This parameter can be one of the following values:
1055   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1056   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1057   * @retval HAL status
1058   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1059 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1060 {
1061   /* Check the parameters */
1062   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1063 
1064   /* Process Locked */
1065   __HAL_LOCK(hrtc);
1066 
1067   hrtc->State = HAL_RTC_STATE_BUSY;
1068 
1069   /* Disable the write protection for RTC registers */
1070   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1071 
1072   /* Configure the RTC_CR register */
1073   MODIFY_REG(RTC->CR, RTC_CR_COSEL, CalibOutput);
1074 
1075   /* Enable calibration output */
1076   SET_BIT(RTC->CR, RTC_CR_COE);
1077 
1078   /* Enable the write protection for RTC registers */
1079   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1080 
1081   /* Change RTC state */
1082   hrtc->State = HAL_RTC_STATE_READY;
1083 
1084   /* Process Unlocked */
1085   __HAL_UNLOCK(hrtc);
1086 
1087   return HAL_OK;
1088 }
1089 
1090 /**
1091   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1092   * @param  hrtc RTC handle
1093   * @retval HAL status
1094   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1095 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1096 {
1097   /* Process Locked */
1098   __HAL_LOCK(hrtc);
1099 
1100   hrtc->State = HAL_RTC_STATE_BUSY;
1101 
1102   /* Disable the write protection for RTC registers */
1103   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1104 
1105   /* Disable calibration output */
1106   CLEAR_BIT(RTC->CR, RTC_CR_COE);
1107 
1108   /* Enable the write protection for RTC registers */
1109   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1110 
1111   /* Change RTC state */
1112   hrtc->State = HAL_RTC_STATE_READY;
1113 
1114   /* Process Unlocked */
1115   __HAL_UNLOCK(hrtc);
1116 
1117   return HAL_OK;
1118 }
1119 
1120 /**
1121   * @brief  Enable the RTC reference clock detection.
1122   * @param  hrtc RTC handle
1123   * @retval HAL status
1124   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1125 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1126 {
1127   HAL_StatusTypeDef status;
1128 
1129   /* Process Locked */
1130   __HAL_LOCK(hrtc);
1131 
1132   hrtc->State = HAL_RTC_STATE_BUSY;
1133 
1134   /* Disable the write protection for RTC registers */
1135   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1136 
1137   /* Enter Initialization mode */
1138   status = RTC_EnterInitMode(hrtc);
1139   if (status == HAL_OK)
1140   {
1141     /* Enable clockref detection */
1142     SET_BIT(RTC->CR, RTC_CR_REFCKON);
1143 
1144     /* Exit Initialization mode */
1145     status = RTC_ExitInitMode(hrtc);
1146   }
1147 
1148   /* Enable the write protection for RTC registers */
1149   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1150 
1151   if (status == HAL_OK)
1152   {
1153     hrtc->State = HAL_RTC_STATE_READY;
1154   }
1155 
1156   /* Process Unlocked */
1157   __HAL_UNLOCK(hrtc);
1158 
1159   return status;
1160 }
1161 
1162 /**
1163   * @brief  Disable the RTC reference clock detection.
1164   * @param  hrtc RTC handle
1165   * @retval HAL status
1166   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1167 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1168 {
1169   HAL_StatusTypeDef status;
1170 
1171   /* Process Locked */
1172   __HAL_LOCK(hrtc);
1173 
1174   hrtc->State = HAL_RTC_STATE_BUSY;
1175 
1176   /* Disable the write protection for RTC registers */
1177   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1178 
1179   /* Enter Initialization mode */
1180   status = RTC_EnterInitMode(hrtc);
1181   if (status == HAL_OK)
1182   {
1183     /* Disable clockref detection */
1184     CLEAR_BIT(RTC->CR, RTC_CR_REFCKON);
1185 
1186     /* Exit Initialization mode */
1187     status = RTC_ExitInitMode(hrtc);
1188   }
1189 
1190   /* Enable the write protection for RTC registers */
1191   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1192 
1193   if (status == HAL_OK)
1194   {
1195     hrtc->State = HAL_RTC_STATE_READY;
1196   }
1197 
1198   /* Process Unlocked */
1199   __HAL_UNLOCK(hrtc);
1200 
1201   return status;
1202 }
1203 
1204 /**
1205   * @brief  Enable the Bypass Shadow feature.
1206   * @note   When the Bypass Shadow is enabled the calendar value are taken
1207   *         directly from the Calendar counter.
1208   * @param  hrtc RTC handle
1209   * @retval HAL status
1210   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1211 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1212 {
1213   /* Process Locked */
1214   __HAL_LOCK(hrtc);
1215 
1216   hrtc->State = HAL_RTC_STATE_BUSY;
1217 
1218   /* Disable the write protection for RTC registers */
1219   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1220 
1221   /* Set the BYPSHAD bit */
1222   SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1223 
1224   /* Enable the write protection for RTC registers */
1225   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1226 
1227   /* Change RTC state */
1228   hrtc->State = HAL_RTC_STATE_READY;
1229 
1230   /* Process Unlocked */
1231   __HAL_UNLOCK(hrtc);
1232 
1233   return HAL_OK;
1234 }
1235 
1236 /**
1237   * @brief  Disable the Bypass Shadow feature.
1238   * @note   When the Bypass Shadow is enabled the calendar value are taken
1239   *         directly from the Calendar counter.
1240   * @param  hrtc RTC handle
1241   * @retval HAL status
1242   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1243 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1244 {
1245   /* Process Locked */
1246   __HAL_LOCK(hrtc);
1247 
1248   hrtc->State = HAL_RTC_STATE_BUSY;
1249 
1250   /* Disable the write protection for RTC registers */
1251   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1252 
1253   /* Reset the BYPSHAD bit */
1254   CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1255 
1256   /* Enable the write protection for RTC registers */
1257   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1258 
1259   /* Change RTC state */
1260   hrtc->State = HAL_RTC_STATE_READY;
1261 
1262   /* Process Unlocked */
1263   __HAL_UNLOCK(hrtc);
1264 
1265   return HAL_OK;
1266 }
1267 
1268 /**
1269   * @brief  Increment Monotonic counter.
1270   * @param  hrtc RTC handle
1271   * @param  Instance  Monotonic counter Instance
1272   *         This parameter can be can be one of the following values :
1273   *           @arg RTC_MONOTONIC_COUNTER_1
1274   * @retval HAL status
1275   */
HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef * hrtc,uint32_t Instance)1276 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef *hrtc, uint32_t Instance)
1277 {
1278   UNUSED(hrtc);
1279   UNUSED(Instance);
1280   /* This register is read-only only and is incremented by one when a write access is done to this
1281      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1282   CLEAR_REG(TAMP->COUNTR);
1283 
1284   return HAL_OK;
1285 }
1286 
1287 /**
1288   * @brief  Monotonic counter incrementation.
1289   * @param  hrtc RTC handle
1290   * @param  Instance  Monotonic counter Instance
1291   *         This parameter can be can be one of the following values :
1292   *           @arg RTC_MONOTONIC_COUNTER_1
1293   * @param  Value Pointer to the counter monotonic counter value
1294   * @retval HAL status
1295   */
HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef * hrtc,uint32_t Instance,uint32_t * Value)1296 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef *hrtc, uint32_t Instance, uint32_t *Value)
1297 {
1298   UNUSED(hrtc);
1299   UNUSED(Instance);
1300 
1301   /* This register is read-only only and is incremented by one when a write access is done to this
1302      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1303   *Value = READ_REG(TAMP->COUNTR);
1304 
1305   return HAL_OK;
1306 }
1307 
1308 /**
1309   * @}
1310   */
1311 
1312 /** @addtogroup RTCEx_Exported_Functions_Group4
1313   * @brief    Extended features functions
1314   *
1315 @verbatim
1316  ===============================================================================
1317                  ##### Extended features functions #####
1318  ===============================================================================
1319     [..]  This section provides functions allowing to:
1320       (+) RTC Alarm B callback
1321       (+) RTC Poll for Alarm B request
1322 
1323 @endverbatim
1324   * @{
1325   */
1326 
1327 /**
1328   * @brief  Alarm B callback.
1329   * @param  hrtc RTC handle
1330   * @retval None
1331   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1332 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1333 {
1334   /* Prevent unused argument(s) compilation warning */
1335   UNUSED(hrtc);
1336 
1337   /* NOTE : This function should not be modified, when the callback is needed,
1338             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1339    */
1340 }
1341 
1342 /**
1343   * @brief  Handle Alarm B Polling request.
1344   * @param  hrtc RTC handle
1345   * @param  Timeout Timeout duration
1346   * @retval HAL status
1347   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1348 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1349 {
1350   uint32_t tickstart = HAL_GetTick();
1351 
1352   while (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1353   {
1354     if (Timeout != HAL_MAX_DELAY)
1355     {
1356       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1357       {
1358         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1359         return HAL_TIMEOUT;
1360       }
1361     }
1362   }
1363 
1364   /* Clear the Alarm Flag */
1365   WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1366 
1367   /* Change RTC state */
1368   hrtc->State = HAL_RTC_STATE_READY;
1369 
1370   return HAL_OK;
1371 }
1372 
1373 /**
1374   * @}
1375   */
1376 
1377 /** @addtogroup RTCEx_Exported_Functions_Group5
1378   * @brief      Extended RTC Tamper functions
1379   *
1380 @verbatim
1381   ==============================================================================
1382                          ##### Tamper functions #####
1383   ==============================================================================
1384   [..]
1385    (+) Before calling any tamper or internal tamper function, you have to call first
1386        HAL_RTC_Init() function.
1387    (+) In that ine you can select to output tamper event on RTC pin.
1388   [..]
1389    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1390        or Level according to the Tamper filter (if equal to 0 Edge else Level)
1391        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1392        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1393        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1394    (+) The default configuration of the Tamper erases the backup registers. To avoid
1395        erase, enable the NoErase field on the TAMP_TAMPCR register.
1396   [..]
1397    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1398        the HAL_RTCEx_SetInternalTamper() function.
1399 
1400 @endverbatim
1401   * @{
1402   */
1403 
1404 
1405 /**
1406   * @brief  Set Tamper
1407   * @param  hrtc RTC handle
1408   * @param  sTamper Pointer to Tamper Structure.
1409   * @retval HAL status
1410   */
HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1411 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
1412 {
1413   uint32_t tmpreg;
1414 
1415   /* Prevent unused argument(s) compilation warning */
1416   UNUSED(hrtc);
1417 
1418   /* Check the parameters */
1419   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1420   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1421   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1422   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1423   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1424   /* Mask flag only supported by TAMPER 1, 2 and 3 */
1425   assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_3)));
1426   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1427   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1428   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1429   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1430   /* Trigger and Filter have exclusive configurations */
1431   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
1432                || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1433 
1434   /* Configuration register 2 */
1435   tmpreg = READ_REG(TAMP->CR2);
1436   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1437 
1438   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1439   {
1440     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1441   }
1442 
1443   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1444   {
1445     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1446   }
1447 
1448   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1449   {
1450     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1451   }
1452   WRITE_REG(TAMP->CR2, tmpreg);
1453 
1454   /* Filter control register */
1455   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp);
1456 
1457   /* Timestamp on tamper */
1458   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1459   {
1460     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1461     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1462     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1463   }
1464 
1465   /* Control register 1 */
1466   SET_BIT(TAMP->CR1, sTamper->Tamper);
1467 
1468   return HAL_OK;
1469 }
1470 
1471 
1472 /**
1473   * @brief  Set Tamper in IT mode
1474   * @param  hrtc RTC handle
1475   * @param  sTamper Pointer to Tamper Structure.
1476   * @retval HAL status
1477   */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1478 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
1479 {
1480   uint32_t tmpreg;
1481 
1482   /* Prevent unused argument(s) compilation warning */
1483   UNUSED(hrtc);
1484 
1485   /* Check the parameters */
1486   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1487   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1488   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1489   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1490   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1491   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1492   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1493   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1494   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1495 
1496   /* Configuration register 2 */
1497   tmpreg = READ_REG(TAMP->CR2);
1498   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1499 
1500   if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1501   {
1502     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1503   }
1504 
1505   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1506   {
1507     /* Feature only supported by TAMPER 1, 2 and 3 */
1508     if (sTamper->Tamper <= RTC_TAMPER_3)
1509     {
1510       tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1511     }
1512     else
1513     {
1514       return HAL_ERROR;
1515     }
1516   }
1517 
1518   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1519   {
1520     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1521   }
1522   WRITE_REG(TAMP->CR2, tmpreg);
1523 
1524   /* Filter control register */
1525   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp);
1526 
1527   /* Timestamp on tamper */
1528   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1529   {
1530     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1531     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1532     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1533   }
1534 
1535   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1536   __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1537 
1538   /* Interrupt enable register */
1539   SET_BIT(TAMP->IER, sTamper->Tamper);
1540 
1541   /* Control register 1 */
1542   SET_BIT(TAMP->CR1, sTamper->Tamper);
1543 
1544   return HAL_OK;
1545 }
1546 
1547 /**
1548   * @brief  Deactivate Tamper.
1549   * @param  hrtc RTC handle
1550   * @param  Tamper Selected tamper pin.
1551   *         This parameter can be a combination of the following values:
1552   *         @arg RTC_TAMPER_1
1553   *         @arg RTC_TAMPER_2
1554   *         @arg RTC_TAMPER_3
1555   *         @arg RTC_TAMPER_4
1556   *         @arg RTC_TAMPER_5
1557   *         @arg RTC_TAMPER_6
1558   *         @arg RTC_TAMPER_7
1559   *         @arg RTC_TAMPER_8
1560   * @retval HAL status
1561   */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)1562 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1563 {
1564   UNUSED(hrtc);
1565   assert_param(IS_RTC_TAMPER(Tamper));
1566 
1567   /* Disable the selected Tamper pin */
1568   CLEAR_BIT(TAMP->CR1, Tamper);
1569 
1570   /* Clear tamper mask/noerase/trigger configuration */
1571   CLEAR_BIT(TAMP->CR2, (Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | (Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1572 
1573   /* Clear tamper interrupt mode configuration */
1574   CLEAR_BIT(TAMP->IER, Tamper);
1575 
1576   /* Clear tamper interrupt and event flags (WO register) */
1577   WRITE_REG(TAMP->SCR, Tamper);
1578 
1579   return HAL_OK;
1580 }
1581 
1582 /**
1583   * @brief  Set all active Tampers at the same time.
1584   * @param  hrtc RTC handle
1585   * @param  sAllTamper Pointer to active Tamper Structure.
1586   * @retval HAL status
1587   */
HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef * hrtc,RTC_ActiveTampersTypeDef * sAllTamper)1588 HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, RTC_ActiveTampersTypeDef *sAllTamper)
1589 {
1590   uint32_t IER, CR1, CR2, ATCR1, CR, i, tickstart;
1591 
1592 #ifdef  USE_FULL_ASSERT
1593   for (i = 0; i < RTC_TAMP_NB; i++)
1594   {
1595     assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
1596     assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
1597     /* Mask flag only supported by TAMPER 1, 2 and 3 */
1598     assert_param(!((sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (i > RTC_TAMPER_3)));
1599   }
1600   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
1601 #endif /* #ifdef  USE_FULL_ASSERT */
1602 
1603   /* Active Tampers must not be already enabled */
1604   if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
1605   {
1606     /* Disable all actives tampers with HAL_RTCEx_DeactivateActiveTampers and try again */
1607     return HAL_ERROR;
1608   }
1609 
1610   /* Set TimeStamp on tamper detection */
1611   CR = READ_REG(RTC->CR);
1612   if ((CR & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
1613   {
1614     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1615     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
1616     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1617   }
1618 
1619   CR1 = READ_REG(TAMP->CR1);
1620   CR2 = READ_REG(TAMP->CR2);
1621   IER = READ_REG(TAMP->IER);
1622 
1623   /* Set common parameters */
1624   ATCR1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) | sAllTamper->ActiveAsyncPrescaler);
1625 
1626   /* Set specific parameters for each active tamper inputs if enable */
1627   for (i = 0; i < RTC_TAMP_NB; i++)
1628   {
1629     if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
1630     {
1631       CR1 |= (TAMP_CR1_TAMP1E << i);
1632       ATCR1 |= (TAMP_ATCR1_TAMP1AM << i);
1633 
1634       if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
1635       {
1636         /* RTC Tamper Interrupt Configuration: EXTI configuration */
1637         __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1638 
1639         /* Interrupt enable register */
1640         IER |= (TAMP_IER_TAMP1IE << i);
1641       }
1642 
1643       if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1644       {
1645         CR2 |= (TAMP_CR2_TAMP1MSK << i);
1646       }
1647 
1648       if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1649       {
1650         CR2 |= (TAMP_CR2_TAMP1NOERASE << i);
1651       }
1652 
1653       if (i != sAllTamper->TampInput[i].Output)
1654       {
1655         ATCR1 |= TAMP_ATCR1_ATOSHARE;
1656       }
1657     }
1658   }
1659 
1660   WRITE_REG(TAMP->IER, IER);
1661   WRITE_REG(TAMP->IER, IER);
1662   WRITE_REG(TAMP->ATCR1, ATCR1);
1663   WRITE_REG(TAMP->CR2, CR2);
1664   WRITE_REG(TAMP->CR1, CR1);
1665 
1666   /* Write seed */
1667   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1668   {
1669     WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
1670   }
1671 
1672   /* Wait till RTC SEEDF flag is set and if Time out is reached exit */
1673   tickstart = HAL_GetTick();
1674   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0u)
1675   {
1676     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1677     {
1678       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1679       return HAL_TIMEOUT;
1680     }
1681   }
1682 
1683   return HAL_OK;
1684 }
1685 
1686 /**
1687   * @brief  Write a new seed. Active tamper must be enabled.
1688   * @param  hrtc RTC handle
1689   * @param  pSeed Pointer to active tamper seed values.
1690   * @retval HAL status
1691   */
HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef * hrtc,uint32_t * pSeed)1692 HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, uint32_t *pSeed)
1693 {
1694   uint32_t i, tickstart;
1695 
1696   /* Active Tampers must be enabled */
1697   if (READ_BIT(TAMP->ATOR,  TAMP_ATOR_INITS) == 0U)
1698   {
1699     return HAL_ERROR;
1700   }
1701 
1702   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1703   {
1704     WRITE_REG(TAMP->ATSEEDR, pSeed[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  Deactivate all Active Tampers at the same time.
1723   * @param  hrtc RTC handle
1724   * @retval HAL status
1725   */
HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef * hrtc)1726 HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef *hrtc)
1727 {
1728   /* Get Active tampers */
1729   uint32_t ATamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
1730 
1731   UNUSED(hrtc);
1732   /* Disable all actives tampers but not passives tampers */
1733   CLEAR_BIT(TAMP->CR1, ATamp_mask);
1734   /* Disable no erase and mask */
1735   CLEAR_BIT(TAMP->CR2, (ATamp_mask | ((ATamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM)) << TAMP_CR2_TAMP1MSK_Pos)));
1736 
1737   /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
1738   WRITE_REG(TAMP->SCR, ATamp_mask);
1739 
1740   /* Clear all active tampers interrupt mode configuration but not passives tampers */
1741   CLEAR_BIT(TAMP->IER, ATamp_mask);
1742 
1743   CLEAR_BIT(TAMP->ATCR1, TAMP_ALL | TAMP_ATCR1_ATCKSEL | TAMP_ATCR1_ATPER | \
1744             TAMP_ATCR1_ATOSHARE | TAMP_ATCR1_FLTEN);
1745 
1746   return HAL_OK;
1747 }
1748 
1749 
1750 /**
1751   * @brief  Tamper event polling.
1752   * @param  hrtc RTC handle
1753   * @param  Tamper Selected tamper pin.
1754   *         This parameter can be a combination of the following values:
1755   *         @arg RTC_TAMPER_1
1756   *         @arg RTC_TAMPER_2
1757   *         @arg RTC_TAMPER_3
1758   *         @arg RTC_TAMPER_4
1759   *         @arg RTC_TAMPER_5
1760   *         @arg RTC_TAMPER_6
1761   *         @arg RTC_TAMPER_7
1762   *         @arg RTC_TAMPER_8
1763   * @param  Timeout Timeout duration
1764   * @retval HAL status
1765   */
HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t Tamper,uint32_t Timeout)1766 HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
1767 {
1768   UNUSED(hrtc);
1769   assert_param(IS_RTC_TAMPER(Tamper));
1770 
1771   uint32_t tickstart = HAL_GetTick();
1772 
1773   /* Get the status of the Interrupt */
1774   while (READ_BIT(TAMP->SR, Tamper) != Tamper)
1775   {
1776     if (Timeout != HAL_MAX_DELAY)
1777     {
1778       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1779       {
1780         return HAL_TIMEOUT;
1781       }
1782     }
1783   }
1784 
1785   /* Clear the Tamper Flag */
1786   WRITE_REG(TAMP->SCR, Tamper);
1787 
1788   return HAL_OK;
1789 }
1790 
1791 
1792 /**
1793   * @brief  Set Internal Tamper in interrupt mode
1794   * @param  hrtc RTC handle
1795   * @param  sIntTamper Pointer to Internal Tamper Structure.
1796   * @retval HAL status
1797   */
HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef * hrtc,RTC_InternalTamperTypeDef * sIntTamper)1798 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
1799 {
1800   /* Prevent unused argument(s) compilation warning */
1801   UNUSED(hrtc);
1802 
1803   /* Check the parameters */
1804   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1805   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1806   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
1807 
1808   /* timestamp on internal tamper */
1809   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1810   {
1811     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1812     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1813     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1814   }
1815 
1816   /* Control register 1 */
1817   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1818 
1819   return HAL_OK;
1820 }
1821 
1822 
1823 /**
1824   * @brief  Set Internal Tamper
1825   * @param  hrtc RTC handle
1826   * @param  sIntTamper Pointer to Internal Tamper Structure.
1827   * @retval HAL status
1828   */
HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef * hrtc,RTC_InternalTamperTypeDef * sIntTamper)1829 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
1830 {
1831   /* Prevent unused argument(s) compilation warning */
1832   UNUSED(hrtc);
1833 
1834   /* Check the parameters */
1835   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1836   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1837   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
1838 
1839   /* timestamp on internal tamper */
1840   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1841   {
1842     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1843     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1844     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1845   }
1846 
1847   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1848   __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1849 
1850   /* Interrupt enable register */
1851   SET_BIT(TAMP->IER, sIntTamper->IntTamper);
1852 
1853   /* Control register 1 */
1854   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1855 
1856   return HAL_OK;
1857 }
1858 
1859 /**
1860   * @brief  Deactivate Internal Tamper.
1861   * @param  hrtc RTC handle
1862   * @param  IntTamper Selected internal tamper event.
1863   *          This parameter can be any combination of existing internal tampers.
1864   * @retval HAL status
1865   */
HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef * hrtc,uint32_t IntTamper)1866 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
1867 {
1868   UNUSED(hrtc);
1869   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1870 
1871   /* Disable the selected Tamper pin */
1872   CLEAR_BIT(TAMP->CR1, IntTamper);
1873 
1874   /* Clear internal tamper interrupt mode configuration */
1875   CLEAR_BIT(TAMP->IER, IntTamper);
1876 
1877   /* Clear internal tamper interrupt */
1878   WRITE_REG(TAMP->SCR, IntTamper);
1879 
1880   return HAL_OK;
1881 }
1882 
1883 
1884 /**
1885   * @brief  Internal Tamper event polling.
1886   * @param  hrtc RTC handle
1887   * @param  IntTamper selected tamper.
1888   *          This parameter can be any combination of existing internal tampers.
1889   * @param  Timeout Timeout duration
1890   * @retval HAL status
1891   */
HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t IntTamper,uint32_t Timeout)1892 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
1893 {
1894   UNUSED(hrtc);
1895   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1896 
1897   uint32_t tickstart = HAL_GetTick();
1898 
1899   /* Get the status of the Interrupt */
1900   while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
1901   {
1902     if (Timeout != HAL_MAX_DELAY)
1903     {
1904       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1905       {
1906         return HAL_TIMEOUT;
1907       }
1908     }
1909   }
1910 
1911   /* Clear the Tamper Flag */
1912   WRITE_REG(TAMP->SCR, IntTamper);
1913 
1914   return HAL_OK;
1915 }
1916 
1917 
1918 #if defined (CORTEX_IN_SECURE_STATE)
1919 /**
1920   * @brief  Handle Tamper secure interrupt request.
1921   * @param  hrtc RTC handle
1922   * @retval None
1923   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)1924 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
1925 {
1926   uint32_t tmp;
1927 
1928   /* Get secure interrupt status */
1929   tmp = READ_REG(TAMP->SMISR);
1930 
1931   /* Immediatly clear flags */
1932   WRITE_REG(TAMP->SCR, tmp);
1933 
1934   /* Check Tamper1 status */
1935   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
1936   {
1937 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1938     /* Call Tamper 1 Event registered secure Callback */
1939     hrtc->Tamper1EventCallback(hrtc);
1940 #else
1941     /* Tamper1 secure callback */
1942     HAL_RTCEx_Tamper1EventCallback(hrtc);
1943 #endif
1944   }
1945 
1946   /* Check Tamper2 status */
1947   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
1948   {
1949 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1950     /* Call Tamper 2 Event registered secure Callback */
1951     hrtc->Tamper2EventCallback(hrtc);
1952 #else
1953     /* Tamper2 secure callback */
1954     HAL_RTCEx_Tamper2EventCallback(hrtc);
1955 #endif
1956   }
1957 
1958   /* Check Tamper3 status */
1959   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
1960   {
1961 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1962     /* Call Tamper 3 Event registered secure Callback */
1963     hrtc->Tamper3EventCallback(hrtc);
1964 #else
1965     /* Tamper3 secure callback */
1966     HAL_RTCEx_Tamper3EventCallback(hrtc);
1967 #endif
1968   }
1969 
1970   /* Check Internal Tamper1 status */
1971   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
1972   {
1973 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1974     /* Call Internal Tamper 1 Event registered secure Callback */
1975     hrtc->InternalTamper1EventCallback(hrtc);
1976 #else
1977     /* Internal Tamper1 secure callback */
1978     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
1979 #endif
1980   }
1981 
1982   /* Check Internal Tamper2 status */
1983   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
1984   {
1985 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1986     /* Call Internal Tamper 2 Event registered secure Callback */
1987     hrtc->InternalTamper2EventCallback(hrtc);
1988 #else
1989     /* Internal Tamper2 secure callback */
1990     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
1991 #endif
1992   }
1993 
1994   /* Check Internal Tamper3 status */
1995   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
1996   {
1997 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1998     /* Call Internal Tamper 3 Event registered secure Callback */
1999     hrtc->InternalTamper3EventCallback(hrtc);
2000 #else
2001     /* Internal Tamper3 secure callback */
2002     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2003 #endif
2004   }
2005 
2006   /* Check Internal Tamper4 status */
2007   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
2008   {
2009 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2010     /* Call Internal Tamper 4 Event registered Callback */
2011     hrtc->InternalTamper4EventCallback(hrtc);
2012 #else
2013     /* Internal Tamper4 callback */
2014     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
2015 #endif
2016   }
2017 
2018   /* Check Internal Tamper5 status */
2019   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2020   {
2021 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2022     /* Call Internal Tamper 5 Event registered secure Callback */
2023     hrtc->InternalTamper5EventCallback(hrtc);
2024 #else
2025     /* Internal Tamper5 secure callback */
2026     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2027 #endif
2028   }
2029 
2030   /* Check Internal Tamper8 status */
2031   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2032   {
2033 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2034     /* Call Internal Tamper 8 Event registered secure Callback */
2035     hrtc->InternalTamper8EventCallback(hrtc);
2036 #else
2037     /* Internal Tamper8 secure callback */
2038     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2039 #endif
2040   }
2041 }
2042 
2043 #else /* #if defined (CORTEX_IN_SECURE_STATE) */
2044 
2045 /**
2046   * @brief  Handle Tamper non-secure interrupt request.
2047   * @param  hrtc RTC handle
2048   * @retval None
2049   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)2050 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
2051 {
2052   /* Get interrupt status */
2053   uint32_t tmp = READ_REG(TAMP->MISR);
2054 
2055   /* Immediately clear flags */
2056   WRITE_REG(TAMP->SCR, tmp);
2057 
2058   /* Check Tamper1 status */
2059   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
2060   {
2061 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2062     /* Call Tamper 1 Event registered Callback */
2063     hrtc->Tamper1EventCallback(hrtc);
2064 #else
2065     /* Tamper1 callback */
2066     HAL_RTCEx_Tamper1EventCallback(hrtc);
2067 #endif
2068   }
2069 
2070   /* Check Tamper2 status */
2071   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
2072   {
2073 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2074     /* Call Tamper 2 Event registered Callback */
2075     hrtc->Tamper2EventCallback(hrtc);
2076 #else
2077     /* Tamper2 callback */
2078     HAL_RTCEx_Tamper2EventCallback(hrtc);
2079 #endif
2080   }
2081 
2082   /* Check Tamper3 status */
2083   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
2084   {
2085 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2086     /* Call Tamper 3 Event registered Callback */
2087     hrtc->Tamper3EventCallback(hrtc);
2088 #else
2089     /* Tamper3 callback */
2090     HAL_RTCEx_Tamper3EventCallback(hrtc);
2091 #endif
2092   }
2093 
2094   /* Check Internal Tamper1 status */
2095   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
2096   {
2097 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2098     /* Call Internal Tamper 1 Event registered Callback */
2099     hrtc->InternalTamper1EventCallback(hrtc);
2100 #else
2101     /* Internal Tamper1 callback */
2102     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
2103 #endif
2104   }
2105 
2106   /* Check Internal Tamper2 status */
2107   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
2108   {
2109 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2110     /* Call Internal Tamper 2 Event registered Callback */
2111     hrtc->InternalTamper2EventCallback(hrtc);
2112 #else
2113     /* Internal Tamper2 callback */
2114     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
2115 #endif
2116   }
2117 
2118   /* Check Internal Tamper3 status */
2119   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
2120   {
2121 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2122     /* Call Internal Tamper 3 Event registered Callback */
2123     hrtc->InternalTamper3EventCallback(hrtc);
2124 #else
2125     /* Internal Tamper3 callback */
2126     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2127 #endif
2128   }
2129 
2130   /* Check Internal Tamper4 status */
2131   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
2132   {
2133 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2134     /* Call Internal Tamper 4 Event registered Callback */
2135     hrtc->InternalTamper4EventCallback(hrtc);
2136 #else
2137     /* Internal Tamper4 callback */
2138     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
2139 #endif
2140   }
2141 
2142   /* Check Internal Tamper5 status */
2143   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2144   {
2145 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2146     /* Call Internal Tamper 5 Event registered Callback */
2147     hrtc->InternalTamper5EventCallback(hrtc);
2148 #else
2149     /* Internal Tamper5 callback */
2150     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2151 #endif
2152   }
2153 
2154   /* Check Internal Tamper8 status */
2155   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2156   {
2157 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2158     /* Call Internal Tamper 8 Event registered Callback */
2159     hrtc->InternalTamper8EventCallback(hrtc);
2160 #else
2161     /* Internal Tamper8 callback */
2162     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2163 #endif
2164   }
2165 }
2166 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
2167 
2168 /**
2169   * @brief  Tamper 1 callback.
2170   * @param  hrtc RTC handle
2171   * @retval None
2172   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)2173 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
2174 {
2175   /* Prevent unused argument(s) compilation warning */
2176   UNUSED(hrtc);
2177 
2178   /* NOTE : This function should not be modified, when the callback is needed,
2179             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
2180    */
2181 }
2182 
2183 /**
2184   * @brief  Tamper 2 callback.
2185   * @param  hrtc RTC handle
2186   * @retval None
2187   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)2188 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
2189 {
2190   /* Prevent unused argument(s) compilation warning */
2191   UNUSED(hrtc);
2192 
2193   /* NOTE : This function should not be modified, when the callback is needed,
2194             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
2195    */
2196 }
2197 
2198 /**
2199   * @brief  Tamper 3 callback.
2200   * @param  hrtc RTC handle
2201   * @retval None
2202   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)2203 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
2204 {
2205   /* Prevent unused argument(s) compilation warning */
2206   UNUSED(hrtc);
2207 
2208   /* NOTE : This function should not be modified, when the callback is needed,
2209             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
2210    */
2211 }
2212 
2213 
2214 /**
2215   * @brief  Internal Tamper 1 callback.
2216   * @param  hrtc RTC handle
2217   * @retval None
2218   */
HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef * hrtc)2219 __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
2220 {
2221   /* Prevent unused argument(s) compilation warning */
2222   UNUSED(hrtc);
2223 
2224   /* NOTE : This function should not be modified, when the callback is needed,
2225             the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
2226    */
2227 }
2228 
2229 /**
2230   * @brief  Internal Tamper 2 callback.
2231   * @param  hrtc RTC handle
2232   * @retval None
2233   */
HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef * hrtc)2234 __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
2235 {
2236   /* Prevent unused argument(s) compilation warning */
2237   UNUSED(hrtc);
2238 
2239   /* NOTE : This function should not be modified, when the callback is needed,
2240             the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
2241    */
2242 }
2243 
2244 /**
2245   * @brief  Internal Tamper 3 callback.
2246   * @param  hrtc RTC handle
2247   * @retval None
2248   */
HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef * hrtc)2249 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
2250 {
2251   /* Prevent unused argument(s) compilation warning */
2252   UNUSED(hrtc);
2253 
2254   /* NOTE : This function should not be modified, when the callback is needed,
2255             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2256    */
2257 }
2258 
2259 /**
2260   * @brief  Internal Tamper 4 callback.
2261   * @param  hrtc RTC handle
2262   * @retval None
2263   */
HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef * hrtc)2264 __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
2265 {
2266   /* Prevent unused argument(s) compilation warning */
2267   UNUSED(hrtc);
2268 
2269   /* NOTE : This function should not be modified, when the callback is needed,
2270             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2271    */
2272 }
2273 
2274 /**
2275   * @brief  Internal Tamper 5 callback.
2276   * @param  hrtc RTC handle
2277   * @retval None
2278   */
HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef * hrtc)2279 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
2280 {
2281   /* Prevent unused argument(s) compilation warning */
2282   UNUSED(hrtc);
2283 
2284   /* NOTE : This function should not be modified, when the callback is needed,
2285             the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
2286    */
2287 }
2288 
2289 
2290 /**
2291   * @brief  Internal Tamper 8 callback.
2292   * @param  hrtc RTC handle
2293   * @retval None
2294   */
HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef * hrtc)2295 __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
2296 {
2297   /* Prevent unused argument(s) compilation warning */
2298   UNUSED(hrtc);
2299 
2300   /* NOTE : This function should not be modified, when the callback is needed,
2301             the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
2302    */
2303 }
2304 /**
2305   * @}
2306   */
2307 
2308 
2309 /** @addtogroup RTCEx_Exported_Functions_Group6
2310   * @brief      Extended RTC Backup register functions
2311   *
2312 @verbatim
2313   ===============================================================================
2314              ##### Extended RTC Backup register functions #####
2315   ===============================================================================
2316   [..]
2317    (+) Before calling any tamper or internal tamper function, you have to call first
2318        HAL_RTC_Init() function.
2319    (+) In that ine you can select to output tamper event on RTC pin.
2320   [..]
2321    This subsection provides functions allowing to
2322    (+) Write a data in a specified RTC Backup data register
2323    (+) Read a data in a specified RTC Backup data register
2324 @endverbatim
2325   * @{
2326   */
2327 
2328 
2329 /**
2330   * @brief  Write a data in a specified RTC Backup data register.
2331   * @param  hrtc RTC handle
2332   * @param  BackupRegister RTC Backup data Register number.
2333   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2334   * @param  Data Data to be written in the specified Backup data register.
2335   * @retval None
2336   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)2337 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2338 {
2339   uint32_t tmp;
2340 
2341   UNUSED(hrtc);
2342   /* Check the parameters */
2343   assert_param(IS_RTC_BKP(BackupRegister));
2344 
2345   tmp = (uint32_t) & (TAMP->BKP0R);
2346   tmp += (BackupRegister * 4U);
2347 
2348   /* Write the specified register */
2349   *(__IO uint32_t *)tmp = (uint32_t)Data;
2350 }
2351 
2352 
2353 /**
2354   * @brief  Reads data from the specified RTC Backup data Register.
2355   * @param  hrtc RTC handle
2356   * @param  BackupRegister RTC Backup data Register number.
2357   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2358   * @retval Read value
2359   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)2360 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2361 {
2362   uint32_t tmp;
2363 
2364   UNUSED(hrtc);
2365   /* Check the parameters */
2366   assert_param(IS_RTC_BKP(BackupRegister));
2367 
2368   tmp = (uint32_t) & (TAMP->BKP0R);
2369   tmp += (BackupRegister * 4U);
2370 
2371   /* Read the specified register */
2372   return (*(__IO uint32_t *)tmp);
2373 }
2374 
2375 /**
2376   * @}
2377   */
2378 
2379 
2380 /** @addtogroup RTCEx_Exported_Functions_Group7
2381   * @brief      Extended RTC security functions
2382   *
2383 @verbatim
2384   ===============================================================================
2385              ##### Extended RTC security functions #####
2386   ===============================================================================
2387   [..]
2388    (+) Before calling security function, you have to call first
2389        HAL_RTC_Init() function.
2390 @endverbatim
2391   * @{
2392   */
2393 
2394 /**
2395   * @brief  Get the security level of the RTC.
2396   *         To set the secure level please call HAL_RTCEx_SecureModeSet.
2397   * @param  hrtc RTC handle
2398   * @param  secureState  Secure state
2399   * @retval HAL_StatusTypeDef
2400   */
HAL_RTCEx_SecureModeGet(RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2401 HAL_StatusTypeDef HAL_RTCEx_SecureModeGet(RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef  *secureState)
2402 {
2403   UNUSED(hrtc);
2404   /* Read registers */
2405   uint32_t rtc_smcr = READ_REG(RTC->SMCR);
2406   uint32_t tamp_smcr = READ_REG(TAMP->SMCR);
2407 
2408   /* RTC */
2409   secureState->rtcSecureFull = READ_BIT(rtc_smcr, RTC_SMCR_DECPROT);
2410 
2411   /* Warning, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2412   secureState->rtcNonSecureFeatures = READ_BIT(rtc_smcr, RTC_NONSECURE_FEATURE_ALL);
2413 
2414   /* TAMP */
2415   secureState->tampSecureFull = READ_BIT(tamp_smcr, TAMP_SMCR_TAMPDPROT);
2416 
2417   /* Backup register start zones
2418      Warning : Backup register start zones are shared with privilege configuration */
2419   secureState->backupRegisterStartZone2 = READ_BIT(tamp_smcr, TAMP_SMCR_BKPRWDPROT) >> TAMP_SMCR_BKPRWDPROT_Pos;
2420   secureState->backupRegisterStartZone3 = READ_BIT(tamp_smcr, TAMP_SMCR_BKPWDPROT) >> TAMP_SMCR_BKPWDPROT_Pos;
2421 
2422   return HAL_OK;
2423 }
2424 
2425 
2426 #if defined (CORTEX_IN_SECURE_STATE)
2427 /**
2428   * @brief  Set the security level of the RTC/TAMP/Backup registers.
2429   *         To get the current security level call HAL_RTCEx_SecureModeGet.
2430   * @param  hrtc RTC handle
2431   * @param  secureState  Secure state
2432   * @retval HAL_StatusTypeDef
2433   */
HAL_RTCEx_SecureModeSet(RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2434 HAL_StatusTypeDef HAL_RTCEx_SecureModeSet(RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef  *secureState)
2435 {
2436   UNUSED(hrtc);
2437   assert_param(IS_RTC_SECURE_FULL(secureState->rtcSecureFull));
2438   assert_param(IS_RTC_NONSECURE_FEATURES(secureState->rtcNonSecureFeatures));
2439   assert_param(IS_TAMP_SECURE_FULL(secureState->tampSecureFull));
2440   assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone2));
2441   assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone3));
2442 
2443   /* RTC, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2444   WRITE_REG(RTC->SMCR, secureState->rtcSecureFull | secureState->rtcNonSecureFeatures);
2445 
2446   /* Tamper + Backup register
2447      Warning : Backup register start zone are Shared with privilege configuration */
2448   WRITE_REG(TAMP->SMCR,
2449             secureState->tampSecureFull |
2450             (TAMP_SMCR_BKPRWDPROT & (secureState->backupRegisterStartZone2 << TAMP_SMCR_BKPRWDPROT_Pos)) |
2451             (TAMP_SMCR_BKPWDPROT & (secureState->backupRegisterStartZone3 << TAMP_SMCR_BKPWDPROT_Pos)));
2452 
2453   return HAL_OK;
2454 }
2455 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
2456 
2457 /**
2458   * @}
2459   */
2460 
2461 /**
2462   * @}
2463   */
2464 #endif /* HAL_RTC_MODULE_ENABLED */
2465 
2466 /**
2467   * @}
2468   */
2469 
2470 
2471 /**
2472   * @}
2473   */
2474