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