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