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