]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_rtc.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_rtc.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_rtc.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   RTC HAL module driver.
8   *          This file provides firmware functions to manage the following
9   *          functionalities of the Real Time Clock (RTC) peripheral:
10   *           + Initialization/de-initialization functions
11   *           + I/O operation functions
12   *           + Peripheral Control functions
13   *           + Peripheral State functions
14   *         
15   @verbatim
16   ==============================================================================
17               ##### Backup Domain Operating Condition #####
18   ==============================================================================
19   [..] As long as the supply voltage remains in the operating range, 
20        the RTC never stops, regardless of the device status (Run mode, 
21        low power modes or under reset).
22
23                    ##### Backup Domain Reset #####
24   ==================================================================
25   [..] The backup domain reset sets all RTC registers and the RCC_CSR register 
26        to their reset values.
27   [..] A backup domain reset is generated when one of the following events occurs:
28     (+) Software reset, triggered by setting the RTCRST bit in the 
29         RCC Control Status register (RCC_CSR).
30     (+) Power reset (BOR/POR/PDR).
31
32                    ##### Backup Domain Access #####
33   ==================================================================
34   [..] After reset, the backup domain (RTC registers and RTC backup data registers) 
35        is protected against possible unwanted write accesses. 
36   [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
37     (+) Enable the Power Controller (PWR) APB1 interface clock using the
38         __HAL_RCC_PWR_CLK_ENABLE() function.
39     (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
40     (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
41     (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
42
43
44                   ##### How to use RTC Driver #####
45  ===================================================================
46   [..]
47     (+) Enable the RTC domain access (see description in the section above).
48     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
49         format using the HAL_RTC_Init() function.
50
51   *** Time and Date configuration ***
52   ===================================
53   [..] 
54     (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
55         and HAL_RTC_SetDate() functions.
56     (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
57
58   *** Alarm configuration ***
59   ===========================
60   [..]
61     (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
62             You can also configure the RTC Alarm with interrupt mode using the 
63             HAL_RTC_SetAlarm_IT() function.
64     (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
65
66   *** RTC Wakeup configuration ***
67   ================================
68   [..] 
69     (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
70         function. You can also configure the RTC Wakeup timer with interrupt mode
71         using the HAL_RTC_SetWakeUpTimer_IT() function.
72     (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
73         function.
74         
75   *** Outputs configuration ***
76   =============================
77   [..]  The RTC has 2 different outputs:
78     (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
79         and WaKeUp signals.
80         To output the selected RTC signal, use the HAL_RTC_Init() function.             
81     (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
82         To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
83     (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB14) managed on 
84         the RTC_OR register.
85     (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
86         automatically configured in output alternate function. 
87         
88   *** Smooth digital Calibration configuration ***
89   ================================================
90   [..]
91     (+) Configure the RTC Original Digital Calibration Value and the corresponding
92         calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib() 
93         function.
94
95   *** TimeStamp configuration ***
96   ===============================
97   [..]
98     (+) Enables the RTC TimeStamp using the HAL_RTC_SetTimeStamp() function.
99         You can also configure the RTC TimeStamp with interrupt mode using the
100         HAL_RTC_SetTimeStamp_IT() function.
101     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
102         function.
103
104   *** Tamper configuration ***
105   ============================
106   [..]
107     (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge 
108         or Level according to the Tamper filter (if equal to 0 Edge else Level) 
109         value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and
110         Pull-UP using the HAL_RTC_SetTamper() function. You can configure RTC Tamper
111         with interrupt mode using HAL_RTC_SetTamper_IT() function.
112     (+) The default configuration of the Tamper erases the backup registers. To avoid
113         erase, enable the NoErase field on the RTC_TAMPCR register.
114
115   *** Backup Data Registers configuration ***
116   ===========================================
117   [..]
118     (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
119         function.
120     (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
121         function.
122     (+) The backup registers are reset when a tamper detection event occurs        
123
124
125                   ##### RTC and low power modes #####
126   ==================================================================
127   [..] The MCU can be woken up from a low power mode by an RTC alternate
128        function.
129   [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
130        RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
131        These RTC alternate functions can wake up the system from the Stop and 
132        Standby low power modes.
133   [..] The system can also wake up from low power modes without depending
134        on an external interrupt (Auto-wakeup mode), by using the RTC alarm
135        or the RTC wakeup events.
136   [..] The RTC provides a programmable time base for waking up from the
137        Stop or Standby mode at regular intervals.
138        Wakeup from STOP and Standby modes is possible only when the RTC clock source
139        is LSE or LSI.
140
141    @endverbatim
142   ******************************************************************************
143   * @attention
144   *
145   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
146   *
147   * Redistribution and use in source and binary forms, with or without modification,
148   * are permitted provided that the following conditions are met:
149   *   1. Redistributions of source code must retain the above copyright notice,
150   *      this list of conditions and the following disclaimer.
151   *   2. Redistributions in binary form must reproduce the above copyright notice,
152   *      this list of conditions and the following disclaimer in the documentation
153   *      and/or other materials provided with the distribution.
154   *   3. Neither the name of STMicroelectronics nor the names of its contributors
155   *      may be used to endorse or promote products derived from this software
156   *      without specific prior written permission.
157   *
158   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
159   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
161   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
162   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
163   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
164   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
165   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
166   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
167   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168   *
169   ******************************************************************************  
170   */
171
172 /* Includes ------------------------------------------------------------------*/
173 #include "stm32l0xx_hal.h"
174
175 /** @addtogroup STM32L0xx_HAL_Driver
176   * @{
177   */
178
179 /** @addtogroup RTC
180   * @brief RTC HAL module driver
181   * @{
182   */
183
184 #ifdef HAL_RTC_MODULE_ENABLED
185
186 /* Private typedef -----------------------------------------------------------*/
187 /* Private define ------------------------------------------------------------*/
188 /* Private macro -------------------------------------------------------------*/
189 /* Private variables ---------------------------------------------------------*/
190 /* Private function prototypes -----------------------------------------------*/
191 /* Exported functions --------------------------------------------------------*/
192
193 /** @addtogroup RTC_Exported_Functions
194   * @{
195   */
196
197 /** @addtogroup RTC_Exported_Functions_Group1
198  *  @brief    Initialization and Configuration functions 
199  *
200 @verbatim
201  ===============================================================================
202               ##### Initialization and de-initialization functions #####
203  ===============================================================================
204    [..] This section provides functions allowing to initialize and configure the 
205          RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable 
206          RTC registers Write protection, enter and exit the RTC initialization mode,
207          RTC registers synchronization check and reference clock detection enable.
208          (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
209              It is split into 2 programmable prescalers to minimize power consumption.
210              (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
211              (++) When both prescalers are used, it is recommended to configure the 
212                  asynchronous prescaler to a high value to minimize power consumption.
213          (#) All RTC registers are Write protected. Writing to the RTC registers
214              is enabled by writing a key into the Write Protection register, RTC_WPR.
215          (#) To configure the RTC Calendar, user application should enter 
216              initialization mode. In this mode, the calendar counter is stopped
217              and its value can be updated. When the initialization sequence is 
218              complete, the calendar restarts counting after 4 RTCCLK cycles.
219          (#) To read the calendar through the shadow registers after Calendar 
220              initialization, calendar update or after wakeup from low power modes
221              the software must first clear the RSF flag. The software must then
222              wait until it is set again before reading the calendar, which means
223              that the calendar registers have been correctly copied into the
224              RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function 
225              implements the above software sequence (RSF clear and RSF check).
226
227 @endverbatim
228   * @{
229   */
230
231 /**
232   * @brief  Initializes the RTC peripheral
233   * @param  hrtc: RTC handle
234   * @retval HAL status
235   */
236 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
237 {
238   /* Check the RTC peripheral state */
239   if(hrtc == NULL)
240   {
241      return HAL_ERROR;
242   }
243
244   /* Check the parameters */
245   assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
246   assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
247   assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
248   assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
249   assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
250   assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
251   assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
252   assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
253   
254   if(hrtc->State == HAL_RTC_STATE_RESET)
255   {
256     /* Initialize RTC MSP */
257     HAL_RTC_MspInit(hrtc);
258   }
259
260   /* Set RTC state */
261   hrtc->State = HAL_RTC_STATE_BUSY;
262
263   /* Disable the write protection for RTC registers */
264   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
265
266   /* Set Initialization mode */
267   if(RTC_EnterInitMode(hrtc) != HAL_OK)
268   {
269     /* Enable the write protection for RTC registers */
270     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
271
272     /* Set RTC state */
273     hrtc->State = HAL_RTC_STATE_ERROR;
274
275     return HAL_ERROR;
276   }
277   else
278   {
279     /* Clear RTC_CR FMT, OSEL and POL Bits */
280     hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
281     /* Set RTC_CR register */
282     hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
283
284     /* Configure the RTC PRER */
285     hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
286     hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
287
288     /* Exit Initialization mode */
289     hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
290
291     hrtc->Instance->OR &= (uint32_t)~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
292     hrtc->Instance->OR |= (uint32_t)(hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
293
294     /* Enable the write protection for RTC registers */
295     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
296
297     /* Set RTC state */
298     hrtc->State = HAL_RTC_STATE_READY;
299
300     return HAL_OK;
301   }
302 }
303
304 /**
305   * @brief  DeInitializes the RTC peripheral
306   * @param  hrtc: RTC handle
307   * @note   This function doesn't reset the RTC Backup Data registers.
308   * @retval HAL status
309   */
310 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
311 {
312   uint32_t tickstart = 0;
313
314   /* Check the parameters */
315   assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
316
317   /* Set RTC state */
318   hrtc->State = HAL_RTC_STATE_BUSY;
319
320   /* Disable the write protection for RTC registers */
321   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
322
323   /* Set Initialization mode */
324   if(RTC_EnterInitMode(hrtc) != HAL_OK)
325   {
326     /* Enable the write protection for RTC registers */
327     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
328
329     /* Set RTC state */
330     hrtc->State = HAL_RTC_STATE_ERROR;
331
332     return HAL_ERROR;
333   }
334   else
335   {
336     /* Reset TR, DR and CR registers */
337     hrtc->Instance->TR = (uint32_t)0x00000000;
338     hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
339     /* Reset All CR bits except CR[2:0] */
340     hrtc->Instance->CR &= RTC_CR_WUCKSEL;
341
342     tickstart = HAL_GetTick();
343
344     /* Wait till WUTWF flag is set and if Time out is reached exit */
345     while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
346     {
347       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
348       {
349         /* Enable the write protection for RTC registers */
350         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
351
352         /* Set RTC state */
353         hrtc->State = HAL_RTC_STATE_TIMEOUT;
354
355         return HAL_TIMEOUT;
356       }
357     }
358
359     /* Reset all RTC CR register bits */
360     hrtc->Instance->CR &= (uint32_t)0x00000000;
361     hrtc->Instance->WUTR = RTC_WUTR_WUT;
362     hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FF));
363     hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
364     hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
365     hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
366     hrtc->Instance->CALR = (uint32_t)0x00000000;
367     hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
368     hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
369
370     /* Reset ISR register and exit initialization mode */
371     hrtc->Instance->ISR = (uint32_t)0x00000000;
372
373     /* Reset Tamper configuration register */
374     hrtc->Instance->TAMPCR = 0x00000000;
375
376     /* Reset Option register */
377     hrtc->Instance->OR = 0x00000000;
378
379     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
380     if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
381     {
382       if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
383       {
384         /* Enable the write protection for RTC registers */
385         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  
386
387         hrtc->State = HAL_RTC_STATE_ERROR;
388
389         return HAL_ERROR;
390       }
391     }
392   }
393
394   /* Enable the write protection for RTC registers */
395   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
396
397   /* De-Initialize RTC MSP */
398   HAL_RTC_MspDeInit(hrtc);
399
400   hrtc->State = HAL_RTC_STATE_RESET;
401   
402   /* Release Lock */
403   __HAL_UNLOCK(hrtc);
404
405   return HAL_OK;
406 }
407
408 /**
409   * @brief  Initializes the RTC MSP.
410   * @param  hrtc: RTC handle  
411   * @retval None
412   */
413 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
414 {
415   /* NOTE : This function Should not be modified, when the callback is needed,
416             the HAL_RTC_MspInit could be implenetd in the user file
417    */
418 }
419
420 /**
421   * @brief  DeInitializes the RTC MSP.
422   * @param  hrtc: RTC handle 
423   * @retval None
424   */
425 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
426 {
427   /* NOTE : This function Should not be modified, when the callback is needed,
428             the HAL_RTC_MspDeInit could be implenetd in the user file
429    */ 
430 }
431
432 /**
433   * @}
434   */
435
436 /** @addtogroup RTC_Exported_Functions_Group2
437  *  @brief   RTC Time and Date functions
438  *
439 @verbatim
440  ===============================================================================
441                  ##### RTC Time and Date functions #####
442  ===============================================================================
443  
444  [..] This section provide functions allowing to configure Time and Date features
445
446 @endverbatim
447   * @{
448   */
449
450 /**
451   * @brief  Sets RTC current time.
452   * @param  hrtc: RTC handle
453   * @param  sTime: Pointer to Time structure
454   * @param  Format: Specifies the format of the entered parameters.
455   *          This parameter can be one of the following values:
456   *            @arg RTC_FORMAT_BIN: Binary data format 
457   *            @arg RTC_FORMAT_BCD
458 : BCD data format
459   * @retval HAL status
460   */
461 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
462 {
463   uint32_t tmpreg = 0;
464
465  /* Check the parameters */
466   assert_param(IS_RTC_FORMAT(Format));
467   assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
468   assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
469
470   /* Process Locked */ 
471   __HAL_LOCK(hrtc);
472
473   hrtc->State = HAL_RTC_STATE_BUSY;
474
475   if(Format == RTC_FORMAT_BIN)
476   {
477     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
478     {
479       assert_param(IS_RTC_HOUR12(sTime->Hours));
480       assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
481     }
482     else
483     {
484       sTime->TimeFormat = 0x00;
485       assert_param(IS_RTC_HOUR24(sTime->Hours));
486     }
487     assert_param(IS_RTC_MINUTES(sTime->Minutes));
488     assert_param(IS_RTC_SECONDS(sTime->Seconds));
489
490     tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
491                         ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
492                         ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
493                         (((uint32_t)sTime->TimeFormat) << 16));
494   }
495   else
496   {
497     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
498     {
499       tmpreg = RTC_Bcd2ToByte(sTime->Hours);
500       assert_param(IS_RTC_HOUR12(tmpreg));
501       assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
502     }
503     else
504     {
505       sTime->TimeFormat = 0x00;
506       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
507     }
508     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
509     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
510     tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
511               ((uint32_t)(sTime->Minutes) << 8) | \
512               ((uint32_t)sTime->Seconds) | \
513               ((uint32_t)(sTime->TimeFormat) << 16));
514   }
515   UNUSED(tmpreg);
516   /* Disable the write protection for RTC registers */
517   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
518
519   /* Set Initialization mode */
520   if(RTC_EnterInitMode(hrtc) != HAL_OK)
521   {
522     /* Enable the write protection for RTC registers */
523     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
524
525     /* Set RTC state */
526     hrtc->State = HAL_RTC_STATE_ERROR;
527
528     /* Process Unlocked */ 
529     __HAL_UNLOCK(hrtc);
530
531     return HAL_ERROR;
532   }
533   else
534   {
535     /* Set the RTC_TR register */
536     hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
537
538     /* Clear the bits to be configured */
539     hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BCK);
540
541     /* Configure the RTC_CR register */
542     hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
543
544     /* Exit Initialization mode */
545     hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
546
547     /* If  CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
548     if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
549     {
550       if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
551       {
552         /* Enable the write protection for RTC registers */
553         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
554
555         hrtc->State = HAL_RTC_STATE_ERROR;
556
557         /* Process Unlocked */ 
558         __HAL_UNLOCK(hrtc);
559
560         return HAL_ERROR;
561       }
562     }
563
564     /* Enable the write protection for RTC registers */
565     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
566     
567    hrtc->State = HAL_RTC_STATE_READY;
568
569    __HAL_UNLOCK(hrtc); 
570
571    return HAL_OK;
572   }
573 }
574
575 /**
576   * @brief  Gets RTC current time.
577   * @param  hrtc: RTC handle
578   * @param  sTime: Pointer to Time structure
579   * @param  Format: Specifies the format of the entered parameters.
580   *          This parameter can be one of the following values:
581   *            @arg RTC_FORMAT_BIN: Binary data format 
582   *            @arg RTC_FORMAT_BCD
583 : BCD data format
584   * @note   You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values in the
585   *         higher-order calendar shadow registers.
586   *         Reading RTC current time locks the values in calendar shadow registers until current
587   *         date is read to ensure consistency between the time and date values.
588   * @retval HAL status
589   */
590 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
591 {
592   uint32_t tmpreg = 0;
593
594   /* Check the parameters */
595   assert_param(IS_RTC_FORMAT(Format));
596
597   /* Get subseconds values from the correspondent registers*/
598   sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
599
600   /* Get the TR register */
601   tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
602
603   /* Fill the structure fields with the read parameters */
604   sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
605   sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
606   sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
607   sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
608
609   /* Check the input parameters format */
610   if(Format == RTC_FORMAT_BIN)
611   {
612     /* Convert the time structure parameters to Binary format */
613     sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
614     sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
615     sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
616   }
617
618   return HAL_OK;
619 }
620
621 /**
622   * @brief  Sets RTC current date.
623   * @param  hrtc: RTC handle
624   * @param  sDate: Pointer to date structure
625   * @param  Format: specifies the format of the entered parameters.
626   *          This parameter can be one of the following values:
627   *            @arg RTC_FORMAT_BIN: Binary data format 
628   *            @arg RTC_FORMAT_BCD
629 : BCD data format
630   * @retval HAL status
631   */
632 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
633 {
634   uint32_t datetmpreg = 0;
635
636  /* Check the parameters */
637   assert_param(IS_RTC_FORMAT(Format));
638
639  /* Process Locked */
640  __HAL_LOCK(hrtc);
641
642   hrtc->State = HAL_RTC_STATE_BUSY;
643
644   if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
645   {
646     sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
647   }
648
649   assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
650
651   if(Format == RTC_FORMAT_BIN)
652   {
653     assert_param(IS_RTC_YEAR(sDate->Year));
654     assert_param(IS_RTC_MONTH(sDate->Month));
655     assert_param(IS_RTC_DATE(sDate->Date));
656
657    datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
658                  ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
659                  ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
660                  ((uint32_t)sDate->WeekDay << 13));
661   }
662   else
663   {
664     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
665     datetmpreg = RTC_Bcd2ToByte(sDate->Month);
666     assert_param(IS_RTC_MONTH(datetmpreg));
667     datetmpreg = RTC_Bcd2ToByte(sDate->Date);
668     assert_param(IS_RTC_DATE(datetmpreg));
669
670     datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
671                   (((uint32_t)sDate->Month) << 8) | \
672                   ((uint32_t)sDate->Date) | \
673                   (((uint32_t)sDate->WeekDay) << 13));
674   }
675
676   /* Disable the write protection for RTC registers */
677   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
678
679   /* Set Initialization mode */
680   if(RTC_EnterInitMode(hrtc) != HAL_OK)
681   {
682     /* Enable the write protection for RTC registers */
683     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
684
685     /* Set RTC state*/
686     hrtc->State = HAL_RTC_STATE_ERROR;
687
688     /* Process Unlocked */ 
689     __HAL_UNLOCK(hrtc);
690
691     return HAL_ERROR;
692   }
693   else
694   {
695     /* Set the RTC_DR register */
696     hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
697
698     /* Exit Initialization mode */
699     hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
700
701     /* If  CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
702     if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
703     {
704       if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
705       {
706         /* Enable the write protection for RTC registers */
707         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);  
708
709         hrtc->State = HAL_RTC_STATE_ERROR;
710
711         /* Process Unlocked */ 
712         __HAL_UNLOCK(hrtc);
713
714         return HAL_ERROR;
715       }
716     }
717
718     /* Enable the write protection for RTC registers */
719     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
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 /**
731   * @brief  Gets RTC current date.
732   * @param  hrtc: RTC handle
733   * @param  sDate: Pointer to Date structure
734   * @param  Format: Specifies the format of the entered parameters.
735   *          This parameter can be one of the following values:
736   *            @arg RTC_FORMAT_BIN:  Binary data format 
737   *            @arg RTC_FORMAT_BCD
738 :  BCD data format
739   * @retval HAL status
740   */
741 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
742 {
743   uint32_t datetmpreg = 0;
744
745   /* Check the parameters */
746   assert_param(IS_RTC_FORMAT(Format));
747
748   /* Get the DR register */
749   datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
750
751   /* Fill the structure fields with the read parameters */
752   sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
753   sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
754   sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
755   sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13); 
756
757   /* Check the input parameters format */
758   if(Format == RTC_FORMAT_BIN)
759   {
760     /* Convert the date structure parameters to Binary format */
761     sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
762     sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
763     sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
764   }
765   return HAL_OK;
766 }
767
768 /**
769   * @}
770   */
771
772 /** @addtogroup RTC_Exported_Functions_Group3
773  *  @brief   RTC Alarm functions
774  *
775 @verbatim   
776  ===============================================================================
777                  ##### RTC Alarm functions #####
778  ===============================================================================  
779  
780  [..] This section provides functions allowing to configure Alarm feature
781
782 @endverbatim
783   * @{
784   */
785 /**
786   * @brief  Sets the specified RTC Alarm.
787   * @param  hrtc: RTC handle
788   * @param  sAlarm: Pointer to Alarm structure
789   * @param  Format: Specifies the format of the entered parameters.
790   *          This parameter can be one of the following values:
791   *             @arg RTC_FORMAT_BIN: Binary data format 
792   *             @arg RTC_FORMAT_BCD
793 : BCD data format
794   * @retval HAL status
795   */
796 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
797 {
798   uint32_t tickstart = 0;
799   uint32_t tmpreg = 0, subsecondtmpreg = 0;
800
801   /* Check the parameters */
802   assert_param(IS_RTC_FORMAT(Format));
803   assert_param(IS_RTC_ALARM(sAlarm->Alarm));
804   assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
805   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
806   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
807   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
808
809   /* Process Locked */
810   __HAL_LOCK(hrtc);
811
812   hrtc->State = HAL_RTC_STATE_BUSY;
813
814   if(Format == RTC_FORMAT_BIN)
815   {
816     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
817     {
818       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
819       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
820     }
821     else
822     {
823       sAlarm->AlarmTime.TimeFormat = 0x00;
824       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
825     }
826     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
827     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
828
829     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
830     {
831       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
832     }
833     else
834     {
835       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
836     }
837
838     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
839               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
840               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
841               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
842               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
843               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
844               ((uint32_t)sAlarm->AlarmMask));
845   }
846   else
847   {
848     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
849     {
850       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
851       assert_param(IS_RTC_HOUR12(tmpreg));
852       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
853     }
854     else
855     {
856       sAlarm->AlarmTime.TimeFormat = 0x00;
857       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
858     }
859
860     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
861     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
862
863     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
864     {
865       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
866       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
867     }
868     else
869     {
870       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
871       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
872     }
873
874     tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
875               ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
876               ((uint32_t) sAlarm->AlarmTime.Seconds) | \
877               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
878               ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
879               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
880               ((uint32_t)sAlarm->AlarmMask));   
881   }
882
883   /* Configure the Alarm A or Alarm B Sub Second registers */
884   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
885
886   /* Disable the write protection for RTC registers */
887   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
888
889   /* Configure the Alarm register */
890   if(sAlarm->Alarm == RTC_ALARM_A)
891   {
892     /* Disable the Alarm A interrupt */
893     __HAL_RTC_ALARMA_DISABLE(hrtc);
894
895     /* In case of interrupt mode is used, the interrupt source must disabled */
896     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
897
898     tickstart = HAL_GetTick();
899     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
900     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
901     {
902       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
903       {
904         /* Enable the write protection for RTC registers */
905         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
906         
907         hrtc->State = HAL_RTC_STATE_TIMEOUT;
908
909         /* Process Unlocked */ 
910         __HAL_UNLOCK(hrtc);
911
912         return HAL_TIMEOUT;
913       }
914     }
915
916     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
917     /* Configure the Alarm A Sub Second register */
918     hrtc->Instance->ALRMASSR = subsecondtmpreg;
919     /* Configure the Alarm state: Enable Alarm */
920     __HAL_RTC_ALARMA_ENABLE(hrtc);
921   }
922   else
923   {
924     /* Disable the Alarm B interrupt */
925     __HAL_RTC_ALARMB_DISABLE(hrtc);
926
927     /* In case of interrupt mode is used, the interrupt source must disabled */
928     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
929
930     tickstart = HAL_GetTick();
931     /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
932     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
933     {
934       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
935       {
936         /* Enable the write protection for RTC registers */
937         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
938
939         hrtc->State = HAL_RTC_STATE_TIMEOUT;
940
941         /* Process Unlocked */
942         __HAL_UNLOCK(hrtc);
943
944         return HAL_TIMEOUT;
945       }
946     }
947
948     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
949     /* Configure the Alarm B Sub Second register */
950     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
951     /* Configure the Alarm state: Enable Alarm */
952     __HAL_RTC_ALARMB_ENABLE(hrtc);
953   }
954
955   /* Enable the write protection for RTC registers */
956   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
957
958   /* Change RTC state */
959   hrtc->State = HAL_RTC_STATE_READY;
960
961   /* Process Unlocked */
962   __HAL_UNLOCK(hrtc);
963
964   return HAL_OK;
965 }
966
967 /**
968   * @brief  Sets the specified RTC Alarm with Interrupt
969   * @param  hrtc: RTC handle
970   * @param  sAlarm: Pointer to Alarm structure
971   * @param  Format: Specifies the format of the entered parameters.
972   *          This parameter can be one of the following values:
973   *             @arg RTC_FORMAT_BIN: Binary data format 
974   *             @arg RTC_FORMAT_BCD
975 : BCD data format
976   * @note   The Alarm register can only be written when the corresponding Alarm
977   *         is disabled (Use the HAL_RTC_DeactivateAlarm()).   
978   * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.   
979   * @retval HAL status
980   */
981 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
982 {
983   uint32_t tickstart = 0;
984   uint32_t tmpreg = 0, subsecondtmpreg = 0;
985
986   /* Check the parameters */
987   assert_param(IS_RTC_FORMAT(Format));
988   assert_param(IS_RTC_ALARM(sAlarm->Alarm));
989   assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
990   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
991   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
992   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
993
994   /* Process Locked */ 
995   __HAL_LOCK(hrtc);
996
997   hrtc->State = HAL_RTC_STATE_BUSY;
998
999   if(Format == RTC_FORMAT_BIN)
1000   {
1001     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1002     {
1003       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1004       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1005     }
1006     else
1007     {
1008       sAlarm->AlarmTime.TimeFormat = 0x00;
1009       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1010     }
1011     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1012     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1013
1014     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1015     {
1016       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1017     }
1018     else
1019     {
1020       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1021     }
1022     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
1023               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
1024               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1025               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1026               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
1027               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1028               ((uint32_t)sAlarm->AlarmMask));
1029   }
1030   else
1031   {
1032     if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1033     {
1034       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1035       assert_param(IS_RTC_HOUR12(tmpreg));
1036       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1037     } 
1038     else
1039     {
1040       sAlarm->AlarmTime.TimeFormat = 0x00;
1041       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1042     }
1043
1044     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1045     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1046     
1047     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1048     {
1049       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1050       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1051     }
1052     else
1053     {
1054       tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1055       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1056     }
1057     tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
1058               ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
1059               ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1060               ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1061               ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
1062               ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1063               ((uint32_t)sAlarm->AlarmMask));     
1064   }
1065   /* Configure the Alarm A or Alarm B Sub Second registers */
1066   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1067
1068   /* Disable the write protection for RTC registers */
1069   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1070
1071   /* Configure the Alarm register */
1072   if(sAlarm->Alarm == RTC_ALARM_A)
1073   {
1074     /* Disable the Alarm A interrupt */
1075     __HAL_RTC_ALARMA_DISABLE(hrtc);
1076     
1077     /* Clear flag alarm A */
1078     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1079
1080     tickstart = HAL_GetTick();
1081     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1082     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1083     {
1084       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1085       {
1086         /* Enable the write protection for RTC registers */
1087         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1088
1089         hrtc->State = HAL_RTC_STATE_TIMEOUT; 
1090
1091         /* Process Unlocked */
1092         __HAL_UNLOCK(hrtc);
1093
1094         return HAL_TIMEOUT;
1095       }
1096     }
1097
1098     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1099     /* Configure the Alarm A Sub Second register */
1100     hrtc->Instance->ALRMASSR = subsecondtmpreg;
1101     /* Configure the Alarm state: Enable Alarm */
1102     __HAL_RTC_ALARMA_ENABLE(hrtc);
1103     /* Configure the Alarm interrupt */
1104     __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1105   }
1106   else
1107   {
1108     /* Disable the Alarm B interrupt */
1109     __HAL_RTC_ALARMB_DISABLE(hrtc);
1110     
1111     /* Clear flag alarm B */
1112     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1113
1114     tickstart = HAL_GetTick();
1115     /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1116     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1117     {
1118       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1119       {
1120         /* Enable the write protection for RTC registers */
1121         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1122
1123         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1124
1125         /* Process Unlocked */
1126         __HAL_UNLOCK(hrtc);
1127
1128         return HAL_TIMEOUT;
1129       }
1130     }
1131
1132     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1133     /* Configure the Alarm B Sub Second register */
1134     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1135     /* Configure the Alarm state: Enable Alarm */
1136     __HAL_RTC_ALARMB_ENABLE(hrtc);
1137     /* Configure the Alarm interrupt */
1138     __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1139   }
1140
1141   /* RTC Alarm Interrupt Configuration: EXTI configuration */
1142   __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1143
1144   __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1145
1146   /* Enable the write protection for RTC registers */
1147   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1148
1149   hrtc->State = HAL_RTC_STATE_READY; 
1150
1151   /* Process Unlocked */
1152   __HAL_UNLOCK(hrtc);
1153
1154   return HAL_OK;
1155 }
1156
1157 /**
1158   * @brief  Deactive the specified RTC Alarm
1159   * @param  hrtc: RTC handle
1160   * @param  Alarm: Specifies the Alarm.
1161   *          This parameter can be one of the following values:
1162   *            @arg RTC_ALARM_A:  AlarmA
1163   *            @arg RTC_ALARM_B:  AlarmB
1164   * @retval HAL status
1165   */
1166 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1167 {
1168   uint32_t tickstart = 0;
1169
1170   /* Check the parameters */
1171   assert_param(IS_RTC_ALARM(Alarm));
1172
1173   /* Process Locked */
1174   __HAL_LOCK(hrtc);
1175
1176   hrtc->State = HAL_RTC_STATE_BUSY;
1177
1178   /* Disable the write protection for RTC registers */
1179   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1180
1181   if(Alarm == RTC_ALARM_A)
1182   {
1183     /* AlarmA */
1184     __HAL_RTC_ALARMA_DISABLE(hrtc);
1185
1186     /* In case of interrupt mode is used, the interrupt source must disabled */
1187     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1188
1189     tickstart = HAL_GetTick();
1190
1191     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1192     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 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   else
1209   {
1210     /* AlarmB */
1211     __HAL_RTC_ALARMB_DISABLE(hrtc);
1212
1213     /* In case of interrupt mode is used, the interrupt source must disabled */
1214     __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
1215
1216     tickstart = HAL_GetTick();
1217
1218     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1219     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1220     {
1221       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1222       {
1223         /* Enable the write protection for RTC registers */
1224         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1225
1226         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1227
1228         /* Process Unlocked */
1229         __HAL_UNLOCK(hrtc);
1230
1231         return HAL_TIMEOUT;
1232       }
1233     }
1234   }
1235   /* Enable the write protection for RTC registers */
1236   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1237
1238   hrtc->State = HAL_RTC_STATE_READY;
1239
1240   /* Process Unlocked */
1241   __HAL_UNLOCK(hrtc);
1242
1243   return HAL_OK; 
1244 }
1245
1246 /**
1247   * @brief  Gets the RTC Alarm value and masks.
1248   * @param  hrtc: RTC handle
1249   * @param  sAlarm: Pointer to Date structure
1250   * @param  Alarm: Specifies the Alarm.
1251   *          This parameter can be one of the following values:
1252   *             @arg RTC_ALARM_A: AlarmA
1253   *             @arg RTC_ALARM_B: AlarmB  
1254   * @param  Format: Specifies the format of the entered parameters.
1255   *          This parameter can be one of the following values:
1256   *             @arg RTC_FORMAT_BIN: Binary data format 
1257   *             @arg RTC_FORMAT_BCD
1258 : BCD data format
1259   * @retval HAL status
1260   */
1261 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1262 {
1263   uint32_t tmpreg = 0, subsecondtmpreg = 0;
1264
1265   /* Check the parameters */
1266   assert_param(IS_RTC_FORMAT(Format));
1267   assert_param(IS_RTC_ALARM(Alarm));
1268
1269   if(Alarm == RTC_ALARM_A)
1270   {
1271     /* AlarmA */
1272     sAlarm->Alarm = RTC_ALARM_A;
1273
1274     tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1275     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1276
1277 /* Fill the structure with the read parameters */
1278   sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
1279   sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
1280   sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1281   sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1282   sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1283   sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1284   sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1285   sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1286   }
1287   else
1288   {
1289     sAlarm->Alarm = RTC_ALARM_B;
1290
1291     tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1292     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1293  
1294   /* Fill the structure with the read parameters */
1295   sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> 16);
1296   sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> 8);
1297   sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU));
1298   sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMBR_PM) >> 16);
1299   sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1300   sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> 24);
1301   sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1302   sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1303  }
1304  
1305   if(Format == RTC_FORMAT_BIN)
1306   {
1307     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1308     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1309     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1310     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1311   }
1312
1313   return HAL_OK;
1314 }
1315
1316 /**
1317   * @brief  This function handles Alarm interrupt request.
1318   * @param  hrtc: RTC handle
1319   * @retval None
1320   */
1321 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1322 {
1323   if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
1324   {
1325     /* Get the status of the Interrupt */
1326     if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
1327     {
1328       /* AlarmA callback */
1329       HAL_RTC_AlarmAEventCallback(hrtc);
1330
1331       /* Clear the Alarm interrupt pending bit */
1332       __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1333     }
1334   }
1335
1336   if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
1337   {
1338     /* Get the status of the Interrupt */
1339     if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
1340     {
1341       /* AlarmB callback */
1342       HAL_RTCEx_AlarmBEventCallback(hrtc);
1343
1344       /* Clear the Alarm interrupt pending bit */
1345       __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
1346     }
1347   }
1348
1349   /* Clear the EXTI's line Flag for RTC Alarm */
1350   __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1351
1352   /* Change RTC state */
1353   hrtc->State = HAL_RTC_STATE_READY;
1354 }
1355
1356 /**
1357   * @brief  Alarm A callback.
1358   * @param  hrtc: RTC handle
1359   * @retval None
1360   */
1361 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1362 {
1363   /* NOTE : This function Should not be modified, when the callback is needed,
1364             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1365    */
1366 }
1367
1368 /**
1369   * @brief  This function handles AlarmA Polling request.
1370   * @param  hrtc: RTC handle
1371   * @param  Timeout: Timeout duration
1372   * @retval HAL status
1373   */
1374 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1375 {
1376
1377   uint32_t tickstart = HAL_GetTick();   
1378   
1379   while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1380   {
1381     if(Timeout != HAL_MAX_DELAY)
1382     {
1383       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1384       {
1385         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1386         return HAL_TIMEOUT;
1387       }
1388     }
1389   }
1390
1391   /* Clear the Alarm interrupt pending bit */
1392   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1393
1394   /* Change RTC state */
1395   hrtc->State = HAL_RTC_STATE_READY;
1396
1397   return HAL_OK;
1398 }
1399
1400 /**
1401   * @}
1402   */
1403
1404 /** @addtogroup RTC_Exported_Functions_Group4
1405  *  @brief   Peripheral Control functions 
1406  *
1407 @verbatim
1408  ===============================================================================
1409                      ##### Peripheral Control functions #####
1410  ===============================================================================
1411     [..]
1412     This subsection provides functions allowing to
1413       (+) Wait for RTC Time and Date Synchronization
1414
1415 @endverbatim
1416   * @{
1417   */
1418
1419 /**
1420   * @brief  Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1421   *         synchronized with RTC APB clock.
1422   * @note   The RTC Resynchronization mode is write protected, use the 
1423   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1424   * @note   To read the calendar through the shadow registers after Calendar 
1425   *         initialization, calendar update or after wakeup from low power modes
1426   *         the software must first clear the RSF flag.
1427   *         The software must then wait until it is set again before reading
1428   *         the calendar, which means that the calendar registers have been
1429   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
1430   * @param  hrtc: RTC handle
1431   * @retval HAL status
1432   */
1433 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1434 {
1435   uint32_t tickstart = 0;
1436
1437   /* Clear RSF flag */
1438   hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1439
1440   tickstart = HAL_GetTick();
1441
1442   /* Wait the registers to be synchronised */
1443   while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1444   {
1445     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1446     {
1447       return HAL_TIMEOUT;
1448     }
1449   }
1450
1451   return HAL_OK;
1452 }
1453
1454 /**
1455   * @}
1456   */
1457
1458 /** @addtogroup RTC_Exported_Functions_Group5
1459  *  @brief   Peripheral State functions 
1460  *
1461 @verbatim   
1462  ===============================================================================
1463                      ##### Peripheral State functions #####
1464  ===============================================================================  
1465     [..]
1466     This subsection provides functions allowing to
1467       (+) Get RTC state
1468
1469 @endverbatim
1470   * @{
1471   */
1472 /**
1473   * @brief  Returns the RTC state.
1474   * @param  hrtc: RTC handle
1475   * @retval HAL state
1476   */
1477 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1478 {
1479   return hrtc->State;
1480 }
1481
1482 /**
1483   * @}
1484   */
1485 /**
1486   * @}
1487   */
1488
1489 /** @addtogroup RTC_Private_Functions
1490   * @{
1491   */
1492 /**
1493   * @brief  Enters the RTC Initialization mode.
1494   * @note   The RTC Initialization mode is write protected, use the
1495   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1496   * @param  hrtc: RTC handle
1497   * @retval HAL status
1498   */
1499 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1500 {
1501   uint32_t tickstart = 0;
1502
1503   /* Check if the Initialization mode is set */
1504   if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1505   {
1506     /* Set the Initialization mode */
1507     hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1508
1509     tickstart = HAL_GetTick();
1510     /* Wait till RTC is in INIT state and if Time out is reached exit */
1511     while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1512     {
1513       if((HAL_GetTick()  - tickstart ) > RTC_TIMEOUT_VALUE)
1514       {
1515         return HAL_TIMEOUT;
1516       }
1517     }
1518   }
1519
1520   return HAL_OK;
1521 }
1522
1523
1524 /**
1525   * @brief  Converts a 2 digit decimal to BCD format.
1526   * @param  Value: Byte to be converted
1527   * @retval Converted byte
1528   */
1529 uint8_t RTC_ByteToBcd2(uint8_t Value)
1530 {
1531   uint32_t bcdhigh = 0;
1532
1533   while(Value >= 10)
1534   {
1535     bcdhigh++;
1536     Value -= 10;
1537   }
1538
1539   return  ((uint8_t)(bcdhigh << 4) | Value);
1540 }
1541
1542 /**
1543   * @brief  Converts from 2 digit BCD to Binary.
1544   * @param  Value: BCD value to be converted
1545   * @retval Converted word
1546   */
1547 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1548 {
1549   uint32_t tmp = 0;
1550   tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1551   return (tmp + (Value & (uint8_t)0x0F));
1552 }
1553 /**
1554   * @}
1555   */
1556
1557 #endif /* HAL_RTC_MODULE_ENABLED */
1558
1559 /**
1560   * @}
1561   */
1562
1563 /**
1564   * @}
1565   */
1566
1567 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1568