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