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