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