]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_rtc_ex.c
Add a qwerty layer
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_rtc_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_rtc_ex.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    5-September-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 Time Stamp functions
11   *           + RTC Tamper functions 
12   *           + RTC Wake-up functions
13   *           + Extension Control functions
14   *           + Extension RTC features functions    
15   *         
16   @verbatim
17   ==============================================================================
18                   ##### How to use this driver #####
19   ==============================================================================
20   [..] 
21     (+) Enable the RTC domain access.
22     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour 
23         format using the HAL_RTC_Init() function.
24   
25   *** RTC Wakeup configuration ***
26   ================================
27   [..] 
28     (+) To configure the RTC Wakeup Clock source and Counter use the HAL_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 enable 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 can be 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 can be 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 "stm32l1xx_hal.h"
95
96 /** @addtogroup STM32L1xx_HAL_Driver
97   * @{
98   */
99   
100 /** @addtogroup RTC
101   * @{
102   */
103   
104 #ifdef HAL_RTC_MODULE_ENABLED
105
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private define ------------------------------------------------------------*/
108 /* Private macro -------------------------------------------------------------*/
109 /* Private variables ---------------------------------------------------------*/
110 /* Private function prototypes -----------------------------------------------*/
111 /* Private functions ---------------------------------------------------------*/
112
113 /** @addtogroup RTC_Exported_Functions
114   * @{
115   */
116
117
118 /** @addtogroup RTC_Exported_Functions_Group1
119   * @{
120   */
121   
122 /**
123   * @brief  DeInitializes the RTC peripheral 
124   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
125   *                the configuration information for RTC.
126   * @note   This function does not reset the RTC Backup Data registers.   
127   * @retval HAL status
128   */
129 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
130 {
131   uint32_t tickstart = 0;
132
133   /* Check the parameters */
134   assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
135
136   /* Set RTC state */
137   hrtc->State = HAL_RTC_STATE_BUSY; 
138   
139   /* Disable the write protection for RTC registers */
140   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
141   
142   /* Set Initialization mode */
143   if(RTC_EnterInitMode(hrtc) != HAL_OK)
144   {
145     /* Enable the write protection for RTC registers */
146     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 
147     
148     /* Set RTC state */
149     hrtc->State = HAL_RTC_STATE_ERROR;
150     
151     return HAL_ERROR;
152   }  
153   else
154   {
155     /* Reset TR, DR and CR registers */
156     hrtc->Instance->TR = (uint32_t)0x00000000;
157     hrtc->Instance->DR = (uint32_t)0x00002101;
158     /* Reset All CR bits except CR[2:0] */
159     hrtc->Instance->CR &= (uint32_t)0x00000007;
160     
161     tickstart = HAL_GetTick();
162     
163     /* Wait till WUTWF flag is set and if Time out is reached exit */
164     while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
165     {
166       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
167       { 
168         /* Enable the write protection for RTC registers */
169         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 
170         
171         /* Set RTC state */
172         hrtc->State = HAL_RTC_STATE_TIMEOUT;
173         
174         return HAL_TIMEOUT;
175       } 
176     }
177     
178     /* Reset all RTC CR register bits */
179     hrtc->Instance->CR &= (uint32_t)0x00000000;
180     hrtc->Instance->WUTR = (uint32_t)0x0000FFFF;
181     hrtc->Instance->PRER = (uint32_t)0x007F00FF;
182     hrtc->Instance->CALIBR = (uint32_t)0x00000000;
183     hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
184     hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
185 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
186     hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
187     hrtc->Instance->CALR = (uint32_t)0x00000000;
188     hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
189     hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
190 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
191     /* Reset ISR register and exit initialization mode */
192     hrtc->Instance->ISR = (uint32_t)0x00000000;
193     
194     /* Reset Tamper and alternate functions configuration register */
195     hrtc->Instance->TAFCR = 0x00000000;
196     
197     /* Wait for synchro */
198     if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
199     {
200       /* Enable the write protection for RTC registers */
201       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  
202       
203       hrtc->State = HAL_RTC_STATE_ERROR;
204       
205       return HAL_ERROR;
206     }  
207   }
208   
209   /* Enable the write protection for RTC registers */
210   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
211   
212   /* De-Initialize RTC MSP */
213   HAL_RTC_MspDeInit(hrtc);
214   
215   hrtc->State = HAL_RTC_STATE_RESET; 
216   
217   /* Release Lock */
218   __HAL_UNLOCK(hrtc);
219
220   return HAL_OK;
221 }
222
223 /**
224   * @}
225   */
226
227 /** @addtogroup RTC_Exported_Functions_Group2
228   * @{
229   */
230   
231 /**
232   * @brief  Gets RTC current time.
233   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
234   *                the configuration information for RTC.
235   * @param  sTime: Pointer to Time structure
236   * @param  Format: Specifies the format of the entered parameters.
237   *          This parameter can be one of the following values:
238   *            @arg FORMAT_BIN: Binary data format 
239   *            @arg FORMAT_BCD: BCD data format
240   * @note   Call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 
241   *         in the higher-order calendar shadow registers.
242   * @retval HAL status
243   */
244 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
245 {
246   uint32_t tmpreg = 0;
247
248   /* Check the parameters */
249   assert_param(IS_RTC_FORMAT(Format));
250
251 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
252   /* Get subseconds values from the correspondent registers*/
253   sTime->SubSeconds = (uint32_t)((hrtc->Instance->SSR) & RTC_SSR_SS);
254 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
255   
256   /* Get the TR register */
257   tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK); 
258   
259   /* Fill the structure fields with the read parameters */
260   sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
261   sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
262   sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
263   sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16); 
264   
265   /* Check the input parameters format */
266   if(Format == FORMAT_BIN)
267   {
268     /* Convert the time structure parameters to Binary format */
269     sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
270     sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
271     sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);  
272   }
273   
274   return HAL_OK;
275 }
276
277 /**
278   * @}
279   */
280
281 /** @addtogroup RTC_Exported_Functions_Group3
282   * @{
283   */
284
285 /**
286   * @brief  Sets the specified RTC Alarm.
287   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
288   *                the configuration information for RTC.
289   * @param  sAlarm: Pointer to Alarm 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_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
297 {
298   uint32_t tickstart = 0;
299   uint32_t tmpreg = 0;
300   
301 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
302   uint32_t subsecondtmpreg = 0;
303 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
304   
305   /* Check the parameters */
306   assert_param(IS_RTC_FORMAT(Format));
307   assert_param(IS_ALARM(sAlarm->Alarm));
308   assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
309   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
310 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
311   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
312   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
313 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
314   
315   /* Process Locked */ 
316   __HAL_LOCK(hrtc);
317   
318   hrtc->State = HAL_RTC_STATE_BUSY;
319   
320   if(Format == FORMAT_BIN)
321   {
322     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
323     {
324       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
325       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
326     } 
327     else
328     {
329       sAlarm->AlarmTime.TimeFormat = 0x00;
330       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
331     }
332     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
333     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
334     
335     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
336     {
337       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
338     }
339     else
340     {
341       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
342     }
343     
344     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
345               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
346               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
347               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
348               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
349               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
350               ((uint32_t)sAlarm->AlarmMask)); 
351   }
352   else
353   {
354     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
355     {
356       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
357       assert_param(IS_RTC_HOUR12(tmpreg));
358       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
359     } 
360     else
361     {
362       sAlarm->AlarmTime.TimeFormat = 0x00;
363       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
364     }
365     
366     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
367     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
368     
369     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
370     {
371       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
372       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));    
373     }
374     else
375     {
376       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
377       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));      
378     }  
379     
380     tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
381               ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
382               ((uint32_t) sAlarm->AlarmTime.Seconds) | \
383               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
384               ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
385               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
386               ((uint32_t)sAlarm->AlarmMask));   
387   }
388   
389 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
390   /* Configure the Alarm A or Alarm B Sub Second registers */
391   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
392 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
393   
394   /* Disable the write protection for RTC registers */
395   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
396
397   /* Configure the Alarm register */
398   if(sAlarm->Alarm == RTC_ALARM_A)
399   {
400     /* Disable the Alarm A interrupt */
401     __HAL_RTC_ALARMA_DISABLE(hrtc);
402     
403     /* In case of interrupt mode is used, the interrupt source must disabled */ 
404     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
405          
406     tickstart = HAL_GetTick();
407     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
408     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
409     {
410       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
411       {
412         /* Enable the write protection for RTC registers */
413         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
414         
415         hrtc->State = HAL_RTC_STATE_TIMEOUT; 
416         
417         /* Process Unlocked */ 
418         __HAL_UNLOCK(hrtc);
419         
420         return HAL_TIMEOUT;
421       }   
422     }
423     
424     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
425 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
426     /* Configure the Alarm A Sub Second register */
427     hrtc->Instance->ALRMASSR = subsecondtmpreg;
428 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
429     /* Configure the Alarm state: Enable Alarm */
430     __HAL_RTC_ALARMA_ENABLE(hrtc);
431   }
432   else
433   {
434     /* Disable the Alarm B interrupt */
435     __HAL_RTC_ALARMB_DISABLE(hrtc);
436     
437     /* In case of interrupt mode is used, the interrupt source must disabled */ 
438     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
439        
440     tickstart = HAL_GetTick();
441     /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
442     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
443     {
444       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
445       {
446         /* Enable the write protection for RTC registers */
447         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
448         
449         hrtc->State = HAL_RTC_STATE_TIMEOUT; 
450         
451         /* Process Unlocked */ 
452         __HAL_UNLOCK(hrtc);
453         
454         return HAL_TIMEOUT;
455       }  
456     }    
457     
458     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
459 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
460     /* Configure the Alarm B Sub Second register */
461     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
462 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
463     /* Configure the Alarm state: Enable Alarm */
464     __HAL_RTC_ALARMB_ENABLE(hrtc); 
465   }
466   
467   /* Enable the write protection for RTC registers */
468   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);   
469   
470   /* Change RTC state */
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  Sets the specified RTC Alarm with Interrupt 
481   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
482   *                the configuration information for RTC.
483   * @param  sAlarm: Pointer to Alarm structure
484   * @param  Format: Specifies the format of the entered parameters.
485   *          This parameter can be one of the following values:
486   *             @arg FORMAT_BIN: Binary data format 
487   *             @arg FORMAT_BCD: BCD data format
488   * @note   The Alarm register can only be written when the corresponding Alarm
489   *         is disabled (Use the HAL_RTC_DeactivateAlarm()).   
490   * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.   
491   * @retval HAL status
492   */
493 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
494 {
495   uint32_t tickstart = 0;
496   uint32_t tmpreg = 0;
497 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
498   uint32_t subsecondtmpreg = 0;
499 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
500   
501   /* Check the parameters */
502   assert_param(IS_RTC_FORMAT(Format));
503   assert_param(IS_ALARM(sAlarm->Alarm));
504   assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
505   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
506 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
507   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
508   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
509 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
510   
511   /* Process Locked */ 
512   __HAL_LOCK(hrtc);
513   
514   hrtc->State = HAL_RTC_STATE_BUSY;
515   
516   if(Format == FORMAT_BIN)
517   {
518     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
519     {
520       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
521       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
522     } 
523     else
524     {
525       sAlarm->AlarmTime.TimeFormat = 0x00;
526       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
527     }
528     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
529     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
530     
531     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
532     {
533       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
534     }
535     else
536     {
537       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
538     }
539     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
540               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
541               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
542               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
543               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
544               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
545               ((uint32_t)sAlarm->AlarmMask)); 
546   }
547   else
548   {
549     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
550     {
551       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
552       assert_param(IS_RTC_HOUR12(tmpreg));
553       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
554     } 
555     else
556     {
557       sAlarm->AlarmTime.TimeFormat = 0x00;
558       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
559     }
560     
561     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
562     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
563     
564     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
565     {
566       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
567       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));    
568     }
569     else
570     {
571       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
572       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));      
573     }
574     tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
575               ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
576               ((uint32_t) sAlarm->AlarmTime.Seconds) | \
577               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
578               ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
579               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
580               ((uint32_t)sAlarm->AlarmMask));     
581   }
582 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
583   /* Configure the Alarm A or Alarm B Sub Second registers */
584   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
585 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
586   
587   /* Disable the write protection for RTC registers */
588   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
589   
590   /* Configure the Alarm register */
591   if(sAlarm->Alarm == RTC_ALARM_A)
592   {
593     /* Disable the Alarm A interrupt */
594     __HAL_RTC_ALARMA_DISABLE(hrtc);
595
596     /* Clear flag alarm A */
597     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
598
599     tickstart = HAL_GetTick();
600     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
601     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
602     {
603       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
604       {
605         /* Enable the write protection for RTC registers */
606         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
607         
608         hrtc->State = HAL_RTC_STATE_TIMEOUT; 
609         
610         /* Process Unlocked */ 
611         __HAL_UNLOCK(hrtc);
612         
613         return HAL_TIMEOUT;
614       }  
615     }
616     
617     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
618 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
619     /* Configure the Alarm A Sub Second register */
620     hrtc->Instance->ALRMASSR = subsecondtmpreg;
621 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
622     /* Configure the Alarm state: Enable Alarm */
623     __HAL_RTC_ALARMA_ENABLE(hrtc);
624     /* Configure the Alarm interrupt */
625     __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
626   }
627   else
628   {
629     /* Disable the Alarm B interrupt */
630     __HAL_RTC_ALARMB_DISABLE(hrtc);
631
632     /* Clear flag alarm B */
633     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
634
635     tickstart = HAL_GetTick();
636     /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
637     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
638     {
639       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
640       {
641         /* Enable the write protection for RTC registers */
642         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
643         
644         hrtc->State = HAL_RTC_STATE_TIMEOUT; 
645         
646         /* Process Unlocked */ 
647         __HAL_UNLOCK(hrtc);
648         
649         return HAL_TIMEOUT;
650       }  
651     }
652
653     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
654 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
655     /* Configure the Alarm B Sub Second register */
656     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
657 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
658     /* Configure the Alarm state: Enable Alarm */
659     __HAL_RTC_ALARMB_ENABLE(hrtc);
660     /* Configure the Alarm interrupt */
661     __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
662   }
663
664   /* RTC Alarm Interrupt Configuration: EXTI configuration */
665   __HAL_RTC_ENABLE_IT(RTC_EXTI_LINE_ALARM_EVENT);
666   
667   EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
668   
669   /* Enable the write protection for RTC registers */
670   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  
671   
672   hrtc->State = HAL_RTC_STATE_READY; 
673   
674   /* Process Unlocked */ 
675   __HAL_UNLOCK(hrtc);  
676   
677   return HAL_OK;
678 }
679
680 /**
681   * @brief  Gets the RTC Alarm value and masks.
682   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
683   *                the configuration information for RTC.
684   * @param  sAlarm: Pointer to Date structure
685   * @param  Alarm: Specifies the Alarm.
686   *          This parameter can be one of the following values:
687   *             @arg RTC_ALARM_A: AlarmA
688   *             @arg RTC_ALARM_B: AlarmB  
689   * @param  Format: Specifies the format of the entered parameters.
690   *          This parameter can be one of the following values:
691   *             @arg FORMAT_BIN: Binary data format 
692   *             @arg FORMAT_BCD: BCD data format
693   * @retval HAL status
694   */
695 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
696 {
697   uint32_t tmpreg = 0;
698 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
699   uint32_t subsecondtmpreg = 0;
700 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
701   
702   /* Check the parameters */
703   assert_param(IS_RTC_FORMAT(Format));
704   assert_param(IS_ALARM(Alarm));
705   
706   if(Alarm == RTC_ALARM_A)
707   {
708     /* AlarmA */
709     sAlarm->Alarm = RTC_ALARM_A;
710     
711     tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
712 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
713     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
714 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */    
715   }
716   else
717   {
718     sAlarm->Alarm = RTC_ALARM_B;
719     
720     tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
721 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
722     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
723 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */    
724   }
725     
726   /* Fill the structure with the read parameters */
727   sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
728   sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
729   sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
730   sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
731 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
732   sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
733 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */  
734   sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
735   sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
736   sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
737     
738   if(Format == FORMAT_BIN)
739   {
740     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
741     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
742     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
743     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
744   }  
745     
746   return HAL_OK;
747 }
748
749 /**
750   * @}
751   */
752
753
754 /** @defgroup RTC_Exported_Functions_Group6 Peripheral Control functions 
755  *  @brief   Peripheral Control functions 
756  *
757 @verbatim   
758  ===============================================================================
759                      ##### Peripheral Control functions #####
760  ===============================================================================  
761     [..]
762     This subsection provides functions allowing to
763       (+) Wait for RTC Time and Date Synchronization
764
765 @endverbatim
766   * @{
767   */
768
769 /**
770   * @brief  Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are 
771   *         synchronized with RTC APB clock.
772   * @note   The RTC Resynchronization mode is write protected, use the 
773   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
774   * @note   To read the calendar through the shadow registers after Calendar 
775   *         initialization, calendar update or after wakeup from low power modes 
776   *         the software must first clear the RSF flag. 
777   *         The software must then wait until it is set again before reading 
778   *         the calendar, which means that the calendar registers have been 
779   *         correctly copied into the RTC_TR and RTC_DR shadow registers.   
780   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
781   *                the configuration information for RTC.
782   * @retval HAL status
783   */
784 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
785 {
786   uint32_t tickstart = 0;
787   
788 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
789   /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
790   if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
791 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
792   {
793     /* Clear RSF flag */
794     hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
795     
796   tickstart = HAL_GetTick();
797
798     /* Wait the registers to be synchronised */
799     while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
800     {
801     if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
802       {       
803         return HAL_TIMEOUT;
804       } 
805     }
806   }
807
808   return HAL_OK;
809 }
810
811 /**
812   * @}
813   */
814
815 /**
816   * @}
817   */
818
819 /**
820   * @}
821   */
822     
823 /** @defgroup RTCEx RTCEx
824   * @brief RTC Extended HAL module driver
825   * @{
826   */
827   
828 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
829   * @{
830   */
831   
832 /** @defgroup RTCEx_Exported_Functions_Group4 RTC TimeStamp and Tamper functions
833   * @brief    RTC TimeStamp and Tamper functions
834   *
835 @verbatim   
836  ===============================================================================
837                  ##### RTC TimeStamp and Tamper functions #####
838  ===============================================================================  
839  
840  [..] This section provides functions allowing to configure TimeStamp feature
841
842 @endverbatim
843   * @{
844   */
845
846 /**
847   * @brief  Sets TimeStamp.
848   * @note   This API must be called before enabling the TimeStamp feature. 
849   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
850   *                the configuration information for RTC.
851   * @param  TimeStampEdge: Specifies the pin edge on which the TimeStamp is 
852   *         activated.
853   *          This parameter can be one of the following values:
854   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the  
855   *                                        rising edge of the related pin.
856   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the 
857   *                                         falling edge of the related pin.
858   * @retval HAL status
859   */
860 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge)
861 {
862   uint32_t tmpreg = 0;
863
864   /* Check the parameters */
865   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
866
867   /* Process Locked */
868   __HAL_LOCK(hrtc);
869
870   hrtc->State = HAL_RTC_STATE_BUSY;
871
872   /* Get the RTC_CR register and clear the bits to be configured */
873   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
874
875   tmpreg|= TimeStampEdge;
876
877   /* Disable the write protection for RTC registers */
878   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
879
880   /* Configure the Time Stamp TSEDGE and Enable bits */
881   hrtc->Instance->CR = (uint32_t)tmpreg;
882
883   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
884
885   /* Enable the write protection for RTC registers */
886   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
887
888   /* Change RTC state */
889   hrtc->State = HAL_RTC_STATE_READY;
890
891   /* Process Unlocked */
892   __HAL_UNLOCK(hrtc);
893
894   return HAL_OK;
895 }
896
897 /**
898   * @brief  Sets TimeStamp with Interrupt. 
899   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
900   *                the configuration information for RTC.
901   * @note   This API must be called before enabling the TimeStamp feature.
902   * @param  TimeStampEdge: Specifies the pin edge on which the TimeStamp is 
903   *         activated.
904   *          This parameter can be one of the following values:
905   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the  
906   *                                        rising edge of the related pin.
907   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the 
908   *                                         falling edge of the related pin.
909   * @retval HAL status
910   */
911 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge)
912 {
913   uint32_t tmpreg = 0;
914
915   /* Check the parameters */
916   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
917
918   /* Process Locked */ 
919   __HAL_LOCK(hrtc);
920
921   hrtc->State = HAL_RTC_STATE_BUSY;
922
923   /* Get the RTC_CR register and clear the bits to be configured */
924   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
925
926   tmpreg |= TimeStampEdge;
927
928   /* Disable the write protection for RTC registers */
929   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
930
931   /* Configure the Time Stamp TSEDGE and Enable bits */
932   hrtc->Instance->CR = (uint32_t)tmpreg;
933
934   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
935
936   /* Enable IT timestamp */
937   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
938
939   /* RTC timestamp Interrupt Configuration: EXTI configuration */
940   __HAL_RTC_ENABLE_IT(RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT);
941
942   EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
943
944   /* Enable the write protection for RTC registers */
945   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
946
947   hrtc->State = HAL_RTC_STATE_READY;
948
949   /* Process Unlocked */
950   __HAL_UNLOCK(hrtc);
951
952   return HAL_OK;
953 }
954
955 /**
956   * @brief  Deactivates TimeStamp. 
957   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
958   *                the configuration information for RTC.
959   * @retval HAL status
960   */
961 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
962 {
963   uint32_t tmpreg = 0;
964
965   /* Process Locked */
966   __HAL_LOCK(hrtc);
967
968   hrtc->State = HAL_RTC_STATE_BUSY;
969
970   /* Disable the write protection for RTC registers */
971   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
972
973   /* In case of interrupt mode is used, the interrupt source must disabled */
974   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
975
976   /* Get the RTC_CR register and clear the bits to be configured */
977   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
978
979   /* Configure the Time Stamp TSEDGE and Enable bits */
980   hrtc->Instance->CR = (uint32_t)tmpreg;
981
982   /* Enable the write protection for RTC registers */
983   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
984
985   hrtc->State = HAL_RTC_STATE_READY;
986
987   /* Process Unlocked */
988   __HAL_UNLOCK(hrtc);
989
990   return HAL_OK;
991 }
992
993 /**
994   * @brief  Gets the RTC TimeStamp value.
995   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
996   *                the configuration information for RTC.
997   * @param  sTimeStamp: Pointer to Time structure
998   * @param  sTimeStampDate: Pointer to Date structure  
999   * @param  Format: specifies the format of the entered parameters.
1000   *          This parameter can be one of the following values:
1001   *             FORMAT_BIN: Binary data format 
1002   *             FORMAT_BCD: BCD data format
1003   * @retval HAL status
1004   */
1005 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
1006 {
1007   uint32_t tmptime = 0, tmpdate = 0;
1008
1009   /* Check the parameters */
1010   assert_param(IS_RTC_FORMAT(Format));
1011
1012   /* Get the TimeStamp time and date registers values */
1013   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
1014   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
1015
1016   /* Fill the Time structure fields with the read parameters */
1017   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
1018   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
1019   sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
1020   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
1021 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1022   sTimeStamp->SubSeconds = (uint32_t)((hrtc->Instance->TSSSR) & RTC_TSSSR_SS);
1023 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1024
1025   /* Fill the Date structure fields with the read parameters */
1026   sTimeStampDate->Year = 0;
1027   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1028   sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
1029   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
1030
1031   /* Check the input parameters format */
1032   if(Format == FORMAT_BIN)
1033   {
1034     /* Convert the TimeStamp structure parameters to Binary format */
1035     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
1036     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
1037     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
1038
1039     /* Convert the DateTimeStamp structure parameters to Binary format */
1040     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
1041     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
1042     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
1043   }
1044
1045   /* Clear the TIMESTAMP Flag */
1046   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
1047
1048   return HAL_OK;
1049 }
1050
1051 /**
1052   * @brief  Sets Tamper
1053   * @note   By calling this API we disable the tamper interrupt for all tampers. 
1054   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1055   *                the configuration information for RTC.
1056   * @param  sTamper: Pointer to Tamper Structure.
1057   * @retval HAL status
1058   */
1059 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1060 {
1061   uint32_t tmpreg = 0;
1062
1063   /* Check the parameters */
1064   assert_param(IS_TAMPER(sTamper->Tamper));
1065   assert_param(IS_TAMPER_TRIGGER(sTamper->Trigger));
1066 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1067   assert_param(IS_TAMPER_FILTER(sTamper->Filter));
1068   assert_param(IS_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1069   assert_param(IS_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1070   assert_param(IS_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1071   assert_param(IS_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1072 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1073
1074   /* Process Locked */
1075   __HAL_LOCK(hrtc);
1076
1077   hrtc->State = HAL_RTC_STATE_BUSY;
1078
1079 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1080   if((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE))
1081   {
1082     /* Configure the RTC_TAFCR register */
1083     sTamper->Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
1084   }
1085   else
1086   {
1087     sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
1088   }
1089
1090   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->Filter |\
1091             (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
1092             (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
1093
1094   hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
1095                                        (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
1096                                        (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPIE);
1097 #else
1098   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Trigger));
1099
1100   hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)RTC_TAFCR_TAMP1E | (uint32_t)RTC_TAFCR_TAMP1TRG);
1101
1102 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1103   hrtc->Instance->TAFCR |= tmpreg;
1104   
1105   hrtc->State = HAL_RTC_STATE_READY; 
1106
1107   /* Process Unlocked */
1108   __HAL_UNLOCK(hrtc);
1109
1110   return HAL_OK;
1111 }
1112
1113 /**
1114   * @brief  Sets Tamper with interrupt.
1115   * @note   By calling this API we force the tamper interrupt for all tampers.
1116   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1117   *                the configuration information for RTC.
1118   * @param  sTamper: Pointer to RTC Tamper.
1119   * @retval HAL status
1120   */
1121 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1122 {
1123   uint32_t tmpreg = 0;
1124
1125   /* Check the parameters */
1126   assert_param(IS_TAMPER(sTamper->Tamper)); 
1127   assert_param(IS_TAMPER_TRIGGER(sTamper->Trigger));
1128 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1129   assert_param(IS_TAMPER_FILTER(sTamper->Filter));
1130   assert_param(IS_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1131   assert_param(IS_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1132   assert_param(IS_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1133   assert_param(IS_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1134 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1135
1136   /* Process Locked */
1137   __HAL_LOCK(hrtc);
1138
1139   hrtc->State = HAL_RTC_STATE_BUSY;
1140
1141 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1142   /* Configure the tamper trigger */
1143   if((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE))
1144   {
1145     sTamper->Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
1146   }
1147   else
1148   {
1149     sTamper->Trigger = (uint32_t) (sTamper->Tamper<<1);
1150   }
1151
1152   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->Filter |\
1153             (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
1154             (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
1155
1156   hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
1157                                        (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
1158                                        (uint32_t)RTC_TAFCR_TAMPPUDIS);
1159 #else
1160   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger);
1161
1162   hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)RTC_TAFCR_TAMP1E | (uint32_t)RTC_TAFCR_TAMP1TRG | (uint32_t)RTC_TAFCR_TAMPIE);
1163 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1164   hrtc->Instance->TAFCR |= tmpreg;
1165
1166   /* Configure the Tamper Interrupt in the RTC_TAFCR */
1167   hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
1168
1169   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1170   __HAL_RTC_ENABLE_IT(RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT);
1171
1172   EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
1173
1174   hrtc->State = HAL_RTC_STATE_READY;
1175
1176   /* Process Unlocked */
1177   __HAL_UNLOCK(hrtc);
1178
1179   return HAL_OK;
1180 }
1181
1182 /**
1183   * @brief  Deactivates Tamper.
1184   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1185   *                the configuration information for RTC.
1186   * @param  Tamper: Selected tamper pin.
1187   *          This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
1188   * @retval HAL status
1189   */
1190 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1191 {
1192   assert_param(IS_TAMPER(Tamper));
1193
1194   /* Process Locked */
1195   __HAL_LOCK(hrtc);
1196
1197   hrtc->State = HAL_RTC_STATE_BUSY;
1198
1199   /* Disable the selected Tamper pin */
1200   hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
1201
1202   hrtc->State = HAL_RTC_STATE_READY;
1203
1204   /* Process Unlocked */
1205   __HAL_UNLOCK(hrtc);
1206
1207   return HAL_OK;
1208 }
1209
1210 /**
1211   * @brief  This function handles TimeStamp interrupt request.
1212   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1213   *                the configuration information for RTC.
1214   * @retval None
1215   */
1216 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
1217 {  
1218   if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
1219   {
1220     /* Get the status of the Interrupt */
1221     if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
1222     {
1223       /* TIMESTAMP callback */ 
1224       HAL_RTCEx_TimeStampEventCallback(hrtc);
1225   
1226       /* Clear the TIMESTAMP interrupt pending bit */
1227       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
1228     }
1229   }
1230
1231   /* Get the status of the Interrupt */
1232   if(__HAL_RTC_TAMPER_GET_IT(hrtc,RTC_IT_TAMP1))
1233   {
1234     /* Get the TAMPER Interrupt enable bit and pending bit */
1235     if(((hrtc->Instance->TAFCR & (RTC_TAFCR_TAMPIE))) != (uint32_t)RESET)
1236     {
1237       /* Tamper callback */ 
1238       HAL_RTCEx_Tamper1EventCallback(hrtc);
1239   
1240       /* Clear the Tamper interrupt pending bit */
1241       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
1242     }
1243   }
1244
1245 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1246   /* Get the status of the Interrupt */
1247   if(__HAL_RTC_TAMPER_GET_IT(hrtc, RTC_IT_TAMP2))
1248   {
1249     /* Get the TAMPER Interrupt enable bit and pending bit */
1250     if(((hrtc->Instance->TAFCR & RTC_TAFCR_TAMPIE)) != (uint32_t)RESET)
1251     {
1252       /* Tamper callback */ 
1253       HAL_RTCEx_Tamper2EventCallback(hrtc);
1254   
1255       /* Clear the Tamper interrupt pending bit */
1256       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
1257     }
1258   }
1259
1260   /* Get the status of the Interrupt */
1261   if(__HAL_RTC_TAMPER_GET_IT(hrtc, RTC_IT_TAMP3))
1262   {
1263     /* Get the TAMPER Interrupt enable bit and pending bit */
1264     if(((hrtc->Instance->TAFCR & RTC_TAFCR_TAMPIE)) != (uint32_t)RESET)
1265     {
1266       /* Tamper callback */
1267       HAL_RTCEx_Tamper3EventCallback(hrtc);
1268
1269       /* Clear the Tamper interrupt pending bit */
1270       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
1271     }
1272   }
1273 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1274
1275   /* Clear the EXTI s Flag for RTC TimeStamp and Tamper */
1276   __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT);
1277
1278   /* Change RTC state */
1279   hrtc->State = HAL_RTC_STATE_READY;
1280 }
1281
1282 /**
1283   * @brief  TimeStamp callback. 
1284   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1285   *                the configuration information for RTC.
1286   * @retval None
1287   */
1288 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
1289 {
1290   /* NOTE : This function Should not be modified, when the callback is needed,
1291             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
1292   */
1293 }
1294
1295 /**
1296   * @brief  Tamper 1 callback. 
1297   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1298   *                the configuration information for RTC.
1299   * @retval None
1300   */
1301 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
1302 {
1303   /* NOTE : This function Should not be modified, when the callback is needed,
1304             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
1305    */
1306 }
1307
1308 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1309 /**
1310   * @brief  Tamper 2 callback. 
1311   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1312   *                the configuration information for RTC.
1313   * @retval None
1314   */
1315 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
1316 {
1317   /* NOTE : This function Should not be modified, when the callback is needed,
1318             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
1319    */
1320 }
1321
1322 /**
1323   * @brief  Tamper 3 callback. 
1324   * @param  hrtc: RTC handle
1325   * @retval None
1326   */
1327 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
1328 {
1329   /* NOTE : This function Should not be modified, when the callback is needed,
1330             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
1331    */
1332 }
1333 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1334
1335 /**
1336   * @brief  This function handles TimeStamp polling request.
1337   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1338   *                the configuration information for RTC.
1339   * @param  Timeout: Timeout duration
1340   * @retval HAL status
1341   */
1342 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1343 {
1344   uint32_t tickstart = HAL_GetTick();
1345
1346   while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
1347   {
1348     if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
1349     {
1350       /* Clear the TIMESTAMP OverRun Flag */
1351       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
1352
1353       /* Change TIMESTAMP state */
1354       hrtc->State = HAL_RTC_STATE_ERROR;
1355
1356       return HAL_ERROR;
1357     }
1358
1359     if(Timeout != HAL_MAX_DELAY)
1360     {
1361       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1362       {
1363         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1364         return HAL_TIMEOUT;
1365       }
1366     }
1367   }
1368
1369   /* Change RTC state */
1370   hrtc->State = HAL_RTC_STATE_READY;
1371
1372   return HAL_OK;
1373 }
1374
1375 /**
1376   * @brief  This function handles Tamper1 Polling.
1377   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1378   *                the configuration information for RTC.
1379   * @param  Timeout: Timeout duration
1380   * @retval HAL status
1381   */
1382 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1383 {  
1384   uint32_t tickstart = HAL_GetTick();
1385
1386   /* Get the status of the Interrupt */
1387   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP1F)== RESET)
1388   {
1389     if(Timeout != HAL_MAX_DELAY)
1390     {
1391       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1392       {
1393         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1394         return HAL_TIMEOUT;
1395       }
1396     }
1397   }
1398
1399   /* Clear the Tamper Flag */
1400   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
1401
1402   /* Change RTC state */
1403   hrtc->State = HAL_RTC_STATE_READY;
1404
1405   return HAL_OK;
1406 }
1407
1408 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1409 /**
1410   * @brief  This function handles Tamper2 Polling.
1411   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1412   *                the configuration information for RTC.
1413   * @param  Timeout: Timeout duration
1414   * @retval HAL status
1415   */
1416 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1417 {  
1418   uint32_t tickstart = HAL_GetTick();
1419
1420   /* Get the status of the Interrupt */
1421   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP2F) == RESET)
1422   {
1423     if(Timeout != HAL_MAX_DELAY)
1424     {
1425       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1426       {
1427         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1428         return HAL_TIMEOUT;
1429       }
1430     }
1431   }
1432
1433   /* Clear the Tamper Flag */
1434   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
1435
1436   /* Change RTC state */
1437   hrtc->State = HAL_RTC_STATE_READY;
1438
1439   return HAL_OK;
1440 }
1441
1442 /**
1443   * @brief  This function handles Tamper3 Polling.
1444   * @param  hrtc: RTC handle
1445   * @param  Timeout: Timeout duration
1446   * @retval HAL status
1447   */
1448 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1449 {  
1450   uint32_t tickstart = HAL_GetTick();
1451
1452   /* Get the status of the Interrupt */
1453   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP3F) == RESET)
1454   {
1455     if(Timeout != HAL_MAX_DELAY)
1456     {
1457       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1458       {
1459         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1460         return HAL_TIMEOUT;
1461       }
1462     }
1463   }
1464
1465   /* Clear the Tamper Flag */
1466   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);
1467
1468   /* Change RTC state */
1469   hrtc->State = HAL_RTC_STATE_READY;
1470
1471   return HAL_OK;
1472 }
1473 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
1474
1475 /**
1476   * @}
1477   */
1478   
1479 /** @defgroup RTCEx_Exported_Functions_Group5 RTC Wake-up functions
1480   * @brief    RTC Wake-up functions
1481   *
1482 @verbatim   
1483  ===============================================================================
1484                         ##### RTC Wake-up functions #####
1485  ===============================================================================  
1486  
1487  [..] This section provides functions allowing to configure Wake-up feature
1488
1489 @endverbatim
1490   * @{
1491   */
1492
1493 /**
1494   * @brief  Sets wake up timer. 
1495   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1496   *                the configuration information for RTC.
1497   * @param  WakeUpCounter: Wake up counter
1498   * @param  WakeUpClock: Wake up clock  
1499   * @retval HAL status
1500   */
1501 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1502 {
1503   uint32_t tickstart = 0;
1504
1505   /* Check the parameters */
1506   assert_param(IS_WAKEUP_CLOCK(WakeUpClock));
1507   assert_param(IS_WAKEUP_COUNTER(WakeUpCounter));
1508
1509   /* Process Locked */
1510   __HAL_LOCK(hrtc);
1511
1512   hrtc->State = HAL_RTC_STATE_BUSY;
1513
1514   /* Disable the write protection for RTC registers */
1515   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1516
1517   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1518      
1519   tickstart = HAL_GetTick();
1520
1521   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1522   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1523   {
1524     if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
1525     {
1526       /* Enable the write protection for RTC registers */
1527       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1528
1529       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1530
1531       /* Process Unlocked */
1532       __HAL_UNLOCK(hrtc);
1533
1534       return HAL_TIMEOUT;
1535     }
1536   }
1537
1538   /* Clear the Wakeup Timer clock source bits in CR register */
1539   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1540
1541   /* Configure the clock source */
1542   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1543
1544   /* Configure the Wakeup Timer counter */
1545   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1546
1547    /* Enable the Wakeup Timer */
1548   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1549
1550   /* Enable the write protection for RTC registers */
1551   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1552
1553   hrtc->State = HAL_RTC_STATE_READY;
1554
1555   /* Process Unlocked */
1556   __HAL_UNLOCK(hrtc);
1557
1558   return HAL_OK;
1559 }
1560
1561 /**
1562   * @brief  Sets wake up timer with interrupt
1563   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1564   *                the configuration information for RTC.
1565   * @param  WakeUpCounter: Wake up counter
1566   * @param  WakeUpClock: Wake up clock  
1567   * @retval HAL status
1568   */
1569 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1570 {
1571   uint32_t tickstart = 0;
1572
1573   /* Check the parameters */
1574   assert_param(IS_WAKEUP_CLOCK(WakeUpClock));
1575   assert_param(IS_WAKEUP_COUNTER(WakeUpCounter));
1576
1577   /* Process Locked */
1578   __HAL_LOCK(hrtc);
1579
1580   hrtc->State = HAL_RTC_STATE_BUSY;
1581
1582   /* Disable the write protection for RTC registers */
1583   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1584
1585   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1586
1587   tickstart = HAL_GetTick();
1588
1589   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1590   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1591   {
1592     if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
1593     {
1594       /* Enable the write protection for RTC registers */
1595       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1596
1597       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1598
1599       /* Process Unlocked */
1600       __HAL_UNLOCK(hrtc);
1601
1602       return HAL_TIMEOUT;
1603     }
1604   }
1605
1606   /* Configure the Wakeup Timer counter */
1607   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1608
1609   /* Clear the Wakeup Timer clock source bits in CR register */
1610   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1611
1612   /* Configure the clock source */
1613   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1614
1615   /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
1616   __HAL_RTC_ENABLE_IT(RTC_EXTI_LINE_WAKEUPTIMER_EVENT);
1617
1618   EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
1619
1620   /* Configure the Interrupt in the RTC_CR register */
1621   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
1622
1623   /* Enable the Wakeup Timer */
1624   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1625
1626   /* Enable the write protection for RTC registers */
1627   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1628
1629   hrtc->State = HAL_RTC_STATE_READY;
1630
1631   /* Process Unlocked */
1632   __HAL_UNLOCK(hrtc);
1633
1634   return HAL_OK;
1635 }
1636
1637 /**
1638   * @brief  Deactivates wake up timer counter.
1639   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1640   *                the configuration information for RTC. 
1641   * @retval HAL status
1642   */
1643 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1644 {
1645   uint32_t tickstart = 0;
1646
1647   /* Process Locked */
1648   __HAL_LOCK(hrtc);
1649
1650   hrtc->State = HAL_RTC_STATE_BUSY;
1651
1652   /* Disable the write protection for RTC registers */
1653   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1654
1655   /* Disable the Wakeup Timer */
1656   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1657
1658   /* In case of interrupt mode is used, the interrupt source must disabled */
1659   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
1660
1661   tickstart = HAL_GetTick();
1662   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1663   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1664   {
1665     if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
1666     {
1667       /* Enable the write protection for RTC registers */
1668       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1669
1670       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1671
1672       /* Process Unlocked */
1673       __HAL_UNLOCK(hrtc);
1674
1675       return HAL_TIMEOUT;
1676     }
1677   }
1678
1679   /* Enable the write protection for RTC registers */
1680   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1681
1682   hrtc->State = HAL_RTC_STATE_READY;
1683
1684   /* Process Unlocked */
1685   __HAL_UNLOCK(hrtc);
1686
1687   return HAL_OK;
1688 }
1689
1690 /**
1691   * @brief  Gets wake up timer counter.
1692   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1693   *                the configuration information for RTC. 
1694   * @retval Counter value
1695   */
1696 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1697 {
1698   /* Get the counter value */
1699   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1700 }
1701
1702 /**
1703   * @brief  This function handles Wake Up Timer interrupt request.
1704   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1705   *                the configuration information for RTC.
1706   * @retval None
1707   */
1708 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1709 {
1710   if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
1711   {
1712     /* Get the status of the Interrupt */
1713     if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
1714     {
1715       /* WAKEUPTIMER callback */
1716       HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1717
1718       /* Clear the WAKEUPTIMER interrupt pending bit */
1719       __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1720     }
1721   }
1722
1723   /* Clear the EXTI s line Flag for RTC WakeUpTimer */
1724   __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_WAKEUPTIMER_EVENT);
1725
1726   /* Change RTC state */
1727   hrtc->State = HAL_RTC_STATE_READY;
1728 }
1729
1730 /**
1731   * @brief  Wake Up Timer callback.
1732   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1733   *                the configuration information for RTC.
1734   * @retval None
1735   */
1736 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1737 {
1738   /* NOTE : This function Should not be modified, when the callback is needed,
1739             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1740    */
1741 }
1742
1743 /**
1744   * @brief  This function handles Wake Up Timer Polling.
1745   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1746   *                the configuration information for RTC.
1747   * @param  Timeout: Timeout duration
1748   * @retval HAL status
1749   */
1750 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1751 {
1752   uint32_t tickstart = HAL_GetTick();
1753
1754   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
1755   {
1756     if(Timeout != HAL_MAX_DELAY)
1757     {
1758       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1759       {
1760         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1761       
1762         return HAL_TIMEOUT;
1763       }
1764     }
1765   }
1766
1767   /* Clear the WAKEUPTIMER Flag */
1768   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1769
1770   /* Change RTC state */
1771   hrtc->State = HAL_RTC_STATE_READY;
1772
1773   return HAL_OK;
1774 }
1775
1776 /**
1777   * @}
1778   */
1779
1780 /** @defgroup RTCEx_Exported_Functions_Group7 Extended Peripheral Control functions
1781   * @brief    Extended Peripheral Control functions
1782   *
1783 @verbatim   
1784  ===============================================================================
1785               ##### Extension Peripheral Control functions #####
1786  ===============================================================================  
1787     [..]
1788     This subsection provides functions allowing to
1789       (+) Writes a data in a specified RTC Backup data register
1790       (+) Read a data in a specified RTC Backup data register
1791       (+) Sets the Coarse calibration parameters.
1792       (+) Deactivates the Coarse calibration parameters
1793       (+) Sets the Smooth calibration parameters.
1794       (+) Configures the Synchronization Shift Control Settings.
1795       (+) Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1796       (+) Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1797       (+) Enables the RTC reference clock detection.
1798       (+) Disable the RTC reference clock detection.
1799       (+) Enables the Bypass Shadow feature.
1800       (+) Disables the Bypass Shadow feature.
1801
1802 @endverbatim
1803   * @{
1804   */
1805
1806 /**
1807   * @brief  Writes a data in a specified RTC Backup data register.
1808   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1809   *                the configuration information for RTC. 
1810   * @param  BackupRegister: RTC Backup data Register number.
1811   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to 
1812   *                                 specify the register.
1813   * @param  Data: Data to be written in the specified RTC Backup data register.                     
1814   * @retval None
1815   */
1816 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1817 {
1818   uint32_t tmp = 0;
1819
1820   /* Check the parameters */
1821   assert_param(IS_RTC_BKP(BackupRegister));
1822   
1823   tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1824   tmp += (BackupRegister * 4);
1825
1826   /* Write the specified register */
1827   *(__IO uint32_t *)tmp = (uint32_t)Data;
1828 }
1829
1830 /**
1831   * @brief  Reads data from the specified RTC Backup data Register.
1832   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1833   *                the configuration information for RTC. 
1834   * @param  BackupRegister: RTC Backup data Register number.
1835   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to 
1836   *                                 specify the register.                   
1837   * @retval Read value
1838   */
1839 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1840 {
1841   uint32_t tmp = 0;
1842
1843   /* Check the parameters */
1844   assert_param(IS_RTC_BKP(BackupRegister));
1845
1846   tmp = (uint32_t)&(hrtc->Instance->BKP0R);
1847   tmp += (BackupRegister * 4);
1848
1849   /* Read the specified register */
1850   return (*(__IO uint32_t *)tmp);
1851 }
1852
1853 /**
1854   * @brief  Sets the Coarse calibration parameters.
1855   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1856   *                the configuration information for RTC.  
1857   * @param  CalibSign: Specifies the sign of the coarse calibration value.
1858   *          This parameter can be  one of the following values :
1859   *             @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive 
1860   *             @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
1861   * @param  Value: value of coarse calibration expressed in ppm (coded on 5 bits).
1862   *    
1863   * @note   This Calibration value should be between 0 and 63 when using negative
1864   *         sign with a 2-ppm step.
1865   *           
1866   * @note   This Calibration value should be between 0 and 126 when using positive
1867   *         sign with a 4-ppm step.
1868   * @retval HAL status
1869   */
1870 HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
1871 {
1872   /* Check the parameters */
1873   assert_param(IS_RTC_CALIB_SIGN(CalibSign));
1874   assert_param(IS_RTC_CALIB_VALUE(Value)); 
1875   
1876   /* Process Locked */ 
1877   __HAL_LOCK(hrtc);
1878   
1879   hrtc->State = HAL_RTC_STATE_BUSY;
1880
1881   /* Disable the write protection for RTC registers */
1882   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1883
1884   /* Set Initialization mode */
1885   if(RTC_EnterInitMode(hrtc) != HAL_OK)
1886   {
1887     /* Enable the write protection for RTC registers */
1888     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 
1889     
1890     /* Set RTC state*/
1891     hrtc->State = HAL_RTC_STATE_ERROR;
1892     
1893     /* Process Unlocked */ 
1894     __HAL_UNLOCK(hrtc);
1895     
1896     return HAL_ERROR;
1897   } 
1898   else
1899   { 
1900     /* Enable the Coarse Calibration */
1901     __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
1902     
1903     /* Set the coarse calibration value */
1904     hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
1905     
1906     /* Exit Initialization mode */
1907     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; 
1908   } 
1909
1910   /* Enable the write protection for RTC registers */
1911   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1912   
1913   /* Change state */
1914   hrtc->State = HAL_RTC_STATE_READY; 
1915   
1916   /* Process Unlocked */ 
1917   __HAL_UNLOCK(hrtc);
1918   
1919   return HAL_OK;
1920 }
1921
1922 /**
1923   * @brief  Deactivates the Coarse calibration parameters.
1924   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1925   *                the configuration information for RTC.  
1926   * @retval HAL status
1927   */
1928 HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
1929
1930   /* Process Locked */ 
1931   __HAL_LOCK(hrtc);
1932   
1933   hrtc->State = HAL_RTC_STATE_BUSY;
1934   
1935   /* Disable the write protection for RTC registers */
1936   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1937
1938   /* Set Initialization mode */
1939   if(RTC_EnterInitMode(hrtc) != HAL_OK)
1940   {
1941     /* Enable the write protection for RTC registers */
1942     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 
1943     
1944     /* Set RTC state*/
1945     hrtc->State = HAL_RTC_STATE_ERROR;
1946     
1947     /* Process Unlocked */ 
1948     __HAL_UNLOCK(hrtc);
1949     
1950     return HAL_ERROR;
1951   } 
1952   else
1953   { 
1954     /* Enable the Coarse Calibration */
1955     __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
1956     
1957     /* Exit Initialization mode */
1958     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; 
1959   } 
1960
1961   /* Enable the write protection for RTC registers */
1962   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1963   
1964   /* Change state */
1965   hrtc->State = HAL_RTC_STATE_READY; 
1966   
1967   /* Process Unlocked */ 
1968   __HAL_UNLOCK(hrtc);
1969   
1970   return HAL_OK;
1971 }
1972
1973 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
1974 /**
1975   * @brief  Sets the Smooth calibration parameters.
1976   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
1977   *                the configuration information for RTC.  
1978   * @param  SmoothCalibPeriod: Select the Smooth Calibration Period.
1979   *          This parameter can be can be one of the following values :
1980   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration periode is 32s.
1981   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration periode is 16s.
1982   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibartion periode is 8s.
1983   * @param  SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
1984   *          This parameter can be one of the following values:
1985   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK puls every 2*11 pulses.
1986   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1987   * @param  SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1988   *          This parameter can be one any value from 0 to 0x000001FF.
1989   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses 
1990   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field 
1991   *         SmouthCalibMinusPulsesValue must be equal to 0.  
1992   * @retval HAL status
1993   */
1994 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
1995 {
1996   uint32_t tickstart = 0;
1997
1998   /* Check the parameters */
1999   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
2000   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
2001   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
2002
2003   /* Process Locked */
2004   __HAL_LOCK(hrtc);
2005
2006   hrtc->State = HAL_RTC_STATE_BUSY;
2007
2008   /* Disable the write protection for RTC registers */
2009   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2010
2011   /* check if a calibration is pending*/
2012   if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
2013   {
2014     tickstart = HAL_GetTick();
2015
2016     /* check if a calibration is pending*/
2017     while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
2018     {
2019       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
2020       {
2021         /* Enable the write protection for RTC registers */
2022         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2023
2024         /* Change RTC state */
2025         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2026
2027         /* Process Unlocked */
2028         __HAL_UNLOCK(hrtc);
2029
2030         return HAL_TIMEOUT;
2031       }
2032     }
2033   }
2034
2035   /* Configure the Smooth calibration settings */
2036   hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
2037
2038   /* Enable the write protection for RTC registers */
2039   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2040
2041   /* Change RTC state */
2042   hrtc->State = HAL_RTC_STATE_READY;
2043
2044   /* Process Unlocked */
2045   __HAL_UNLOCK(hrtc);
2046
2047   return HAL_OK;
2048 }
2049
2050 /**
2051   * @brief  Configures the Synchronization Shift Control Settings.
2052   * @note   When REFCKON is set, firmware must not write to Shift control register. 
2053   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2054   *                the configuration information for RTC.    
2055   * @param  ShiftAdd1S: Select to add or not 1 second to the time calendar.
2056   *          This parameter can be one of the following values :
2057   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar. 
2058   *             @arg RTC_SHIFTADD1S_RESET: No effect.
2059   * @param  ShiftSubFS: Select the number of Second Fractions to substitute.
2060   *          This parameter can be one any value from 0 to 0x7FFF.
2061   * @retval HAL status
2062   */
2063 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
2064 {
2065   uint32_t tickstart = 0;
2066
2067   /* Check the parameters */
2068   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
2069   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
2070
2071   /* Process Locked */
2072   __HAL_LOCK(hrtc);
2073
2074   hrtc->State = HAL_RTC_STATE_BUSY;
2075
2076   /* Disable the write protection for RTC registers */
2077   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2078
2079     tickstart = HAL_GetTick();
2080
2081     /* Wait until the shift is completed*/
2082     while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
2083     {
2084       if((HAL_GetTick() - tickstart ) >  RTC_TIMEOUT_VALUE)
2085       {
2086         /* Enable the write protection for RTC registers */
2087         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2088
2089         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2090
2091         /* Process Unlocked */
2092         __HAL_UNLOCK(hrtc);
2093
2094         return HAL_TIMEOUT;
2095       }
2096     }
2097
2098     /* Check if the reference clock detection is disabled */
2099     if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
2100     {
2101       /* Configure the Shift settings */
2102       hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
2103
2104       /* Wait for synchro */
2105       if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2106       {
2107         /* Enable the write protection for RTC registers */
2108         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2109
2110         hrtc->State = HAL_RTC_STATE_ERROR;
2111
2112         /* Process Unlocked */
2113         __HAL_UNLOCK(hrtc);
2114
2115         return HAL_ERROR;
2116       }
2117     }
2118     else
2119     {
2120       /* Enable the write protection for RTC registers */
2121       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2122
2123       /* Change RTC state */
2124       hrtc->State = HAL_RTC_STATE_ERROR;
2125
2126       /* Process Unlocked */
2127       __HAL_UNLOCK(hrtc);
2128
2129       return HAL_ERROR;
2130     }
2131
2132   /* Enable the write protection for RTC registers */
2133   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2134
2135   /* Change RTC state */
2136   hrtc->State = HAL_RTC_STATE_READY;
2137
2138   /* Process Unlocked */
2139   __HAL_UNLOCK(hrtc);
2140
2141   return HAL_OK;
2142 }
2143 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
2144
2145
2146 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
2147 /**
2148   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2149   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2150   *                the configuration information for RTC.    
2151   * @param  CalibOutput : Select the Calibration output Selection .
2152   *          This parameter can be one of the following values:
2153   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz. 
2154   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
2155   * @retval HAL status
2156   */
2157 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
2158 #else
2159 /**
2160   * @brief  Configure the Calibration Pinout (RTC_CALIB).
2161   * @param  hrtc : RTC handle    
2162   * @retval HAL status
2163   */
2164 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc)
2165 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
2166 {
2167 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
2168   /* Check the parameters */
2169   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
2170 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
2171   
2172   /* Process Locked */
2173   __HAL_LOCK(hrtc);
2174
2175   hrtc->State = HAL_RTC_STATE_BUSY;
2176
2177   /* Disable the write protection for RTC registers */
2178   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2179
2180 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)   
2181   /* Clear flags before config */
2182   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
2183
2184   /* Configure the RTC_CR register */
2185   hrtc->Instance->CR |= (uint32_t)CalibOutput;
2186 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
2187   
2188   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
2189
2190   /* Enable the write protection for RTC registers */
2191   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2192
2193   /* Change RTC state */
2194   hrtc->State = HAL_RTC_STATE_READY;
2195
2196   /* Process Unlocked */
2197   __HAL_UNLOCK(hrtc);
2198
2199   return HAL_OK;
2200 }
2201
2202 /**
2203   * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2204   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2205   *                the configuration information for RTC.    
2206   * @retval HAL status
2207   */
2208 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
2209 {
2210   /* Process Locked */
2211   __HAL_LOCK(hrtc);
2212
2213   hrtc->State = HAL_RTC_STATE_BUSY;
2214
2215   /* Disable the write protection for RTC registers */
2216   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2217
2218   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
2219
2220   /* Enable the write protection for RTC registers */
2221   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2222
2223   /* Change RTC state */
2224   hrtc->State = HAL_RTC_STATE_READY;
2225
2226   /* Process Unlocked */
2227   __HAL_UNLOCK(hrtc);
2228
2229   return HAL_OK;
2230 }
2231
2232 /**
2233   * @brief  Enables the RTC reference clock detection.
2234   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2235   *                the configuration information for RTC.    
2236   * @retval HAL status
2237   */
2238 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
2239 {
2240   /* Process Locked */
2241   __HAL_LOCK(hrtc);
2242
2243   hrtc->State = HAL_RTC_STATE_BUSY;
2244
2245   /* Disable the write protection for RTC registers */
2246   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2247
2248   /* Set Initialization mode */
2249   if(RTC_EnterInitMode(hrtc) != HAL_OK)
2250   {
2251     /* Enable the write protection for RTC registers */
2252     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2253
2254     /* Set RTC state*/
2255     hrtc->State = HAL_RTC_STATE_ERROR;
2256
2257     /* Process Unlocked */
2258     __HAL_UNLOCK(hrtc);
2259
2260     return HAL_ERROR;
2261   }
2262   else
2263   {
2264     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
2265
2266     /* Exit Initialization mode */
2267     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
2268   }
2269
2270   /* Enable the write protection for RTC registers */
2271   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2272
2273    /* Change RTC state */
2274   hrtc->State = HAL_RTC_STATE_READY;
2275
2276   /* Process Unlocked */
2277   __HAL_UNLOCK(hrtc);
2278
2279   return HAL_OK;
2280 }
2281
2282 /**
2283   * @brief  Disable the RTC reference clock detection.
2284   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2285   *                the configuration information for RTC.    
2286   * @retval HAL status
2287   */
2288 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
2289 {
2290   /* Process Locked */
2291   __HAL_LOCK(hrtc);
2292
2293   hrtc->State = HAL_RTC_STATE_BUSY;
2294
2295   /* Disable the write protection for RTC registers */
2296   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2297
2298   /* Set Initialization mode */
2299   if(RTC_EnterInitMode(hrtc) != HAL_OK)
2300   {
2301     /* Enable the write protection for RTC registers */
2302     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2303
2304     /* Set RTC state*/
2305     hrtc->State = HAL_RTC_STATE_ERROR;
2306
2307     /* Process Unlocked */
2308     __HAL_UNLOCK(hrtc);
2309
2310     return HAL_ERROR;
2311   }
2312   else
2313   {
2314     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
2315
2316     /* Exit Initialization mode */
2317     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
2318   }
2319
2320   /* Enable the write protection for RTC registers */
2321   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2322
2323   /* Change RTC state */
2324   hrtc->State = HAL_RTC_STATE_READY;
2325
2326   /* Process Unlocked */
2327   __HAL_UNLOCK(hrtc);
2328
2329   return HAL_OK;
2330 }
2331
2332 #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE) 
2333 /**
2334   * @brief  Enables the Bypass Shadow feature.
2335   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2336   *                the configuration information for RTC.  
2337   * @note   When the Bypass Shadow is enabled the calendar value are taken 
2338   *         directly from the Calendar counter.
2339   * @retval HAL status
2340   */
2341 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
2342 {
2343   /* Process Locked */
2344   __HAL_LOCK(hrtc);
2345
2346   hrtc->State = HAL_RTC_STATE_BUSY;
2347
2348   /* Disable the write protection for RTC registers */
2349   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2350
2351   /* Set the BYPSHAD bit */
2352   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
2353
2354   /* Enable the write protection for RTC registers */
2355   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2356
2357   /* Change RTC state */
2358   hrtc->State = HAL_RTC_STATE_READY;
2359
2360   /* Process Unlocked */
2361   __HAL_UNLOCK(hrtc);
2362
2363   return HAL_OK;
2364 }
2365
2366 /**
2367   * @brief  Disables the Bypass Shadow feature.
2368   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2369   *                the configuration information for RTC.  
2370   * @note   When the Bypass Shadow is enabled the calendar value are taken
2371   *         directly from the Calendar counter.
2372   * @retval HAL status
2373   */
2374 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
2375 {
2376   /* Process Locked */
2377   __HAL_LOCK(hrtc);
2378
2379   hrtc->State = HAL_RTC_STATE_BUSY;
2380
2381   /* Disable the write protection for RTC registers */
2382   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2383
2384   /* Reset the BYPSHAD bit */
2385   hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
2386
2387   /* Enable the write protection for RTC registers */
2388   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2389
2390   /* Change RTC state */
2391   hrtc->State = HAL_RTC_STATE_READY;
2392
2393   /* Process Unlocked */
2394   __HAL_UNLOCK(hrtc);
2395
2396   return HAL_OK;
2397 }
2398 #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
2399
2400 /**
2401   * @}
2402   */
2403
2404 /** @defgroup RTCEx_Exported_Functions_Group8 Extended features functions
2405   * @brief    Extended features functions
2406   *
2407 @verbatim   
2408  ===============================================================================
2409                  ##### Extended features functions #####
2410  ===============================================================================  
2411     [..]  This section provides functions allowing to:
2412       (+) RTC Alram B callback
2413       (+) RTC Poll for Alarm B request
2414                
2415 @endverbatim
2416   * @{
2417   */
2418
2419 /**
2420   * @brief  Alarm B callback.
2421   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2422   *                the configuration information for RTC.
2423   * @retval None
2424   */
2425 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
2426 {
2427   /* NOTE : This function Should not be modified, when the callback is needed,
2428             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
2429    */
2430 }
2431
2432 /**
2433   * @brief  This function handles AlarmB Polling request.
2434   * @param  hrtc: pointer to a RTC_HandleTypeDef structure that contains
2435   *                the configuration information for RTC.
2436   * @param  Timeout: Timeout duration
2437   * @retval HAL status
2438   */
2439 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2440 {  
2441   uint32_t tickstart = HAL_GetTick();
2442   
2443   while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
2444   {
2445     if(Timeout != HAL_MAX_DELAY)
2446     {
2447       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2448       {
2449         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2450         return HAL_TIMEOUT;
2451       }
2452     }
2453   }
2454   
2455   /* Clear the Alarm Flag */
2456   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2457   
2458   /* Change RTC state */
2459   hrtc->State = HAL_RTC_STATE_READY; 
2460   
2461   return HAL_OK; 
2462 }
2463
2464 /**
2465   * @}
2466   */
2467
2468 /**
2469   * @}
2470   */
2471   
2472 /**
2473   * @}
2474   */
2475
2476 #endif /* HAL_RTC_MODULE_ENABLED */
2477
2478 /**
2479   * @}
2480   */
2481
2482 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/