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