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