2 ******************************************************************************
3 * @file stm32f3xx_hal_rtc.c
4 * @author MCD Application Team
7 * @brief RTC HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Real-Time Clock (RTC) peripheral:
12 * - Calendar (Time and Date) configuration
13 * - Alarms (Alarm A and Alarm B) configuration
14 * - WakeUp Timer configuration
15 * - TimeStamp configuration
16 * - Tampers configuration
17 * - Backup Data Registers configuration
18 * - RTC Tamper and TimeStamp Pins Selection
19 * - Interrupts and flags management
23 ===============================================================================
24 ##### RTC Operating Condition #####
25 ===============================================================================
26 [..] The real-time clock (RTC) and the RTC backup registers can be powered
27 from the VBAT voltage when the main VDD supply is powered off.
28 To retain the content of the RTC backup registers and supply the RTC
29 when VDD is turned off, VBAT pin can be connected to an optional
30 standby voltage supplied by a battery or by another source.
32 [..] To allow the RTC to operate even when the main digital supply (VDD)
33 is turned off, the VBAT pin powers the following blocks:
35 (#) The LSE oscillator
36 (#) PC13 to PC15 I/Os (when available)
38 [..] When the backup domain is supplied by VDD (analog switch connected
39 to VDD), the following functions are available:
40 (#) PC14 and PC15 can be used as either GPIO or LSE pins
41 (#) PC13 can be used as a GPIO or as the RTC_AF pin
43 [..] When the backup domain is supplied by VBAT (analog switch connected
44 to VBAT because VDD is not present), the following functions are available:
45 (#) PC14 and PC15 can be used as LSE pins only
46 (#) PC13 can be used as the RTC_AF pin
48 ##### Backup Domain Reset #####
49 ===============================================================================
50 [..] The backup domain reset sets all RTC registers and the RCC_BDCR
51 register to their reset values.
52 A backup domain reset is generated when one of the following events
54 (#) Software reset, triggered by setting the BDRST bit in the
55 RCC Backup domain control register (RCC_BDCR).
56 (#) VDD or VBAT power on, if both supplies have previously been
59 ##### Backup Domain Access #####
60 ===================================================================
61 [..] After reset, the backup domain (RTC registers, RTC backup data
62 registers and backup SRAM) is protected against possible unwanted write
65 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
66 (#) Enable the Power Controller (PWR) APB1 interface clock using the
67 __PWR_CLK_ENABLE() function.
68 (#) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
69 (#) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
70 (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
73 ##### How to use RTC Driver #####
74 ===================================================================
76 (+) Enable the RTC domain access (see description in the section above).
77 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
78 format using the HAL_RTC_Init() function.
80 *** Time and Date configuration ***
81 ===================================
83 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
84 and HAL_RTC_SetDate() functions.
85 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
87 *** Alarm configuration ***
88 ===========================
90 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
91 You can also configure the RTC Alarm with interrupt mode using the
92 HAL_RTC_SetAlarm_IT() function.
93 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
95 *** RTC Wakeup configuration ***
96 ================================
98 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
99 function. You can also configure the RTC Wakeup timer with interrupt mode
100 using the HAL_RTC_SetWakeUpTimer_IT() function.
101 (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
104 *** TimeStamp configuration ***
105 ===============================
107 (+) Configure the RTC_AF trigger and enables the RTC TimeStamp using the
108 HAL_RTC_SetTimeStamp() function. You can also configure the RTC TimeStamp with
109 interrupt mode using the HAL_RTC_SetTimeStamp_IT() function.
110 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
113 *** Tamper configuration ***
114 ============================
116 (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
117 or Level according to the Tamper filter (if equal to 0 Edge else Level)
118 value, sampling frequency, precharge or discharge and Pull-UP using the
119 HAL_RTC_SetTamper() function. You can configure RTC Tamper with interrupt
120 mode using HAL_RTC_SetTamper_IT() function.
122 *** Backup Data Registers configuration ***
123 ===========================================
125 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
127 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
131 ##### RTC and low power modes #####
132 ===================================================================
133 [..] The MCU can be woken up from a low power mode by an RTC alternate
135 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
136 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
137 These RTC alternate functions can wake up the system from the Stop and
138 Standby low power modes.
139 [..] The system can also wake up from low power modes without depending
140 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
141 or the RTC wakeup events.
142 [..] The RTC provides a programmable time base for waking up from the
143 Stop or Standby mode at regular intervals.
144 Wakeup from STOP and Standby modes is possible only when the RTC clock source
149 ******************************************************************************
152 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
154 * Redistribution and use in source and binary forms, with or without modification,
155 * are permitted provided that the following conditions are met:
156 * 1. Redistributions of source code must retain the above copyright notice,
157 * this list of conditions and the following disclaimer.
158 * 2. Redistributions in binary form must reproduce the above copyright notice,
159 * this list of conditions and the following disclaimer in the documentation
160 * and/or other materials provided with the distribution.
161 * 3. Neither the name of STMicroelectronics nor the names of its contributors
162 * may be used to endorse or promote products derived from this software
163 * without specific prior written permission.
165 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
166 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
168 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
171 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
172 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
173 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
174 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
176 ******************************************************************************
179 /* Includes ------------------------------------------------------------------*/
180 #include "stm32f3xx_hal.h"
182 /** @addtogroup STM32F3xx_HAL_Driver
186 /** @defgroup RTC RTC HAL module driver
187 * @brief RTC HAL module driver
191 #ifdef HAL_RTC_MODULE_ENABLED
193 /* Private typedef -----------------------------------------------------------*/
194 /* Private define ------------------------------------------------------------*/
195 /* Private macro -------------------------------------------------------------*/
196 /* Private variables ---------------------------------------------------------*/
197 /* Private function prototypes -----------------------------------------------*/
198 /* Exported functions ---------------------------------------------------------*/
200 /** @defgroup RTC_Exported_Functions RTC Exported Functions
204 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
205 * @brief Initialization and Configuration functions
208 ===============================================================================
209 ##### Initialization and de-initialization functions #####
210 ===============================================================================
211 [..] This section provide functions allowing to initialize and configure the
212 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
213 RTC registers Write protection, enter and exit the RTC initialization mode,
214 RTC registers synchronization check and reference clock detection enable.
215 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
216 It is split into 2 programmable prescalers to minimize power consumption.
217 (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
218 (++) When both prescalers are used, it is recommended to configure the
219 asynchronous prescaler to a high value to minimize consumption.
220 (#) All RTC registers are Write protected. Writing to the RTC registers
221 is enabled by writing a key into the Write Protection register, RTC_WPR.
222 (#) To Configure the RTC Calendar, user application should enter
223 initialization mode. In this mode, the calendar counter is stopped
224 and its value can be updated. When the initialization sequence is
225 complete, the calendar restarts counting after 4 RTCCLK cycles.
226 (#) To read the calendar through the shadow registers after Calendar
227 initialization, calendar update or after wakeup from low power modes
228 the software must first clear the RSF flag. The software must then
229 wait until it is set again before reading the calendar, which means
230 that the calendar registers have been correctly copied into the
231 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
232 implements the above software sequence (RSF clear and RSF check).
239 * @brief Initializes the RTC peripheral
240 * @param hrtc: RTC handle
243 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
245 /* Check the RTC peripheral state */
251 /* Check the parameters */
252 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
253 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
254 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
255 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
256 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
257 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
258 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
260 if(hrtc->State == HAL_RTC_STATE_RESET)
262 /* Initialize RTC MSP */
263 HAL_RTC_MspInit(hrtc);
267 hrtc->State = HAL_RTC_STATE_BUSY;
269 /* Disable the write protection for RTC registers */
270 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
272 /* Set Initialization mode */
273 if(RTC_EnterInitMode(hrtc) != HAL_OK)
275 /* Enable the write protection for RTC registers */
276 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
279 hrtc->State = HAL_RTC_STATE_ERROR;
285 /* Clear RTC_CR FMT, OSEL and POL Bits */
286 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
287 /* Set RTC_CR register */
288 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
290 /* Configure the RTC PRER */
291 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
292 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
294 /* Exit Initialization mode */
295 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
297 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
298 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
300 /* Enable the write protection for RTC registers */
301 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
304 hrtc->State = HAL_RTC_STATE_READY;
311 * @brief DeInitializes the RTC peripheral
312 * @param hrtc: RTC handle
313 * @note This function doesn't reset the RTC Backup Data registers.
316 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
318 uint32_t tickstart = 0;
320 /* Check the parameters */
321 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
324 hrtc->State = HAL_RTC_STATE_BUSY;
326 /* Disable the write protection for RTC registers */
327 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
329 /* Set Initialization mode */
330 if(RTC_EnterInitMode(hrtc) != HAL_OK)
332 /* Enable the write protection for RTC registers */
333 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
336 hrtc->State = HAL_RTC_STATE_ERROR;
342 /* Reset TR, DR and CR registers */
343 hrtc->Instance->TR = (uint32_t)0x00000000;
344 hrtc->Instance->DR = (uint32_t)0x00002101;
345 /* Reset All CR bits except CR[2:0] */
346 hrtc->Instance->CR &= (uint32_t)0x00000007;
348 tickstart = HAL_GetTick();
350 /* Wait till WUTWF flag is set and if Time out is reached exit */
351 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
353 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
355 /* Enable the write protection for RTC registers */
356 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
359 hrtc->State = HAL_RTC_STATE_TIMEOUT;
365 /* Reset all RTC CR register bits */
366 hrtc->Instance->CR &= (uint32_t)0x00000000;
367 hrtc->Instance->WUTR = (uint32_t)0x0000FFFF;
368 hrtc->Instance->PRER = (uint32_t)0x007F00FF;
369 hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
370 hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
371 hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
372 hrtc->Instance->CALR = (uint32_t)0x00000000;
373 hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
374 hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
376 /* Reset ISR register and exit initialization mode */
377 hrtc->Instance->ISR = (uint32_t)0x00000000;
379 /* Reset Tamper and alternate functions configuration register */
380 hrtc->Instance->TAFCR = 0x00000000;
382 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
383 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
385 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
387 /* Enable the write protection for RTC registers */
388 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
390 hrtc->State = HAL_RTC_STATE_ERROR;
397 /* Enable the write protection for RTC registers */
398 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
400 /* De-Initialize RTC MSP */
401 HAL_RTC_MspDeInit(hrtc);
403 hrtc->State = HAL_RTC_STATE_RESET;
412 * @brief Initializes the RTC MSP.
413 * @param hrtc: RTC handle
416 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
418 /* NOTE : This function Should not be modified, when the callback is needed,
419 the HAL_RTC_MspInit could be implenetd in the user file
424 * @brief DeInitializes the RTC MSP.
425 * @param hrtc: RTC handle
428 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
430 /* NOTE : This function Should not be modified, when the callback is needed,
431 the HAL_RTC_MspDeInit could be implenetd in the user file
439 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
440 * @brief RTC Time and Date functions
443 ===============================================================================
444 ##### RTC Time and Date functions #####
445 ===============================================================================
447 [..] This section provide functions allowing to configure Time and Date features
454 * @brief Sets RTC current time.
455 * @param hrtc: RTC handle
456 * @param sTime: Pointer to Time structure
457 * @param Format: Specifies the format of the entered parameters.
458 * This parameter can be one of the following values:
459 * @arg Format_BIN: Binary data format
460 * @arg Format_BCD: BCD data format
463 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
467 /* Check the parameters */
468 assert_param(IS_RTC_FORMAT(Format));
469 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
470 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
475 hrtc->State = HAL_RTC_STATE_BUSY;
477 if(Format == FORMAT_BIN)
479 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
481 assert_param(IS_RTC_HOUR12(sTime->Hours));
482 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
486 sTime->TimeFormat = 0x00;
487 assert_param(IS_RTC_HOUR24(sTime->Hours));
489 assert_param(IS_RTC_MINUTES(sTime->Minutes));
490 assert_param(IS_RTC_SECONDS(sTime->Seconds));
492 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
493 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
494 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
495 (((uint32_t)sTime->TimeFormat) << 16));
499 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
501 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
502 assert_param(IS_RTC_HOUR12(tmpreg));
503 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
507 sTime->TimeFormat = 0x00;
508 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
510 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
511 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
512 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
513 ((uint32_t)(sTime->Minutes) << 8) | \
514 ((uint32_t)sTime->Seconds) | \
515 ((uint32_t)(sTime->TimeFormat) << 16));
518 /* Disable the write protection for RTC registers */
519 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
521 /* Set Initialization mode */
522 if(RTC_EnterInitMode(hrtc) != HAL_OK)
524 /* Enable the write protection for RTC registers */
525 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
528 hrtc->State = HAL_RTC_STATE_ERROR;
530 /* Process Unlocked */
537 /* Set the RTC_TR register */
538 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
540 /* Clear the bits to be configured */
541 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
543 /* Configure the RTC_CR register */
544 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
546 /* Exit Initialization mode */
547 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
549 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
550 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
552 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
554 /* Enable the write protection for RTC registers */
555 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
557 hrtc->State = HAL_RTC_STATE_ERROR;
559 /* Process Unlocked */
566 /* Enable the write protection for RTC registers */
567 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
569 hrtc->State = HAL_RTC_STATE_READY;
578 * @brief Gets RTC current time.
579 * @param hrtc: RTC handle
580 * @param sTime: Pointer to Time structure
581 * @param Format: Specifies the format of the entered parameters.
582 * This parameter can be one of the following values:
583 * @arg Format_BIN: Binary data format
584 * @arg Format_BCD: BCD data format
585 * @note Call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
586 * in the higher-order calendar shadow registers.
589 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
593 /* Check the parameters */
594 assert_param(IS_RTC_FORMAT(Format));
596 /* Get subseconds values from the correspondent registers*/
597 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
599 /* Get the TR register */
600 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
602 /* Fill the structure fields with the read parameters */
603 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
604 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
605 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
606 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
608 /* Check the input parameters format */
609 if(Format == FORMAT_BIN)
611 /* Convert the time structure parameters to Binary format */
612 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
613 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
614 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
621 * @brief Sets RTC current date.
622 * @param hrtc: RTC handle
623 * @param sDate: Pointer to date structure
624 * @param Format: specifies the format of the entered parameters.
625 * This parameter can be one of the following values:
626 * @arg Format_BIN: Binary data format
627 * @arg Format_BCD: BCD data format
630 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
632 uint32_t datetmpreg = 0;
634 /* Check the parameters */
635 assert_param(IS_RTC_FORMAT(Format));
640 hrtc->State = HAL_RTC_STATE_BUSY;
642 if((Format == FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
644 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
647 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
649 if(Format == FORMAT_BIN)
651 assert_param(IS_RTC_YEAR(sDate->Year));
652 assert_param(IS_RTC_MONTH(sDate->Month));
653 assert_param(IS_RTC_DATE(sDate->Date));
655 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
656 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
657 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
658 ((uint32_t)sDate->WeekDay << 13));
662 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
663 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
664 assert_param(IS_RTC_MONTH(datetmpreg));
665 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
666 assert_param(IS_RTC_DATE(datetmpreg));
668 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
669 (((uint32_t)sDate->Month) << 8) | \
670 ((uint32_t)sDate->Date) | \
671 (((uint32_t)sDate->WeekDay) << 13));
674 /* Disable the write protection for RTC registers */
675 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
677 /* Set Initialization mode */
678 if(RTC_EnterInitMode(hrtc) != HAL_OK)
680 /* Enable the write protection for RTC registers */
681 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
684 hrtc->State = HAL_RTC_STATE_ERROR;
686 /* Process Unlocked */
693 /* Set the RTC_DR register */
694 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
696 /* Exit Initialization mode */
697 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
699 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
700 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
702 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
704 /* Enable the write protection for RTC registers */
705 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
707 hrtc->State = HAL_RTC_STATE_ERROR;
709 /* Process Unlocked */
716 /* Enable the write protection for RTC registers */
717 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
719 hrtc->State = HAL_RTC_STATE_READY ;
721 /* Process Unlocked */
729 * @brief Gets RTC current date.
730 * @param hrtc: RTC handle
731 * @param sDate: Pointer to Date structure
732 * @param Format: Specifies the format of the entered parameters.
733 * This parameter can be one of the following values:
734 * @arg Format_BIN : Binary data format
735 * @arg Format_BCD : BCD data format
738 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
740 uint32_t datetmpreg = 0;
742 /* Check the parameters */
743 assert_param(IS_RTC_FORMAT(Format));
745 /* Get the DR register */
746 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
748 /* Fill the structure fields with the read parameters */
749 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
750 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
751 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
752 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
754 /* Check the input parameters format */
755 if(Format == FORMAT_BIN)
757 /* Convert the date structure parameters to Binary format */
758 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
759 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
760 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
769 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
770 * @brief RTC Alarm functions
773 ===============================================================================
774 ##### RTC Alarm functions #####
775 ===============================================================================
777 [..] This section provide functions allowing to configure Alarm feature
783 * @brief Sets the specified RTC Alarm.
784 * @param hrtc: RTC handle
785 * @param sAlarm: Pointer to Alarm structure
786 * @param Format: Specifies the format of the entered parameters.
787 * This parameter can be one of the following values:
788 * @arg Format_BIN: Binary data format
789 * @arg Format_BCD: BCD data format
792 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
794 uint32_t tickstart = 0;
795 uint32_t tmpreg = 0, subsecondtmpreg = 0;
797 /* Check the parameters */
798 assert_param(IS_RTC_FORMAT(Format));
799 assert_param(IS_ALARM(sAlarm->Alarm));
800 assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
801 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
802 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
803 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
808 hrtc->State = HAL_RTC_STATE_BUSY;
810 if(Format == FORMAT_BIN)
812 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
814 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
815 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
819 sAlarm->AlarmTime.TimeFormat = 0x00;
820 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
822 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
823 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
825 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
827 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
831 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
834 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
835 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
836 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
837 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
838 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
839 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
840 ((uint32_t)sAlarm->AlarmMask));
844 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
846 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
847 assert_param(IS_RTC_HOUR12(tmpreg));
848 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
852 sAlarm->AlarmTime.TimeFormat = 0x00;
853 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
856 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
857 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
859 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
861 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
862 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
866 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
867 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
870 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
871 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
872 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
873 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
874 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
875 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
876 ((uint32_t)sAlarm->AlarmMask));
879 /* Configure the Alarm A or Alarm B Sub Second registers */
880 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
882 /* Disable the write protection for RTC registers */
883 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
885 /* Configure the Alarm register */
886 if(sAlarm->Alarm == RTC_ALARM_A)
888 /* Disable the Alarm A interrupt */
889 __HAL_RTC_ALARMA_DISABLE(hrtc);
891 /* In case of interrupt mode is used, the interrupt source must disabled */
892 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
894 tickstart = HAL_GetTick();
895 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
896 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
898 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
900 /* Enable the write protection for RTC registers */
901 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
903 hrtc->State = HAL_RTC_STATE_TIMEOUT;
905 /* Process Unlocked */
912 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
913 /* Configure the Alarm A Sub Second register */
914 hrtc->Instance->ALRMASSR = subsecondtmpreg;
915 /* Configure the Alarm state: Enable Alarm */
916 __HAL_RTC_ALARMA_ENABLE(hrtc);
920 /* Disable the Alarm B interrupt */
921 __HAL_RTC_ALARMB_DISABLE(hrtc);
923 /* In case of interrupt mode is used, the interrupt source must disabled */
924 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
926 tickstart = HAL_GetTick();
927 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
928 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
930 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
932 /* Enable the write protection for RTC registers */
933 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
935 hrtc->State = HAL_RTC_STATE_TIMEOUT;
937 /* Process Unlocked */
944 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
945 /* Configure the Alarm B Sub Second register */
946 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
947 /* Configure the Alarm state: Enable Alarm */
948 __HAL_RTC_ALARMB_ENABLE(hrtc);
951 /* Enable the write protection for RTC registers */
952 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
954 /* Change RTC state */
955 hrtc->State = HAL_RTC_STATE_READY;
957 /* Process Unlocked */
964 * @brief Sets the specified RTC Alarm with Interrupt
965 * @param hrtc: RTC handle
966 * @param sAlarm: Pointer to Alarm structure
967 * @param Format: Specifies the format of the entered parameters.
968 * This parameter can be one of the following values:
969 * @arg Format_BIN: Binary data format
970 * @arg Format_BCD: BCD data format
971 * @note The Alarm register can only be written when the corresponding Alarm
972 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
973 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
976 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
978 uint32_t tickstart = 0;
979 uint32_t tmpreg = 0, subsecondtmpreg = 0;
981 /* Check the parameters */
982 assert_param(IS_RTC_FORMAT(Format));
983 assert_param(IS_ALARM(sAlarm->Alarm));
984 assert_param(IS_ALARM_MASK(sAlarm->AlarmMask));
985 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
986 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
987 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
992 hrtc->State = HAL_RTC_STATE_BUSY;
994 if(Format == FORMAT_BIN)
996 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
998 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
999 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1003 sAlarm->AlarmTime.TimeFormat = 0x00;
1004 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1006 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1007 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1009 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1011 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1015 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1017 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
1018 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
1019 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1020 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1021 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
1022 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1023 ((uint32_t)sAlarm->AlarmMask));
1027 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1029 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1030 assert_param(IS_RTC_HOUR12(tmpreg));
1031 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1035 sAlarm->AlarmTime.TimeFormat = 0x00;
1036 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1039 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1040 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1042 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1044 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1045 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1049 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1050 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1052 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
1053 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
1054 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1055 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1056 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
1057 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1058 ((uint32_t)sAlarm->AlarmMask));
1060 /* Configure the Alarm A or Alarm B Sub Second registers */
1061 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1063 /* Disable the write protection for RTC registers */
1064 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1066 /* Configure the Alarm register */
1067 if(sAlarm->Alarm == RTC_ALARM_A)
1069 /* Disable the Alarm A interrupt */
1070 __HAL_RTC_ALARMA_DISABLE(hrtc);
1072 /* Clear flag alarm A */
1073 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1075 tickstart = HAL_GetTick();
1076 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1077 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1079 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1081 /* Enable the write protection for RTC registers */
1082 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1084 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1086 /* Process Unlocked */
1093 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1094 /* Configure the Alarm A Sub Second register */
1095 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1096 /* Configure the Alarm state: Enable Alarm */
1097 __HAL_RTC_ALARMA_ENABLE(hrtc);
1098 /* Configure the Alarm interrupt */
1099 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1103 /* Disable the Alarm B interrupt */
1104 __HAL_RTC_ALARMB_DISABLE(hrtc);
1106 /* Clear flag alarm B */
1107 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1109 tickstart = HAL_GetTick();
1110 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1111 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1113 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1115 /* Enable the write protection for RTC registers */
1116 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1118 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1120 /* Process Unlocked */
1127 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1128 /* Configure the Alarm B Sub Second register */
1129 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1130 /* Configure the Alarm state: Enable Alarm */
1131 __HAL_RTC_ALARMB_ENABLE(hrtc);
1132 /* Configure the Alarm interrupt */
1133 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1136 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1137 __HAL_RTC_ENABLE_IT(RTC_EXTI_LINE_ALARM_EVENT);
1139 EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
1141 /* Enable the write protection for RTC registers */
1142 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1144 hrtc->State = HAL_RTC_STATE_READY;
1146 /* Process Unlocked */
1153 * @brief Deactive the specified RTC Alarm
1154 * @param hrtc: RTC handle
1155 * @param Alarm: Specifies the Alarm.
1156 * This parameter can be one of the following values:
1157 * @arg ALARM_A : AlarmA
1158 * @arg ALARM_B : AlarmB
1159 * @retval HAL status
1161 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1163 uint32_t tickstart = 0;
1165 /* Check the parameters */
1166 assert_param(IS_ALARM(Alarm));
1168 /* Process Locked */
1171 hrtc->State = HAL_RTC_STATE_BUSY;
1173 /* Disable the write protection for RTC registers */
1174 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1176 if(Alarm == RTC_ALARM_A)
1179 __HAL_RTC_ALARMA_DISABLE(hrtc);
1181 /* In case of interrupt mode is used, the interrupt source must disabled */
1182 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1184 tickstart = HAL_GetTick();
1186 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1187 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1189 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1191 /* Enable the write protection for RTC registers */
1192 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1194 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1196 /* Process Unlocked */
1206 __HAL_RTC_ALARMB_DISABLE(hrtc);
1208 /* In case of interrupt mode is used, the interrupt source must disabled */
1209 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
1211 tickstart = HAL_GetTick();
1213 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1214 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1216 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1218 /* Enable the write protection for RTC registers */
1219 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1221 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1223 /* Process Unlocked */
1230 /* Enable the write protection for RTC registers */
1231 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1233 hrtc->State = HAL_RTC_STATE_READY;
1235 /* Process Unlocked */
1242 * @brief Gets the RTC Alarm value and masks.
1243 * @param hrtc: RTC handle
1244 * @param sAlarm: Pointer to Date structure
1245 * @param Alarm: Specifies the Alarm
1246 * This parameter can be one of the following values:
1247 * @arg ALARM_A: AlarmA
1248 * @arg ALARM_B: AlarmB
1249 * @param Format: Specifies the format of the entered parameters.
1250 * This parameter can be one of the following values:
1251 * @arg Format_BIN: Binary data format
1252 * @arg Format_BCD: BCD data format
1253 * @retval HAL status
1255 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1257 uint32_t tmpreg = 0, subsecondtmpreg = 0;
1259 /* Check the parameters */
1260 assert_param(IS_RTC_FORMAT(Format));
1261 assert_param(IS_ALARM(Alarm));
1263 if(Alarm == RTC_ALARM_A)
1266 sAlarm->Alarm = RTC_ALARM_A;
1268 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1269 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1273 sAlarm->Alarm = RTC_ALARM_B;
1275 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1276 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1279 /* Fill the structure with the read parameters */
1280 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
1281 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
1282 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1283 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1284 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1285 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1286 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1287 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1289 if(Format == FORMAT_BIN)
1291 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1292 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1293 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1294 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1301 * @brief This function handles Alarm interrupt request.
1302 * @param hrtc: RTC handle
1305 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1307 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
1309 /* Get the status of the Interrupt */
1310 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
1312 /* AlarmA callback */
1313 HAL_RTC_AlarmAEventCallback(hrtc);
1315 /* Clear the Alarm interrupt pending bit */
1316 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1320 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
1322 /* Get the status of the Interrupt */
1323 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
1325 /* AlarmB callback */
1326 HAL_RTCEx_AlarmBEventCallback(hrtc);
1328 /* Clear the Alarm interrupt pending bit */
1329 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
1333 /* Clear the EXTI's line Flag for RTC Alarm */
1334 __HAL_RTC_CLEAR_FLAG(RTC_EXTI_LINE_ALARM_EVENT);
1336 /* Change RTC state */
1337 hrtc->State = HAL_RTC_STATE_READY;
1341 * @brief Alarm A callback.
1342 * @param hrtc: RTC handle
1345 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1347 /* NOTE : This function Should not be modified, when the callback is needed,
1348 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1353 * @brief This function handles AlarmA Polling request.
1354 * @param hrtc: RTC handle
1355 * @param Timeout: Timeout duration
1356 * @retval HAL status
1358 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1361 uint32_t tickstart = HAL_GetTick();
1363 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1365 if(Timeout != HAL_MAX_DELAY)
1367 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1369 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1375 /* Clear the Alarm interrupt pending bit */
1376 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1378 /* Change RTC state */
1379 hrtc->State = HAL_RTC_STATE_READY;
1388 /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
1389 * @brief Peripheral Control functions
1392 ===============================================================================
1393 ##### Peripheral Control functions #####
1394 ===============================================================================
1396 This subsection provides functions allowing to
1397 (+) Wait for RTC Time and Date Synchronization
1404 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1405 * synchronized with RTC APB clock.
1406 * @note The RTC Resynchronization mode is write protected, use the
1407 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1408 * @note To read the calendar through the shadow registers after Calendar
1409 * initialization, calendar update or after wakeup from low power modes
1410 * the software must first clear the RSF flag.
1411 * The software must then wait until it is set again before reading
1412 * the calendar, which means that the calendar registers have been
1413 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1414 * @param hrtc: RTC handle
1415 * @retval HAL status
1417 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1419 uint32_t tickstart = 0;
1421 /* Clear RSF flag */
1422 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1424 tickstart = HAL_GetTick();
1426 /* Wait the registers to be synchronised */
1427 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1429 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1442 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
1443 * @brief Peripheral State functions
1446 ===============================================================================
1447 ##### Peripheral State functions #####
1448 ===============================================================================
1450 This subsection provides functions allowing to
1457 * @brief Returns the Alarm state.
1458 * @param hrtc: RTC handle
1461 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1474 /** @defgroup RTC_Private_Functions RTC Private Functions
1479 * @brief Enters the RTC Initialization mode.
1480 * @note The RTC Initialization mode is write protected, use the
1481 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1482 * @param hrtc: RTC handle
1483 * @retval An ErrorStatus enumeration value:
1484 * - HAL_OK : RTC is in Init mode
1485 * - HAL_TIMEOUT : RTC is not in Init mode and in Timeout
1487 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1489 uint32_t tickstart = 0;
1491 /* Check if the Initialization mode is set */
1492 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1494 /* Set the Initialization mode */
1495 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1497 tickstart = HAL_GetTick();
1498 /* Wait till RTC is in INIT state and if Time out is reached exit */
1499 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1501 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1513 * @brief Converts a 2 digit decimal to BCD format.
1514 * @param Value: Byte to be converted
1515 * @retval Converted byte
1517 uint8_t RTC_ByteToBcd2(uint8_t Value)
1519 uint32_t bcdhigh = 0;
1527 return ((uint8_t)(bcdhigh << 4) | Value);
1531 * @brief Converts from 2 digit BCD to Binary.
1532 * @param Value: BCD value to be converted
1533 * @retval Converted word
1535 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1538 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1539 return (tmp + (Value & (uint8_t)0x0F));
1546 #endif /* HAL_RTC_MODULE_ENABLED */
1556 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/