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