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