2 ******************************************************************************
3 * @file stm32f0xx_hal_rtc.c
4 * @author MCD Application Team
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:
11 * + RTC Time and Date functions
12 * + RTC Alarm functions
13 * + Backup Data Registers configuration
14 * + Interrupts and flags management
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.
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:
30 (#) The LSE oscillator
31 (#) PC13 to PC15 I/Os (when available)
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
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
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
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.
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
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.
66 ##### How to use this driver #####
67 ===================================================================
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.
73 *** Time and Date configuration ***
74 ===================================
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.
80 *** Alarm configuration ***
81 ===========================
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.
87 ##### RTC and low power modes #####
88 ===================================================================
89 [..] The MCU can be woken up from a low power mode by an RTC alternate
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
104 ******************************************************************************
107 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
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.
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.
131 ******************************************************************************
134 /* Includes ------------------------------------------------------------------*/
135 #include "stm32f0xx_hal.h"
137 /** @addtogroup STM32F0xx_HAL_Driver
141 /** @defgroup RTC RTC HAL module driver
142 * @brief RTC HAL module driver
146 #ifdef HAL_RTC_MODULE_ENABLED
148 /* Private typedef -----------------------------------------------------------*/
149 /* Private define ------------------------------------------------------------*/
150 /* Private macro -------------------------------------------------------------*/
151 /* Private variables ---------------------------------------------------------*/
152 /* Private function prototypes -----------------------------------------------*/
153 /* Exported functions ---------------------------------------------------------*/
155 /** @defgroup RTC_Exported_Functions RTC Exported Functions
159 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
160 * @brief Initialization and Configuration functions
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).
194 * @brief Initializes the RTC peripheral
195 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
196 * the configuration information for RTC.
199 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
201 /* Check the RTC peripheral state */
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));
216 if(hrtc->State == HAL_RTC_STATE_RESET)
218 /* Initialize RTC MSP */
219 HAL_RTC_MspInit(hrtc);
223 hrtc->State = HAL_RTC_STATE_BUSY;
225 /* Disable the write protection for RTC registers */
226 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
228 /* Set Initialization mode */
229 if(RTC_EnterInitMode(hrtc) != HAL_OK)
231 /* Enable the write protection for RTC registers */
232 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
235 hrtc->State = HAL_RTC_STATE_ERROR;
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);
246 /* Configure the RTC PRER */
247 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
248 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
250 /* Exit Initialization mode */
251 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
253 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
254 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
256 /* Enable the write protection for RTC registers */
257 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
260 hrtc->State = HAL_RTC_STATE_READY;
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.
273 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
275 /* Check the parameters */
276 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
279 hrtc->State = HAL_RTC_STATE_BUSY;
281 /* Disable the write protection for RTC registers */
282 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
284 /* Set Initialization mode */
285 if(RTC_EnterInitMode(hrtc) != HAL_OK)
287 /* Enable the write protection for RTC registers */
288 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
291 hrtc->State = HAL_RTC_STATE_ERROR;
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;
308 /* Reset ISR register and exit initialization mode */
309 hrtc->Instance->ISR = (uint32_t)0x00000000;
311 /* Reset Tamper and alternate functions configuration register */
312 hrtc->Instance->TAFCR = 0x00000000;
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)
317 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
319 /* Enable the write protection for RTC registers */
320 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
322 hrtc->State = HAL_RTC_STATE_ERROR;
329 /* Enable the write protection for RTC registers */
330 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
332 /* De-Initialize RTC MSP */
333 HAL_RTC_MspDeInit(hrtc);
335 hrtc->State = HAL_RTC_STATE_RESET;
344 * @brief Initializes the RTC MSP.
345 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
346 * the configuration information for RTC.
349 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
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
357 * @brief DeInitializes the RTC MSP.
358 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
359 * the configuration information for RTC.
362 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
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
373 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
374 * @brief RTC Time and Date functions
377 ===============================================================================
378 ##### RTC Time and Date functions #####
379 ===============================================================================
381 [..] This section provide functions allowing to configure Time and Date features
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
398 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
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));
410 hrtc->State = HAL_RTC_STATE_BUSY;
412 if(Format == FORMAT_BIN)
414 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
416 assert_param(IS_RTC_HOUR12(sTime->Hours));
417 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
421 sTime->TimeFormat = 0x00;
422 assert_param(IS_RTC_HOUR24(sTime->Hours));
424 assert_param(IS_RTC_MINUTES(sTime->Minutes));
425 assert_param(IS_RTC_SECONDS(sTime->Seconds));
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));
434 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
436 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
437 assert_param(IS_RTC_HOUR12(tmpreg));
438 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
442 sTime->TimeFormat = 0x00;
443 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
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));
453 /* Disable the write protection for RTC registers */
454 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
456 /* Set Initialization mode */
457 if(RTC_EnterInitMode(hrtc) != HAL_OK)
459 /* Enable the write protection for RTC registers */
460 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
463 hrtc->State = HAL_RTC_STATE_ERROR;
465 /* Process Unlocked */
472 /* Set the RTC_TR register */
473 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
475 /* Clear the bits to be configured */
476 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
478 /* Configure the RTC_CR register */
479 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
481 /* Exit Initialization mode */
482 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
484 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
485 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
487 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
489 /* Enable the write protection for RTC registers */
490 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
492 hrtc->State = HAL_RTC_STATE_ERROR;
494 /* Process Unlocked */
501 /* Enable the write protection for RTC registers */
502 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
504 hrtc->State = HAL_RTC_STATE_READY;
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.
526 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
530 /* Check the parameters */
531 assert_param(IS_RTC_FORMAT(Format));
533 /* Get subseconds values from the correspondent registers*/
534 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
536 /* Get the TR register */
537 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
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);
545 /* Check the input parameters format */
546 if(Format == FORMAT_BIN)
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);
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
568 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
570 uint32_t datetmpreg = 0;
572 /* Check the parameters */
573 assert_param(IS_RTC_FORMAT(Format));
578 hrtc->State = HAL_RTC_STATE_BUSY;
580 if((Format == FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
582 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
585 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
587 if(Format == FORMAT_BIN)
589 assert_param(IS_RTC_YEAR(sDate->Year));
590 assert_param(IS_RTC_MONTH(sDate->Month));
591 assert_param(IS_RTC_DATE(sDate->Date));
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));
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));
606 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
607 (((uint32_t)sDate->Month) << 8) | \
608 ((uint32_t)sDate->Date) | \
609 (((uint32_t)sDate->WeekDay) << 13));
612 /* Disable the write protection for RTC registers */
613 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
615 /* Set Initialization mode */
616 if(RTC_EnterInitMode(hrtc) != HAL_OK)
618 /* Enable the write protection for RTC registers */
619 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
622 hrtc->State = HAL_RTC_STATE_ERROR;
624 /* Process Unlocked */
631 /* Set the RTC_DR register */
632 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
634 /* Exit Initialization mode */
635 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
637 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
638 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
640 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
642 /* Enable the write protection for RTC registers */
643 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
645 hrtc->State = HAL_RTC_STATE_ERROR;
647 /* Process Unlocked */
654 /* Enable the write protection for RTC registers */
655 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
657 hrtc->State = HAL_RTC_STATE_READY ;
659 /* Process Unlocked */
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.
680 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
682 uint32_t datetmpreg = 0;
684 /* Check the parameters */
685 assert_param(IS_RTC_FORMAT(Format));
687 /* Get the DR register */
688 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
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);
696 /* Check the input parameters format */
697 if(Format == FORMAT_BIN)
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);
711 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
712 * @brief RTC Alarm functions
715 ===============================================================================
716 ##### RTC Alarm functions #####
717 ===============================================================================
719 [..] This section provides functions allowing to configure Alarm feature
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
735 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
737 uint32_t tickstart = 0;
738 uint32_t tmpreg = 0, subsecondtmpreg = 0;
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));
751 hrtc->State = HAL_RTC_STATE_BUSY;
753 if(Format == FORMAT_BIN)
755 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
757 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
758 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
762 sAlarm->AlarmTime.TimeFormat = 0x00;
763 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
765 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
766 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
768 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
770 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
774 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
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));
787 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
789 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
790 assert_param(IS_RTC_HOUR12(tmpreg));
791 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
795 sAlarm->AlarmTime.TimeFormat = 0x00;
796 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
799 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
800 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
802 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
804 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
805 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
809 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
810 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
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));
822 /* Configure the Alarm A Sub Second registers */
823 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
825 /* Disable the write protection for RTC registers */
826 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
828 /* Disable the Alarm A interrupt */
829 __HAL_RTC_ALARMA_DISABLE(hrtc);
831 /* In case of interrupt mode is used, the interrupt source must disabled */
832 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
835 tickstart = HAL_GetTick();
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)
840 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
842 /* Enable the write protection for RTC registers */
843 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
845 hrtc->State = HAL_RTC_STATE_TIMEOUT;
847 /* Process Unlocked */
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);
860 /* Enable the write protection for RTC registers */
861 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
863 /* Change RTC state */
864 hrtc->State = HAL_RTC_STATE_READY;
866 /* Process Unlocked */
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.
886 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
888 uint32_t tickstart = 0;
889 uint32_t tmpreg = 0, subsecondtmpreg = 0;
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));
902 hrtc->State = HAL_RTC_STATE_BUSY;
904 if(Format == FORMAT_BIN)
906 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
908 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
909 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
913 sAlarm->AlarmTime.TimeFormat = 0x00;
914 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
916 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
917 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
919 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
921 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
925 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
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));
937 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
939 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
940 assert_param(IS_RTC_HOUR12(tmpreg));
941 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
945 sAlarm->AlarmTime.TimeFormat = 0x00;
946 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
949 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
950 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
952 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
954 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
955 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
959 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
960 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
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));
970 /* Configure the Alarm A Sub Second registers */
971 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
973 /* Disable the write protection for RTC registers */
974 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
976 /* Disable the Alarm A interrupt */
977 __HAL_RTC_ALARMA_DISABLE(hrtc);
979 /* Clear flag alarm A */
980 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
982 tickstart = HAL_GetTick();
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)
987 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
989 /* Enable the write protection for RTC registers */
990 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
992 hrtc->State = HAL_RTC_STATE_TIMEOUT;
994 /* Process Unlocked */
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);
1009 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1010 __HAL_RTC_EXTI_ENABLE_IT(RTC_EXTI_LINE_ALARM_EVENT);
1012 EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
1014 /* Enable the write protection for RTC registers */
1015 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1017 hrtc->State = HAL_RTC_STATE_READY;
1019 /* Process Unlocked */
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
1034 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1036 uint32_t tickstart = 0;
1038 /* Check the parameters */
1039 assert_param(IS_ALARM(Alarm));
1041 /* Process Locked */
1044 hrtc->State = HAL_RTC_STATE_BUSY;
1046 /* Disable the write protection for RTC registers */
1047 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1049 __HAL_RTC_ALARMA_DISABLE(hrtc);
1051 /* In case of interrupt mode is used, the interrupt source must disabled */
1052 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1055 tickstart = HAL_GetTick();
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)
1060 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1062 /* Enable the write protection for RTC registers */
1063 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1065 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1067 /* Process Unlocked */
1073 /* Enable the write protection for RTC registers */
1074 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1076 hrtc->State = HAL_RTC_STATE_READY;
1078 /* Process Unlocked */
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
1098 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1100 uint32_t tmpreg = 0, subsecondtmpreg = 0;
1102 /* Check the parameters */
1103 assert_param(IS_RTC_FORMAT(Format));
1104 assert_param(IS_ALARM(Alarm));
1106 sAlarm->Alarm = RTC_ALARM_A;
1108 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1109 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
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);
1121 if(Format == FORMAT_BIN)
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);
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.
1138 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1140 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
1142 /* Get the status of the Interrupt */
1143 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
1145 /* AlarmA callback */
1146 HAL_RTC_AlarmAEventCallback(hrtc);
1148 /* Clear the Alarm interrupt pending bit */
1149 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1153 /* Clear the EXTI's line Flag for RTC Alarm */
1154 __HAL_RTC_EXTI_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT);
1156 /* Change RTC state */
1157 hrtc->State = HAL_RTC_STATE_READY;
1161 * @brief Alarm A callback.
1162 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1163 * the configuration information for RTC.
1166 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
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
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
1180 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1182 uint32_t tickstart = 0;
1185 tickstart = HAL_GetTick();
1187 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1189 if(Timeout != HAL_MAX_DELAY)
1191 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
1193 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1199 /* Clear the Alarm interrupt pending bit */
1200 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1202 /* Change RTC state */
1203 hrtc->State = HAL_RTC_STATE_READY;
1212 /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
1213 * @brief Peripheral Control functions
1216 ===============================================================================
1217 ##### Peripheral Control functions #####
1218 ===============================================================================
1220 This subsection provides functions allowing to
1221 (+) Wait for RTC Time and Date Synchronization
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
1242 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1244 uint32_t tickstart = 0;
1246 /* Clear RSF flag */
1247 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1250 tickstart = HAL_GetTick();
1252 /* Wait the registers to be synchronised */
1253 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1255 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1268 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
1269 * @brief Peripheral State functions
1272 ===============================================================================
1273 ##### Peripheral State functions #####
1274 ===============================================================================
1276 This subsection provides functions allowing to
1283 * @brief Returns the RTC state.
1284 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1285 * the configuration information for RTC.
1288 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1301 /** @defgroup RTC_Private_Functions RTC Private Functions
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
1315 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1317 uint32_t tickstart = 0;
1319 /* Check if the Initialization mode is set */
1320 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1322 /* Set the Initialization mode */
1323 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1326 tickstart = HAL_GetTick();
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)
1331 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1343 * @brief Converts a 2 digit decimal to BCD format.
1344 * @param Value: Byte to be converted
1345 * @retval Converted byte
1347 uint8_t RTC_ByteToBcd2(uint8_t Value)
1349 uint32_t bcdhigh = 0;
1357 return ((uint8_t)(bcdhigh << 4) | Value);
1361 * @brief Converts from 2 digit BCD to Binary.
1362 * @param Value: BCD value to be converted
1363 * @retval Converted word
1365 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1368 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1369 return (tmp + (Value & (uint8_t)0x0F));
1376 #endif /* HAL_RTC_MODULE_ENABLED */
1385 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/