1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_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 
44   *** Timestamp configuration ***
45   ===============================
46   [..]
47     (+) To configure the RTC Timestamp use the HAL_RTCEx_SetTimeStamp() function.
48         You can also configure the RTC Timestamp with interrupt mode using the
49         HAL_RTCEx_SetTimeStamp_IT() function.
50     (+) To read the RTC Timestamp Time and Date register, use the
51         HAL_RTCEx_GetTimeStamp() function.
52     (+) The Timestamp alternate function can be mapped either to RTC_AF1 (PC13)
53         or RTC_AF2 (PI8) depending on the value of TSINSEL bit in RTC_TAFCR
54         register.
55         The corresponding pin is also selected by HAL_RTCEx_SetTimeStamp()
56         or HAL_RTCEx_SetTimeStamp_IT() functions.
57 
58   *** Tamper configuration ***
59   ============================
60   [..]
61     (+) To Enable the RTC Tamper and configure the Tamper filter count, trigger
62         Edge or Level according to the Tamper filter value (if equal to 0 Edge
63         else Level), sampling frequency, precharge or discharge and Pull-UP use
64         the HAL_RTCEx_SetTamper() function.
65         You can configure RTC Tamper in interrupt mode using HAL_RTCEx_SetTamper_IT()
66         function.
67     (+) The TAMPER1 alternate function can be mapped either to RTC_AF1 (PC13)
68         or RTC_AF2 (PI8) depending on the value of TAMP1INSEL bit in RTC_TAFCR
69         register.
70         The corresponding pin is also selected by HAL_RTCEx_SetTamper()
71         or HAL_RTCEx_SetTamper_IT() functions.
72 
73   *** Backup Data Registers configuration ***
74   ===========================================
75   [..]
76     (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
77         function.
78     (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
79         function.
80 
81   *** Coarse Digital Calibration configuration ***
82   ================================================
83   [..]
84     (+) The Coarse Digital Calibration can be used to compensate crystal inaccuracy
85         by setting the DCS bit in RTC_CALIBR register.
86     (+) When positive calibration is enabled (DCS = ‘0’), 2 asynchronous prescaler
87         clock cycles are added every minute during 2xDC minutes.
88         This causes the calendar to be updated sooner, thereby adjusting the
89         effective RTC frequency to be a bit higher.
90     (+) When negative calibration is enabled (DCS = ‘1’), 1 asynchronous prescaler
91         clock cycle is removed every minute during 2xDC minutes.
92         This causes the calendar to be updated later, thereby adjusting the
93         effective RTC frequency to be a bit lower.
94     (+) DC is configured through bits DC[4:0] of RTC_CALIBR register. This number
95         ranges from 0 to 31 corresponding to a time interval (2xDC) ranging from
96         0 to 62.
97     (+) In order to measure the clock deviation, a 512 Hz clock is output for
98         calibration.
99     (+) The RTC Coarse Digital Calibration value and sign can be calibrated using
100         the HAL_RTCEx_SetCoarseCalib() function.
101 
102   @endverbatim
103   ******************************************************************************
104   */
105 
106 /* Includes ------------------------------------------------------------------*/
107 #include "stm32f2xx_hal.h"
108 
109 /** @addtogroup STM32F2xx_HAL_Driver
110   * @{
111   */
112 
113 /** @defgroup RTCEx RTCEx
114   * @brief    RTC Extended HAL module driver
115   * @{
116   */
117 
118 #ifdef HAL_RTC_MODULE_ENABLED
119 
120 /* Private typedef -----------------------------------------------------------*/
121 /* Private define ------------------------------------------------------------*/
122 /* Private macro -------------------------------------------------------------*/
123 /* Private variables ---------------------------------------------------------*/
124 /* Private function prototypes -----------------------------------------------*/
125 /* Exported functions --------------------------------------------------------*/
126 
127 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
128   * @{
129   */
130 
131 /** @defgroup RTCEx_Exported_Functions_Group1 RTC Timestamp and Tamper functions
132   * @brief    RTC Timestamp and Tamper functions
133   *
134 @verbatim
135  ===============================================================================
136                  ##### RTC Timestamp and Tamper functions #####
137  ===============================================================================
138 
139  [..] This section provides functions allowing to configure Timestamp feature
140 
141 @endverbatim
142   * @{
143   */
144 
145 /**
146   * @brief  Sets Timestamp.
147   * @note   This API must be called before enabling the Timestamp feature.
148   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
149   *                the configuration information for RTC.
150   * @param  RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
151   *         activated.
152   *          This parameter can be one of the following values:
153   *             @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
154   *                                        the rising edge of the related pin.
155   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
156   *                                        the falling edge of the related pin.
157   * @param  RTC_TimeStampPin Specifies the RTC Timestamp Pin.
158   *          This parameter can be one of the following values:
159   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
160   *             @arg RTC_TIMESTAMPPIN_POS1: PI8 is selected as RTC Timestamp Pin.
161   * @retval HAL status
162   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t RTC_TimeStampEdge,uint32_t RTC_TimeStampPin)163 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
164 {
165   uint32_t tmpreg = 0U;
166 
167   /* Check the parameters */
168   assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
169   assert_param(IS_RTC_TIMESTAMP_PIN(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   hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
178   hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
179 
180   /* Get the RTC_CR register and clear the bits to be configured */
181   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
182 
183   /* Configure the Timestamp TSEDGE bit */
184   tmpreg |= RTC_TimeStampEdge;
185 
186   /* Disable the write protection for RTC registers */
187   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
188 
189   /* Copy the desired configuration into the CR register */
190   hrtc->Instance->CR = (uint32_t)tmpreg;
191 
192   /* Clear RTC Timestamp flag */
193   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
194 
195   /* Clear RTC Timestamp overrun Flag */
196   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
197 
198   /* Enable the Timestamp saving */
199   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
200 
201   /* Enable the write protection for RTC registers */
202   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
203 
204   /* Change RTC state back to READY */
205   hrtc->State = HAL_RTC_STATE_READY;
206 
207   /* Process Unlocked */
208   __HAL_UNLOCK(hrtc);
209 
210   return HAL_OK;
211 }
212 
213 /**
214   * @brief  Sets Timestamp with Interrupt.
215   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
216   *                the configuration information for RTC.
217   * @note   This API must be called before enabling the Timestamp feature.
218   * @param  RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
219   *         activated.
220   *          This parameter can be one of the following values:
221   *             @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
222   *                                        the rising edge of the related pin.
223   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
224   *                                        the falling edge of the related pin.
225   * @param  RTC_TimeStampPin Specifies the RTC Timestamp Pin.
226   *          This parameter can be one of the following values:
227   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
228   *             @arg RTC_TIMESTAMPPIN_POS1: PI8 is selected as RTC Timestamp Pin.
229   * @retval HAL status
230   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t RTC_TimeStampEdge,uint32_t RTC_TimeStampPin)231 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
232 {
233   uint32_t tmpreg = 0U;
234 
235   /* Check the parameters */
236   assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
237   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
238 
239   /* Process Locked */
240   __HAL_LOCK(hrtc);
241 
242   /* Change RTC state to BUSY */
243   hrtc->State = HAL_RTC_STATE_BUSY;
244 
245   hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
246   hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
247 
248   /* Get the RTC_CR register and clear the bits to be configured */
249   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
250 
251   /* Configure the Timestamp TSEDGE bit */
252   tmpreg |= RTC_TimeStampEdge;
253 
254   /* Disable the write protection for RTC registers */
255   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
256 
257   /* Copy the desired configuration into the CR register */
258   hrtc->Instance->CR = (uint32_t)tmpreg;
259 
260   /* Clear RTC Timestamp flag */
261   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
262 
263   /* Clear RTC Timestamp overrun Flag */
264   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
265 
266   /* Enable the Timestamp saving */
267   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
268 
269   /* Enable IT Timestamp */
270   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
271 
272   /* Enable the write protection for RTC registers */
273   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
274 
275   /* RTC Timestamp Interrupt Configuration: EXTI configuration */
276   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
277   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
278 
279   /* Change RTC state back to READY */
280   hrtc->State = HAL_RTC_STATE_READY;
281 
282   /* Process Unlocked */
283   __HAL_UNLOCK(hrtc);
284 
285   return HAL_OK;
286 }
287 
288 /**
289   * @brief  Deactivates Timestamp.
290   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
291   *                the configuration information for RTC.
292   * @retval HAL status
293   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)294 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
295 {
296   uint32_t tmpreg = 0U;
297 
298   /* Process Locked */
299   __HAL_LOCK(hrtc);
300 
301   hrtc->State = HAL_RTC_STATE_BUSY;
302 
303   /* Disable the write protection for RTC registers */
304   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
305 
306   /* In case of interrupt mode is used, the interrupt source must disabled */
307   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
308 
309   /* Get the RTC_CR register and clear the bits to be configured */
310   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
311 
312   /* Configure the Timestamp TSEDGE and Enable bits */
313   hrtc->Instance->CR = (uint32_t)tmpreg;
314 
315   /* Enable the write protection for RTC registers */
316   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
317 
318   hrtc->State = HAL_RTC_STATE_READY;
319 
320   /* Process Unlocked */
321   __HAL_UNLOCK(hrtc);
322 
323   return HAL_OK;
324 }
325 
326 /**
327   * @brief  Gets the RTC Timestamp value.
328   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
329   *                the configuration information for RTC.
330   * @param  sTimeStamp Pointer to Time structure
331   * @param  sTimeStampDate Pointer to Date structure
332   * @param  Format specifies the format of the entered parameters.
333   *          This parameter can be one of the following values:
334   *             @arg RTC_FORMAT_BIN: Binary data format
335   *             @arg RTC_FORMAT_BCD: BCD data format
336   * @retval HAL status
337   */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)338 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
339 {
340   uint32_t tmptime = 0U;
341   uint32_t tmpdate = 0U;
342 
343   /* Check the parameters */
344   assert_param(IS_RTC_FORMAT(Format));
345 
346   /* Get the Timestamp time and date registers values */
347   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
348   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
349 
350   /* Fill the Time structure fields with the read parameters */
351   sTimeStamp->Hours      = (uint8_t)((tmptime & (RTC_TSTR_HT  | RTC_TSTR_HU))  >> RTC_TSTR_HU_Pos);
352   sTimeStamp->Minutes    = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
353   sTimeStamp->Seconds    = (uint8_t)((tmptime & (RTC_TSTR_ST  | RTC_TSTR_SU))  >> RTC_TSTR_SU_Pos);
354   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM))                 >> RTC_TSTR_PM_Pos);
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_PIN(sTamper->PinSelection));
397   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
398 
399   /* Process Locked */
400   __HAL_LOCK(hrtc);
401 
402   hrtc->State = HAL_RTC_STATE_BUSY;
403 
404   /* Copy control register into temporary variable */
405   tmpreg = hrtc->Instance->TAFCR;
406 
407   /* Enable selected tamper */
408   tmpreg |= (sTamper->Tamper);
409 
410   /* Configure the tamper trigger bit (this bit is just on the right of the
411        tamper enable bit, hence the one-time right shift before updating it) */
412   if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
413   {
414     /* Set the tamper trigger bit (case of falling edge or high level) */
415     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
416   }
417   else
418   {
419     /* Clear the tamper trigger bit (case of rising edge or low level) */
420     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
421   }
422 
423   /* Clear remaining field before setting it */
424   tmpreg &= ~RTC_TAFCR_TAMP1INSEL;
425 
426   /* Set remaining parameter of desired configuration into temporary variable */
427   tmpreg |= (uint32_t)sTamper->PinSelection;
428 
429   /* Disable tamper global interrupt in case it is enabled */
430   tmpreg &= (uint32_t)~RTC_TAFCR_TAMPIE;
431 
432   /* Copy desired configuration into configuration register */
433   hrtc->Instance->TAFCR = tmpreg;
434 
435   hrtc->State = HAL_RTC_STATE_READY;
436 
437   /* Process Unlocked */
438   __HAL_UNLOCK(hrtc);
439 
440   return HAL_OK;
441 }
442 
443 /**
444   * @brief  Sets Tamper with interrupt.
445   * @note   By calling this API the tamper global interrupt will be enabled.
446   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
447   *                the configuration information for RTC.
448   * @param  sTamper Pointer to RTC Tamper.
449   * @retval HAL status
450   */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)451 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
452 {
453   uint32_t tmpreg = 0U;
454 
455   /* Check the parameters */
456   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
457   assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
458   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
459 
460   /* Process Locked */
461   __HAL_LOCK(hrtc);
462 
463   hrtc->State = HAL_RTC_STATE_BUSY;
464 
465   /* Copy control register into temporary variable */
466   tmpreg = hrtc->Instance->TAFCR;
467 
468   /* Enable selected tamper */
469   tmpreg |= (sTamper->Tamper);
470 
471   /* Configure the tamper trigger bit (this bit is just on the right of the
472        tamper enable bit, hence the one-time right shift before updating it) */
473   if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
474   {
475     /* Set the tamper trigger bit (case of falling edge or high level) */
476     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
477   }
478   else
479   {
480     /* Clear the tamper trigger bit (case of rising edge or low level) */
481     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
482   }
483 
484   /* Clear remaining field before setting it */
485   tmpreg &= ~RTC_TAFCR_TAMP1INSEL;
486 
487   /* Set remaining parameter of desired configuration into temporary variable */
488   tmpreg |= (uint32_t)sTamper->PinSelection;
489 
490   /* Enable global tamper interrupt */
491   tmpreg |= (uint32_t)RTC_TAFCR_TAMPIE;
492 
493   /* Copy desired configuration into configuration register */
494   hrtc->Instance->TAFCR = tmpreg;
495 
496   /* RTC Tamper Interrupt Configuration: EXTI configuration */
497   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
498   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
499 
500   hrtc->State = HAL_RTC_STATE_READY;
501 
502   /* Process Unlocked */
503   __HAL_UNLOCK(hrtc);
504 
505   return HAL_OK;
506 }
507 
508 /**
509   * @brief  Deactivates Tamper.
510   * @note   The tamper global interrupt bit will remain unchanged.
511   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
512   *                the configuration information for RTC.
513   * @param  Tamper Selected tamper pin.
514   *          This parameter can be:
515   *            @arg RTC_TAMPER_1:  Tamper 1
516   * @retval HAL status
517   */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)518 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
519 {
520   assert_param(IS_RTC_TAMPER(Tamper));
521 
522   /* Process Locked */
523   __HAL_LOCK(hrtc);
524 
525   hrtc->State = HAL_RTC_STATE_BUSY;
526 
527   /* Disable the selected Tamper pin */
528   hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
529 
530   hrtc->State = HAL_RTC_STATE_READY;
531 
532   /* Process Unlocked */
533   __HAL_UNLOCK(hrtc);
534 
535   return HAL_OK;
536 }
537 
538 /**
539   * @brief  Handles Timestamp and Tamper interrupt request.
540   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
541   *                the configuration information for RTC.
542   * @retval None
543   */
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)544 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
545 {
546   /* Clear the EXTI's Flag for RTC Timestamp and Tamper */
547   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
548 
549   /* Get the Timestamp interrupt source enable status */
550   if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
551   {
552     /* Get the pending status of the Timestamp Interrupt */
553     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
554     {
555       /* Timestamp callback */
556 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
557       hrtc->TimeStampEventCallback(hrtc);
558 #else
559       HAL_RTCEx_TimeStampEventCallback(hrtc);
560 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
561 
562       /* Clear the Timestamp interrupt pending bit after returning from callback
563          as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
564       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
565     }
566   }
567 
568   /* Get the Tamper 1 interrupt source enable status */
569   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
570   {
571     /* Get the pending status of the Tamper 1 Interrupt */
572     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
573     {
574       /* Clear the Tamper interrupt pending bit */
575       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
576 
577       /* Tamper callback */
578 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
579       hrtc->Tamper1EventCallback(hrtc);
580 #else
581       HAL_RTCEx_Tamper1EventCallback(hrtc);
582 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
583     }
584   }
585 
586   /* Change RTC state */
587   hrtc->State = HAL_RTC_STATE_READY;
588 }
589 
590 /**
591   * @brief  Timestamp callback.
592   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
593   *                the configuration information for RTC.
594   * @retval None
595   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)596 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
597 {
598   /* Prevent unused argument(s) compilation warning */
599   UNUSED(hrtc);
600 
601   /* NOTE: This function should not be modified, when the callback is needed,
602            the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
603   */
604 }
605 
606 /**
607   * @brief  Tamper 1 callback.
608   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
609   *                the configuration information for RTC.
610   * @retval None
611   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)612 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
613 {
614   /* Prevent unused argument(s) compilation warning */
615   UNUSED(hrtc);
616 
617   /* NOTE: This function should not be modified, when the callback is needed,
618            the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
619    */
620 }
621 
622 /**
623   * @brief  Handles Timestamp polling request.
624   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
625   *                the configuration information for RTC.
626   * @param  Timeout Timeout duration
627   * @retval HAL status
628   */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)629 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
630 {
631   uint32_t tickstart = 0U;
632 
633   /* Get tick */
634   tickstart = HAL_GetTick();
635 
636   while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
637   {
638     if (Timeout != HAL_MAX_DELAY)
639     {
640       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
641       {
642         hrtc->State = HAL_RTC_STATE_TIMEOUT;
643         return HAL_TIMEOUT;
644       }
645     }
646 
647     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
648     {
649       /* Clear the Timestamp Overrun Flag */
650       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
651 
652       /* Change Timestamp state */
653       hrtc->State = HAL_RTC_STATE_ERROR;
654 
655       return HAL_ERROR;
656     }
657   }
658 
659   /* Change RTC state */
660   hrtc->State = HAL_RTC_STATE_READY;
661 
662   return HAL_OK;
663 }
664 
665 /**
666   * @brief  Handles Tamper 1 Polling.
667   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
668   *                the configuration information for RTC.
669   * @param  Timeout Timeout duration
670   * @retval HAL status
671   */
HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)672 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
673 {
674   uint32_t tickstart = 0U;
675 
676   /* Get tick */
677   tickstart = HAL_GetTick();
678 
679   /* Get the status of the Interrupt */
680   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
681   {
682     if (Timeout != HAL_MAX_DELAY)
683     {
684       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
685       {
686         hrtc->State = HAL_RTC_STATE_TIMEOUT;
687         return HAL_TIMEOUT;
688       }
689     }
690   }
691 
692   /* Clear the Tamper Flag */
693   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
694 
695   /* Change RTC state */
696   hrtc->State = HAL_RTC_STATE_READY;
697 
698   return HAL_OK;
699 }
700 
701 /**
702   * @}
703   */
704 
705 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wakeup functions
706   * @brief    RTC Wakeup functions
707   *
708 @verbatim
709  ===============================================================================
710                         ##### RTC Wakeup functions #####
711  ===============================================================================
712 
713  [..] This section provides functions allowing to configure Wakeup feature
714 
715 @endverbatim
716   * @{
717   */
718 
719 /**
720   * @brief  Sets wakeup timer.
721   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
722   *                the configuration information for RTC.
723   * @param  WakeUpCounter Wakeup counter
724   * @param  WakeUpClock Wakeup clock
725   * @retval HAL status
726   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)727 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
728 {
729   uint32_t tickstart = 0U;
730 
731   /* Check the parameters */
732   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
733   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
734 
735   /* Process Locked */
736   __HAL_LOCK(hrtc);
737 
738   hrtc->State = HAL_RTC_STATE_BUSY;
739 
740   /* Disable the write protection for RTC registers */
741   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
742 
743   /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
744   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
745   {
746     tickstart = HAL_GetTick();
747 
748     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
749     while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
750     {
751       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
752       {
753         /* Enable the write protection for RTC registers */
754         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
755 
756         hrtc->State = HAL_RTC_STATE_TIMEOUT;
757 
758         /* Process Unlocked */
759         __HAL_UNLOCK(hrtc);
760 
761         return HAL_TIMEOUT;
762       }
763     }
764   }
765 
766   /* Disable the Wakeup timer */
767   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
768 
769   /* Clear the Wakeup flag */
770   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
771 
772   /* Get tick */
773   tickstart = HAL_GetTick();
774 
775   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
776   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
777   {
778     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
779     {
780       /* Enable the write protection for RTC registers */
781       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
782 
783       hrtc->State = HAL_RTC_STATE_TIMEOUT;
784 
785       /* Process Unlocked */
786       __HAL_UNLOCK(hrtc);
787 
788       return HAL_TIMEOUT;
789     }
790   }
791 
792   /* Clear the Wakeup Timer clock source bits in CR register */
793   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
794 
795   /* Configure the clock source */
796   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
797 
798   /* Configure the Wakeup Timer counter */
799   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
800 
801   /* Enable the Wakeup Timer */
802   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
803 
804   /* Enable the write protection for RTC registers */
805   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
806 
807   hrtc->State = HAL_RTC_STATE_READY;
808 
809   /* Process Unlocked */
810   __HAL_UNLOCK(hrtc);
811 
812   return HAL_OK;
813 }
814 
815 /**
816   * @brief  Sets wakeup timer with interrupt.
817   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
818   *                the configuration information for RTC.
819   * @param  WakeUpCounter Wakeup counter
820   * @param  WakeUpClock Wakeup clock
821   * @retval HAL status
822   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)823 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
824 {
825   __IO uint32_t count  = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
826 
827   /* Check the parameters */
828   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
829   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
830 
831   /* Process Locked */
832   __HAL_LOCK(hrtc);
833 
834   hrtc->State = HAL_RTC_STATE_BUSY;
835 
836   /* Disable the write protection for RTC registers */
837   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
838 
839   /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
840   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
841   {
842     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
843     do
844     {
845       count = count - 1U;
846       if (count == 0U)
847       {
848         /* Enable the write protection for RTC registers */
849         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
850 
851         hrtc->State = HAL_RTC_STATE_TIMEOUT;
852 
853         /* Process Unlocked */
854         __HAL_UNLOCK(hrtc);
855 
856         return HAL_TIMEOUT;
857       }
858     } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
859   }
860 
861   /* Disable the Wakeup timer */
862   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
863 
864   /* Clear the Wakeup flag */
865   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
866 
867   /* Reload the counter */
868   count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
869 
870   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
871   do
872   {
873     count = count - 1U;
874     if (count == 0U)
875     {
876       /* Enable the write protection for RTC registers */
877       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
878 
879       hrtc->State = HAL_RTC_STATE_TIMEOUT;
880 
881       /* Process Unlocked */
882       __HAL_UNLOCK(hrtc);
883 
884       return HAL_TIMEOUT;
885     }
886   } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
887 
888   /* Clear the Wakeup Timer clock source bits in CR register */
889   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
890 
891   /* Configure the clock source */
892   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
893 
894   /* Configure the Wakeup Timer counter */
895   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
896 
897   /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
898   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
899   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
900 
901   /* Configure the interrupt in the RTC_CR register */
902   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
903 
904   /* Enable the Wakeup Timer */
905   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
906 
907   /* Enable the write protection for RTC registers */
908   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
909 
910   hrtc->State = HAL_RTC_STATE_READY;
911 
912   /* Process Unlocked */
913   __HAL_UNLOCK(hrtc);
914 
915   return HAL_OK;
916 }
917 
918 /**
919   * @brief  Deactivates wakeup timer counter.
920   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
921   *                the configuration information for RTC.
922   * @retval HAL status
923   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)924 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
925 {
926   uint32_t tickstart = 0U;
927 
928   /* Process Locked */
929   __HAL_LOCK(hrtc);
930 
931   hrtc->State = HAL_RTC_STATE_BUSY;
932 
933   /* Disable the write protection for RTC registers */
934   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
935 
936   /* Disable the Wakeup Timer */
937   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
938 
939   /* In case of interrupt mode is used, the interrupt source must disabled */
940   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
941 
942   /* Get tick */
943   tickstart = HAL_GetTick();
944 
945   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
946   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
947   {
948     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
949     {
950       /* Enable the write protection for RTC registers */
951       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
952 
953       hrtc->State = HAL_RTC_STATE_TIMEOUT;
954 
955       /* Process Unlocked */
956       __HAL_UNLOCK(hrtc);
957 
958       return HAL_TIMEOUT;
959     }
960   }
961 
962   /* Enable the write protection for RTC registers */
963   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
964 
965   hrtc->State = HAL_RTC_STATE_READY;
966 
967   /* Process Unlocked */
968   __HAL_UNLOCK(hrtc);
969 
970   return HAL_OK;
971 }
972 
973 /**
974   * @brief  Gets wakeup timer counter.
975   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
976   *                the configuration information for RTC.
977   * @retval Counter value
978   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)979 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
980 {
981   /* Get the counter value */
982   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
983 }
984 
985 /**
986   * @brief  Handles Wakeup Timer interrupt request.
987   * @note   Unlike alarm interrupt line (shared by Alarms A and B) or tamper
988   *         interrupt line (shared by timestamp and tampers) wakeup timer
989   *         interrupt line is exclusive to the wakeup timer.
990   *         There is no need in this case to check on the interrupt enable
991   *         status via __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE().
992   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
993   *                the configuration information for RTC.
994   * @retval None
995   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)996 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
997 {
998   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
999   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1000 
1001   /* Get the pending status of the Wakeup timer Interrupt */
1002   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1003   {
1004     /* Clear the Wakeup timer interrupt pending bit */
1005     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1006 
1007     /* Wakeup timer callback */
1008 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1009     hrtc->WakeUpTimerEventCallback(hrtc);
1010 #else
1011     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1012 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1013   }
1014 
1015   /* Change RTC state */
1016   hrtc->State = HAL_RTC_STATE_READY;
1017 }
1018 
1019 /**
1020   * @brief  Wakeup Timer callback.
1021   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1022   *                the configuration information for RTC.
1023   * @retval None
1024   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)1025 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1026 {
1027   /* Prevent unused argument(s) compilation warning */
1028   UNUSED(hrtc);
1029 
1030   /* NOTE: This function should not be modified, when the callback is needed,
1031            the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1032    */
1033 }
1034 
1035 /**
1036   * @brief  Handles Wakeup Timer Polling.
1037   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1038   *                the configuration information for RTC.
1039   * @param  Timeout Timeout duration
1040   * @retval HAL status
1041   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1042 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1043 {
1044   uint32_t tickstart = 0U;
1045 
1046   /* Get tick */
1047   tickstart = HAL_GetTick();
1048 
1049   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1050   {
1051     if (Timeout != HAL_MAX_DELAY)
1052     {
1053       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1054       {
1055         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1056         return HAL_TIMEOUT;
1057       }
1058     }
1059   }
1060 
1061   /* Clear the Wakeup timer Flag */
1062   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1063 
1064   /* Change RTC state */
1065   hrtc->State = HAL_RTC_STATE_READY;
1066 
1067   return HAL_OK;
1068 }
1069 
1070 /**
1071   * @}
1072   */
1073 
1074 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
1075   * @brief    Extended Peripheral Control functions
1076   *
1077 @verbatim
1078  ===============================================================================
1079               ##### Extended Peripheral Control functions #####
1080  ===============================================================================
1081     [..]
1082     This subsection provides functions allowing to
1083       (+) Write a data in a specified RTC Backup data register
1084       (+) Read a data in a specified RTC Backup data register
1085       (+) Set the Coarse calibration parameters.
1086       (+) Deactivate the Coarse calibration parameters
1087       (+) Configure the Synchronization Shift Control Settings.
1088       (+) Configure the Calibration Pinout (RTC_CALIB).
1089       (+) Deactivate the Calibration Pinout (RTC_CALIB).
1090       (+) Enable the RTC reference clock detection.
1091       (+) Disable the RTC reference clock detection.
1092 
1093 @endverbatim
1094   * @{
1095   */
1096 
1097 /**
1098   * @brief  Writes a data in a specified RTC Backup data register.
1099   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1100   *                the configuration information for RTC.
1101   * @param  BackupRegister RTC Backup data Register number.
1102   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 19)
1103   *                                 to specify the register.
1104   * @param  Data Data to be written in the specified RTC Backup data register.
1105   * @retval None
1106   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)1107 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1108 {
1109   uint32_t tmp = 0U;
1110 
1111   /* Check the parameters */
1112   assert_param(IS_RTC_BKP(BackupRegister));
1113 
1114   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1115   tmp += (BackupRegister * 4U);
1116 
1117   /* Write the specified register */
1118   *(__IO uint32_t *)tmp = (uint32_t)Data;
1119 }
1120 
1121 /**
1122   * @brief  Reads data from the specified RTC Backup data Register.
1123   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1124   *                the configuration information for RTC.
1125   * @param  BackupRegister RTC Backup data Register number.
1126   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 19)
1127   *                                 to specify the register.
1128   * @retval Read value
1129   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)1130 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1131 {
1132   uint32_t tmp = 0U;
1133 
1134   /* Check the parameters */
1135   assert_param(IS_RTC_BKP(BackupRegister));
1136 
1137   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1138   tmp += (BackupRegister * 4U);
1139 
1140   /* Read the specified register */
1141   return (*(__IO uint32_t *)tmp);
1142 }
1143 
1144 /**
1145   * @brief  Sets the Coarse calibration parameters.
1146   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1147   *                the configuration information for RTC.
1148   * @param  CalibSign Specifies the sign of the coarse calibration value.
1149   *          This parameter can be  one of the following values:
1150   *             @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
1151   *             @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
1152   * @param  Value value of coarse calibration expressed in ppm (coded on 5 bits).
1153   *
1154   * @note   This Calibration value should be between 0 and 63 when using negative
1155   *         sign with a 2-ppm step.
1156   *
1157   * @note   This Calibration value should be between 0 and 126 when using positive
1158   *         sign with a 4-ppm step.
1159   * @retval HAL status
1160   */
HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef * hrtc,uint32_t CalibSign,uint32_t Value)1161 HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef *hrtc, uint32_t CalibSign, uint32_t Value)
1162 {
1163   HAL_StatusTypeDef status;
1164 
1165   /* Check the parameters */
1166   assert_param(IS_RTC_CALIB_SIGN(CalibSign));
1167   assert_param(IS_RTC_CALIB_VALUE(Value));
1168 
1169   /* Process Locked */
1170   __HAL_LOCK(hrtc);
1171 
1172   hrtc->State = HAL_RTC_STATE_BUSY;
1173 
1174   /* Disable the write protection for RTC registers */
1175   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1176 
1177   /* Enter Initialization mode */
1178   status = RTC_EnterInitMode(hrtc);
1179 
1180   if (status == HAL_OK)
1181   {
1182     /* Enable the Coarse Calibration */
1183     __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
1184 
1185     /* Set the coarse calibration value */
1186     hrtc->Instance->CALIBR = (uint32_t)(CalibSign | Value);
1187 
1188     /* Exit Initialization mode */
1189     status = RTC_ExitInitMode(hrtc);
1190   }
1191 
1192   if (status == HAL_OK)
1193   {
1194     hrtc->State = HAL_RTC_STATE_READY;
1195   }
1196 
1197   /* Enable the write protection for RTC registers */
1198   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1199 
1200   /* Process Unlocked */
1201   __HAL_UNLOCK(hrtc);
1202 
1203   return status;
1204 }
1205 
1206 /**
1207   * @brief  Deactivates the Coarse calibration parameters.
1208   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1209   *                the configuration information for RTC.
1210   * @retval HAL status
1211   */
HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef * hrtc)1212 HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef *hrtc)
1213 {
1214   HAL_StatusTypeDef status;
1215 
1216   /* Process Locked */
1217   __HAL_LOCK(hrtc);
1218 
1219   hrtc->State = HAL_RTC_STATE_BUSY;
1220 
1221   /* Disable the write protection for RTC registers */
1222   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1223 
1224   /* Enter Initialization mode */
1225   status = RTC_EnterInitMode(hrtc);
1226 
1227   if (status == HAL_OK)
1228   {
1229     /* Disable the Coarse Calibration */
1230     __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
1231 
1232     /* Exit Initialization mode */
1233     status = RTC_ExitInitMode(hrtc);
1234   }
1235 
1236   if (status == HAL_OK)
1237   {
1238     hrtc->State = HAL_RTC_STATE_READY;
1239   }
1240 
1241   /* Enable the write protection for RTC registers */
1242   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1243 
1244   /* Process Unlocked */
1245   __HAL_UNLOCK(hrtc);
1246 
1247   return status;
1248 }
1249 
1250 /**
1251   * @brief  Configures the Calibration Pinout (RTC_CALIB).
1252   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1253   *                the configuration information for RTC.
1254   * @retval HAL status
1255   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc)1256 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1257 {
1258   /* Process Locked */
1259   __HAL_LOCK(hrtc);
1260 
1261   hrtc->State = HAL_RTC_STATE_BUSY;
1262 
1263   /* Disable the write protection for RTC registers */
1264   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1265 
1266   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1267 
1268   /* Enable the write protection for RTC registers */
1269   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1270 
1271   /* Change RTC state */
1272   hrtc->State = HAL_RTC_STATE_READY;
1273 
1274   /* Process Unlocked */
1275   __HAL_UNLOCK(hrtc);
1276 
1277   return HAL_OK;
1278 }
1279 
1280 /**
1281   * @brief  Deactivates the Calibration Pinout (RTC_CALIB).
1282   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1283   *                the configuration information for RTC.
1284   * @retval HAL status
1285   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1286 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1287 {
1288   /* Process Locked */
1289   __HAL_LOCK(hrtc);
1290 
1291   hrtc->State = HAL_RTC_STATE_BUSY;
1292 
1293   /* Disable the write protection for RTC registers */
1294   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1295 
1296   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1297 
1298   /* Enable the write protection for RTC registers */
1299   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1300 
1301   /* Change RTC state */
1302   hrtc->State = HAL_RTC_STATE_READY;
1303 
1304   /* Process Unlocked */
1305   __HAL_UNLOCK(hrtc);
1306 
1307   return HAL_OK;
1308 }
1309 
1310 /**
1311   * @brief  Enables the RTC reference clock detection.
1312   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1313   *                the configuration information for RTC.
1314   * @retval HAL status
1315   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1316 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1317 {
1318   HAL_StatusTypeDef status;
1319 
1320   /* Process Locked */
1321   __HAL_LOCK(hrtc);
1322 
1323   hrtc->State = HAL_RTC_STATE_BUSY;
1324 
1325   /* Disable the write protection for RTC registers */
1326   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1327 
1328   /* Enter Initialization mode */
1329   status = RTC_EnterInitMode(hrtc);
1330 
1331   if (status == HAL_OK)
1332   {
1333     /* Enable the reference clock detection */
1334     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1335 
1336     /* Exit Initialization mode */
1337     status = RTC_ExitInitMode(hrtc);
1338   }
1339 
1340   if (status == HAL_OK)
1341   {
1342     hrtc->State = HAL_RTC_STATE_READY;
1343   }
1344 
1345   /* Enable the write protection for RTC registers */
1346   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1347 
1348   /* Process Unlocked */
1349   __HAL_UNLOCK(hrtc);
1350 
1351   return status;
1352 }
1353 
1354 /**
1355   * @brief  Disable the RTC reference clock detection.
1356   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1357   *                the configuration information for RTC.
1358   * @retval HAL status
1359   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1360 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1361 {
1362   HAL_StatusTypeDef status;
1363 
1364   /* Process Locked */
1365   __HAL_LOCK(hrtc);
1366 
1367   hrtc->State = HAL_RTC_STATE_BUSY;
1368 
1369   /* Disable the write protection for RTC registers */
1370   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1371 
1372   /* Enter Initialization mode */
1373   status = RTC_EnterInitMode(hrtc);
1374 
1375   if (status == HAL_OK)
1376   {
1377     /* Disable the reference clock detection */
1378     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1379 
1380     /* Exit Initialization mode */
1381     status = RTC_ExitInitMode(hrtc);
1382   }
1383 
1384   if (status == HAL_OK)
1385   {
1386     hrtc->State = HAL_RTC_STATE_READY;
1387   }
1388 
1389   /* Enable the write protection for RTC registers */
1390   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1391 
1392   /* Process Unlocked */
1393   __HAL_UNLOCK(hrtc);
1394 
1395   return status;
1396 }
1397 
1398 /**
1399   * @}
1400   */
1401 
1402 /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
1403   * @brief    Extended features functions
1404   *
1405 @verbatim
1406  ===============================================================================
1407                  ##### Extended features functions #####
1408  ===============================================================================
1409     [..]  This section provides functions allowing to:
1410       (+) RTC Alarm B callback
1411       (+) RTC Poll for Alarm B request
1412 
1413 @endverbatim
1414   * @{
1415   */
1416 
1417 /**
1418   * @brief  Alarm B callback.
1419   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1420   *                the configuration information for RTC.
1421   * @retval None
1422   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1423 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1424 {
1425   /* Prevent unused argument(s) compilation warning */
1426   UNUSED(hrtc);
1427 
1428   /* NOTE: This function should not be modified, when the callback is needed,
1429            the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1430    */
1431 }
1432 
1433 /**
1434   * @brief  Handles Alarm B Polling request.
1435   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1436   *                the configuration information for RTC.
1437   * @param  Timeout Timeout duration
1438   * @retval HAL status
1439   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1440 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1441 {
1442   uint32_t tickstart = 0U;
1443 
1444   /* Get tick */
1445   tickstart = HAL_GetTick();
1446 
1447   /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
1448   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
1449   {
1450     if (Timeout != HAL_MAX_DELAY)
1451     {
1452       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1453       {
1454         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1455         return HAL_TIMEOUT;
1456       }
1457     }
1458   }
1459 
1460   /* Clear the Alarm flag */
1461   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1462 
1463   /* Change RTC state */
1464   hrtc->State = HAL_RTC_STATE_READY;
1465 
1466   return HAL_OK;
1467 }
1468 
1469 /**
1470   * @}
1471   */
1472 
1473 /**
1474   * @}
1475   */
1476 
1477 #endif /* HAL_RTC_MODULE_ENABLED */
1478 /**
1479   * @}
1480   */
1481 
1482 /**
1483   * @}
1484   */
1485