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