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 __HAL_RCC_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 wake-up, 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-wake-up mode), by using the RTC alarm
101 or the RTC wake-up events.
102 [..] The RTC provides a programmable time base for waking up from the
103 Stop or Standby mode at regular intervals.
104 Wake-up from STOP and STANDBY modes is possible only when the RTC clock source
108 ******************************************************************************
111 * <h2><center>© COPYRIGHT(c) 2017 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
145 /** @defgroup RTC RTC
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_Exported_Functions RTC Exported Functions
163 /** @defgroup RTC_Exported_Functions_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 wake-up 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 /* Allocate lock resource and initialize it */
222 hrtc
->Lock
= HAL_UNLOCKED
;
223 /* Initialize RTC MSP */
224 HAL_RTC_MspInit(hrtc
);
228 hrtc
->State
= HAL_RTC_STATE_BUSY
;
230 /* Disable the write protection for RTC registers */
231 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
233 /* Set Initialization mode */
234 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
236 /* Enable the write protection for RTC registers */
237 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
240 hrtc
->State
= HAL_RTC_STATE_ERROR
;
246 /* Clear RTC_CR FMT, OSEL and POL Bits */
247 hrtc
->Instance
->CR
&= ((uint32_t)~(RTC_CR_FMT
| RTC_CR_OSEL
| RTC_CR_POL
));
248 /* Set RTC_CR register */
249 hrtc
->Instance
->CR
|= (uint32_t)(hrtc
->Init
.HourFormat
| hrtc
->Init
.OutPut
| hrtc
->Init
.OutPutPolarity
);
251 /* Configure the RTC PRER */
252 hrtc
->Instance
->PRER
= (uint32_t)(hrtc
->Init
.SynchPrediv
);
253 hrtc
->Instance
->PRER
|= (uint32_t)(hrtc
->Init
.AsynchPrediv
<< 16U);
255 /* Exit Initialization mode */
256 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
258 hrtc
->Instance
->TAFCR
&= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE
;
259 hrtc
->Instance
->TAFCR
|= (uint32_t)(hrtc
->Init
.OutPutType
);
261 /* Enable the write protection for RTC registers */
262 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
265 hrtc
->State
= HAL_RTC_STATE_READY
;
272 * @brief DeInitializes the RTC peripheral
273 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
274 * the configuration information for RTC.
275 * @note This function doesn't reset the RTC Backup Data registers.
278 HAL_StatusTypeDef
HAL_RTC_DeInit(RTC_HandleTypeDef
*hrtc
)
280 uint32_t tickstart
= 0U;
283 hrtc
->State
= HAL_RTC_STATE_BUSY
;
285 /* Disable the write protection for RTC registers */
286 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
288 /* Set Initialization mode */
289 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
291 /* Enable the write protection for RTC registers */
292 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
295 hrtc
->State
= HAL_RTC_STATE_ERROR
;
301 /* Reset TR, DR and CR registers */
302 hrtc
->Instance
->TR
= 0x00000000U
;
303 hrtc
->Instance
->DR
= 0x00002101U
;
304 /* Reset All CR bits except CR[2:0] */
305 hrtc
->Instance
->CR
&= 0x00000007U
;
308 tickstart
= HAL_GetTick();
310 /* Wait till WUTWF flag is set and if Time out is reached exit */
311 while(((hrtc
->Instance
->ISR
) & RTC_ISR_WUTWF
) == (uint32_t)RESET
)
313 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
315 /* Enable the write protection for RTC registers */
316 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
319 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
325 /* Reset all RTC CR register bits */
326 hrtc
->Instance
->CR
&= 0x00000000U
;
327 hrtc
->Instance
->WUTR
= 0x0000FFFFU
;
328 hrtc
->Instance
->PRER
= 0x007F00FFU
;
329 hrtc
->Instance
->CALIBR
= 0x00000000U
;
330 hrtc
->Instance
->ALRMAR
= 0x00000000U
;
331 hrtc
->Instance
->ALRMBR
= 0x00000000U
;
332 hrtc
->Instance
->SHIFTR
= 0x00000000U
;
333 hrtc
->Instance
->CALR
= 0x00000000U
;
334 hrtc
->Instance
->ALRMASSR
= 0x00000000U
;
335 hrtc
->Instance
->ALRMBSSR
= 0x00000000U
;
337 /* Reset ISR register and exit initialization mode */
338 hrtc
->Instance
->ISR
= 0x00000000U
;
340 /* Reset Tamper and alternate functions configuration register */
341 hrtc
->Instance
->TAFCR
= 0x00000000U
;
343 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
344 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
346 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
348 /* Enable the write protection for RTC registers */
349 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
351 hrtc
->State
= HAL_RTC_STATE_ERROR
;
358 /* Enable the write protection for RTC registers */
359 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
361 /* De-Initialize RTC MSP */
362 HAL_RTC_MspDeInit(hrtc
);
364 hrtc
->State
= HAL_RTC_STATE_RESET
;
373 * @brief Initializes the RTC MSP.
374 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
375 * the configuration information for RTC.
378 __weak
void HAL_RTC_MspInit(RTC_HandleTypeDef
* hrtc
)
380 /* Prevent unused argument(s) compilation warning */
382 /* NOTE : This function Should not be modified, when the callback is needed,
383 the HAL_RTC_MspInit could be implemented in the user file
388 * @brief DeInitializes the RTC MSP.
389 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
390 * the configuration information for RTC.
393 __weak
void HAL_RTC_MspDeInit(RTC_HandleTypeDef
* hrtc
)
395 /* Prevent unused argument(s) compilation warning */
397 /* NOTE : This function Should not be modified, when the callback is needed,
398 the HAL_RTC_MspDeInit could be implemented in the user file
406 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
407 * @brief RTC Time and Date functions
410 ===============================================================================
411 ##### RTC Time and Date functions #####
412 ===============================================================================
414 [..] This section provides functions allowing to configure Time and Date features
421 * @brief Sets RTC current time.
422 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
423 * the configuration information for RTC.
424 * @param sTime: Pointer to Time structure
425 * @param Format: Specifies the format of the entered parameters.
426 * This parameter can be one of the following values:
427 * @arg RTC_FORMAT_BIN: Binary data format
428 * @arg RTC_FORMAT_BCD: BCD data format
431 HAL_StatusTypeDef
HAL_RTC_SetTime(RTC_HandleTypeDef
*hrtc
, RTC_TimeTypeDef
*sTime
, uint32_t Format
)
433 uint32_t tmpreg
= 0U;
435 /* Check the parameters */
436 assert_param(IS_RTC_FORMAT(Format
));
437 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime
->DayLightSaving
));
438 assert_param(IS_RTC_STORE_OPERATION(sTime
->StoreOperation
));
443 hrtc
->State
= HAL_RTC_STATE_BUSY
;
445 if(Format
== RTC_FORMAT_BIN
)
447 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
449 assert_param(IS_RTC_HOUR12(sTime
->Hours
));
450 assert_param(IS_RTC_HOURFORMAT12(sTime
->TimeFormat
));
454 sTime
->TimeFormat
= 0x00U
;
455 assert_param(IS_RTC_HOUR24(sTime
->Hours
));
457 assert_param(IS_RTC_MINUTES(sTime
->Minutes
));
458 assert_param(IS_RTC_SECONDS(sTime
->Seconds
));
460 tmpreg
= (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime
->Hours
) << 16U) | \
461 ((uint32_t)RTC_ByteToBcd2(sTime
->Minutes
) << 8U) | \
462 ((uint32_t)RTC_ByteToBcd2(sTime
->Seconds
)) | \
463 (((uint32_t)sTime
->TimeFormat
) << 16U));
467 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
469 tmpreg
= RTC_Bcd2ToByte(sTime
->Hours
);
470 assert_param(IS_RTC_HOUR12(tmpreg
));
471 assert_param(IS_RTC_HOURFORMAT12(sTime
->TimeFormat
));
475 sTime
->TimeFormat
= 0x00U
;
476 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime
->Hours
)));
478 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime
->Minutes
)));
479 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime
->Seconds
)));
480 tmpreg
= (((uint32_t)(sTime
->Hours
) << 16U) | \
481 ((uint32_t)(sTime
->Minutes
) << 8U) | \
482 ((uint32_t)sTime
->Seconds
) | \
483 ((uint32_t)(sTime
->TimeFormat
) << 16U));
486 /* Disable the write protection for RTC registers */
487 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
489 /* Set Initialization mode */
490 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
492 /* Enable the write protection for RTC registers */
493 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
496 hrtc
->State
= HAL_RTC_STATE_ERROR
;
498 /* Process Unlocked */
505 /* Set the RTC_TR register */
506 hrtc
->Instance
->TR
= (uint32_t)(tmpreg
& RTC_TR_RESERVED_MASK
);
508 /* Clear the bits to be configured */
509 hrtc
->Instance
->CR
&= (uint32_t)~RTC_CR_BCK
;
511 /* Configure the RTC_CR register */
512 hrtc
->Instance
->CR
|= (uint32_t)(sTime
->DayLightSaving
| sTime
->StoreOperation
);
514 /* Exit Initialization mode */
515 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
517 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
518 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
520 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
522 /* Enable the write protection for RTC registers */
523 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
525 hrtc
->State
= HAL_RTC_STATE_ERROR
;
527 /* Process Unlocked */
534 /* Enable the write protection for RTC registers */
535 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
537 hrtc
->State
= HAL_RTC_STATE_READY
;
546 * @brief Gets RTC current time.
547 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
548 * the configuration information for RTC.
549 * @param sTime: Pointer to Time structure
550 * @param Format: Specifies the format of the entered parameters.
551 * This parameter can be one of the following values:
552 * @arg RTC_FORMAT_BIN: Binary data format
553 * @arg RTC_FORMAT_BCD: BCD data format
554 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
555 * value in second fraction ratio with time unit following generic formula:
556 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
557 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
558 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
559 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
560 * Reading RTC current time locks the values in calendar shadow registers until current date is read.
563 HAL_StatusTypeDef
HAL_RTC_GetTime(RTC_HandleTypeDef
*hrtc
, RTC_TimeTypeDef
*sTime
, uint32_t Format
)
565 uint32_t tmpreg
= 0U;
567 /* Check the parameters */
568 assert_param(IS_RTC_FORMAT(Format
));
570 /* Get subseconds structure field from the corresponding register */
571 sTime
->SubSeconds
= (uint32_t)(hrtc
->Instance
->SSR
);
573 /* Get SecondFraction structure field from the corresponding register field*/
574 sTime
->SecondFraction
= (uint32_t)(hrtc
->Instance
->PRER
& RTC_PRER_PREDIV_S
);
576 /* Get the TR register */
577 tmpreg
= (uint32_t)(hrtc
->Instance
->TR
& RTC_TR_RESERVED_MASK
);
579 /* Fill the structure fields with the read parameters */
580 sTime
->Hours
= (uint8_t)((tmpreg
& (RTC_TR_HT
| RTC_TR_HU
)) >> 16U);
581 sTime
->Minutes
= (uint8_t)((tmpreg
& (RTC_TR_MNT
| RTC_TR_MNU
)) >> 8U);
582 sTime
->Seconds
= (uint8_t)(tmpreg
& (RTC_TR_ST
| RTC_TR_SU
));
583 sTime
->TimeFormat
= (uint8_t)((tmpreg
& (RTC_TR_PM
)) >> 16U);
585 /* Check the input parameters format */
586 if(Format
== RTC_FORMAT_BIN
)
588 /* Convert the time structure parameters to Binary format */
589 sTime
->Hours
= (uint8_t)RTC_Bcd2ToByte(sTime
->Hours
);
590 sTime
->Minutes
= (uint8_t)RTC_Bcd2ToByte(sTime
->Minutes
);
591 sTime
->Seconds
= (uint8_t)RTC_Bcd2ToByte(sTime
->Seconds
);
598 * @brief Sets RTC current date.
599 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
600 * the configuration information for RTC.
601 * @param sDate: Pointer to date structure
602 * @param Format: specifies the format of the entered parameters.
603 * This parameter can be one of the following values:
604 * @arg RTC_FORMAT_BIN: Binary data format
605 * @arg RTC_FORMAT_BCD: BCD data format
608 HAL_StatusTypeDef
HAL_RTC_SetDate(RTC_HandleTypeDef
*hrtc
, RTC_DateTypeDef
*sDate
, uint32_t Format
)
610 uint32_t datetmpreg
= 0U;
612 /* Check the parameters */
613 assert_param(IS_RTC_FORMAT(Format
));
618 hrtc
->State
= HAL_RTC_STATE_BUSY
;
620 if((Format
== RTC_FORMAT_BIN
) && ((sDate
->Month
& 0x10U
) == 0x10U
))
622 sDate
->Month
= (uint8_t)((sDate
->Month
& (uint8_t)~(0x10U
)) + (uint8_t)0x0AU
);
625 assert_param(IS_RTC_WEEKDAY(sDate
->WeekDay
));
627 if(Format
== RTC_FORMAT_BIN
)
629 assert_param(IS_RTC_YEAR(sDate
->Year
));
630 assert_param(IS_RTC_MONTH(sDate
->Month
));
631 assert_param(IS_RTC_DATE(sDate
->Date
));
633 datetmpreg
= (((uint32_t)RTC_ByteToBcd2(sDate
->Year
) << 16U) | \
634 ((uint32_t)RTC_ByteToBcd2(sDate
->Month
) << 8U) | \
635 ((uint32_t)RTC_ByteToBcd2(sDate
->Date
)) | \
636 ((uint32_t)sDate
->WeekDay
<< 13U));
640 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate
->Year
)));
641 datetmpreg
= RTC_Bcd2ToByte(sDate
->Month
);
642 assert_param(IS_RTC_MONTH(datetmpreg
));
643 datetmpreg
= RTC_Bcd2ToByte(sDate
->Date
);
644 assert_param(IS_RTC_DATE(datetmpreg
));
646 datetmpreg
= ((((uint32_t)sDate
->Year
) << 16U) | \
647 (((uint32_t)sDate
->Month
) << 8U) | \
648 ((uint32_t)sDate
->Date
) | \
649 (((uint32_t)sDate
->WeekDay
) << 13U));
652 /* Disable the write protection for RTC registers */
653 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
655 /* Set Initialization mode */
656 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
658 /* Enable the write protection for RTC registers */
659 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
662 hrtc
->State
= HAL_RTC_STATE_ERROR
;
664 /* Process Unlocked */
671 /* Set the RTC_DR register */
672 hrtc
->Instance
->DR
= (uint32_t)(datetmpreg
& RTC_DR_RESERVED_MASK
);
674 /* Exit Initialization mode */
675 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
677 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
678 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
680 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
682 /* Enable the write protection for RTC registers */
683 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
685 hrtc
->State
= HAL_RTC_STATE_ERROR
;
687 /* Process Unlocked */
694 /* Enable the write protection for RTC registers */
695 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
697 hrtc
->State
= HAL_RTC_STATE_READY
;
699 /* Process Unlocked */
707 * @brief Gets RTC current date.
708 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
709 * the configuration information for RTC.
710 * @param sDate: Pointer to Date structure
711 * @param Format: Specifies the format of the entered parameters.
712 * This parameter can be one of the following values:
713 * @arg RTC_FORMAT_BIN: Binary data format
714 * @arg RTC_FORMAT_BCD: BCD data format
715 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
716 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
717 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
720 HAL_StatusTypeDef
HAL_RTC_GetDate(RTC_HandleTypeDef
*hrtc
, RTC_DateTypeDef
*sDate
, uint32_t Format
)
722 uint32_t datetmpreg
= 0U;
724 /* Check the parameters */
725 assert_param(IS_RTC_FORMAT(Format
));
727 /* Get the DR register */
728 datetmpreg
= (uint32_t)(hrtc
->Instance
->DR
& RTC_DR_RESERVED_MASK
);
730 /* Fill the structure fields with the read parameters */
731 sDate
->Year
= (uint8_t)((datetmpreg
& (RTC_DR_YT
| RTC_DR_YU
)) >> 16U);
732 sDate
->Month
= (uint8_t)((datetmpreg
& (RTC_DR_MT
| RTC_DR_MU
)) >> 8U);
733 sDate
->Date
= (uint8_t)(datetmpreg
& (RTC_DR_DT
| RTC_DR_DU
));
734 sDate
->WeekDay
= (uint8_t)((datetmpreg
& (RTC_DR_WDU
)) >> 13U);
736 /* Check the input parameters format */
737 if(Format
== RTC_FORMAT_BIN
)
739 /* Convert the date structure parameters to Binary format */
740 sDate
->Year
= (uint8_t)RTC_Bcd2ToByte(sDate
->Year
);
741 sDate
->Month
= (uint8_t)RTC_Bcd2ToByte(sDate
->Month
);
742 sDate
->Date
= (uint8_t)RTC_Bcd2ToByte(sDate
->Date
);
751 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
752 * @brief RTC Alarm functions
755 ===============================================================================
756 ##### RTC Alarm functions #####
757 ===============================================================================
759 [..] This section provides functions allowing to configure Alarm feature
765 * @brief Sets the specified RTC Alarm.
766 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
767 * the configuration information for RTC.
768 * @param sAlarm: Pointer to Alarm structure
769 * @param Format: Specifies the format of the entered parameters.
770 * This parameter can be one of the following values:
771 * @arg RTC_FORMAT_BIN: Binary data format
772 * @arg RTC_FORMAT_BCD: BCD data format
775 HAL_StatusTypeDef
HAL_RTC_SetAlarm(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Format
)
777 uint32_t tickstart
= 0U;
778 uint32_t tmpreg
= 0U, subsecondtmpreg
= 0U;
780 /* Check the parameters */
781 assert_param(IS_RTC_FORMAT(Format
));
782 assert_param(IS_RTC_ALARM(sAlarm
->Alarm
));
783 assert_param(IS_RTC_ALARM_MASK(sAlarm
->AlarmMask
));
784 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm
->AlarmDateWeekDaySel
));
785 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm
->AlarmTime
.SubSeconds
));
786 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm
->AlarmSubSecondMask
));
791 hrtc
->State
= HAL_RTC_STATE_BUSY
;
793 if(Format
== RTC_FORMAT_BIN
)
795 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
797 assert_param(IS_RTC_HOUR12(sAlarm
->AlarmTime
.Hours
));
798 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
802 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
803 assert_param(IS_RTC_HOUR24(sAlarm
->AlarmTime
.Hours
));
805 assert_param(IS_RTC_MINUTES(sAlarm
->AlarmTime
.Minutes
));
806 assert_param(IS_RTC_SECONDS(sAlarm
->AlarmTime
.Seconds
));
808 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
810 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm
->AlarmDateWeekDay
));
814 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm
->AlarmDateWeekDay
));
817 tmpreg
= (((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Hours
) << 16U) | \
818 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
819 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Seconds
)) | \
820 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
821 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmDateWeekDay
) << 24U) | \
822 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
823 ((uint32_t)sAlarm
->AlarmMask
));
827 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
829 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
830 assert_param(IS_RTC_HOUR12(tmpreg
));
831 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
835 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
836 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
)));
839 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
)));
840 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
)));
842 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
844 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
845 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg
));
849 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
850 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg
));
853 tmpreg
= (((uint32_t)(sAlarm
->AlarmTime
.Hours
) << 16U) | \
854 ((uint32_t)(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
855 ((uint32_t) sAlarm
->AlarmTime
.Seconds
) | \
856 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
857 ((uint32_t)(sAlarm
->AlarmDateWeekDay
) << 24U) | \
858 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
859 ((uint32_t)sAlarm
->AlarmMask
));
862 /* Configure the Alarm A or Alarm B Sub Second registers */
863 subsecondtmpreg
= (uint32_t)((uint32_t)(sAlarm
->AlarmTime
.SubSeconds
) | (uint32_t)(sAlarm
->AlarmSubSecondMask
));
865 /* Disable the write protection for RTC registers */
866 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
868 /* Configure the Alarm register */
869 if(sAlarm
->Alarm
== RTC_ALARM_A
)
871 /* Disable the Alarm A interrupt */
872 __HAL_RTC_ALARMA_DISABLE(hrtc
);
874 /* In case of interrupt mode is used, the interrupt source must disabled */
875 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRA
);
878 tickstart
= HAL_GetTick();
880 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
881 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
883 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
885 /* Enable the write protection for RTC registers */
886 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
888 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
890 /* Process Unlocked */
897 hrtc
->Instance
->ALRMAR
= (uint32_t)tmpreg
;
898 /* Configure the Alarm A Sub Second register */
899 hrtc
->Instance
->ALRMASSR
= subsecondtmpreg
;
900 /* Configure the Alarm state: Enable Alarm */
901 __HAL_RTC_ALARMA_ENABLE(hrtc
);
905 /* Disable the Alarm B interrupt */
906 __HAL_RTC_ALARMB_DISABLE(hrtc
);
908 /* In case of interrupt mode is used, the interrupt source must disabled */
909 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRB
);
912 tickstart
= HAL_GetTick();
914 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
915 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
917 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
919 /* Enable the write protection for RTC registers */
920 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
922 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
924 /* Process Unlocked */
931 hrtc
->Instance
->ALRMBR
= (uint32_t)tmpreg
;
932 /* Configure the Alarm B Sub Second register */
933 hrtc
->Instance
->ALRMBSSR
= subsecondtmpreg
;
934 /* Configure the Alarm state: Enable Alarm */
935 __HAL_RTC_ALARMB_ENABLE(hrtc
);
938 /* Enable the write protection for RTC registers */
939 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
941 /* Change RTC state */
942 hrtc
->State
= HAL_RTC_STATE_READY
;
944 /* Process Unlocked */
951 * @brief Sets the specified RTC Alarm with Interrupt
952 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
953 * the configuration information for RTC.
954 * @param sAlarm: Pointer to Alarm structure
955 * @param Format: Specifies the format of the entered parameters.
956 * This parameter can be one of the following values:
957 * @arg RTC_FORMAT_BIN: Binary data format
958 * @arg RTC_FORMAT_BCD: BCD data format
961 HAL_StatusTypeDef
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Format
)
963 uint32_t tmpreg
= 0U, subsecondtmpreg
= 0U;
964 __IO
uint32_t count
= RTC_TIMEOUT_VALUE
* (SystemCoreClock
/ 32U / 1000U) ;
966 /* Check the parameters */
967 assert_param(IS_RTC_FORMAT(Format
));
968 assert_param(IS_RTC_ALARM(sAlarm
->Alarm
));
969 assert_param(IS_RTC_ALARM_MASK(sAlarm
->AlarmMask
));
970 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm
->AlarmDateWeekDaySel
));
971 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm
->AlarmTime
.SubSeconds
));
972 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm
->AlarmSubSecondMask
));
977 hrtc
->State
= HAL_RTC_STATE_BUSY
;
979 if(Format
== RTC_FORMAT_BIN
)
981 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
983 assert_param(IS_RTC_HOUR12(sAlarm
->AlarmTime
.Hours
));
984 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
988 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
989 assert_param(IS_RTC_HOUR24(sAlarm
->AlarmTime
.Hours
));
991 assert_param(IS_RTC_MINUTES(sAlarm
->AlarmTime
.Minutes
));
992 assert_param(IS_RTC_SECONDS(sAlarm
->AlarmTime
.Seconds
));
994 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
996 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm
->AlarmDateWeekDay
));
1000 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm
->AlarmDateWeekDay
));
1002 tmpreg
= (((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Hours
) << 16U) | \
1003 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
1004 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Seconds
)) | \
1005 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
1006 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmDateWeekDay
) << 24U) | \
1007 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
1008 ((uint32_t)sAlarm
->AlarmMask
));
1012 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
1014 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
1015 assert_param(IS_RTC_HOUR12(tmpreg
));
1016 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
1020 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
1021 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
)));
1024 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
)));
1025 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
)));
1027 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
1029 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1030 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg
));
1034 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1035 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg
));
1037 tmpreg
= (((uint32_t)(sAlarm
->AlarmTime
.Hours
) << 16U) | \
1038 ((uint32_t)(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
1039 ((uint32_t) sAlarm
->AlarmTime
.Seconds
) | \
1040 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
1041 ((uint32_t)(sAlarm
->AlarmDateWeekDay
) << 24U) | \
1042 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
1043 ((uint32_t)sAlarm
->AlarmMask
));
1045 /* Configure the Alarm A or Alarm B Sub Second registers */
1046 subsecondtmpreg
= (uint32_t)((uint32_t)(sAlarm
->AlarmTime
.SubSeconds
) | (uint32_t)(sAlarm
->AlarmSubSecondMask
));
1048 /* Disable the write protection for RTC registers */
1049 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
1051 /* Configure the Alarm register */
1052 if(sAlarm
->Alarm
== RTC_ALARM_A
)
1054 /* Disable the Alarm A interrupt */
1055 __HAL_RTC_ALARMA_DISABLE(hrtc
);
1057 /* Clear flag alarm A */
1058 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRAF
);
1060 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1065 /* Enable the write protection for RTC registers */
1066 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1068 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1070 /* Process Unlocked */
1076 while (__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
);
1078 hrtc
->Instance
->ALRMAR
= (uint32_t)tmpreg
;
1079 /* Configure the Alarm A Sub Second register */
1080 hrtc
->Instance
->ALRMASSR
= subsecondtmpreg
;
1081 /* Configure the Alarm state: Enable Alarm */
1082 __HAL_RTC_ALARMA_ENABLE(hrtc
);
1083 /* Configure the Alarm interrupt */
1084 __HAL_RTC_ALARM_ENABLE_IT(hrtc
,RTC_IT_ALRA
);
1088 /* Disable the Alarm B interrupt */
1089 __HAL_RTC_ALARMB_DISABLE(hrtc
);
1091 /* Clear flag alarm B */
1092 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRBF
);
1094 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1099 /* Enable the write protection for RTC registers */
1100 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1102 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1104 /* Process Unlocked */
1110 while (__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
);
1112 hrtc
->Instance
->ALRMBR
= (uint32_t)tmpreg
;
1113 /* Configure the Alarm B Sub Second register */
1114 hrtc
->Instance
->ALRMBSSR
= subsecondtmpreg
;
1115 /* Configure the Alarm state: Enable Alarm */
1116 __HAL_RTC_ALARMB_ENABLE(hrtc
);
1117 /* Configure the Alarm interrupt */
1118 __HAL_RTC_ALARM_ENABLE_IT(hrtc
, RTC_IT_ALRB
);
1121 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1122 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1124 EXTI
->RTSR
|= RTC_EXTI_LINE_ALARM_EVENT
;
1126 /* Enable the write protection for RTC registers */
1127 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1129 hrtc
->State
= HAL_RTC_STATE_READY
;
1131 /* Process Unlocked */
1138 * @brief Deactivate the specified RTC Alarm
1139 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1140 * the configuration information for RTC.
1141 * @param Alarm: Specifies the Alarm.
1142 * This parameter can be one of the following values:
1143 * @arg RTC_ALARM_A: AlarmA
1144 * @arg RTC_ALARM_B: AlarmB
1145 * @retval HAL status
1147 HAL_StatusTypeDef
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef
*hrtc
, uint32_t Alarm
)
1149 uint32_t tickstart
= 0U;
1151 /* Check the parameters */
1152 assert_param(IS_RTC_ALARM(Alarm
));
1154 /* Process Locked */
1157 hrtc
->State
= HAL_RTC_STATE_BUSY
;
1159 /* Disable the write protection for RTC registers */
1160 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
1162 if(Alarm
== RTC_ALARM_A
)
1165 __HAL_RTC_ALARMA_DISABLE(hrtc
);
1167 /* In case of interrupt mode is used, the interrupt source must disabled */
1168 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRA
);
1171 tickstart
= HAL_GetTick();
1173 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1174 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
1176 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1178 /* Enable the write protection for RTC registers */
1179 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1181 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1183 /* Process Unlocked */
1193 __HAL_RTC_ALARMB_DISABLE(hrtc
);
1195 /* In case of interrupt mode is used, the interrupt source must disabled */
1196 __HAL_RTC_ALARM_DISABLE_IT(hrtc
,RTC_IT_ALRB
);
1199 tickstart
= HAL_GetTick();
1201 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1202 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
1204 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1206 /* Enable the write protection for RTC registers */
1207 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1209 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1211 /* Process Unlocked */
1218 /* Enable the write protection for RTC registers */
1219 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1221 hrtc
->State
= HAL_RTC_STATE_READY
;
1223 /* Process Unlocked */
1230 * @brief Gets the RTC Alarm value and masks.
1231 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1232 * the configuration information for RTC.
1233 * @param sAlarm: Pointer to Date structure
1234 * @param Alarm: Specifies the Alarm.
1235 * This parameter can be one of the following values:
1236 * @arg RTC_ALARM_A: AlarmA
1237 * @arg RTC_ALARM_B: AlarmB
1238 * @param Format: Specifies the format of the entered parameters.
1239 * This parameter can be one of the following values:
1240 * @arg RTC_FORMAT_BIN: Binary data format
1241 * @arg RTC_FORMAT_BCD: BCD data format
1242 * @retval HAL status
1244 HAL_StatusTypeDef
HAL_RTC_GetAlarm(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Alarm
, uint32_t Format
)
1246 uint32_t tmpreg
= 0U, subsecondtmpreg
= 0U;
1248 /* Check the parameters */
1249 assert_param(IS_RTC_FORMAT(Format
));
1250 assert_param(IS_RTC_ALARM(Alarm
));
1252 if(Alarm
== RTC_ALARM_A
)
1255 sAlarm
->Alarm
= RTC_ALARM_A
;
1257 tmpreg
= (uint32_t)(hrtc
->Instance
->ALRMAR
);
1258 subsecondtmpreg
= (uint32_t)((hrtc
->Instance
->ALRMASSR
) & RTC_ALRMASSR_SS
);
1262 sAlarm
->Alarm
= RTC_ALARM_B
;
1264 tmpreg
= (uint32_t)(hrtc
->Instance
->ALRMBR
);
1265 subsecondtmpreg
= (uint32_t)((hrtc
->Instance
->ALRMBSSR
) & RTC_ALRMBSSR_SS
);
1268 /* Fill the structure with the read parameters */
1269 sAlarm
->AlarmTime
.Hours
= (uint32_t)((tmpreg
& (RTC_ALRMAR_HT
| RTC_ALRMAR_HU
)) >> 16U);
1270 sAlarm
->AlarmTime
.Minutes
= (uint32_t)((tmpreg
& (RTC_ALRMAR_MNT
| RTC_ALRMAR_MNU
)) >> 8U);
1271 sAlarm
->AlarmTime
.Seconds
= (uint32_t)(tmpreg
& (RTC_ALRMAR_ST
| RTC_ALRMAR_SU
));
1272 sAlarm
->AlarmTime
.TimeFormat
= (uint32_t)((tmpreg
& RTC_ALRMAR_PM
) >> 16U);
1273 sAlarm
->AlarmTime
.SubSeconds
= (uint32_t) subsecondtmpreg
;
1274 sAlarm
->AlarmDateWeekDay
= (uint32_t)((tmpreg
& (RTC_ALRMAR_DT
| RTC_ALRMAR_DU
)) >> 24U);
1275 sAlarm
->AlarmDateWeekDaySel
= (uint32_t)(tmpreg
& RTC_ALRMAR_WDSEL
);
1276 sAlarm
->AlarmMask
= (uint32_t)(tmpreg
& RTC_ALARMMASK_ALL
);
1278 if(Format
== RTC_FORMAT_BIN
)
1280 sAlarm
->AlarmTime
.Hours
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
1281 sAlarm
->AlarmTime
.Minutes
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
);
1282 sAlarm
->AlarmTime
.Seconds
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
);
1283 sAlarm
->AlarmDateWeekDay
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1290 * @brief This function handles Alarm interrupt request.
1291 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1292 * the configuration information for RTC.
1295 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef
* hrtc
)
1297 if(__HAL_RTC_ALARM_GET_IT(hrtc
, RTC_IT_ALRA
))
1299 /* Get the status of the Interrupt */
1300 if((uint32_t)(hrtc
->Instance
->CR
& RTC_IT_ALRA
) != (uint32_t)RESET
)
1302 /* AlarmA callback */
1303 HAL_RTC_AlarmAEventCallback(hrtc
);
1305 /* Clear the Alarm interrupt pending bit */
1306 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
,RTC_FLAG_ALRAF
);
1310 if(__HAL_RTC_ALARM_GET_IT(hrtc
, RTC_IT_ALRB
))
1312 /* Get the status of the Interrupt */
1313 if((uint32_t)(hrtc
->Instance
->CR
& RTC_IT_ALRB
) != (uint32_t)RESET
)
1315 /* AlarmB callback */
1316 HAL_RTCEx_AlarmBEventCallback(hrtc
);
1318 /* Clear the Alarm interrupt pending bit */
1319 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
,RTC_FLAG_ALRBF
);
1323 /* Clear the EXTI's line Flag for RTC Alarm */
1324 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1326 /* Change RTC state */
1327 hrtc
->State
= HAL_RTC_STATE_READY
;
1331 * @brief Alarm A callback.
1332 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1333 * the configuration information for RTC.
1336 __weak
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef
*hrtc
)
1338 /* Prevent unused argument(s) compilation warning */
1340 /* NOTE : This function Should not be modified, when the callback is needed,
1341 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1346 * @brief This function handles AlarmA Polling request.
1347 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1348 * the configuration information for RTC.
1349 * @param Timeout: Timeout duration
1350 * @retval HAL status
1352 HAL_StatusTypeDef
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef
*hrtc
, uint32_t Timeout
)
1354 uint32_t tickstart
= 0U;
1357 tickstart
= HAL_GetTick();
1359 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAF
) == RESET
)
1361 if(Timeout
!= HAL_MAX_DELAY
)
1363 if((Timeout
== 0U)||((HAL_GetTick() - tickstart
) > Timeout
))
1365 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1371 /* Clear the Alarm interrupt pending bit */
1372 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRAF
);
1374 /* Change RTC state */
1375 hrtc
->State
= HAL_RTC_STATE_READY
;
1384 /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
1385 * @brief Peripheral Control functions
1388 ===============================================================================
1389 ##### Peripheral Control functions #####
1390 ===============================================================================
1392 This subsection provides functions allowing to
1393 (+) Wait for RTC Time and Date Synchronization
1400 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1401 * synchronized with RTC APB clock.
1402 * @note The RTC Resynchronization mode is write protected, use the
1403 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1404 * @note To read the calendar through the shadow registers after Calendar
1405 * initialization, calendar update or after wake-up from low power modes
1406 * the software must first clear the RSF flag.
1407 * The software must then wait until it is set again before reading
1408 * the calendar, which means that the calendar registers have been
1409 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1410 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1411 * the configuration information for RTC.
1412 * @retval HAL status
1414 HAL_StatusTypeDef
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef
* hrtc
)
1416 uint32_t tickstart
= 0U;
1418 /* Clear RSF flag */
1419 hrtc
->Instance
->ISR
&= (uint32_t)RTC_RSF_MASK
;
1422 tickstart
= HAL_GetTick();
1424 /* Wait the registers to be synchronised */
1425 while((hrtc
->Instance
->ISR
& RTC_ISR_RSF
) == (uint32_t)RESET
)
1427 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1440 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
1441 * @brief Peripheral State functions
1444 ===============================================================================
1445 ##### Peripheral State functions #####
1446 ===============================================================================
1448 This subsection provides functions allowing to
1455 * @brief Returns the RTC state.
1456 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1457 * the configuration information for RTC.
1460 HAL_RTCStateTypeDef
HAL_RTC_GetState(RTC_HandleTypeDef
* hrtc
)
1470 * @brief Enters the RTC Initialization mode.
1471 * @note The RTC Initialization mode is write protected, use the
1472 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1473 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1474 * the configuration information for RTC.
1475 * @retval HAL status
1477 HAL_StatusTypeDef
RTC_EnterInitMode(RTC_HandleTypeDef
* hrtc
)
1479 uint32_t tickstart
= 0U;
1481 /* Check if the Initialization mode is set */
1482 if((hrtc
->Instance
->ISR
& RTC_ISR_INITF
) == (uint32_t)RESET
)
1484 /* Set the Initialization mode */
1485 hrtc
->Instance
->ISR
= (uint32_t)RTC_INIT_MASK
;
1488 tickstart
= HAL_GetTick();
1490 /* Wait till RTC is in INIT state and if Time out is reached exit */
1491 while((hrtc
->Instance
->ISR
& RTC_ISR_INITF
) == (uint32_t)RESET
)
1493 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1505 * @brief Converts a 2 digit decimal to BCD format.
1506 * @param Value: Byte to be converted
1507 * @retval Converted byte
1509 uint8_t RTC_ByteToBcd2(uint8_t Value
)
1511 uint32_t bcdhigh
= 0U;
1519 return ((uint8_t)(bcdhigh
<< 4U) | Value
);
1523 * @brief Converts from 2 digit BCD to Binary.
1524 * @param Value: BCD value to be converted
1525 * @retval Converted word
1527 uint8_t RTC_Bcd2ToByte(uint8_t Value
)
1530 tmp
= ((uint8_t)(Value
& (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1531 return (tmp
+ (Value
& (uint8_t)0x0F));
1538 #endif /* HAL_RTC_MODULE_ENABLED */
1547 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/