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       count = count - 1U;
1247       if (count == 0U)
1248       {
1249         /* Enable the write protection for RTC registers */
1250         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1251 
1252         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1253 
1254         /* Process Unlocked */
1255         __HAL_UNLOCK(hrtc);
1256 
1257         return HAL_TIMEOUT;
1258       }
1259     } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
1260   }
1261 
1262   /* Disable the Wakeup timer */
1263   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1264 
1265   /* Clear the Wakeup flag */
1266   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1267 
1268   /* Reload the counter */
1269   count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1270 
1271   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1272   do
1273   {
1274     count = count - 1U;
1275     if (count == 0U)
1276     {
1277       /* Enable the write protection for RTC registers */
1278       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1279 
1280       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1281 
1282       /* Process Unlocked */
1283       __HAL_UNLOCK(hrtc);
1284 
1285       return HAL_TIMEOUT;
1286     }
1287   } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
1288 
1289   /* Clear the Wakeup Timer clock source bits in CR register */
1290   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1291 
1292   /* Configure the clock source */
1293   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1294 
1295   /* Configure the Wakeup Timer counter */
1296   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1297 
1298   /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
1299   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1300   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1301 
1302   /* Configure the interrupt in the RTC_CR register */
1303   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
1304 
1305   /* Enable the Wakeup Timer */
1306   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1307 
1308   /* Enable the write protection for RTC registers */
1309   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1310 
1311   hrtc->State = HAL_RTC_STATE_READY;
1312 
1313   /* Process Unlocked */
1314   __HAL_UNLOCK(hrtc);
1315 
1316   return HAL_OK;
1317 }
1318 
1319 /**
1320   * @brief  Deactivates wakeup timer counter.
1321   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1322   *                the configuration information for RTC.
1323   * @retval HAL status
1324   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)1325 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1326 {
1327   uint32_t tickstart = 0U;
1328 
1329   /* Process Locked */
1330   __HAL_LOCK(hrtc);
1331 
1332   hrtc->State = HAL_RTC_STATE_BUSY;
1333 
1334   /* Disable the write protection for RTC registers */
1335   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1336 
1337   /* Disable the Wakeup Timer */
1338   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1339 
1340   /* In case of interrupt mode is used, the interrupt source must disabled */
1341   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
1342 
1343   /* Get tick */
1344   tickstart = HAL_GetTick();
1345 
1346   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1347   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1348   {
1349     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1350     {
1351       /* Enable the write protection for RTC registers */
1352       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1353 
1354       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1355 
1356       /* Process Unlocked */
1357       __HAL_UNLOCK(hrtc);
1358 
1359       return HAL_TIMEOUT;
1360     }
1361   }
1362 
1363   /* Enable the write protection for RTC registers */
1364   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1365 
1366   hrtc->State = HAL_RTC_STATE_READY;
1367 
1368   /* Process Unlocked */
1369   __HAL_UNLOCK(hrtc);
1370 
1371   return HAL_OK;
1372 }
1373 
1374 /**
1375   * @brief  Gets wakeup timer counter.
1376   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1377   *                the configuration information for RTC.
1378   * @retval Counter value
1379   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)1380 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1381 {
1382   /* Get the counter value */
1383   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1384 }
1385 
1386 /**
1387   * @brief  Handles Wakeup Timer interrupt request.
1388   * @note   Unlike alarm interrupt line (shared by Alarms A and B) or tamper
1389   *         interrupt line (shared by timestamp and tampers) wakeup timer
1390   *         interrupt line is exclusive to the wakeup timer.
1391   *         There is no need in this case to check on the interrupt enable
1392   *         status via __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE().
1393   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1394   *                the configuration information for RTC.
1395   * @retval None
1396   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)1397 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1398 {
1399   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1400   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1401 
1402   /* Get the pending status of the Wakeup timer Interrupt */
1403   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1404   {
1405     /* Clear the Wakeup timer interrupt pending bit */
1406     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1407 
1408     /* Wakeup timer callback */
1409 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1410     hrtc->WakeUpTimerEventCallback(hrtc);
1411 #else
1412     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1413 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1414   }
1415 
1416   /* Change RTC state */
1417   hrtc->State = HAL_RTC_STATE_READY;
1418 }
1419 
1420 /**
1421   * @brief  Wakeup Timer callback.
1422   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1423   *                the configuration information for RTC.
1424   * @retval None
1425   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)1426 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1427 {
1428   /* Prevent unused argument(s) compilation warning */
1429   UNUSED(hrtc);
1430 
1431   /* NOTE: This function should not be modified, when the callback is needed,
1432            the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1433    */
1434 }
1435 
1436 /**
1437   * @brief  Handles Wakeup Timer Polling.
1438   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1439   *                the configuration information for RTC.
1440   * @param  Timeout Timeout duration
1441   * @retval HAL status
1442   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1443 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1444 {
1445   uint32_t tickstart = 0U;
1446 
1447   /* Get tick */
1448   tickstart = HAL_GetTick();
1449 
1450   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1451   {
1452     if (Timeout != HAL_MAX_DELAY)
1453     {
1454       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1455       {
1456         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1457         return HAL_TIMEOUT;
1458       }
1459     }
1460   }
1461 
1462   /* Clear the Wakeup timer Flag */
1463   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1464 
1465   /* Change RTC state */
1466   hrtc->State = HAL_RTC_STATE_READY;
1467 
1468   return HAL_OK;
1469 }
1470 
1471 /**
1472   * @}
1473   */
1474 
1475 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
1476   * @brief    Extended Peripheral Control functions
1477   *
1478 @verbatim
1479  ===============================================================================
1480               ##### Extended Peripheral Control functions #####
1481  ===============================================================================
1482     [..]
1483     This subsection provides functions allowing to
1484       (+) Write a data in a specified RTC Backup data register
1485       (+) Read a data in a specified RTC Backup data register
1486       (+) Set the Smooth calibration parameters.
1487       (+) Configure the Synchronization Shift Control Settings.
1488       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1489       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1490       (+) Enable the RTC reference clock detection.
1491       (+) Disable the RTC reference clock detection.
1492       (+) Enable the Bypass Shadow feature.
1493       (+) Disable the Bypass Shadow feature.
1494 
1495 @endverbatim
1496   * @{
1497   */
1498 
1499 /**
1500   * @brief  Writes a data in a specified RTC Backup data register.
1501   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1502   *                the configuration information for RTC.
1503   * @param  BackupRegister RTC Backup data Register number.
1504   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 31)
1505   *                                 to specify the register.
1506   * @param  Data Data to be written in the specified RTC Backup data register.
1507   * @retval None
1508   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)1509 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1510 {
1511   uint32_t tmp = 0U;
1512 
1513   /* Check the parameters */
1514   assert_param(IS_RTC_BKP(BackupRegister));
1515 
1516   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1517   tmp += (BackupRegister * 4U);
1518 
1519   /* Write the specified register */
1520   *(__IO uint32_t *)tmp = (uint32_t)Data;
1521 }
1522 
1523 /**
1524   * @brief  Reads data from the specified RTC Backup data Register.
1525   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1526   *                the configuration information for RTC.
1527   * @param  BackupRegister RTC Backup data Register number.
1528   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 31)
1529   *                                 to specify the register.
1530   * @retval Read value
1531   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)1532 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1533 {
1534   uint32_t tmp = 0U;
1535 
1536   /* Check the parameters */
1537   assert_param(IS_RTC_BKP(BackupRegister));
1538 
1539   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1540   tmp += (BackupRegister * 4U);
1541 
1542   /* Read the specified register */
1543   return (*(__IO uint32_t *)tmp);
1544 }
1545 
1546 /**
1547   * @brief  Sets the Smooth calibration parameters.
1548   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1549   *                the configuration information for RTC.
1550   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
1551   *          This parameter can be can be one of the following values:
1552   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
1553   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
1554   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
1555   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
1556   *          This parameter can be one of the following values:
1557   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
1558   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1559   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
1560   *          This parameter can be one any value from 0 to 0x000001FF.
1561   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1562   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1563   *         SmoothCalibMinusPulsesValue must be equal to 0.
1564   * @retval HAL status
1565   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)1566 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1567 {
1568   uint32_t tickstart = 0U;
1569 
1570   /* Check the parameters */
1571   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1572   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1573   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1574 
1575   /* Process Locked */
1576   __HAL_LOCK(hrtc);
1577 
1578   hrtc->State = HAL_RTC_STATE_BUSY;
1579 
1580   /* Disable the write protection for RTC registers */
1581   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1582 
1583   /* check if a calibration is pending*/
1584   if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1585   {
1586     /* Get tick */
1587     tickstart = HAL_GetTick();
1588 
1589     /* check if a calibration is pending*/
1590     while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1591     {
1592       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1593       {
1594         /* Enable the write protection for RTC registers */
1595         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1596 
1597         /* Change RTC state */
1598         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1599 
1600         /* Process Unlocked */
1601         __HAL_UNLOCK(hrtc);
1602 
1603         return HAL_TIMEOUT;
1604       }
1605     }
1606   }
1607 
1608   /* Configure the Smooth calibration settings */
1609   hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod     | \
1610                                     (uint32_t)SmoothCalibPlusPulses | \
1611                                     (uint32_t)SmoothCalibMinusPulsesValue);
1612 
1613   /* Enable the write protection for RTC registers */
1614   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1615 
1616   /* Change RTC state */
1617   hrtc->State = HAL_RTC_STATE_READY;
1618 
1619   /* Process Unlocked */
1620   __HAL_UNLOCK(hrtc);
1621 
1622   return HAL_OK;
1623 }
1624 
1625 /**
1626   * @brief  Configures the Synchronization Shift Control Settings.
1627   * @note   When REFCKON is set, firmware must not write to Shift control register.
1628   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1629   *                the configuration information for RTC.
1630   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
1631   *          This parameter can be one of the following values:
1632   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1633   *             @arg RTC_SHIFTADD1S_RESET: No effect.
1634   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
1635   *          This parameter can be one any value from 0 to 0x7FFF.
1636   * @retval HAL status
1637   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)1638 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1639 {
1640   uint32_t tickstart = 0U;
1641 
1642   /* Check the parameters */
1643   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1644   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1645 
1646   /* Process Locked */
1647   __HAL_LOCK(hrtc);
1648 
1649   hrtc->State = HAL_RTC_STATE_BUSY;
1650 
1651   /* Disable the write protection for RTC registers */
1652   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1653 
1654   /* Get tick */
1655   tickstart = HAL_GetTick();
1656 
1657   /* Wait until the shift is completed */
1658   while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
1659   {
1660     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1661     {
1662       /* Enable the write protection for RTC registers */
1663       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1664 
1665       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1666 
1667       /* Process Unlocked */
1668       __HAL_UNLOCK(hrtc);
1669 
1670       return HAL_TIMEOUT;
1671     }
1672   }
1673 
1674   /* Check if the reference clock detection is disabled */
1675   if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
1676   {
1677     /* Configure the Shift settings */
1678     hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1679 
1680     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1681     if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1682     {
1683       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1684       {
1685         /* Enable the write protection for RTC registers */
1686         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1687 
1688         hrtc->State = HAL_RTC_STATE_ERROR;
1689 
1690         /* Process Unlocked */
1691         __HAL_UNLOCK(hrtc);
1692 
1693         return HAL_ERROR;
1694       }
1695     }
1696   }
1697   else
1698   {
1699     /* Enable the write protection for RTC registers */
1700     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1701 
1702     /* Change RTC state */
1703     hrtc->State = HAL_RTC_STATE_ERROR;
1704 
1705     /* Process Unlocked */
1706     __HAL_UNLOCK(hrtc);
1707 
1708     return HAL_ERROR;
1709   }
1710 
1711   /* Enable the write protection for RTC registers */
1712   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1713 
1714   /* Change RTC state */
1715   hrtc->State = HAL_RTC_STATE_READY;
1716 
1717   /* Process Unlocked */
1718   __HAL_UNLOCK(hrtc);
1719 
1720   return HAL_OK;
1721 }
1722 
1723 /**
1724   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1725   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1726   *                the configuration information for RTC.
1727   * @param  CalibOutput Select the Calibration output Selection.
1728   *          This parameter can be one of the following values:
1729   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1730   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1731   * @retval HAL status
1732   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1733 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1734 {
1735   /* Check the parameters */
1736   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1737 
1738   /* Process Locked */
1739   __HAL_LOCK(hrtc);
1740 
1741   hrtc->State = HAL_RTC_STATE_BUSY;
1742 
1743   /* Disable the write protection for RTC registers */
1744   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1745 
1746   /* Clear flags before config */
1747   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1748 
1749   /* Configure the RTC_CR register */
1750   hrtc->Instance->CR |= (uint32_t)CalibOutput;
1751 
1752   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1753 
1754   /* Enable the write protection for RTC registers */
1755   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1756 
1757   /* Change RTC state */
1758   hrtc->State = HAL_RTC_STATE_READY;
1759 
1760   /* Process Unlocked */
1761   __HAL_UNLOCK(hrtc);
1762 
1763   return HAL_OK;
1764 }
1765 
1766 /**
1767   * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1768   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1769   *                the configuration information for RTC.
1770   * @retval HAL status
1771   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1772 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1773 {
1774   /* Process Locked */
1775   __HAL_LOCK(hrtc);
1776 
1777   hrtc->State = HAL_RTC_STATE_BUSY;
1778 
1779   /* Disable the write protection for RTC registers */
1780   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1781 
1782   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1783 
1784   /* Enable the write protection for RTC registers */
1785   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1786 
1787   /* Change RTC state */
1788   hrtc->State = HAL_RTC_STATE_READY;
1789 
1790   /* Process Unlocked */
1791   __HAL_UNLOCK(hrtc);
1792 
1793   return HAL_OK;
1794 }
1795 
1796 /**
1797   * @brief  Enables the RTC reference clock detection.
1798   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1799   *                the configuration information for RTC.
1800   * @retval HAL status
1801   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1802 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1803 {
1804   HAL_StatusTypeDef status;
1805 
1806   /* Process Locked */
1807   __HAL_LOCK(hrtc);
1808 
1809   hrtc->State = HAL_RTC_STATE_BUSY;
1810 
1811   /* Disable the write protection for RTC registers */
1812   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1813 
1814   /* Enter Initialization mode */
1815   status = RTC_EnterInitMode(hrtc);
1816 
1817   if (status == HAL_OK)
1818   {
1819     /* Enable the reference clock detection */
1820     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1821 
1822     /* Exit Initialization mode */
1823     status = RTC_ExitInitMode(hrtc);
1824   }
1825 
1826   if (status == HAL_OK)
1827   {
1828     hrtc->State = HAL_RTC_STATE_READY;
1829   }
1830 
1831   /* Enable the write protection for RTC registers */
1832   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1833 
1834   /* Process Unlocked */
1835   __HAL_UNLOCK(hrtc);
1836 
1837   return status;
1838 }
1839 
1840 /**
1841   * @brief  Disable the RTC reference clock detection.
1842   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1843   *                the configuration information for RTC.
1844   * @retval HAL status
1845   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1846 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1847 {
1848   HAL_StatusTypeDef status;
1849 
1850   /* Process Locked */
1851   __HAL_LOCK(hrtc);
1852 
1853   hrtc->State = HAL_RTC_STATE_BUSY;
1854 
1855   /* Disable the write protection for RTC registers */
1856   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1857 
1858   /* Enter Initialization mode */
1859   status = RTC_EnterInitMode(hrtc);
1860 
1861   if (status == HAL_OK)
1862   {
1863     /* Disable the reference clock detection */
1864     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1865 
1866     /* Exit Initialization mode */
1867     status = RTC_ExitInitMode(hrtc);
1868   }
1869 
1870   if (status == HAL_OK)
1871   {
1872     hrtc->State = HAL_RTC_STATE_READY;
1873   }
1874 
1875   /* Enable the write protection for RTC registers */
1876   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1877 
1878   /* Process Unlocked */
1879   __HAL_UNLOCK(hrtc);
1880 
1881   return status;
1882 }
1883 
1884 /**
1885   * @brief  Enables the Bypass Shadow feature.
1886   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1887   *                the configuration information for RTC.
1888   * @note   When the Bypass Shadow is enabled the calendar value are taken
1889   *         directly from the Calendar counter.
1890   * @retval HAL status
1891   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1892 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1893 {
1894   /* Process Locked */
1895   __HAL_LOCK(hrtc);
1896 
1897   hrtc->State = HAL_RTC_STATE_BUSY;
1898 
1899   /* Disable the write protection for RTC registers */
1900   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1901 
1902   /* Set the BYPSHAD bit */
1903   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1904 
1905   /* Enable the write protection for RTC registers */
1906   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1907 
1908   /* Change RTC state */
1909   hrtc->State = HAL_RTC_STATE_READY;
1910 
1911   /* Process Unlocked */
1912   __HAL_UNLOCK(hrtc);
1913 
1914   return HAL_OK;
1915 }
1916 
1917 /**
1918   * @brief  Disables the Bypass Shadow feature.
1919   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1920   *                the configuration information for RTC.
1921   * @note   When the Bypass Shadow is enabled the calendar value are taken
1922   *         directly from the Calendar counter.
1923   * @retval HAL status
1924   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1925 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1926 {
1927   /* Process Locked */
1928   __HAL_LOCK(hrtc);
1929 
1930   hrtc->State = HAL_RTC_STATE_BUSY;
1931 
1932   /* Disable the write protection for RTC registers */
1933   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1934 
1935   /* Reset the BYPSHAD bit */
1936   hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1937 
1938   /* Enable the write protection for RTC registers */
1939   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1940 
1941   /* Change RTC state */
1942   hrtc->State = HAL_RTC_STATE_READY;
1943 
1944   /* Process Unlocked */
1945   __HAL_UNLOCK(hrtc);
1946 
1947   return HAL_OK;
1948 }
1949 
1950 /**
1951   * @}
1952   */
1953 
1954 /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
1955   * @brief    Extended features functions
1956   *
1957 @verbatim
1958  ===============================================================================
1959                  ##### Extended features functions #####
1960  ===============================================================================
1961     [..]  This section provides functions allowing to:
1962       (+) RTC Alarm B callback
1963       (+) RTC Poll for Alarm B request
1964 
1965 @endverbatim
1966   * @{
1967   */
1968 
1969 /**
1970   * @brief  Alarm B callback.
1971   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1972   *                the configuration information for RTC.
1973   * @retval None
1974   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1975 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1976 {
1977   /* Prevent unused argument(s) compilation warning */
1978   UNUSED(hrtc);
1979 
1980   /* NOTE: This function should not be modified, when the callback is needed,
1981            the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1982    */
1983 }
1984 
1985 /**
1986   * @brief  Handles Alarm B Polling request.
1987   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1988   *                the configuration information for RTC.
1989   * @param  Timeout Timeout duration
1990   * @retval HAL status
1991   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1992 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1993 {
1994   uint32_t tickstart = 0U;
1995 
1996   /* Get tick */
1997   tickstart = HAL_GetTick();
1998 
1999   /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
2000   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
2001   {
2002     if (Timeout != HAL_MAX_DELAY)
2003     {
2004       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
2005       {
2006         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2007         return HAL_TIMEOUT;
2008       }
2009     }
2010   }
2011 
2012   /* Clear the Alarm flag */
2013   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2014 
2015   /* Change RTC state */
2016   hrtc->State = HAL_RTC_STATE_READY;
2017 
2018   return HAL_OK;
2019 }
2020 
2021 /**
2022   * @}
2023   */
2024 
2025 /**
2026   * @}
2027   */
2028 
2029 #endif /* HAL_RTC_MODULE_ENABLED */
2030 /**
2031   * @}
2032   */
2033 
2034 /**
2035   * @}
2036   */
2037