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