2 ******************************************************************************
3 * @file stm32l1xx_hal_rtc.c
4 * @author MCD Application Team
6 * @date 5-September-2014
7 * @brief RTC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Real Time Clock (RTC) peripheral:
10 * + Initialization and de-initialization functions
11 * + RTC Time and Date functions
12 * + RTC Alarm functions
13 * + Peripheral Control functions
14 * + Peripheral State functions
17 ==============================================================================
18 ##### Backup Domain Operating Condition #####
19 ==============================================================================
20 [..] The real-time clock (RTC) and the RTC backup registers can be powered
21 from the VBAT voltage when the main VDD supply is powered off.
22 To retain the content of the RTC backup registers and supply the RTC
23 when VDD is turned off, VBAT pin can be connected to an optional
24 standby voltage supplied by a battery or by another source.
26 [..] To allow the RTC operating even when the main digital supply (VDD) is turned
27 off, the VBAT pin powers the following blocks:
29 (#) The LSE oscillator
30 (#) PC13 to PC15 I/Os (when available)
32 [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
33 the following pins are available:
34 (#) PC14 and PC15 can be used as either GPIO or LSE pins
35 (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
37 [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
38 because VDD is not present), the following pins are available:
39 (#) PC14 and PC15 can be used as LSE pins only
40 (#) PC13 can be used as the RTC_AF1 pin
42 ##### Backup Domain Reset #####
43 ==================================================================
44 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
45 to their reset values.
46 [..] A backup domain reset is generated when one of the following events occurs:
47 (#) Software reset, triggered by setting the BDRST bit in the
48 RCC Backup domain control register (RCC_BDCR).
49 (#) VDD or VBAT power on, if both supplies have previously been powered off.
51 ##### Backup Domain Access #####
52 ==================================================================
53 [..] After reset, the backup domain (RTC registers, RTC backup data
54 registers and backup SRAM) is protected against possible unwanted write
56 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
57 (+) Enable the Power Controller (PWR) APB1 interface clock using the
58 __PWR_CLK_ENABLE() function.
59 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
60 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
61 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
64 ##### How to use this driver #####
65 ==================================================================
67 (+) Enable the RTC domain access (see description in the section above).
68 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
69 format using the HAL_RTC_Init() function.
71 *** Time and Date configuration ***
72 ===================================
74 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
75 and HAL_RTC_SetDate() functions.
76 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
78 *** Alarm configuration ***
79 ===========================
81 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
82 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
83 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
85 ##### RTC and low power modes #####
86 ==================================================================
87 [..] The MCU can be woken up from a low power mode by an RTC alternate
89 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
90 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
91 These RTC alternate functions can wake up the system from the Stop and
92 Standby low power modes.
93 [..] The system can also wake up from low power modes without depending
94 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
95 or the RTC wakeup events.
96 [..] The RTC provides a programmable time base for waking up from the
97 Stop or Standby mode at regular intervals.
98 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
102 ******************************************************************************
105 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
107 * Redistribution and use in source and binary forms, with or without modification,
108 * are permitted provided that the following conditions are met:
109 * 1. Redistributions of source code must retain the above copyright notice,
110 * this list of conditions and the following disclaimer.
111 * 2. Redistributions in binary form must reproduce the above copyright notice,
112 * this list of conditions and the following disclaimer in the documentation
113 * and/or other materials provided with the distribution.
114 * 3. Neither the name of STMicroelectronics nor the names of its contributors
115 * may be used to endorse or promote products derived from this software
116 * without specific prior written permission.
118 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
119 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
120 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
121 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
122 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
123 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
124 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
125 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
126 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
127 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
129 ******************************************************************************
132 /* Includes ------------------------------------------------------------------*/
133 #include "stm32l1xx_hal.h"
135 /** @addtogroup STM32L1xx_HAL_Driver
139 /** @defgroup RTC RTC
140 * @brief RTC HAL module driver
144 #ifdef HAL_RTC_MODULE_ENABLED
146 /* Private typedef -----------------------------------------------------------*/
147 /* Private define ------------------------------------------------------------*/
148 /* Private macro -------------------------------------------------------------*/
149 /* Private variables ---------------------------------------------------------*/
150 /* Private function prototypes -----------------------------------------------*/
151 /* Private functions ---------------------------------------------------------*/
152 /** @defgroup RTC_Exported_Functions RTC Exported Functions
156 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
157 * @brief Initialization and Configuration functions
160 ===============================================================================
161 ##### Initialization and de-initialization functions #####
162 ===============================================================================
163 [..] This section provides functions allowing to initialize and configure the
164 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
165 RTC registers Write protection, enter and exit the RTC initialization mode,
166 RTC registers synchronization check and reference clock detection enable.
167 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
168 It is split into 2 programmable prescalers to minimize power consumption.
169 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
170 (++) When both prescalers are used, it is recommended to configure the
171 asynchronous prescaler to a high value to minimize power consumption.
172 (#) All RTC registers are Write protected. Writing to the RTC registers
173 is enabled by writing a key into the Write Protection register, RTC_WPR.
174 (#) To configure the RTC Calendar, user application should enter
175 initialization mode. In this mode, the calendar counter is stopped
176 and its value can be updated. When the initialization sequence is
177 complete, the calendar restarts counting after 4 RTCCLK cycles.
178 (#) To read the calendar through the shadow registers after Calendar
179 initialization, calendar update or after wakeup from low power modes
180 the software must first clear the RSF flag. The software must then
181 wait until it is set again before reading the calendar, which means
182 that the calendar registers have been correctly copied into the
183 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
184 implements the above software sequence (RSF clear and RSF check).
191 * @brief Initializes the RTC peripheral
192 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
193 * the configuration information for RTC.
196 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
198 /* Check the RTC peripheral state */
204 /* Check the parameters */
205 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
206 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
207 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
208 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
209 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
210 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
211 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
213 if(hrtc->State == HAL_RTC_STATE_RESET)
215 /* Initialize RTC MSP */
216 HAL_RTC_MspInit(hrtc);
220 hrtc->State = HAL_RTC_STATE_BUSY;
222 /* Disable the write protection for RTC registers */
223 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
225 /* Set Initialization mode */
226 if(RTC_EnterInitMode(hrtc) != HAL_OK)
228 /* Enable the write protection for RTC registers */
229 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
232 hrtc->State = HAL_RTC_STATE_ERROR;
238 /* Clear RTC_CR FMT, OSEL and POL Bits */
239 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
240 /* Set RTC_CR register */
241 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
243 /* Configure the RTC PRER */
244 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
245 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
247 /* Exit Initialization mode */
248 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
250 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
251 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
253 /* Enable the write protection for RTC registers */
254 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
257 hrtc->State = HAL_RTC_STATE_READY;
264 * @brief DeInitializes the RTC peripheral
265 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
266 * the configuration information for RTC.
267 * @note This function doesn't reset the RTC Backup Data registers.
270 __weak HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
272 /* Note : This function is defined into this file for library reference. */
273 /* Function content is located into file stm32l1xx_hal_rtc_ex.c */
275 /* Return function status */
280 * @brief Initializes the RTC MSP.
281 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
282 * the configuration information for RTC.
285 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
287 /* NOTE : This function Should not be modified, when the callback is needed,
288 the HAL_RTC_MspInit could be implemented in the user file
293 * @brief DeInitializes the RTC MSP.
294 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
295 * the configuration information for RTC.
298 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
300 /* NOTE : This function Should not be modified, when the callback is needed,
301 the HAL_RTC_MspDeInit could be implemented in the user file
309 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
310 * @brief RTC Time and Date functions
313 ===============================================================================
314 ##### RTC Time and Date functions #####
315 ===============================================================================
317 [..] This section provides functions allowing to configure Time and Date features
324 * @brief Sets RTC current time.
325 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
326 * the configuration information for RTC.
327 * @param sTime: Pointer to Time structure
328 * @param Format: Specifies the format of the entered parameters.
329 * This parameter can be one of the following values:
330 * @arg FORMAT_BIN: Binary data format
331 * @arg FORMAT_BCD: BCD data format
334 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
338 /* Check the parameters */
339 assert_param(IS_RTC_FORMAT(Format));
340 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
341 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
346 hrtc->State = HAL_RTC_STATE_BUSY;
348 if(Format == FORMAT_BIN)
350 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
352 assert_param(IS_RTC_HOUR12(sTime->Hours));
353 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
357 sTime->TimeFormat = 0x00;
358 assert_param(IS_RTC_HOUR24(sTime->Hours));
360 assert_param(IS_RTC_MINUTES(sTime->Minutes));
361 assert_param(IS_RTC_SECONDS(sTime->Seconds));
363 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
364 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
365 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
366 (((uint32_t)sTime->TimeFormat) << 16));
370 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
372 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
373 assert_param(IS_RTC_HOUR12(tmpreg));
374 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
378 sTime->TimeFormat = 0x00;
379 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
381 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
382 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
383 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
384 ((uint32_t)(sTime->Minutes) << 8) | \
385 ((uint32_t)sTime->Seconds) | \
386 ((uint32_t)(sTime->TimeFormat) << 16));
389 /* Disable the write protection for RTC registers */
390 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
392 /* Set Initialization mode */
393 if(RTC_EnterInitMode(hrtc) != HAL_OK)
395 /* Enable the write protection for RTC registers */
396 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
399 hrtc->State = HAL_RTC_STATE_ERROR;
401 /* Process Unlocked */
408 /* Set the RTC_TR register */
409 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
411 /* Clear the bits to be configured */
412 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
414 /* Configure the RTC_CR register */
415 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
417 /* Exit Initialization mode */
418 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
420 /* Wait for synchro */
421 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
423 /* Enable the write protection for RTC registers */
424 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
426 hrtc->State = HAL_RTC_STATE_ERROR;
428 /* Process Unlocked */
434 /* Enable the write protection for RTC registers */
435 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
437 hrtc->State = HAL_RTC_STATE_READY;
447 * @brief Sets RTC current date.
448 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
449 * the configuration information for RTC.
450 * @param sDate: Pointer to date structure
451 * @param Format: specifies the format of the entered parameters.
452 * This parameter can be one of the following values:
453 * @arg FORMAT_BIN: Binary data format
454 * @arg FORMAT_BCD: BCD data format
457 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
459 uint32_t datetmpreg = 0;
461 /* Check the parameters */
462 assert_param(IS_RTC_FORMAT(Format));
467 hrtc->State = HAL_RTC_STATE_BUSY;
469 if((Format == FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
471 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
474 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
476 if(Format == FORMAT_BIN)
478 assert_param(IS_RTC_YEAR(sDate->Year));
479 assert_param(IS_RTC_MONTH(sDate->Month));
480 assert_param(IS_RTC_DATE(sDate->Date));
482 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
483 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
484 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
485 ((uint32_t)sDate->WeekDay << 13));
489 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
490 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
491 assert_param(IS_RTC_MONTH(datetmpreg));
492 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
493 assert_param(IS_RTC_DATE(datetmpreg));
495 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
496 (((uint32_t)sDate->Month) << 8) | \
497 ((uint32_t)sDate->Date) | \
498 (((uint32_t)sDate->WeekDay) << 13));
501 /* Disable the write protection for RTC registers */
502 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
504 /* Set Initialization mode */
505 if(RTC_EnterInitMode(hrtc) != HAL_OK)
507 /* Enable the write protection for RTC registers */
508 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
511 hrtc->State = HAL_RTC_STATE_ERROR;
513 /* Process Unlocked */
520 /* Set the RTC_DR register */
521 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
523 /* Exit Initialization mode */
524 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
526 /* Wait for synchro */
527 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
529 /* Enable the write protection for RTC registers */
530 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
532 hrtc->State = HAL_RTC_STATE_ERROR;
534 /* Process Unlocked */
540 /* Enable the write protection for RTC registers */
541 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
543 hrtc->State = HAL_RTC_STATE_READY ;
545 /* Process Unlocked */
553 * @brief Gets RTC current date.
554 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
555 * the configuration information for RTC.
556 * @param sDate: Pointer to Date structure
557 * @param Format: Specifies the format of the entered parameters.
558 * This parameter can be one of the following values:
559 * @arg FORMAT_BIN: Binary data format
560 * @arg FORMAT_BCD: BCD data format
563 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
565 uint32_t datetmpreg = 0;
567 /* Check the parameters */
568 assert_param(IS_RTC_FORMAT(Format));
570 /* Get the DR register */
571 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
573 /* Fill the structure fields with the read parameters */
574 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
575 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
576 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
577 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
579 /* Check the input parameters format */
580 if(Format == FORMAT_BIN)
582 /* Convert the date structure parameters to Binary format */
583 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
584 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
585 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
594 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
595 * @brief RTC Alarm functions
598 ===============================================================================
599 ##### RTC Alarm functions #####
600 ===============================================================================
602 [..] This section provides functions allowing to configure Alarm feature
609 * @brief Deactive the specified RTC Alarm
610 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
611 * the configuration information for RTC.
612 * @param Alarm: Specifies the Alarm.
613 * This parameter can be one of the following values:
614 * @arg RTC_ALARM_A: AlarmA
615 * @arg RTC_ALARM_B: AlarmB
618 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
620 uint32_t tickstart = 0;
622 /* Check the parameters */
623 assert_param(IS_ALARM(Alarm));
628 hrtc->State = HAL_RTC_STATE_BUSY;
630 /* Disable the write protection for RTC registers */
631 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
633 if(Alarm == RTC_ALARM_A)
636 __HAL_RTC_ALARMA_DISABLE(hrtc);
638 /* In case of interrupt mode is used, the interrupt source must disabled */
639 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
641 tickstart = HAL_GetTick();
643 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
644 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
646 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
648 /* Enable the write protection for RTC registers */
649 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
651 hrtc->State = HAL_RTC_STATE_TIMEOUT;
653 /* Process Unlocked */
663 __HAL_RTC_ALARMB_DISABLE(hrtc);
665 /* In case of interrupt mode is used, the interrupt source must disabled */
666 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
668 tickstart = HAL_GetTick();
670 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
671 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
673 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
675 /* Enable the write protection for RTC registers */
676 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
678 hrtc->State = HAL_RTC_STATE_TIMEOUT;
680 /* Process Unlocked */
687 /* Enable the write protection for RTC registers */
688 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
690 hrtc->State = HAL_RTC_STATE_READY;
692 /* Process Unlocked */
699 * @brief This function handles Alarm interrupt request.
700 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
701 * the configuration information for RTC.
704 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
706 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
708 /* Get the status of the Interrupt */
709 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
711 /* AlarmA callback */
712 HAL_RTC_AlarmAEventCallback(hrtc);
714 /* Clear the Alarm interrupt pending bit */
715 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
719 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
721 /* Get the status of the Interrupt */
722 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
724 /* AlarmB callback */
725 HAL_RTCEx_AlarmBEventCallback(hrtc);
727 /* Clear the Alarm interrupt pending bit */
728 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
732 /* Clear the EXTI's line Flag for RTC Alarm */
733 __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT);
735 /* Change RTC state */
736 hrtc->State = HAL_RTC_STATE_READY;
740 * @brief Alarm A callback.
741 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
742 * the configuration information for RTC.
745 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
747 /* NOTE : This function Should not be modified, when the callback is needed,
748 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
753 * @brief This function handles AlarmA Polling request.
754 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
755 * the configuration information for RTC.
756 * @param Timeout: Timeout duration
759 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
762 uint32_t tickstart = HAL_GetTick();
764 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
766 if(Timeout != HAL_MAX_DELAY)
768 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
770 hrtc->State = HAL_RTC_STATE_TIMEOUT;
776 /* Clear the Alarm interrupt pending bit */
777 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
779 /* Change RTC state */
780 hrtc->State = HAL_RTC_STATE_READY;
789 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
790 * @brief Peripheral State functions
793 ===============================================================================
794 ##### Peripheral State functions #####
795 ===============================================================================
797 This subsection provides functions allowing to
804 * @brief Returns the RTC state.
805 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
806 * the configuration information for RTC.
809 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
822 /** @defgroup RTC_Internal_Functions RTC Internal function
827 * @brief Enters the RTC Initialization mode.
828 * @note The RTC Initialization mode is write protected, use the
829 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
830 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
831 * the configuration information for RTC.
834 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
836 uint32_t tickstart = 0;
838 /* Check if the Initialization mode is set */
839 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
841 /* Set the Initialization mode */
842 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
844 tickstart = HAL_GetTick();
845 /* Wait till RTC is in INIT state and if Time out is reached exit */
846 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
848 if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
859 * @brief Converts a 2 digit decimal to BCD format.
860 * @param Value: Byte to be converted
861 * @retval Converted byte
863 uint8_t RTC_ByteToBcd2(uint8_t Value)
865 uint32_t bcdhigh = 0;
873 return ((uint8_t)(bcdhigh << 4) | Value);
877 * @brief Converts from 2 digit BCD to Binary.
878 * @param Value: BCD value to be converted
879 * @retval Converted word
881 uint8_t RTC_Bcd2ToByte(uint8_t Value)
884 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
885 return (tmp + (Value & (uint8_t)0x0F));
893 #endif /* HAL_RTC_MODULE_ENABLED */
902 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/