1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_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 Timestamp functions
9   *           + RTC Tamper functions
10   *           + RTC Wakeup 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
38         HAL_RTCEx_SetWakeUpTimer() function.
39         You can also configure the RTC Wakeup timer in interrupt mode using the
40         HAL_RTCEx_SetWakeUpTimer_IT() function.
41     (+) To read the RTC Wakeup Counter register, use the HAL_RTCEx_GetWakeUpTimer()
42         function.
43 
44   *** Timestamp configuration ***
45   ===============================
46   [..]
47     (+) To configure the RTC Timestamp use the HAL_RTCEx_SetTimeStamp() function.
48         You can also configure the RTC Timestamp with interrupt mode using the
49         HAL_RTCEx_SetTimeStamp_IT() function.
50     (+) To read the RTC Timestamp Time and Date register, use the
51         HAL_RTCEx_GetTimeStamp() function.
52     (+) The Timestamp alternate function can be mapped either to RTC_AF1 (PC13),
53         RTC_AF2 (PI8), or RTC_AF3 (PC1) depending on the value of TSINSEL field
54         in RTC_OR register.
55         The corresponding pin is also selected by HAL_RTCEx_SetTimeStamp()
56         or HAL_RTCEx_SetTimeStamp_IT() functions.
57 
58   *** Internal Timestamp configuration ***
59   ===============================
60   [..]
61     (+) To Enable the RTC internal Timestamp use the HAL_RTCEx_SetInternalTimeStamp()
62         function.
63     (+) To read the RTC Timestamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
64         function.
65 
66   *** Tamper configuration ***
67   ============================
68   [..]
69     (+) To Enable the RTC Tamper and configure the Tamper filter count, trigger
70         Edge or Level according to the Tamper filter value (if equal to 0 Edge
71         else Level), sampling frequency, NoErase, MaskFlag, precharge or
72         discharge and Pull-UP use the HAL_RTCEx_SetTamper() function.
73         You can configure RTC Tamper in interrupt mode using HAL_RTCEx_SetTamper_IT()
74         function.
75     (+) The default configuration of the Tamper erases the backup registers.
76         To avoid this, enable the NoErase field on the RTC_TAMPCR register.
77     (+) The TAMPER1 alternate function is mapped to RTC_AF1 (PC13).
78     (+) The TAMPER2 alternate function is mapped to RTC_AF2 (PI8).
79     (+) The TAMPER3 alternate function is mapped to RTC_AF3 (PC1).
80 
81   *** Backup Data Registers configuration ***
82   ===========================================
83   [..]
84     (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
85         function.
86     (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
87         function.
88 
89   *** Smooth Digital Calibration configuration ***
90   ================================================
91   [..]
92     (+) RTC frequency can be digitally calibrated with a resolution of about
93         0.954 ppm with a range from -487.1 ppm to +488.5 ppm.
94         The correction of the frequency is performed using a series of small
95         adjustments (adding and/or subtracting individual RTCCLK pulses).
96     (+) The smooth digital calibration is performed during a cycle of about 2^20
97         RTCCLK pulses (or 32 seconds) when the input frequency is 32,768 Hz.
98         This cycle is maintained by a 20-bit counter clocked by RTCCLK.
99     (+) The smooth calibration register (RTC_CALR) specifies the number of RTCCLK
100         clock cycles to be masked during the 32-second cycle.
101     (+) The RTC Smooth Digital Calibration value and the corresponding calibration
102         cycle period (32s, 16s, or 8s) can be calibrated using the
103         HAL_RTCEx_SetSmoothCalib() function.
104 
105   @endverbatim
106   ******************************************************************************
107   */
108 
109 /* Includes ------------------------------------------------------------------*/
110 #include "stm32f7xx_hal.h"
111 
112 /** @addtogroup STM32F7xx_HAL_Driver
113   * @{
114   */
115 
116 /** @defgroup RTCEx RTCEx
117   * @brief    RTC Extended HAL module driver
118   * @{
119   */
120 
121 #ifdef HAL_RTC_MODULE_ENABLED
122 
123 /* Private typedef -----------------------------------------------------------*/
124 /* Private define ------------------------------------------------------------*/
125 /* Private macro -------------------------------------------------------------*/
126 /* Private variables ---------------------------------------------------------*/
127 /* Private function prototypes -----------------------------------------------*/
128 /* Exported functions --------------------------------------------------------*/
129 
130 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
131   * @{
132   */
133 
134 /** @defgroup RTCEx_Exported_Functions_Group1 RTC Timestamp and Tamper functions
135   * @brief    RTC Timestamp and Tamper functions
136   *
137 @verbatim
138  ===============================================================================
139                  ##### RTC Timestamp and Tamper functions #####
140  ===============================================================================
141 
142  [..] This section provides functions allowing to configure Timestamp feature
143 
144 @endverbatim
145   * @{
146   */
147 
148 /**
149   * @brief  Sets Timestamp.
150   * @note   This API must be called before enabling the Timestamp feature.
151   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
152   *                the configuration information for RTC.
153   * @param  RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
154   *         activated.
155   *          This parameter can be one of the following values:
156   *             @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
157   *                                        the rising edge of the related pin.
158   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
159   *                                        the falling edge of the related pin.
160   * @param  RTC_TimeStampPin Specifies the RTC Timestamp Pin.
161   *          This parameter can be one of the following values:
162   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
163   *             @arg RTC_TIMESTAMPPIN_POS1: PI8 is selected as RTC Timestamp Pin.
164   *             @arg RTC_TIMESTAMPPIN_POS2: PC1 is selected as RTC Timestamp Pin.
165   * @retval HAL status
166   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t RTC_TimeStampEdge,uint32_t RTC_TimeStampPin)167 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
168 {
169   uint32_t tmpreg = 0U;
170 
171   /* Check the parameters */
172   assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
173   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
174 
175   /* Process Locked */
176   __HAL_LOCK(hrtc);
177 
178   /* Change RTC state to BUSY */
179   hrtc->State = HAL_RTC_STATE_BUSY;
180 
181   hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;
182   hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin);
183 
184   /* Get the RTC_CR register and clear the bits to be configured */
185   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
186 
187   /* Configure the Timestamp TSEDGE bit */
188   tmpreg |= RTC_TimeStampEdge;
189 
190   /* Disable the write protection for RTC registers */
191   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
192 
193   /* Copy the desired configuration into the CR register */
194   hrtc->Instance->CR = (uint32_t)tmpreg;
195 
196   /* Clear RTC Timestamp flag */
197   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
198 
199   /* Clear RTC Timestamp overrun Flag */
200   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
201 
202   /* Enable the Timestamp saving */
203   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
204 
205   /* Enable the write protection for RTC registers */
206   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
207 
208   /* Change RTC state back to READY */
209   hrtc->State = HAL_RTC_STATE_READY;
210 
211   /* Process Unlocked */
212   __HAL_UNLOCK(hrtc);
213 
214   return HAL_OK;
215 }
216 
217 /**
218   * @brief  Sets Timestamp with Interrupt.
219   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
220   *                the configuration information for RTC.
221   * @note   This API must be called before enabling the Timestamp feature.
222   * @param  RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
223   *         activated.
224   *          This parameter can be one of the following values:
225   *             @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
226   *                                        the rising edge of the related pin.
227   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
228   *                                        the falling edge of the related pin.
229   * @param  RTC_TimeStampPin Specifies the RTC Timestamp Pin.
230   *          This parameter can be one of the following values:
231   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
232   *             @arg RTC_TIMESTAMPPIN_POS1: PI8 is selected as RTC Timestamp Pin.
233   *             @arg RTC_TIMESTAMPPIN_POS2: PC1 is selected as RTC Timestamp Pin.
234   * @retval HAL status
235   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t RTC_TimeStampEdge,uint32_t RTC_TimeStampPin)236 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
237 {
238   uint32_t tmpreg = 0U;
239 
240   /* Check the parameters */
241   assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
242   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
243 
244   /* Process Locked */
245   __HAL_LOCK(hrtc);
246 
247   /* Change RTC state to BUSY */
248   hrtc->State = HAL_RTC_STATE_BUSY;
249 
250   hrtc->Instance->OR &= (uint32_t)~RTC_OR_TSINSEL;
251   hrtc->Instance->OR |= (uint32_t)(RTC_TimeStampPin);
252 
253   /* Get the RTC_CR register and clear the bits to be configured */
254   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
255 
256   /* Configure the Timestamp TSEDGE bit */
257   tmpreg |= RTC_TimeStampEdge;
258 
259   /* Disable the write protection for RTC registers */
260   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
261 
262   /* Copy the desired configuration into the CR register */
263   hrtc->Instance->CR = (uint32_t)tmpreg;
264 
265   /* Clear RTC Timestamp flag */
266   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
267 
268   /* Clear RTC Timestamp overrun Flag */
269   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
270 
271   /* Enable the Timestamp saving */
272   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
273 
274   /* Enable IT Timestamp */
275   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
276 
277   /* Enable the write protection for RTC registers */
278   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
279 
280   /* RTC Timestamp Interrupt Configuration: EXTI configuration */
281   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
282   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
283 
284   /* Change RTC state back to READY */
285   hrtc->State = HAL_RTC_STATE_READY;
286 
287   /* Process Unlocked */
288   __HAL_UNLOCK(hrtc);
289 
290   return HAL_OK;
291 }
292 
293 /**
294   * @brief  Deactivates Timestamp.
295   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
296   *                the configuration information for RTC.
297   * @retval HAL status
298   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)299 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
300 {
301   uint32_t tmpreg = 0U;
302 
303   /* Process Locked */
304   __HAL_LOCK(hrtc);
305 
306   hrtc->State = HAL_RTC_STATE_BUSY;
307 
308   /* Disable the write protection for RTC registers */
309   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
310 
311   /* In case of interrupt mode is used, the interrupt source must disabled */
312   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
313 
314   /* Get the RTC_CR register and clear the bits to be configured */
315   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
316 
317   /* Configure the Timestamp TSEDGE and Enable bits */
318   hrtc->Instance->CR = (uint32_t)tmpreg;
319 
320   /* Enable the write protection for RTC registers */
321   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
322 
323   hrtc->State = HAL_RTC_STATE_READY;
324 
325   /* Process Unlocked */
326   __HAL_UNLOCK(hrtc);
327 
328   return HAL_OK;
329 }
330 
331 /**
332   * @brief  Sets Internal Timestamp.
333   * @note   This API must be called before enabling the internal Timestamp feature.
334   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
335   *                the configuration information for RTC.
336   * @retval HAL status
337   */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)338 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
339 {
340   /* Process Locked */
341   __HAL_LOCK(hrtc);
342 
343   hrtc->State = HAL_RTC_STATE_BUSY;
344 
345   /* Disable the write protection for RTC registers */
346   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
347 
348   /* Configure the internal Timestamp Enable bits */
349   __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
350 
351   /* Enable the write protection for RTC registers */
352   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
353 
354   /* Change RTC state */
355   hrtc->State = HAL_RTC_STATE_READY;
356 
357   /* Process Unlocked */
358   __HAL_UNLOCK(hrtc);
359 
360   return HAL_OK;
361 }
362 
363 /**
364   * @brief  Deactivates internal Timestamp.
365   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
366   *                the configuration information for RTC.
367   * @retval HAL status
368   */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)369 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
370 {
371   /* Process Locked */
372   __HAL_LOCK(hrtc);
373 
374   hrtc->State = HAL_RTC_STATE_BUSY;
375 
376   /* Disable the write protection for RTC registers */
377   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
378 
379   /* Configure the internal Timestamp Enable bits */
380   __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
381 
382   /* Enable the write protection for RTC registers */
383   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
384 
385   hrtc->State = HAL_RTC_STATE_READY;
386 
387   /* Process Unlocked */
388   __HAL_UNLOCK(hrtc);
389 
390   return HAL_OK;
391 }
392 
393 /**
394   * @brief  Gets the RTC Timestamp value.
395   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
396   *                the configuration information for RTC.
397   * @param  sTimeStamp Pointer to Time structure
398   * @param  sTimeStampDate Pointer to Date structure
399   * @param  Format specifies the format of the entered parameters.
400   *          This parameter can be one of the following values:
401   *             @arg RTC_FORMAT_BIN: Binary data format
402   *             @arg RTC_FORMAT_BCD: BCD data format
403   * @retval HAL status
404   */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)405 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
406 {
407   uint32_t tmptime = 0U;
408   uint32_t tmpdate = 0U;
409 
410   /* Check the parameters */
411   assert_param(IS_RTC_FORMAT(Format));
412 
413   /* Get the Timestamp time and date registers values */
414   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
415   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
416 
417   /* Fill the Time structure fields with the read parameters */
418   sTimeStamp->Hours      = (uint8_t)((tmptime & (RTC_TSTR_HT  | RTC_TSTR_HU))  >> RTC_TSTR_HU_Pos);
419   sTimeStamp->Minutes    = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
420   sTimeStamp->Seconds    = (uint8_t)((tmptime & (RTC_TSTR_ST  | RTC_TSTR_SU))  >> RTC_TSTR_SU_Pos);
421   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM))                 >> RTC_TSTR_PM_Pos);
422   sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
423 
424   /* Fill the Date structure fields with the read parameters */
425   sTimeStampDate->Year    = 0U;
426   sTimeStampDate->Month   = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
427   sTimeStampDate->Date    = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
428   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU))              >> RTC_TSDR_WDU_Pos);
429 
430   /* Check the input parameters format */
431   if (Format == RTC_FORMAT_BIN)
432   {
433     /* Convert the Timestamp structure parameters to Binary format */
434     sTimeStamp->Hours   = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
435     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
436     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
437 
438     /* Convert the DateTimeStamp structure parameters to Binary format */
439     sTimeStampDate->Month   = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
440     sTimeStampDate->Date    = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
441     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
442   }
443 
444   /* Clear the internal Timestamp Flag */
445   __HAL_RTC_INTERNAL_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_ITSF);
446 
447   /* Clear the Timestamp Flag */
448   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
449 
450   return HAL_OK;
451 }
452 
453 /**
454   * @brief  Sets Tamper.
455   * @note   By calling this API the tamper global interrupt will be disabled and
456   *         the selected tamper's interrupt as well.
457   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
458   *                the configuration information for RTC.
459   * @param  sTamper Pointer to Tamper Structure.
460   * @retval HAL status
461   */
HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)462 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
463 {
464   uint32_t tmpreg = 0U;
465 
466   /* Check the parameters */
467   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
468   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
469   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
470   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
471   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
472   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
473   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
474   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
475   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
476   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
477 
478   /* Process Locked */
479   __HAL_LOCK(hrtc);
480 
481   hrtc->State = HAL_RTC_STATE_BUSY;
482 
483   /* Copy control register into temporary variable */
484   tmpreg = hrtc->Instance->TAMPCR;
485 
486   /* Enable selected tamper */
487   tmpreg |= (sTamper->Tamper);
488 
489   /* Configure the tamper trigger bit (this bit is just on the right of the
490        tamper enable bit, hence the one-time right shift before updating it) */
491   if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
492   {
493     /* Set the tamper trigger bit (case of falling edge or high level) */
494     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
495   }
496   else
497   {
498     /* Clear the tamper trigger bit (case of rising edge or low level) */
499     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
500   }
501 
502   /* Configure the backup registers erasure enabling bits */
503   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
504   {
505     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
506     {
507       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
508     }
509     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
510     {
511       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
512     }
513     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
514     {
515       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
516     }
517   }
518   else
519   {
520     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
521     {
522       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
523     }
524     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
525     {
526       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
527     }
528     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
529     {
530       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
531     }
532   }
533 
534   /* Configure the tamper flags masking bits */
535   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
536   {
537     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
538     {
539       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
540     }
541     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
542     {
543       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
544     }
545     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
546     {
547       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
548     }
549   }
550   else
551   {
552     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
553     {
554       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
555     }
556     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
557     {
558       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
559     }
560     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
561     {
562       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
563     }
564   }
565 
566   /* Clear remaining fields before setting them */
567   tmpreg &= ~(RTC_TAMPERFILTER_MASK              | \
568               RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
569               RTC_TAMPERPRECHARGEDURATION_MASK   | \
570               RTC_TAMPER_PULLUP_MASK             | \
571               RTC_TIMESTAMPONTAMPERDETECTION_MASK);
572 
573   /* Set remaining parameters of desired configuration into temporary variable */
574   tmpreg |= ((uint32_t)sTamper->Filter            | \
575              (uint32_t)sTamper->SamplingFrequency | \
576              (uint32_t)sTamper->PrechargeDuration | \
577              (uint32_t)sTamper->TamperPullUp      | \
578              (uint32_t)sTamper->TimeStampOnTamperDetection);
579 
580   /* Disable interrupt on selected tamper in case it is enabled */
581   if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
582   {
583     tmpreg &= (uint32_t)~RTC_IT_TAMP1;
584   }
585   if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
586   {
587     tmpreg &= (uint32_t)~RTC_IT_TAMP2;
588   }
589   if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
590   {
591     tmpreg &= (uint32_t)~RTC_IT_TAMP3;
592   }
593 
594   /* Disable tamper global interrupt in case it is enabled */
595   tmpreg &= (uint32_t)~RTC_TAMPCR_TAMPIE;
596 
597   /* Copy desired configuration into configuration register */
598   hrtc->Instance->TAMPCR = tmpreg;
599 
600   hrtc->State = HAL_RTC_STATE_READY;
601 
602   /* Process Unlocked */
603   __HAL_UNLOCK(hrtc);
604 
605   return HAL_OK;
606 }
607 
608 /**
609   * @brief  Sets Tamper with interrupt.
610   * @note   By setting the tamper global interrupt bit, interrupts will be
611   *         enabled for all tampers.
612   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
613   *                the configuration information for RTC.
614   * @param  sTamper Pointer to RTC Tamper.
615   * @retval HAL status
616   */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)617 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
618 {
619   uint32_t tmpreg = 0U;
620 
621   /* Check the parameters */
622   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
623   assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
624   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
625   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
626   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
627   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
628   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
629   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
630   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
631   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
632   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
633 
634   /* Process Locked */
635   __HAL_LOCK(hrtc);
636 
637   hrtc->State = HAL_RTC_STATE_BUSY;
638 
639   /* Copy control register into temporary variable */
640   tmpreg = hrtc->Instance->TAMPCR;
641 
642   /* Enable selected tamper */
643   tmpreg |= (sTamper->Tamper);
644 
645   /* Configure the tamper trigger bit (this bit is just on the right of the
646        tamper enable bit, hence the one-time right shift before updating it) */
647   if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
648   {
649     /* Set the tamper trigger bit (case of falling edge or high level) */
650     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
651   }
652   else
653   {
654     /* Clear the tamper trigger bit (case of rising edge or low level) */
655     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
656   }
657 
658   /* Configure the backup registers erasure enabling bits */
659   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
660   {
661     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
662     {
663       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
664     }
665     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
666     {
667       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
668     }
669     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
670     {
671       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
672     }
673   }
674   else
675   {
676     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
677     {
678       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
679     }
680     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
681     {
682       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
683     }
684     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
685     {
686       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
687     }
688   }
689 
690   /* Configure the tamper flags masking bits */
691   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
692   {
693     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
694     {
695       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
696     }
697     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
698     {
699       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
700     }
701     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
702     {
703       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
704     }
705   }
706   else
707   {
708     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
709     {
710       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
711     }
712     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
713     {
714       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
715     }
716     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
717     {
718       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
719     }
720   }
721 
722   /* Clear remaining fields before setting them */
723   tmpreg &= ~(RTC_TAMPERFILTER_MASK              | \
724               RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
725               RTC_TAMPERPRECHARGEDURATION_MASK   | \
726               RTC_TAMPER_PULLUP_MASK             | \
727               RTC_TIMESTAMPONTAMPERDETECTION_MASK);
728 
729   /* Set remaining parameters of desired configuration into temporary variable */
730   tmpreg |= ((uint32_t)sTamper->Filter            | \
731              (uint32_t)sTamper->SamplingFrequency | \
732              (uint32_t)sTamper->PrechargeDuration | \
733              (uint32_t)sTamper->TamperPullUp      | \
734              (uint32_t)sTamper->TimeStampOnTamperDetection);
735 
736   /* Enable interrupt on selected tamper */
737   tmpreg |= (uint32_t)sTamper->Interrupt;
738 
739   /* Copy desired configuration into configuration register */
740   hrtc->Instance->TAMPCR = tmpreg;
741 
742   /* RTC Tamper Interrupt Configuration: EXTI configuration */
743   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
744   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
745 
746   hrtc->State = HAL_RTC_STATE_READY;
747 
748   /* Process Unlocked */
749   __HAL_UNLOCK(hrtc);
750 
751   return HAL_OK;
752 }
753 
754 /**
755   * @brief  Deactivates Tamper.
756   * @note   By calling this API the tamper global interrupt will be disabled.
757   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
758   *                the configuration information for RTC.
759   * @param  Tamper Selected tamper pin.
760   *          This parameter can be any combination of the following values:
761   *            @arg RTC_TAMPER_1:  Tamper 1
762   *            @arg RTC_TAMPER_2:  Tamper 2
763   *            @arg RTC_TAMPER_3:  Tamper 3
764   * @retval HAL status
765   */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)766 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
767 {
768   assert_param(IS_RTC_TAMPER(Tamper));
769 
770   /* Process Locked */
771   __HAL_LOCK(hrtc);
772 
773   hrtc->State = HAL_RTC_STATE_BUSY;
774 
775   /* Disable the selected Tamper pin */
776   hrtc->Instance->TAMPCR &= (uint32_t)~Tamper;
777 
778   if ((Tamper & RTC_TAMPER_1) != 0U)
779   {
780     /* Disable the Tamper 1 interrupt */
781     hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1);
782   }
783   if ((Tamper & RTC_TAMPER_2) != 0U)
784   {
785     /* Disable the Tamper 2 interrupt */
786     hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2);
787   }
788   if ((Tamper & RTC_TAMPER_3) != 0U)
789   {
790     /* Disable the Tamper 3 interrupt */
791     hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3);
792   }
793 
794   hrtc->State = HAL_RTC_STATE_READY;
795 
796   /* Process Unlocked */
797   __HAL_UNLOCK(hrtc);
798 
799   return HAL_OK;
800 }
801 
802 /**
803   * @brief  Handles Timestamp and Tamper interrupt request.
804   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
805   *                the configuration information for RTC.
806   * @retval None
807   */
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)808 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
809 {
810   /* Clear the EXTI's Flag for RTC Timestamp and Tamper */
811   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
812 
813   /* Get the Timestamp interrupt source enable status */
814   if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
815   {
816     /* Get the pending status of the Timestamp Interrupt */
817     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
818     {
819       /* Timestamp callback */
820 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
821       hrtc->TimeStampEventCallback(hrtc);
822 #else
823       HAL_RTCEx_TimeStampEventCallback(hrtc);
824 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
825 
826       /* Clear the Timestamp interrupt pending bit after returning from callback
827          as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
828       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
829     }
830   }
831 
832   /* Get the Tamper 1 interrupt source enable status */
833   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
834   {
835     /* Get the pending status of the Tamper 1 Interrupt */
836     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
837     {
838       /* Clear the Tamper interrupt pending bit */
839       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
840 
841       /* Tamper callback */
842 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
843       hrtc->Tamper1EventCallback(hrtc);
844 #else
845       HAL_RTCEx_Tamper1EventCallback(hrtc);
846 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
847     }
848   }
849 
850   /* Get the Tamper 2 interrupt source enable status */
851   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
852   {
853     /* Get the pending status of the Tamper 2 Interrupt */
854     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
855     {
856       /* Clear the Tamper interrupt pending bit */
857       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
858 
859       /* Tamper callback */
860 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
861       hrtc->Tamper2EventCallback(hrtc);
862 #else
863       HAL_RTCEx_Tamper2EventCallback(hrtc);
864 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
865     }
866   }
867 
868   /* Get the Tamper 3 interrupt source enable status */
869   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
870   {
871     /* Get the pending status of the Tamper 3 Interrupt */
872     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
873     {
874       /* Clear the Tamper interrupt pending bit */
875       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
876 
877       /* Tamper callback */
878 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
879       hrtc->Tamper3EventCallback(hrtc);
880 #else
881       HAL_RTCEx_Tamper3EventCallback(hrtc);
882 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
883     }
884   }
885 
886   /* Change RTC state */
887   hrtc->State = HAL_RTC_STATE_READY;
888 }
889 
890 /**
891   * @brief  Timestamp callback.
892   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
893   *                the configuration information for RTC.
894   * @retval None
895   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)896 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
897 {
898   /* Prevent unused argument(s) compilation warning */
899   UNUSED(hrtc);
900 
901   /* NOTE: This function should not be modified, when the callback is needed,
902            the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
903   */
904 }
905 
906 /**
907   * @brief  Tamper 1 callback.
908   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
909   *                the configuration information for RTC.
910   * @retval None
911   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)912 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
913 {
914   /* Prevent unused argument(s) compilation warning */
915   UNUSED(hrtc);
916 
917   /* NOTE: This function should not be modified, when the callback is needed,
918            the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
919    */
920 }
921 
922 /**
923   * @brief  Tamper 2 callback.
924   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
925   *                the configuration information for RTC.
926   * @retval None
927   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)928 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
929 {
930   /* Prevent unused argument(s) compilation warning */
931   UNUSED(hrtc);
932 
933   /* NOTE: This function should not be modified, when the callback is needed,
934            the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
935    */
936 }
937 
938 /**
939   * @brief  Tamper 3 callback.
940   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
941   *                the configuration information for RTC.
942   * @retval None
943   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)944 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
945 {
946   /* Prevent unused argument(s) compilation warning */
947   UNUSED(hrtc);
948 
949   /* NOTE: This function should not be modified, when the callback is needed,
950            the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
951    */
952 }
953 
954 /**
955   * @brief  Handles Timestamp polling request.
956   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
957   *                the configuration information for RTC.
958   * @param  Timeout Timeout duration
959   * @retval HAL status
960   */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)961 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
962 {
963   uint32_t tickstart = 0U;
964 
965   /* Get tick */
966   tickstart = HAL_GetTick();
967 
968   while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
969   {
970     if (Timeout != HAL_MAX_DELAY)
971     {
972       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
973       {
974         hrtc->State = HAL_RTC_STATE_TIMEOUT;
975         return HAL_TIMEOUT;
976       }
977     }
978 
979     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
980     {
981       /* Clear the Timestamp Overrun Flag */
982       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
983 
984       /* Change Timestamp state */
985       hrtc->State = HAL_RTC_STATE_ERROR;
986 
987       return HAL_ERROR;
988     }
989   }
990 
991   /* Change RTC state */
992   hrtc->State = HAL_RTC_STATE_READY;
993 
994   return HAL_OK;
995 }
996 
997 /**
998   * @brief  Handles Tamper 1 Polling.
999   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1000   *                the configuration information for RTC.
1001   * @param  Timeout Timeout duration
1002   * @retval HAL status
1003   */
HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1004 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1005 {
1006   uint32_t tickstart = 0U;
1007 
1008   /* Get tick */
1009   tickstart = HAL_GetTick();
1010 
1011   /* Get the status of the Interrupt */
1012   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
1013   {
1014     if (Timeout != HAL_MAX_DELAY)
1015     {
1016       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1017       {
1018         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1019         return HAL_TIMEOUT;
1020       }
1021     }
1022   }
1023 
1024   /* Clear the Tamper Flag */
1025   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
1026 
1027   /* Change RTC state */
1028   hrtc->State = HAL_RTC_STATE_READY;
1029 
1030   return HAL_OK;
1031 }
1032 
1033 /**
1034   * @brief  Handles Tamper 2 Polling.
1035   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1036   *                the configuration information for RTC.
1037   * @param  Timeout Timeout duration
1038   * @retval HAL status
1039   */
HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1040 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1041 {
1042   uint32_t tickstart = 0U;
1043 
1044   /* Get tick */
1045   tickstart = HAL_GetTick();
1046 
1047   /* Get the status of the Interrupt */
1048   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
1049   {
1050     if (Timeout != HAL_MAX_DELAY)
1051     {
1052       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1053       {
1054         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1055         return HAL_TIMEOUT;
1056       }
1057     }
1058   }
1059 
1060   /* Clear the Tamper Flag */
1061   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
1062 
1063   /* Change RTC state */
1064   hrtc->State = HAL_RTC_STATE_READY;
1065 
1066   return HAL_OK;
1067 }
1068 
1069 /**
1070   * @brief  Handles Tamper 3 Polling.
1071   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1072   *                the configuration information for RTC.
1073   * @param  Timeout Timeout duration
1074   * @retval HAL status
1075   */
HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1076 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1077 {
1078   uint32_t tickstart = HAL_GetTick();
1079 
1080   /* Get the status of the Interrupt */
1081   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
1082   {
1083     if (Timeout != HAL_MAX_DELAY)
1084     {
1085       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1086       {
1087         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1088         return HAL_TIMEOUT;
1089       }
1090     }
1091   }
1092 
1093   /* Clear the Tamper Flag */
1094   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
1095 
1096   /* Change RTC state */
1097   hrtc->State = HAL_RTC_STATE_READY;
1098 
1099   return HAL_OK;
1100 }
1101 
1102 /**
1103   * @}
1104   */
1105 
1106 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wakeup functions
1107   * @brief    RTC Wakeup functions
1108   *
1109 @verbatim
1110  ===============================================================================
1111                         ##### RTC Wakeup functions #####
1112  ===============================================================================
1113 
1114  [..] This section provides functions allowing to configure Wakeup feature
1115 
1116 @endverbatim
1117   * @{
1118   */
1119 
1120 /**
1121   * @brief  Sets wakeup timer.
1122   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1123   *                the configuration information for RTC.
1124   * @param  WakeUpCounter Wakeup counter
1125   * @param  WakeUpClock Wakeup clock
1126   * @retval HAL status
1127   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1128 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1129 {
1130   uint32_t tickstart = 0U;
1131 
1132   /* Check the parameters */
1133   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1134   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1135 
1136   /* Process Locked */
1137   __HAL_LOCK(hrtc);
1138 
1139   hrtc->State = HAL_RTC_STATE_BUSY;
1140 
1141   /* Disable the write protection for RTC registers */
1142   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1143 
1144   /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
1145   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
1146   {
1147     tickstart = HAL_GetTick();
1148 
1149     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
1150     while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
1151     {
1152       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1153       {
1154         /* Enable the write protection for RTC registers */
1155         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1156 
1157         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1158 
1159         /* Process Unlocked */
1160         __HAL_UNLOCK(hrtc);
1161 
1162         return HAL_TIMEOUT;
1163       }
1164     }
1165   }
1166 
1167   /* Disable the Wakeup timer */
1168   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1169 
1170   /* Clear the Wakeup flag */
1171   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1172 
1173   /* Get tick */
1174   tickstart = HAL_GetTick();
1175 
1176   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1177   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1178   {
1179     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1180     {
1181       /* Enable the write protection for RTC registers */
1182       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1183 
1184       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1185 
1186       /* Process Unlocked */
1187       __HAL_UNLOCK(hrtc);
1188 
1189       return HAL_TIMEOUT;
1190     }
1191   }
1192 
1193   /* Clear the Wakeup Timer clock source bits in CR register */
1194   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1195 
1196   /* Configure the clock source */
1197   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1198 
1199   /* Configure the Wakeup Timer counter */
1200   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1201 
1202   /* Enable the Wakeup Timer */
1203   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1204 
1205   /* Enable the write protection for RTC registers */
1206   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1207 
1208   hrtc->State = HAL_RTC_STATE_READY;
1209 
1210   /* Process Unlocked */
1211   __HAL_UNLOCK(hrtc);
1212 
1213   return HAL_OK;
1214 }
1215 
1216 /**
1217   * @brief  Sets wakeup timer with interrupt.
1218   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1219   *                the configuration information for RTC.
1220   * @param  WakeUpCounter Wakeup counter
1221   * @param  WakeUpClock Wakeup clock
1222   * @retval HAL status
1223   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1224 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1225 {
1226   __IO uint32_t count  = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1227 
1228   /* Check the parameters */
1229   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1230   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1231 
1232   /* Process Locked */
1233   __HAL_LOCK(hrtc);
1234 
1235   hrtc->State = HAL_RTC_STATE_BUSY;
1236 
1237   /* Disable the write protection for RTC registers */
1238   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1239 
1240   /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
1241   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
1242   {
1243     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
1244     do
1245     {
1246       if (count-- == 0U)
1247       {
1248         /* Enable the write protection for RTC registers */
1249         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1250 
1251         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1252 
1253         /* Process Unlocked */
1254         __HAL_UNLOCK(hrtc);
1255 
1256         return HAL_TIMEOUT;
1257       }
1258     } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
1259   }
1260 
1261   /* Disable the Wakeup timer */
1262   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1263 
1264   /* Clear the Wakeup flag */
1265   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1266 
1267   /* Reload the counter */
1268   count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1269 
1270   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1271   do
1272   {
1273     if (count-- == 0U)
1274     {
1275       /* Enable the write protection for RTC registers */
1276       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1277 
1278       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1279 
1280       /* Process Unlocked */
1281       __HAL_UNLOCK(hrtc);
1282 
1283       return HAL_TIMEOUT;
1284     }
1285   } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
1286 
1287   /* Clear the Wakeup Timer clock source bits in CR register */
1288   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1289 
1290   /* Configure the clock source */
1291   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1292 
1293   /* Configure the Wakeup Timer counter */
1294   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1295 
1296   /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
1297   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1298   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1299 
1300   /* Configure the interrupt in the RTC_CR register */
1301   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
1302 
1303   /* Enable the Wakeup Timer */
1304   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1305 
1306   /* Enable the write protection for RTC registers */
1307   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1308 
1309   hrtc->State = HAL_RTC_STATE_READY;
1310 
1311   /* Process Unlocked */
1312   __HAL_UNLOCK(hrtc);
1313 
1314   return HAL_OK;
1315 }
1316 
1317 /**
1318   * @brief  Deactivates wakeup timer counter.
1319   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1320   *                the configuration information for RTC.
1321   * @retval HAL status
1322   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)1323 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1324 {
1325   uint32_t tickstart = 0U;
1326 
1327   /* Process Locked */
1328   __HAL_LOCK(hrtc);
1329 
1330   hrtc->State = HAL_RTC_STATE_BUSY;
1331 
1332   /* Disable the write protection for RTC registers */
1333   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1334 
1335   /* Disable the Wakeup Timer */
1336   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1337 
1338   /* In case of interrupt mode is used, the interrupt source must disabled */
1339   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
1340 
1341   /* Get tick */
1342   tickstart = HAL_GetTick();
1343 
1344   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1345   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1346   {
1347     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1348     {
1349       /* Enable the write protection for RTC registers */
1350       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1351 
1352       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1353 
1354       /* Process Unlocked */
1355       __HAL_UNLOCK(hrtc);
1356 
1357       return HAL_TIMEOUT;
1358     }
1359   }
1360 
1361   /* Enable the write protection for RTC registers */
1362   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1363 
1364   hrtc->State = HAL_RTC_STATE_READY;
1365 
1366   /* Process Unlocked */
1367   __HAL_UNLOCK(hrtc);
1368 
1369   return HAL_OK;
1370 }
1371 
1372 /**
1373   * @brief  Gets wakeup timer counter.
1374   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1375   *                the configuration information for RTC.
1376   * @retval Counter value
1377   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)1378 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1379 {
1380   /* Get the counter value */
1381   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1382 }
1383 
1384 /**
1385   * @brief  Handles Wakeup Timer interrupt request.
1386   * @note   Unlike alarm interrupt line (shared by Alarms A and B) or tamper
1387   *         interrupt line (shared by timestamp and tampers) wakeup timer
1388   *         interrupt line is exclusive to the wakeup timer.
1389   *         There is no need in this case to check on the interrupt enable
1390   *         status via __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE().
1391   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1392   *                the configuration information for RTC.
1393   * @retval None
1394   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)1395 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1396 {
1397   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1398   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1399 
1400   /* Get the pending status of the Wakeup timer Interrupt */
1401   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1402   {
1403     /* Clear the Wakeup timer interrupt pending bit */
1404     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1405 
1406     /* Wakeup timer callback */
1407 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1408     hrtc->WakeUpTimerEventCallback(hrtc);
1409 #else
1410     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1411 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1412   }
1413 
1414   /* Change RTC state */
1415   hrtc->State = HAL_RTC_STATE_READY;
1416 }
1417 
1418 /**
1419   * @brief  Wakeup Timer callback.
1420   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1421   *                the configuration information for RTC.
1422   * @retval None
1423   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)1424 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1425 {
1426   /* Prevent unused argument(s) compilation warning */
1427   UNUSED(hrtc);
1428 
1429   /* NOTE: This function should not be modified, when the callback is needed,
1430            the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1431    */
1432 }
1433 
1434 /**
1435   * @brief  Handles Wakeup Timer Polling.
1436   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1437   *                the configuration information for RTC.
1438   * @param  Timeout Timeout duration
1439   * @retval HAL status
1440   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1441 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1442 {
1443   uint32_t tickstart = 0U;
1444 
1445   /* Get tick */
1446   tickstart = HAL_GetTick();
1447 
1448   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1449   {
1450     if (Timeout != HAL_MAX_DELAY)
1451     {
1452       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1453       {
1454         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1455         return HAL_TIMEOUT;
1456       }
1457     }
1458   }
1459 
1460   /* Clear the Wakeup timer Flag */
1461   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1462 
1463   /* Change RTC state */
1464   hrtc->State = HAL_RTC_STATE_READY;
1465 
1466   return HAL_OK;
1467 }
1468 
1469 /**
1470   * @}
1471   */
1472 
1473 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
1474   * @brief    Extended Peripheral Control functions
1475   *
1476 @verbatim
1477  ===============================================================================
1478               ##### Extended Peripheral Control functions #####
1479  ===============================================================================
1480     [..]
1481     This subsection provides functions allowing to
1482       (+) Write a data in a specified RTC Backup data register
1483       (+) Read a data in a specified RTC Backup data register
1484       (+) Set the Smooth calibration parameters.
1485       (+) Configure the Synchronization Shift Control Settings.
1486       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1487       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1488       (+) Enable the RTC reference clock detection.
1489       (+) Disable the RTC reference clock detection.
1490       (+) Enable the Bypass Shadow feature.
1491       (+) Disable the Bypass Shadow feature.
1492 
1493 @endverbatim
1494   * @{
1495   */
1496 
1497 /**
1498   * @brief  Writes a data in a specified RTC Backup data register.
1499   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1500   *                the configuration information for RTC.
1501   * @param  BackupRegister RTC Backup data Register number.
1502   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 31)
1503   *                                 to specify the register.
1504   * @param  Data Data to be written in the specified RTC Backup data register.
1505   * @retval None
1506   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)1507 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1508 {
1509   uint32_t tmp = 0U;
1510 
1511   /* Check the parameters */
1512   assert_param(IS_RTC_BKP(BackupRegister));
1513 
1514   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1515   tmp += (BackupRegister * 4U);
1516 
1517   /* Write the specified register */
1518   *(__IO uint32_t *)tmp = (uint32_t)Data;
1519 }
1520 
1521 /**
1522   * @brief  Reads data from the specified RTC Backup data Register.
1523   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1524   *                the configuration information for RTC.
1525   * @param  BackupRegister RTC Backup data Register number.
1526   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 31)
1527   *                                 to specify the register.
1528   * @retval Read value
1529   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)1530 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1531 {
1532   uint32_t tmp = 0U;
1533 
1534   /* Check the parameters */
1535   assert_param(IS_RTC_BKP(BackupRegister));
1536 
1537   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1538   tmp += (BackupRegister * 4U);
1539 
1540   /* Read the specified register */
1541   return (*(__IO uint32_t *)tmp);
1542 }
1543 
1544 /**
1545   * @brief  Sets the Smooth calibration parameters.
1546   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1547   *                the configuration information for RTC.
1548   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
1549   *          This parameter can be can be one of the following values:
1550   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
1551   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
1552   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
1553   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
1554   *          This parameter can be one of the following values:
1555   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
1556   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1557   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
1558   *          This parameter can be one any value from 0 to 0x000001FF.
1559   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1560   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1561   *         SmoothCalibMinusPulsesValue must be equal to 0.
1562   * @retval HAL status
1563   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)1564 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1565 {
1566   uint32_t tickstart = 0U;
1567 
1568   /* Check the parameters */
1569   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1570   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1571   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1572 
1573   /* Process Locked */
1574   __HAL_LOCK(hrtc);
1575 
1576   hrtc->State = HAL_RTC_STATE_BUSY;
1577 
1578   /* Disable the write protection for RTC registers */
1579   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1580 
1581   /* check if a calibration is pending*/
1582   if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1583   {
1584     /* Get tick */
1585     tickstart = HAL_GetTick();
1586 
1587     /* check if a calibration is pending*/
1588     while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1589     {
1590       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1591       {
1592         /* Enable the write protection for RTC registers */
1593         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1594 
1595         /* Change RTC state */
1596         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1597 
1598         /* Process Unlocked */
1599         __HAL_UNLOCK(hrtc);
1600 
1601         return HAL_TIMEOUT;
1602       }
1603     }
1604   }
1605 
1606   /* Configure the Smooth calibration settings */
1607   hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod     | \
1608                                     (uint32_t)SmoothCalibPlusPulses | \
1609                                     (uint32_t)SmoothCalibMinusPulsesValue);
1610 
1611   /* Enable the write protection for RTC registers */
1612   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1613 
1614   /* Change RTC state */
1615   hrtc->State = HAL_RTC_STATE_READY;
1616 
1617   /* Process Unlocked */
1618   __HAL_UNLOCK(hrtc);
1619 
1620   return HAL_OK;
1621 }
1622 
1623 /**
1624   * @brief  Configures the Synchronization Shift Control Settings.
1625   * @note   When REFCKON is set, firmware must not write to Shift control register.
1626   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1627   *                the configuration information for RTC.
1628   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
1629   *          This parameter can be one of the following values:
1630   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1631   *             @arg RTC_SHIFTADD1S_RESET: No effect.
1632   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
1633   *          This parameter can be one any value from 0 to 0x7FFF.
1634   * @retval HAL status
1635   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)1636 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1637 {
1638   uint32_t tickstart = 0U;
1639 
1640   /* Check the parameters */
1641   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1642   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1643 
1644   /* Process Locked */
1645   __HAL_LOCK(hrtc);
1646 
1647   hrtc->State = HAL_RTC_STATE_BUSY;
1648 
1649   /* Disable the write protection for RTC registers */
1650   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1651 
1652   /* Get tick */
1653   tickstart = HAL_GetTick();
1654 
1655   /* Wait until the shift is completed */
1656   while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
1657   {
1658     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1659     {
1660       /* Enable the write protection for RTC registers */
1661       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1662 
1663       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1664 
1665       /* Process Unlocked */
1666       __HAL_UNLOCK(hrtc);
1667 
1668       return HAL_TIMEOUT;
1669     }
1670   }
1671 
1672   /* Check if the reference clock detection is disabled */
1673   if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
1674   {
1675     /* Configure the Shift settings */
1676     hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1677 
1678     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1679     if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1680     {
1681       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1682       {
1683         /* Enable the write protection for RTC registers */
1684         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1685 
1686         hrtc->State = HAL_RTC_STATE_ERROR;
1687 
1688         /* Process Unlocked */
1689         __HAL_UNLOCK(hrtc);
1690 
1691         return HAL_ERROR;
1692       }
1693     }
1694   }
1695   else
1696   {
1697     /* Enable the write protection for RTC registers */
1698     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1699 
1700     /* Change RTC state */
1701     hrtc->State = HAL_RTC_STATE_ERROR;
1702 
1703     /* Process Unlocked */
1704     __HAL_UNLOCK(hrtc);
1705 
1706     return HAL_ERROR;
1707   }
1708 
1709   /* Enable the write protection for RTC registers */
1710   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1711 
1712   /* Change RTC state */
1713   hrtc->State = HAL_RTC_STATE_READY;
1714 
1715   /* Process Unlocked */
1716   __HAL_UNLOCK(hrtc);
1717 
1718   return HAL_OK;
1719 }
1720 
1721 /**
1722   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1723   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1724   *                the configuration information for RTC.
1725   * @param  CalibOutput Select the Calibration output Selection.
1726   *          This parameter can be one of the following values:
1727   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1728   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1729   * @retval HAL status
1730   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1731 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1732 {
1733   /* Check the parameters */
1734   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1735 
1736   /* Process Locked */
1737   __HAL_LOCK(hrtc);
1738 
1739   hrtc->State = HAL_RTC_STATE_BUSY;
1740 
1741   /* Disable the write protection for RTC registers */
1742   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1743 
1744   /* Clear flags before config */
1745   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1746 
1747   /* Configure the RTC_CR register */
1748   hrtc->Instance->CR |= (uint32_t)CalibOutput;
1749 
1750   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1751 
1752   /* Enable the write protection for RTC registers */
1753   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1754 
1755   /* Change RTC state */
1756   hrtc->State = HAL_RTC_STATE_READY;
1757 
1758   /* Process Unlocked */
1759   __HAL_UNLOCK(hrtc);
1760 
1761   return HAL_OK;
1762 }
1763 
1764 /**
1765   * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1766   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1767   *                the configuration information for RTC.
1768   * @retval HAL status
1769   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1770 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1771 {
1772   /* Process Locked */
1773   __HAL_LOCK(hrtc);
1774 
1775   hrtc->State = HAL_RTC_STATE_BUSY;
1776 
1777   /* Disable the write protection for RTC registers */
1778   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1779 
1780   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1781 
1782   /* Enable the write protection for RTC registers */
1783   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1784 
1785   /* Change RTC state */
1786   hrtc->State = HAL_RTC_STATE_READY;
1787 
1788   /* Process Unlocked */
1789   __HAL_UNLOCK(hrtc);
1790 
1791   return HAL_OK;
1792 }
1793 
1794 /**
1795   * @brief  Enables the RTC reference clock detection.
1796   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1797   *                the configuration information for RTC.
1798   * @retval HAL status
1799   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1800 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1801 {
1802   HAL_StatusTypeDef status;
1803 
1804   /* Process Locked */
1805   __HAL_LOCK(hrtc);
1806 
1807   hrtc->State = HAL_RTC_STATE_BUSY;
1808 
1809   /* Disable the write protection for RTC registers */
1810   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1811 
1812   /* Enter Initialization mode */
1813   status = RTC_EnterInitMode(hrtc);
1814 
1815   if (status == HAL_OK)
1816   {
1817     /* Enable the reference clock detection */
1818     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1819 
1820     /* Exit Initialization mode */
1821     status = RTC_ExitInitMode(hrtc);
1822   }
1823 
1824   if (status == HAL_OK)
1825   {
1826     hrtc->State = HAL_RTC_STATE_READY;
1827   }
1828 
1829   /* Enable the write protection for RTC registers */
1830   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1831 
1832   /* Process Unlocked */
1833   __HAL_UNLOCK(hrtc);
1834 
1835   return status;
1836 }
1837 
1838 /**
1839   * @brief  Disable the RTC reference clock detection.
1840   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1841   *                the configuration information for RTC.
1842   * @retval HAL status
1843   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1844 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1845 {
1846   HAL_StatusTypeDef status;
1847 
1848   /* Process Locked */
1849   __HAL_LOCK(hrtc);
1850 
1851   hrtc->State = HAL_RTC_STATE_BUSY;
1852 
1853   /* Disable the write protection for RTC registers */
1854   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1855 
1856   /* Enter Initialization mode */
1857   status = RTC_EnterInitMode(hrtc);
1858 
1859   if (status == HAL_OK)
1860   {
1861     /* Disable the reference clock detection */
1862     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1863 
1864     /* Exit Initialization mode */
1865     status = RTC_ExitInitMode(hrtc);
1866   }
1867 
1868   if (status == HAL_OK)
1869   {
1870     hrtc->State = HAL_RTC_STATE_READY;
1871   }
1872 
1873   /* Enable the write protection for RTC registers */
1874   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1875 
1876   /* Process Unlocked */
1877   __HAL_UNLOCK(hrtc);
1878 
1879   return status;
1880 }
1881 
1882 /**
1883   * @brief  Enables the Bypass Shadow feature.
1884   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1885   *                the configuration information for RTC.
1886   * @note   When the Bypass Shadow is enabled the calendar value are taken
1887   *         directly from the Calendar counter.
1888   * @retval HAL status
1889   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1890 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1891 {
1892   /* Process Locked */
1893   __HAL_LOCK(hrtc);
1894 
1895   hrtc->State = HAL_RTC_STATE_BUSY;
1896 
1897   /* Disable the write protection for RTC registers */
1898   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1899 
1900   /* Set the BYPSHAD bit */
1901   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1902 
1903   /* Enable the write protection for RTC registers */
1904   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1905 
1906   /* Change RTC state */
1907   hrtc->State = HAL_RTC_STATE_READY;
1908 
1909   /* Process Unlocked */
1910   __HAL_UNLOCK(hrtc);
1911 
1912   return HAL_OK;
1913 }
1914 
1915 /**
1916   * @brief  Disables the Bypass Shadow feature.
1917   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1918   *                the configuration information for RTC.
1919   * @note   When the Bypass Shadow is enabled the calendar value are taken
1920   *         directly from the Calendar counter.
1921   * @retval HAL status
1922   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1923 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1924 {
1925   /* Process Locked */
1926   __HAL_LOCK(hrtc);
1927 
1928   hrtc->State = HAL_RTC_STATE_BUSY;
1929 
1930   /* Disable the write protection for RTC registers */
1931   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1932 
1933   /* Reset the BYPSHAD bit */
1934   hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1935 
1936   /* Enable the write protection for RTC registers */
1937   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1938 
1939   /* Change RTC state */
1940   hrtc->State = HAL_RTC_STATE_READY;
1941 
1942   /* Process Unlocked */
1943   __HAL_UNLOCK(hrtc);
1944 
1945   return HAL_OK;
1946 }
1947 
1948 /**
1949   * @}
1950   */
1951 
1952 /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
1953   * @brief    Extended features functions
1954   *
1955 @verbatim
1956  ===============================================================================
1957                  ##### Extended features functions #####
1958  ===============================================================================
1959     [..]  This section provides functions allowing to:
1960       (+) RTC Alarm B callback
1961       (+) RTC Poll for Alarm B request
1962 
1963 @endverbatim
1964   * @{
1965   */
1966 
1967 /**
1968   * @brief  Alarm B callback.
1969   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1970   *                the configuration information for RTC.
1971   * @retval None
1972   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1973 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1974 {
1975   /* Prevent unused argument(s) compilation warning */
1976   UNUSED(hrtc);
1977 
1978   /* NOTE: This function should not be modified, when the callback is needed,
1979            the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1980    */
1981 }
1982 
1983 /**
1984   * @brief  Handles Alarm B Polling request.
1985   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1986   *                the configuration information for RTC.
1987   * @param  Timeout Timeout duration
1988   * @retval HAL status
1989   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1990 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1991 {
1992   uint32_t tickstart = 0U;
1993 
1994   /* Get tick */
1995   tickstart = HAL_GetTick();
1996 
1997   /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
1998   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
1999   {
2000     if (Timeout != HAL_MAX_DELAY)
2001     {
2002       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
2003       {
2004         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2005         return HAL_TIMEOUT;
2006       }
2007     }
2008   }
2009 
2010   /* Clear the Alarm flag */
2011   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2012 
2013   /* Change RTC state */
2014   hrtc->State = HAL_RTC_STATE_READY;
2015 
2016   return HAL_OK;
2017 }
2018 
2019 /**
2020   * @}
2021   */
2022 
2023 /**
2024   * @}
2025   */
2026 
2027 #endif /* HAL_RTC_MODULE_ENABLED */
2028 /**
2029   * @}
2030   */
2031 
2032 /**
2033   * @}
2034   */
2035