2 ******************************************************************************
3 * @file stm32f4xx_hal_rtc.c
4 * @author MCD Application Team
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), the RTC backup registers, and the backup
21 SRAM (BKP SRAM) can be powered from the VBAT voltage when the main
22 VDD supply is powered off.
23 To retain the content of the RTC backup registers, backup SRAM, and supply
24 the RTC when VDD is turned off, VBAT pin can be connected to an optional
25 standby voltage supplied by a battery or by another source.
27 [..] To allow the RTC operating even when the main digital supply (VDD) is turned
28 off, the VBAT pin powers the following blocks:
30 (#) The LSE oscillator
31 (#) The backup SRAM when the low power backup regulator is enabled
32 (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
34 [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
35 the following pins are available:
36 (#) PC14 and PC15 can be used as either GPIO or LSE pins
37 (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
38 (#) PI8 can be used as a GPIO or as the RTC_AF2 pin
40 [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
41 because VDD is not present), the following pins are available:
42 (#) PC14 and PC15 can be used as LSE pins only
43 (#) PC13 can be used as the RTC_AF1 pin
44 (#) PI8 can be used as the RTC_AF2 pin
46 ##### Backup Domain Reset #####
47 ==================================================================
48 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
49 to their reset values. The BKPSRAM is not affected by this reset. The only
50 way to reset the BKPSRAM is through the Flash interface by requesting
51 a protection level change from 1 to 0.
52 [..] A backup domain reset is generated when one of the following events occurs:
53 (#) Software reset, triggered by setting the BDRST bit in the
54 RCC Backup domain control register (RCC_BDCR).
55 (#) VDD or VBAT power on, if both supplies have previously been powered off.
57 ##### Backup Domain Access #####
58 ==================================================================
59 [..] After reset, the backup domain (RTC registers, RTC backup data
60 registers and backup SRAM) is protected against possible unwanted write
62 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
63 (+) Enable the Power Controller (PWR) APB1 interface clock using the
64 __PWR_CLK_ENABLE() function.
65 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
66 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
67 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
70 ##### How to use this driver #####
71 ==================================================================
73 (+) Enable the RTC domain access (see description in the section above).
74 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
75 format using the HAL_RTC_Init() function.
77 *** Time and Date configuration ***
78 ===================================
80 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
81 and HAL_RTC_SetDate() functions.
82 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
84 *** Alarm configuration ***
85 ===========================
87 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
88 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
89 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
91 ##### RTC and low power modes #####
92 ==================================================================
93 [..] The MCU can be woken up from a low power mode by an RTC alternate
95 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
96 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
97 These RTC alternate functions can wake up the system from the Stop and
98 Standby low power modes.
99 [..] The system can also wake up from low power modes without depending
100 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
101 or the RTC wakeup events.
102 [..] The RTC provides a programmable time base for waking up from the
103 Stop or Standby mode at regular intervals.
104 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
108 ******************************************************************************
111 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
113 * Redistribution and use in source and binary forms, with or without modification,
114 * are permitted provided that the following conditions are met:
115 * 1. Redistributions of source code must retain the above copyright notice,
116 * this list of conditions and the following disclaimer.
117 * 2. Redistributions in binary form must reproduce the above copyright notice,
118 * this list of conditions and the following disclaimer in the documentation
119 * and/or other materials provided with the distribution.
120 * 3. Neither the name of STMicroelectronics nor the names of its contributors
121 * may be used to endorse or promote products derived from this software
122 * without specific prior written permission.
124 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
125 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
126 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
127 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
128 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
129 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
130 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
131 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
132 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
133 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135 ******************************************************************************
138 /* Includes ------------------------------------------------------------------*/
139 #include "stm32f4xx_hal.h"
141 /** @addtogroup STM32F4xx_HAL_Driver
146 * @brief RTC HAL module driver
150 #ifdef HAL_RTC_MODULE_ENABLED
152 /* Private typedef -----------------------------------------------------------*/
153 /* Private define ------------------------------------------------------------*/
154 /* Private macro -------------------------------------------------------------*/
155 /* Private variables ---------------------------------------------------------*/
156 /* Private function prototypes -----------------------------------------------*/
157 /* Private functions ---------------------------------------------------------*/
159 /** @defgroup RTC_Private_Functions
163 /** @defgroup RTC_Group1 Initialization and de-initialization functions
164 * @brief Initialization and Configuration functions
167 ===============================================================================
168 ##### Initialization and de-initialization functions #####
169 ===============================================================================
170 [..] This section provides functions allowing to initialize and configure the
171 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
172 RTC registers Write protection, enter and exit the RTC initialization mode,
173 RTC registers synchronization check and reference clock detection enable.
174 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
175 It is split into 2 programmable prescalers to minimize power consumption.
176 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
177 (++) When both prescalers are used, it is recommended to configure the
178 asynchronous prescaler to a high value to minimize power consumption.
179 (#) All RTC registers are Write protected. Writing to the RTC registers
180 is enabled by writing a key into the Write Protection register, RTC_WPR.
181 (#) To configure the RTC Calendar, user application should enter
182 initialization mode. In this mode, the calendar counter is stopped
183 and its value can be updated. When the initialization sequence is
184 complete, the calendar restarts counting after 4 RTCCLK cycles.
185 (#) To read the calendar through the shadow registers after Calendar
186 initialization, calendar update or after wakeup from low power modes
187 the software must first clear the RSF flag. The software must then
188 wait until it is set again before reading the calendar, which means
189 that the calendar registers have been correctly copied into the
190 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
191 implements the above software sequence (RSF clear and RSF check).
198 * @brief Initializes the RTC peripheral
199 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
200 * the configuration information for RTC.
203 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
205 /* Check the RTC peripheral state */
211 /* Check the parameters */
212 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
213 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
214 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
215 assert_param (IS_RTC_OUTPUT(hrtc->Init.OutPut));
216 assert_param (IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
217 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
219 if(hrtc->State == HAL_RTC_STATE_RESET)
221 /* Initialize RTC MSP */
222 HAL_RTC_MspInit(hrtc);
226 hrtc->State = HAL_RTC_STATE_BUSY;
228 /* Disable the write protection for RTC registers */
229 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
231 /* Set Initialization mode */
232 if(RTC_EnterInitMode(hrtc) != HAL_OK)
234 /* Enable the write protection for RTC registers */
235 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
238 hrtc->State = HAL_RTC_STATE_ERROR;
244 /* Clear RTC_CR FMT, OSEL and POL Bits */
245 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
246 /* Set RTC_CR register */
247 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
249 /* Configure the RTC PRER */
250 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
251 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
253 /* Exit Initialization mode */
254 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
256 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
257 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
259 /* Enable the write protection for RTC registers */
260 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
263 hrtc->State = HAL_RTC_STATE_READY;
270 * @brief DeInitializes the RTC peripheral
271 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
272 * the configuration information for RTC.
273 * @note This function doesn't reset the RTC Backup Data registers.
276 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
278 uint32_t tickstart = 0;
281 hrtc->State = HAL_RTC_STATE_BUSY;
283 /* Disable the write protection for RTC registers */
284 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
286 /* Set Initialization mode */
287 if(RTC_EnterInitMode(hrtc) != HAL_OK)
289 /* Enable the write protection for RTC registers */
290 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
293 hrtc->State = HAL_RTC_STATE_ERROR;
299 /* Reset TR, DR and CR registers */
300 hrtc->Instance->TR = (uint32_t)0x00000000;
301 hrtc->Instance->DR = (uint32_t)0x00002101;
302 /* Reset All CR bits except CR[2:0] */
303 hrtc->Instance->CR &= (uint32_t)0x00000007;
306 tickstart = HAL_GetTick();
308 /* Wait till WUTWF flag is set and if Time out is reached exit */
309 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
311 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
313 /* Enable the write protection for RTC registers */
314 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
317 hrtc->State = HAL_RTC_STATE_TIMEOUT;
323 /* Reset all RTC CR register bits */
324 hrtc->Instance->CR &= (uint32_t)0x00000000;
325 hrtc->Instance->WUTR = (uint32_t)0x0000FFFF;
326 hrtc->Instance->PRER = (uint32_t)0x007F00FF;
327 hrtc->Instance->CALIBR = (uint32_t)0x00000000;
328 hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
329 hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
330 hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
331 hrtc->Instance->CALR = (uint32_t)0x00000000;
332 hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
333 hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
335 /* Reset ISR register and exit initialization mode */
336 hrtc->Instance->ISR = (uint32_t)0x00000000;
338 /* Reset Tamper and alternate functions configuration register */
339 hrtc->Instance->TAFCR = 0x00000000;
341 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
342 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
344 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
346 /* Enable the write protection for RTC registers */
347 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
349 hrtc->State = HAL_RTC_STATE_ERROR;
356 /* Enable the write protection for RTC registers */
357 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
359 /* De-Initialize RTC MSP */
360 HAL_RTC_MspDeInit(hrtc);
362 hrtc->State = HAL_RTC_STATE_RESET;
371 * @brief Initializes the RTC MSP.
372 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
373 * the configuration information for RTC.
376 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
378 /* NOTE : This function Should not be modified, when the callback is needed,
379 the HAL_RTC_MspInit could be implenetd in the user file
384 * @brief DeInitializes the RTC MSP.
385 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
386 * the configuration information for RTC.
389 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
391 /* NOTE : This function Should not be modified, when the callback is needed,
392 the HAL_RTC_MspDeInit could be implenetd in the user file
400 /** @defgroup RTC_Group2 RTC Time and Date functions
401 * @brief RTC Time and Date functions
404 ===============================================================================
405 ##### RTC Time and Date functions #####
406 ===============================================================================
408 [..] This section provides functions allowing to configure Time and Date features
415 * @brief Sets RTC current time.
416 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
417 * the configuration information for RTC.
418 * @param sTime: Pointer to Time structure
419 * @param Format: Specifies the format of the entered parameters.
420 * This parameter can be one of the following values:
421 * @arg FORMAT_BIN: Binary data format
422 * @arg FORMAT_BCD: BCD data format
425 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
429 /* Check the parameters */
430 assert_param(IS_RTC_FORMAT(Format));
431 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
432 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
437 hrtc->State = HAL_RTC_STATE_BUSY;
439 if(Format == FORMAT_BIN)
441 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
443 assert_param(IS_RTC_HOUR12(sTime->Hours));
444 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
448 sTime->TimeFormat = 0x00;
449 assert_param(IS_RTC_HOUR24(sTime->Hours));
451 assert_param(IS_RTC_MINUTES(sTime->Minutes));
452 assert_param(IS_RTC_SECONDS(sTime->Seconds));
454 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
455 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
456 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
457 (((uint32_t)sTime->TimeFormat) << 16));
461 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
463 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
464 assert_param(IS_RTC_HOUR12(tmpreg));
465 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
469 sTime->TimeFormat = 0x00;
470 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
472 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
473 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
474 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
475 ((uint32_t)(sTime->Minutes) << 8) | \
476 ((uint32_t)sTime->Seconds) | \
477 ((uint32_t)(sTime->TimeFormat) << 16));
480 /* Disable the write protection for RTC registers */
481 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
483 /* Set Initialization mode */
484 if(RTC_EnterInitMode(hrtc) != HAL_OK)
486 /* Enable the write protection for RTC registers */
487 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
490 hrtc->State = HAL_RTC_STATE_ERROR;
492 /* Process Unlocked */
499 /* Set the RTC_TR register */
500 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
502 /* Clear the bits to be configured */
503 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
505 /* Configure the RTC_CR register */
506 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
508 /* Exit Initialization mode */
509 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
511 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
512 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
514 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
516 /* Enable the write protection for RTC registers */
517 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
519 hrtc->State = HAL_RTC_STATE_ERROR;
521 /* Process Unlocked */
528 /* Enable the write protection for RTC registers */
529 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
531 hrtc->State = HAL_RTC_STATE_READY;
540 * @brief Gets RTC current time.
541 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
542 * the configuration information for RTC.
543 * @param sTime: Pointer to Time structure
544 * @param Format: Specifies the format of the entered parameters.
545 * This parameter can be one of the following values:
546 * @arg FORMAT_BIN: Binary data format
547 * @arg FORMAT_BCD: BCD data format
548 * @note Call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
549 * in the higher-order calendar shadow registers.
552 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
556 /* Check the parameters */
557 assert_param(IS_RTC_FORMAT(Format));
559 /* Get subseconds values from the correspondent registers*/
560 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
562 /* Get the TR register */
563 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
565 /* Fill the structure fields with the read parameters */
566 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
567 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
568 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
569 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
571 /* Check the input parameters format */
572 if(Format == FORMAT_BIN)
574 /* Convert the time structure parameters to Binary format */
575 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
576 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
577 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
584 * @brief Sets RTC current date.
585 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
586 * the configuration information for RTC.
587 * @param sDate: Pointer to date structure
588 * @param Format: specifies the format of the entered parameters.
589 * This parameter can be one of the following values:
590 * @arg FORMAT_BIN: Binary data format
591 * @arg FORMAT_BCD: BCD data format
594 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
596 uint32_t datetmpreg = 0;
598 /* Check the parameters */
599 assert_param(IS_RTC_FORMAT(Format));
604 hrtc->State = HAL_RTC_STATE_BUSY;
606 if((Format == FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
608 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
611 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
613 if(Format == FORMAT_BIN)
615 assert_param(IS_RTC_YEAR(sDate->Year));
616 assert_param(IS_RTC_MONTH(sDate->Month));
617 assert_param(IS_RTC_DATE(sDate->Date));
619 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
620 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
621 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
622 ((uint32_t)sDate->WeekDay << 13));
626 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
627 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
628 assert_param(IS_RTC_MONTH(datetmpreg));
629 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
630 assert_param(IS_RTC_DATE(datetmpreg));
632 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
633 (((uint32_t)sDate->Month) << 8) | \
634 ((uint32_t)sDate->Date) | \
635 (((uint32_t)sDate->WeekDay) << 13));
638 /* Disable the write protection for RTC registers */
639 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
641 /* Set Initialization mode */
642 if(RTC_EnterInitMode(hrtc) != HAL_OK)
644 /* Enable the write protection for RTC registers */
645 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
648 hrtc->State = HAL_RTC_STATE_ERROR;
650 /* Process Unlocked */
657 /* Set the RTC_DR register */
658 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
660 /* Exit Initialization mode */
661 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
663 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
664 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
666 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
668 /* Enable the write protection for RTC registers */
669 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
671 hrtc->State = HAL_RTC_STATE_ERROR;
673 /* Process Unlocked */
680 /* Enable the write protection for RTC registers */
681 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
683 hrtc->State = HAL_RTC_STATE_READY ;
685 /* Process Unlocked */
693 * @brief Gets RTC current date.
694 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
695 * the configuration information for RTC.
696 * @param sDate: Pointer to Date structure
697 * @param Format: Specifies the format of the entered parameters.
698 * This parameter can be one of the following values:
699 * @arg FORMAT_BIN: Binary data format
700 * @arg FORMAT_BCD: BCD data format
703 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
705 uint32_t datetmpreg = 0;
707 /* Check the parameters */
708 assert_param(IS_RTC_FORMAT(Format));
710 /* Get the DR register */
711 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
713 /* Fill the structure fields with the read parameters */
714 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
715 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
716 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
717 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
719 /* Check the input parameters format */
720 if(Format == FORMAT_BIN)
722 /* Convert the date structure parameters to Binary format */
723 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
724 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
725 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
734 /** @defgroup RTC_Group3 RTC Alarm functions
735 * @brief RTC Alarm functions
738 ===============================================================================
739 ##### RTC Alarm functions #####
740 ===============================================================================
742 [..] This section provides functions allowing to configure Alarm feature
748 * @brief Sets the specified RTC Alarm.
749 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
750 * the configuration information for RTC.
751 * @param sAlarm: Pointer to Alarm structure
752 * @param Format: Specifies the format of the entered parameters.
753 * This parameter can be one of the following values:
754 * @arg FORMAT_BIN: Binary data format
755 * @arg FORMAT_BCD: BCD data format
758 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
760 uint32_t tickstart = 0;
761 uint32_t tmpreg = 0, subsecondtmpreg = 0;
763 /* Check the parameters */
764 assert_param(IS_RTC_FORMAT(Format));
765 assert_param(IS_ALARM(sAlarm->Alarm));
766 assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
767 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
768 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
769 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
774 hrtc->State = HAL_RTC_STATE_BUSY;
776 if(Format == FORMAT_BIN)
778 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
780 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
781 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
785 sAlarm->AlarmTime.TimeFormat = 0x00;
786 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
788 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
789 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
791 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
793 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
797 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
800 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
801 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
802 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
803 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
804 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
805 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
806 ((uint32_t)sAlarm->AlarmMask));
810 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
812 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
813 assert_param(IS_RTC_HOUR12(tmpreg));
814 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
818 sAlarm->AlarmTime.TimeFormat = 0x00;
819 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
822 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
823 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
825 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
827 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
828 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
832 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
833 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
836 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
837 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
838 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
839 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
840 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
841 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
842 ((uint32_t)sAlarm->AlarmMask));
845 /* Configure the Alarm A or Alarm B Sub Second registers */
846 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
848 /* Disable the write protection for RTC registers */
849 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
851 /* Configure the Alarm register */
852 if(sAlarm->Alarm == RTC_ALARM_A)
854 /* Disable the Alarm A interrupt */
855 __HAL_RTC_ALARMA_DISABLE(hrtc);
857 /* In case of interrupt mode is used, the interrupt source must disabled */
858 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
861 tickstart = HAL_GetTick();
863 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
864 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
866 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
868 /* Enable the write protection for RTC registers */
869 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
871 hrtc->State = HAL_RTC_STATE_TIMEOUT;
873 /* Process Unlocked */
880 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
881 /* Configure the Alarm A Sub Second register */
882 hrtc->Instance->ALRMASSR = subsecondtmpreg;
883 /* Configure the Alarm state: Enable Alarm */
884 __HAL_RTC_ALARMA_ENABLE(hrtc);
888 /* Disable the Alarm B interrupt */
889 __HAL_RTC_ALARMB_DISABLE(hrtc);
891 /* In case of interrupt mode is used, the interrupt source must disabled */
892 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
895 tickstart = HAL_GetTick();
897 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
898 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
900 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
902 /* Enable the write protection for RTC registers */
903 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
905 hrtc->State = HAL_RTC_STATE_TIMEOUT;
907 /* Process Unlocked */
914 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
915 /* Configure the Alarm B Sub Second register */
916 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
917 /* Configure the Alarm state: Enable Alarm */
918 __HAL_RTC_ALARMB_ENABLE(hrtc);
921 /* Enable the write protection for RTC registers */
922 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
924 /* Change RTC state */
925 hrtc->State = HAL_RTC_STATE_READY;
927 /* Process Unlocked */
934 * @brief Sets the specified RTC Alarm with Interrupt
935 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
936 * the configuration information for RTC.
937 * @param sAlarm: Pointer to Alarm structure
938 * @param Format: Specifies the format of the entered parameters.
939 * This parameter can be one of the following values:
940 * @arg FORMAT_BIN: Binary data format
941 * @arg FORMAT_BCD: BCD data format
944 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
946 uint32_t tickstart = 0;
947 uint32_t tmpreg = 0, subsecondtmpreg = 0;
949 /* Check the parameters */
950 assert_param(IS_RTC_FORMAT(Format));
951 assert_param(IS_ALARM(sAlarm->Alarm));
952 assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
953 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
954 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
955 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
960 hrtc->State = HAL_RTC_STATE_BUSY;
962 if(Format == FORMAT_BIN)
964 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
966 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
967 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
971 sAlarm->AlarmTime.TimeFormat = 0x00;
972 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
974 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
975 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
977 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
979 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
983 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
985 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
986 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
987 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
988 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
989 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
990 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
991 ((uint32_t)sAlarm->AlarmMask));
995 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
997 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
998 assert_param(IS_RTC_HOUR12(tmpreg));
999 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1003 sAlarm->AlarmTime.TimeFormat = 0x00;
1004 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1007 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1008 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1010 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1012 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1013 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1017 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1018 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1020 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
1021 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
1022 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1023 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1024 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
1025 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1026 ((uint32_t)sAlarm->AlarmMask));
1028 /* Configure the Alarm A or Alarm B Sub Second registers */
1029 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1031 /* Disable the write protection for RTC registers */
1032 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1034 /* Configure the Alarm register */
1035 if(sAlarm->Alarm == RTC_ALARM_A)
1037 /* Disable the Alarm A interrupt */
1038 __HAL_RTC_ALARMA_DISABLE(hrtc);
1040 /* Clear flag alarm A */
1041 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1044 tickstart = HAL_GetTick();
1046 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1047 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1049 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1051 /* Enable the write protection for RTC registers */
1052 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1054 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1056 /* Process Unlocked */
1063 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1064 /* Configure the Alarm A Sub Second register */
1065 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1066 /* Configure the Alarm state: Enable Alarm */
1067 __HAL_RTC_ALARMA_ENABLE(hrtc);
1068 /* Configure the Alarm interrupt */
1069 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1073 /* Disable the Alarm B interrupt */
1074 __HAL_RTC_ALARMB_DISABLE(hrtc);
1076 /* Clear flag alarm B */
1077 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1080 tickstart = HAL_GetTick();
1082 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1083 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1085 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1087 /* Enable the write protection for RTC registers */
1088 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1090 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1092 /* Process Unlocked */
1099 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1100 /* Configure the Alarm B Sub Second register */
1101 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1102 /* Configure the Alarm state: Enable Alarm */
1103 __HAL_RTC_ALARMB_ENABLE(hrtc);
1104 /* Configure the Alarm interrupt */
1105 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1108 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1109 __HAL_RTC_EXTI_ENABLE_IT(RTC_EXTI_LINE_ALARM_EVENT);
1111 EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
1113 /* Enable the write protection for RTC registers */
1114 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1116 hrtc->State = HAL_RTC_STATE_READY;
1118 /* Process Unlocked */
1125 * @brief Deactive the specified RTC Alarm
1126 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1127 * the configuration information for RTC.
1128 * @param Alarm: Specifies the Alarm.
1129 * This parameter can be one of the following values:
1130 * @arg RTC_ALARM_A: AlarmA
1131 * @arg RTC_ALARM_B: AlarmB
1132 * @retval HAL status
1134 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1136 uint32_t tickstart = 0;
1138 /* Check the parameters */
1139 assert_param(IS_ALARM(Alarm));
1141 /* Process Locked */
1144 hrtc->State = HAL_RTC_STATE_BUSY;
1146 /* Disable the write protection for RTC registers */
1147 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1149 if(Alarm == RTC_ALARM_A)
1152 __HAL_RTC_ALARMA_DISABLE(hrtc);
1154 /* In case of interrupt mode is used, the interrupt source must disabled */
1155 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1158 tickstart = HAL_GetTick();
1160 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1161 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1163 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1165 /* Enable the write protection for RTC registers */
1166 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1168 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1170 /* Process Unlocked */
1180 __HAL_RTC_ALARMB_DISABLE(hrtc);
1182 /* In case of interrupt mode is used, the interrupt source must disabled */
1183 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
1186 tickstart = HAL_GetTick();
1188 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1189 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1191 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1193 /* Enable the write protection for RTC registers */
1194 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1196 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1198 /* Process Unlocked */
1205 /* Enable the write protection for RTC registers */
1206 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1208 hrtc->State = HAL_RTC_STATE_READY;
1210 /* Process Unlocked */
1217 * @brief Gets the RTC Alarm value and masks.
1218 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1219 * the configuration information for RTC.
1220 * @param sAlarm: Pointer to Date structure
1221 * @param Alarm: Specifies the Alarm.
1222 * This parameter can be one of the following values:
1223 * @arg RTC_ALARM_A: AlarmA
1224 * @arg RTC_ALARM_B: AlarmB
1225 * @param Format: Specifies the format of the entered parameters.
1226 * This parameter can be one of the following values:
1227 * @arg FORMAT_BIN: Binary data format
1228 * @arg FORMAT_BCD: BCD data format
1229 * @retval HAL status
1231 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1233 uint32_t tmpreg = 0, subsecondtmpreg = 0;
1235 /* Check the parameters */
1236 assert_param(IS_RTC_FORMAT(Format));
1237 assert_param(IS_ALARM(Alarm));
1239 if(Alarm == RTC_ALARM_A)
1242 sAlarm->Alarm = RTC_ALARM_A;
1244 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1245 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1249 sAlarm->Alarm = RTC_ALARM_B;
1251 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1252 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1255 /* Fill the structure with the read parameters */
1256 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
1257 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
1258 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1259 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1260 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1261 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1262 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1263 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1265 if(Format == FORMAT_BIN)
1267 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1268 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1269 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1270 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1277 * @brief This function handles Alarm interrupt request.
1278 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1279 * the configuration information for RTC.
1282 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1284 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
1286 /* Get the status of the Interrupt */
1287 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
1289 /* AlarmA callback */
1290 HAL_RTC_AlarmAEventCallback(hrtc);
1292 /* Clear the Alarm interrupt pending bit */
1293 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1297 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
1299 /* Get the status of the Interrupt */
1300 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
1302 /* AlarmB callback */
1303 HAL_RTCEx_AlarmBEventCallback(hrtc);
1305 /* Clear the Alarm interrupt pending bit */
1306 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
1310 /* Clear the EXTI's line Flag for RTC Alarm */
1311 __HAL_RTC_EXTI_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT);
1313 /* Change RTC state */
1314 hrtc->State = HAL_RTC_STATE_READY;
1318 * @brief Alarm A callback.
1319 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1320 * the configuration information for RTC.
1323 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1325 /* NOTE : This function Should not be modified, when the callback is needed,
1326 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1331 * @brief This function handles AlarmA Polling request.
1332 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1333 * the configuration information for RTC.
1334 * @param Timeout: Timeout duration
1335 * @retval HAL status
1337 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1339 uint32_t tickstart = 0;
1342 tickstart = HAL_GetTick();
1344 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1346 if(Timeout != HAL_MAX_DELAY)
1348 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1350 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1356 /* Clear the Alarm interrupt pending bit */
1357 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1359 /* Change RTC state */
1360 hrtc->State = HAL_RTC_STATE_READY;
1369 /** @defgroup RTC_Group4 Peripheral Control functions
1370 * @brief Peripheral Control functions
1373 ===============================================================================
1374 ##### Peripheral Control functions #####
1375 ===============================================================================
1377 This subsection provides functions allowing to
1378 (+) Wait for RTC Time and Date Synchronization
1385 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1386 * synchronized with RTC APB clock.
1387 * @note The RTC Resynchronization mode is write protected, use the
1388 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1389 * @note To read the calendar through the shadow registers after Calendar
1390 * initialization, calendar update or after wakeup from low power modes
1391 * the software must first clear the RSF flag.
1392 * The software must then wait until it is set again before reading
1393 * the calendar, which means that the calendar registers have been
1394 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1395 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1396 * the configuration information for RTC.
1397 * @retval HAL status
1399 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1401 uint32_t tickstart = 0;
1403 /* Clear RSF flag */
1404 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1407 tickstart = HAL_GetTick();
1409 /* Wait the registers to be synchronised */
1410 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1412 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1421 /** @defgroup RTC_Group5 Peripheral State functions
1422 * @brief Peripheral State functions
1425 ===============================================================================
1426 ##### Peripheral State functions #####
1427 ===============================================================================
1429 This subsection provides functions allowing to
1436 * @brief Returns the RTC state.
1437 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1438 * the configuration information for RTC.
1441 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1451 * @brief Enters the RTC Initialization mode.
1452 * @note The RTC Initialization mode is write protected, use the
1453 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1454 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1455 * the configuration information for RTC.
1456 * @retval HAL status
1458 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1460 uint32_t tickstart = 0;
1462 /* Check if the Initialization mode is set */
1463 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1465 /* Set the Initialization mode */
1466 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1469 tickstart = HAL_GetTick();
1471 /* Wait till RTC is in INIT state and if Time out is reached exit */
1472 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1474 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1486 * @brief Converts a 2 digit decimal to BCD format.
1487 * @param Value: Byte to be converted
1488 * @retval Converted byte
1490 uint8_t RTC_ByteToBcd2(uint8_t Value)
1492 uint32_t bcdhigh = 0;
1500 return ((uint8_t)(bcdhigh << 4) | Value);
1504 * @brief Converts from 2 digit BCD to Binary.
1505 * @param Value: BCD value to be converted
1506 * @retval Converted word
1508 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1511 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1512 return (tmp + (Value & (uint8_t)0x0F));
1519 #endif /* HAL_RTC_MODULE_ENABLED */
1528 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/