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