2 ******************************************************************************
3 * @file stm32f7xx_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
45 (#) PC1 can be used as the RTC_AF3 pin
47 ##### Backup Domain Reset #####
48 ==================================================================
49 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
50 to their reset values. The BKPSRAM is not affected by this reset. The only
51 way to reset the BKPSRAM is through the Flash interface by requesting
52 a protection level change from 1 to 0.
53 [..] A backup domain reset is generated when one of the following events occurs:
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 powered off.
58 ##### Backup Domain Access #####
59 ==================================================================
60 [..] After reset, the backup domain (RTC registers, RTC backup data
61 registers and backup SRAM) is protected against possible unwanted write
63 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
64 (+) Enable the Power Controller (PWR) APB1 interface clock using the
65 __HAL_RCC_PWR_CLK_ENABLE() function.
66 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
67 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
68 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
71 ##### How to use this driver #####
72 ==================================================================
74 (+) Enable the RTC domain access (see description in the section above).
75 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
76 format using the HAL_RTC_Init() function.
78 *** Time and Date configuration ***
79 ===================================
81 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
82 and HAL_RTC_SetDate() functions.
83 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
85 *** Alarm configuration ***
86 ===========================
88 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
89 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
90 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
92 ##### RTC and low power modes #####
93 ==================================================================
94 [..] The MCU can be woken up from a low power mode by an RTC alternate
96 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
97 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
98 These RTC alternate functions can wake up the system from the Stop and
99 Standby low power modes.
100 [..] The system can also wake up from low power modes without depending
101 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
102 or the RTC wakeup events.
103 [..] The RTC provides a programmable time base for waking up from the
104 Stop or Standby mode at regular intervals.
105 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
109 ******************************************************************************
112 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
114 * Redistribution and use in source and binary forms, with or without modification,
115 * are permitted provided that the following conditions are met:
116 * 1. Redistributions of source code must retain the above copyright notice,
117 * this list of conditions and the following disclaimer.
118 * 2. Redistributions in binary form must reproduce the above copyright notice,
119 * this list of conditions and the following disclaimer in the documentation
120 * and/or other materials provided with the distribution.
121 * 3. Neither the name of STMicroelectronics nor the names of its contributors
122 * may be used to endorse or promote products derived from this software
123 * without specific prior written permission.
125 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
126 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
127 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
128 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
129 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
130 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
131 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
132 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
133 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
134 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
136 ******************************************************************************
139 /* Includes ------------------------------------------------------------------*/
140 #include "stm32f7xx_hal.h"
142 /** @addtogroup STM32F7xx_HAL_Driver
146 /** @defgroup RTC RTC
147 * @brief RTC HAL module driver
151 #ifdef HAL_RTC_MODULE_ENABLED
153 /* Private typedef -----------------------------------------------------------*/
154 /* Private define ------------------------------------------------------------*/
155 /* Private macro -------------------------------------------------------------*/
156 /* Private variables ---------------------------------------------------------*/
157 /* Private function prototypes -----------------------------------------------*/
158 /* Private functions ---------------------------------------------------------*/
160 /** @defgroup RTC_Exported_Functions RTC Exported Functions
164 /** @defgroup RTC_Group1 Initialization and de-initialization functions
165 * @brief Initialization and Configuration functions
168 ===============================================================================
169 ##### Initialization and de-initialization functions #####
170 ===============================================================================
171 [..] This section provides functions allowing to initialize and configure the
172 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
173 RTC registers Write protection, enter and exit the RTC initialization mode,
174 RTC registers synchronization check and reference clock detection enable.
175 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
176 It is split into 2 programmable prescalers to minimize power consumption.
177 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
178 (++) When both prescalers are used, it is recommended to configure the
179 asynchronous prescaler to a high value to minimize power consumption.
180 (#) All RTC registers are Write protected. Writing to the RTC registers
181 is enabled by writing a key into the Write Protection register, RTC_WPR.
182 (#) To configure the RTC Calendar, user application should enter
183 initialization mode. In this mode, the calendar counter is stopped
184 and its value can be updated. When the initialization sequence is
185 complete, the calendar restarts counting after 4 RTCCLK cycles.
186 (#) To read the calendar through the shadow registers after Calendar
187 initialization, calendar update or after wakeup from low power modes
188 the software must first clear the RSF flag. The software must then
189 wait until it is set again before reading the calendar, which means
190 that the calendar registers have been correctly copied into the
191 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
192 implements the above software sequence (RSF clear and RSF check).
199 * @brief Initializes the RTC peripheral
200 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
201 * the configuration information for RTC.
204 HAL_StatusTypeDef
HAL_RTC_Init(RTC_HandleTypeDef
*hrtc
)
206 /* Check the RTC peripheral state */
212 /* Check the parameters */
213 assert_param(IS_RTC_ALL_INSTANCE(hrtc
->Instance
));
214 assert_param(IS_RTC_HOUR_FORMAT(hrtc
->Init
.HourFormat
));
215 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc
->Init
.AsynchPrediv
));
216 assert_param(IS_RTC_SYNCH_PREDIV(hrtc
->Init
.SynchPrediv
));
217 assert_param (IS_RTC_OUTPUT(hrtc
->Init
.OutPut
));
218 assert_param (IS_RTC_OUTPUT_POL(hrtc
->Init
.OutPutPolarity
));
219 assert_param(IS_RTC_OUTPUT_TYPE(hrtc
->Init
.OutPutType
));
221 if(hrtc
->State
== HAL_RTC_STATE_RESET
)
223 /* Allocate lock resource and initialize it */
224 hrtc
->Lock
= HAL_UNLOCKED
;
225 /* Initialize RTC MSP */
226 HAL_RTC_MspInit(hrtc
);
230 hrtc
->State
= HAL_RTC_STATE_BUSY
;
232 /* Disable the write protection for RTC registers */
233 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
235 /* Set Initialization mode */
236 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
238 /* Enable the write protection for RTC registers */
239 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
242 hrtc
->State
= HAL_RTC_STATE_ERROR
;
248 /* Clear RTC_CR FMT, OSEL and POL Bits */
249 hrtc
->Instance
->CR
&= ((uint32_t)~(RTC_CR_FMT
| RTC_CR_OSEL
| RTC_CR_POL
));
250 /* Set RTC_CR register */
251 hrtc
->Instance
->CR
|= (uint32_t)(hrtc
->Init
.HourFormat
| hrtc
->Init
.OutPut
| hrtc
->Init
.OutPutPolarity
);
253 /* Configure the RTC PRER */
254 hrtc
->Instance
->PRER
= (uint32_t)(hrtc
->Init
.SynchPrediv
);
255 hrtc
->Instance
->PRER
|= (uint32_t)(hrtc
->Init
.AsynchPrediv
<< 16);
257 /* Exit Initialization mode */
258 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
260 hrtc
->Instance
->OR
&= (uint32_t)~RTC_OR_ALARMTYPE
;
261 hrtc
->Instance
->OR
|= (uint32_t)(hrtc
->Init
.OutPutType
);
263 /* Enable the write protection for RTC registers */
264 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
267 hrtc
->State
= HAL_RTC_STATE_READY
;
274 * @brief DeInitializes the RTC peripheral
275 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
276 * the configuration information for RTC.
277 * @note This function doesn't reset the RTC Backup Data registers.
280 HAL_StatusTypeDef
HAL_RTC_DeInit(RTC_HandleTypeDef
*hrtc
)
282 uint32_t tickstart
= 0;
284 /* Check the parameters */
285 assert_param(IS_RTC_ALL_INSTANCE(hrtc
->Instance
));
288 hrtc
->State
= HAL_RTC_STATE_BUSY
;
290 /* Disable the write protection for RTC registers */
291 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
293 /* Set Initialization mode */
294 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
296 /* Enable the write protection for RTC registers */
297 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
300 hrtc
->State
= HAL_RTC_STATE_ERROR
;
306 /* Reset TR, DR and CR registers */
307 hrtc
->Instance
->TR
= (uint32_t)0x00000000;
308 hrtc
->Instance
->DR
= (uint32_t)0x00002101;
309 /* Reset All CR bits except CR[2:0] */
310 hrtc
->Instance
->CR
&= (uint32_t)0x00000007;
313 tickstart
= HAL_GetTick();
315 /* Wait till WUTWF flag is set and if Time out is reached exit */
316 while(((hrtc
->Instance
->ISR
) & RTC_ISR_WUTWF
) == (uint32_t)RESET
)
318 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
320 /* Enable the write protection for RTC registers */
321 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
324 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
330 /* Reset all RTC CR register bits */
331 hrtc
->Instance
->CR
&= (uint32_t)0x00000000;
332 hrtc
->Instance
->WUTR
= (uint32_t)0x0000FFFF;
333 hrtc
->Instance
->PRER
= (uint32_t)0x007F00FF;
334 hrtc
->Instance
->ALRMAR
= (uint32_t)0x00000000;
335 hrtc
->Instance
->ALRMBR
= (uint32_t)0x00000000;
336 hrtc
->Instance
->SHIFTR
= (uint32_t)0x00000000;
337 hrtc
->Instance
->CALR
= (uint32_t)0x00000000;
338 hrtc
->Instance
->ALRMASSR
= (uint32_t)0x00000000;
339 hrtc
->Instance
->ALRMBSSR
= (uint32_t)0x00000000;
341 /* Reset ISR register and exit initialization mode */
342 hrtc
->Instance
->ISR
= (uint32_t)0x00000000;
344 /* Reset Tamper and alternate functions configuration register */
345 hrtc
->Instance
->TAMPCR
= 0x00000000;
347 /* Reset Option register */
348 hrtc
->Instance
->OR
= 0x00000000;
350 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
351 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
353 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
355 /* Enable the write protection for RTC registers */
356 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
358 hrtc
->State
= HAL_RTC_STATE_ERROR
;
365 /* Enable the write protection for RTC registers */
366 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
368 /* De-Initialize RTC MSP */
369 HAL_RTC_MspDeInit(hrtc
);
371 hrtc
->State
= HAL_RTC_STATE_RESET
;
380 * @brief Initializes the RTC MSP.
381 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
382 * the configuration information for RTC.
385 __weak
void HAL_RTC_MspInit(RTC_HandleTypeDef
* hrtc
)
387 /* Prevent unused argument(s) compilation warning */
390 /* NOTE : This function Should not be modified, when the callback is needed,
391 the HAL_RTC_MspInit could be implemented in the user file
396 * @brief DeInitializes the RTC MSP.
397 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
398 * the configuration information for RTC.
401 __weak
void HAL_RTC_MspDeInit(RTC_HandleTypeDef
* hrtc
)
403 /* Prevent unused argument(s) compilation warning */
406 /* NOTE : This function Should not be modified, when the callback is needed,
407 the HAL_RTC_MspDeInit could be implemented in the user file
415 /** @defgroup RTC_Group2 RTC Time and Date functions
416 * @brief RTC Time and Date functions
419 ===============================================================================
420 ##### RTC Time and Date functions #####
421 ===============================================================================
423 [..] This section provides functions allowing to configure Time and Date features
430 * @brief Sets RTC current time.
431 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
432 * the configuration information for RTC.
433 * @param sTime: Pointer to Time structure
434 * @param Format: Specifies the format of the entered parameters.
435 * This parameter can be one of the following values:
436 * @arg FORMAT_BIN: Binary data format
437 * @arg FORMAT_BCD: BCD data format
440 HAL_StatusTypeDef
HAL_RTC_SetTime(RTC_HandleTypeDef
*hrtc
, RTC_TimeTypeDef
*sTime
, uint32_t Format
)
444 /* Check the parameters */
445 assert_param(IS_RTC_FORMAT(Format
));
446 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime
->DayLightSaving
));
447 assert_param(IS_RTC_STORE_OPERATION(sTime
->StoreOperation
));
452 hrtc
->State
= HAL_RTC_STATE_BUSY
;
454 if(Format
== RTC_FORMAT_BIN
)
456 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
458 assert_param(IS_RTC_HOUR12(sTime
->Hours
));
459 assert_param(IS_RTC_HOURFORMAT12(sTime
->TimeFormat
));
463 sTime
->TimeFormat
= 0x00;
464 assert_param(IS_RTC_HOUR24(sTime
->Hours
));
466 assert_param(IS_RTC_MINUTES(sTime
->Minutes
));
467 assert_param(IS_RTC_SECONDS(sTime
->Seconds
));
469 tmpreg
= (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime
->Hours
) << 16) | \
470 ((uint32_t)RTC_ByteToBcd2(sTime
->Minutes
) << 8) | \
471 ((uint32_t)RTC_ByteToBcd2(sTime
->Seconds
)) | \
472 (((uint32_t)sTime
->TimeFormat
) << 16));
476 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
478 tmpreg
= RTC_Bcd2ToByte(sTime
->Hours
);
479 assert_param(IS_RTC_HOUR12(tmpreg
));
480 assert_param(IS_RTC_HOURFORMAT12(sTime
->TimeFormat
));
484 sTime
->TimeFormat
= 0x00;
485 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime
->Hours
)));
487 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime
->Minutes
)));
488 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime
->Seconds
)));
489 tmpreg
= (((uint32_t)(sTime
->Hours
) << 16) | \
490 ((uint32_t)(sTime
->Minutes
) << 8) | \
491 ((uint32_t)sTime
->Seconds
) | \
492 ((uint32_t)(sTime
->TimeFormat
) << 16));
495 /* Disable the write protection for RTC registers */
496 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
498 /* Set Initialization mode */
499 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
501 /* Enable the write protection for RTC registers */
502 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
505 hrtc
->State
= HAL_RTC_STATE_ERROR
;
507 /* Process Unlocked */
514 /* Set the RTC_TR register */
515 hrtc
->Instance
->TR
= (uint32_t)(tmpreg
& RTC_TR_RESERVED_MASK
);
517 /* Clear the bits to be configured */
518 hrtc
->Instance
->CR
&= (uint32_t)~RTC_CR_BKP
;
520 /* Configure the RTC_CR register */
521 hrtc
->Instance
->CR
|= (uint32_t)(sTime
->DayLightSaving
| sTime
->StoreOperation
);
523 /* Exit Initialization mode */
524 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
526 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
527 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
529 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
531 /* Enable the write protection for RTC registers */
532 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
534 hrtc
->State
= HAL_RTC_STATE_ERROR
;
536 /* Process Unlocked */
543 /* Enable the write protection for RTC registers */
544 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
546 hrtc
->State
= HAL_RTC_STATE_READY
;
555 * @brief Gets RTC current time.
556 * @param hrtc: RTC handle
557 * @param sTime: Pointer to Time structure with Hours, Minutes and Seconds fields returned
558 * with input format (BIN or BCD), also SubSeconds field returning the
559 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
560 * factor to be used for second fraction ratio computation.
561 * @param Format: Specifies the format of the entered parameters.
562 * This parameter can be one of the following values:
563 * @arg RTC_FORMAT_BIN: Binary data format
564 * @arg RTC_FORMAT_BCD: BCD data format
565 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
566 * value in second fraction ratio with time unit following generic formula:
567 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
568 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
569 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
570 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
571 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
572 * to ensure consistency between the time and date values.
575 HAL_StatusTypeDef
HAL_RTC_GetTime(RTC_HandleTypeDef
*hrtc
, RTC_TimeTypeDef
*sTime
, uint32_t Format
)
579 /* Check the parameters */
580 assert_param(IS_RTC_FORMAT(Format
));
582 /* Get subseconds values from the correspondent registers*/
583 sTime
->SubSeconds
= (uint32_t)(hrtc
->Instance
->SSR
);
585 /* Get SecondFraction structure field from the corresponding register field*/
586 sTime
->SecondFraction
= (uint32_t)(hrtc
->Instance
->PRER
& RTC_PRER_PREDIV_S
);
588 /* Get the TR register */
589 tmpreg
= (uint32_t)(hrtc
->Instance
->TR
& RTC_TR_RESERVED_MASK
);
591 /* Fill the structure fields with the read parameters */
592 sTime
->Hours
= (uint8_t)((tmpreg
& (RTC_TR_HT
| RTC_TR_HU
)) >> 16);
593 sTime
->Minutes
= (uint8_t)((tmpreg
& (RTC_TR_MNT
| RTC_TR_MNU
)) >>8);
594 sTime
->Seconds
= (uint8_t)(tmpreg
& (RTC_TR_ST
| RTC_TR_SU
));
595 sTime
->TimeFormat
= (uint8_t)((tmpreg
& (RTC_TR_PM
)) >> 16);
597 /* Check the input parameters format */
598 if(Format
== RTC_FORMAT_BIN
)
600 /* Convert the time structure parameters to Binary format */
601 sTime
->Hours
= (uint8_t)RTC_Bcd2ToByte(sTime
->Hours
);
602 sTime
->Minutes
= (uint8_t)RTC_Bcd2ToByte(sTime
->Minutes
);
603 sTime
->Seconds
= (uint8_t)RTC_Bcd2ToByte(sTime
->Seconds
);
610 * @brief Sets RTC current date.
611 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
612 * the configuration information for RTC.
613 * @param sDate: Pointer to date structure
614 * @param Format: specifies the format of the entered parameters.
615 * This parameter can be one of the following values:
616 * @arg RTC_FORMAT_BIN: Binary data format
617 * @arg RTC_FORMAT_BCD: BCD data format
620 HAL_StatusTypeDef
HAL_RTC_SetDate(RTC_HandleTypeDef
*hrtc
, RTC_DateTypeDef
*sDate
, uint32_t Format
)
622 uint32_t datetmpreg
= 0;
624 /* Check the parameters */
625 assert_param(IS_RTC_FORMAT(Format
));
630 hrtc
->State
= HAL_RTC_STATE_BUSY
;
632 if((Format
== RTC_FORMAT_BIN
) && ((sDate
->Month
& 0x10U
) == 0x10U
))
634 sDate
->Month
= (uint8_t)((sDate
->Month
& (uint8_t)~(0x10U
)) + (uint8_t)0x0AU
);
637 assert_param(IS_RTC_WEEKDAY(sDate
->WeekDay
));
639 if(Format
== RTC_FORMAT_BIN
)
641 assert_param(IS_RTC_YEAR(sDate
->Year
));
642 assert_param(IS_RTC_MONTH(sDate
->Month
));
643 assert_param(IS_RTC_DATE(sDate
->Date
));
645 datetmpreg
= (((uint32_t)RTC_ByteToBcd2(sDate
->Year
) << 16) | \
646 ((uint32_t)RTC_ByteToBcd2(sDate
->Month
) << 8) | \
647 ((uint32_t)RTC_ByteToBcd2(sDate
->Date
)) | \
648 ((uint32_t)sDate
->WeekDay
<< 13));
652 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate
->Year
)));
653 datetmpreg
= RTC_Bcd2ToByte(sDate
->Month
);
654 assert_param(IS_RTC_MONTH(datetmpreg
));
655 datetmpreg
= RTC_Bcd2ToByte(sDate
->Date
);
656 assert_param(IS_RTC_DATE(datetmpreg
));
658 datetmpreg
= ((((uint32_t)sDate
->Year
) << 16) | \
659 (((uint32_t)sDate
->Month
) << 8) | \
660 ((uint32_t)sDate
->Date
) | \
661 (((uint32_t)sDate
->WeekDay
) << 13));
664 /* Disable the write protection for RTC registers */
665 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
667 /* Set Initialization mode */
668 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
670 /* Enable the write protection for RTC registers */
671 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
674 hrtc
->State
= HAL_RTC_STATE_ERROR
;
676 /* Process Unlocked */
683 /* Set the RTC_DR register */
684 hrtc
->Instance
->DR
= (uint32_t)(datetmpreg
& RTC_DR_RESERVED_MASK
);
686 /* Exit Initialization mode */
687 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
689 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
690 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
692 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
694 /* Enable the write protection for RTC registers */
695 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
697 hrtc
->State
= HAL_RTC_STATE_ERROR
;
699 /* Process Unlocked */
706 /* Enable the write protection for RTC registers */
707 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
709 hrtc
->State
= HAL_RTC_STATE_READY
;
711 /* Process Unlocked */
719 * @brief Gets RTC current date.
720 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
721 * the configuration information for RTC.
722 * @param sDate: Pointer to Date structure
723 * @param Format: Specifies the format of the entered parameters.
724 * This parameter can be one of the following values:
725 * @arg RTC_FORMAT_BIN: Binary data format
726 * @arg RTC_FORMAT_BCD: BCD data format
727 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
728 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
729 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
732 HAL_StatusTypeDef
HAL_RTC_GetDate(RTC_HandleTypeDef
*hrtc
, RTC_DateTypeDef
*sDate
, uint32_t Format
)
734 uint32_t datetmpreg
= 0;
736 /* Check the parameters */
737 assert_param(IS_RTC_FORMAT(Format
));
739 /* Get the DR register */
740 datetmpreg
= (uint32_t)(hrtc
->Instance
->DR
& RTC_DR_RESERVED_MASK
);
742 /* Fill the structure fields with the read parameters */
743 sDate
->Year
= (uint8_t)((datetmpreg
& (RTC_DR_YT
| RTC_DR_YU
)) >> 16);
744 sDate
->Month
= (uint8_t)((datetmpreg
& (RTC_DR_MT
| RTC_DR_MU
)) >> 8);
745 sDate
->Date
= (uint8_t)(datetmpreg
& (RTC_DR_DT
| RTC_DR_DU
));
746 sDate
->WeekDay
= (uint8_t)((datetmpreg
& (RTC_DR_WDU
)) >> 13);
748 /* Check the input parameters format */
749 if(Format
== RTC_FORMAT_BIN
)
751 /* Convert the date structure parameters to Binary format */
752 sDate
->Year
= (uint8_t)RTC_Bcd2ToByte(sDate
->Year
);
753 sDate
->Month
= (uint8_t)RTC_Bcd2ToByte(sDate
->Month
);
754 sDate
->Date
= (uint8_t)RTC_Bcd2ToByte(sDate
->Date
);
763 /** @defgroup RTC_Group3 RTC Alarm functions
764 * @brief RTC Alarm functions
767 ===============================================================================
768 ##### RTC Alarm functions #####
769 ===============================================================================
771 [..] This section provides functions allowing to configure Alarm feature
777 * @brief Sets the specified RTC Alarm.
778 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
779 * the configuration information for RTC.
780 * @param sAlarm: Pointer to Alarm structure
781 * @param Format: Specifies the format of the entered parameters.
782 * This parameter can be one of the following values:
783 * @arg FORMAT_BIN: Binary data format
784 * @arg FORMAT_BCD: BCD data format
787 HAL_StatusTypeDef
HAL_RTC_SetAlarm(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Format
)
789 uint32_t tickstart
= 0;
790 uint32_t tmpreg
= 0, subsecondtmpreg
= 0;
792 /* Check the parameters */
793 assert_param(IS_RTC_FORMAT(Format
));
794 assert_param(IS_RTC_ALARM(sAlarm
->Alarm
));
795 assert_param(IS_RTC_ALARM_MASK(sAlarm
->AlarmMask
));
796 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm
->AlarmDateWeekDaySel
));
797 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm
->AlarmTime
.SubSeconds
));
798 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm
->AlarmSubSecondMask
));
803 hrtc
->State
= HAL_RTC_STATE_BUSY
;
805 if(Format
== RTC_FORMAT_BIN
)
807 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
809 assert_param(IS_RTC_HOUR12(sAlarm
->AlarmTime
.Hours
));
810 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
814 sAlarm
->AlarmTime
.TimeFormat
= 0x00;
815 assert_param(IS_RTC_HOUR24(sAlarm
->AlarmTime
.Hours
));
817 assert_param(IS_RTC_MINUTES(sAlarm
->AlarmTime
.Minutes
));
818 assert_param(IS_RTC_SECONDS(sAlarm
->AlarmTime
.Seconds
));
820 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
822 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm
->AlarmDateWeekDay
));
826 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm
->AlarmDateWeekDay
));
829 tmpreg
= (((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Hours
) << 16) | \
830 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Minutes
) << 8) | \
831 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Seconds
)) | \
832 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16) | \
833 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmDateWeekDay
) << 24) | \
834 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
835 ((uint32_t)sAlarm
->AlarmMask
));
839 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
841 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
842 assert_param(IS_RTC_HOUR12(tmpreg
));
843 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
847 sAlarm
->AlarmTime
.TimeFormat
= 0x00;
848 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
)));
851 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
)));
852 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
)));
854 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
856 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
857 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg
));
861 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
862 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg
));
865 tmpreg
= (((uint32_t)(sAlarm
->AlarmTime
.Hours
) << 16) | \
866 ((uint32_t)(sAlarm
->AlarmTime
.Minutes
) << 8) | \
867 ((uint32_t) sAlarm
->AlarmTime
.Seconds
) | \
868 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16) | \
869 ((uint32_t)(sAlarm
->AlarmDateWeekDay
) << 24) | \
870 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
871 ((uint32_t)sAlarm
->AlarmMask
));
874 /* Configure the Alarm A or Alarm B Sub Second registers */
875 subsecondtmpreg
= (uint32_t)((uint32_t)(sAlarm
->AlarmTime
.SubSeconds
) | (uint32_t)(sAlarm
->AlarmSubSecondMask
));
877 /* Disable the write protection for RTC registers */
878 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
880 /* Configure the Alarm register */
881 if(sAlarm
->Alarm
== RTC_ALARM_A
)
883 /* Disable the Alarm A interrupt */
884 __HAL_RTC_ALARMA_DISABLE(hrtc
);
886 /* In case of interrupt mode is used, the interrupt source must disabled */
887 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRA
);
890 tickstart
= HAL_GetTick();
892 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
893 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
895 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
897 /* Enable the write protection for RTC registers */
898 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
900 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
902 /* Process Unlocked */
909 hrtc
->Instance
->ALRMAR
= (uint32_t)tmpreg
;
910 /* Configure the Alarm A Sub Second register */
911 hrtc
->Instance
->ALRMASSR
= subsecondtmpreg
;
912 /* Configure the Alarm state: Enable Alarm */
913 __HAL_RTC_ALARMA_ENABLE(hrtc
);
917 /* Disable the Alarm B interrupt */
918 __HAL_RTC_ALARMB_DISABLE(hrtc
);
920 /* In case of interrupt mode is used, the interrupt source must disabled */
921 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRB
);
924 tickstart
= HAL_GetTick();
926 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
927 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
929 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
931 /* Enable the write protection for RTC registers */
932 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
934 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
936 /* Process Unlocked */
943 hrtc
->Instance
->ALRMBR
= (uint32_t)tmpreg
;
944 /* Configure the Alarm B Sub Second register */
945 hrtc
->Instance
->ALRMBSSR
= subsecondtmpreg
;
946 /* Configure the Alarm state: Enable Alarm */
947 __HAL_RTC_ALARMB_ENABLE(hrtc
);
950 /* Enable the write protection for RTC registers */
951 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
953 /* Change RTC state */
954 hrtc
->State
= HAL_RTC_STATE_READY
;
956 /* Process Unlocked */
963 * @brief Sets the specified RTC Alarm with Interrupt
964 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
965 * the configuration information for RTC.
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_RTC_ALARM(sAlarm
->Alarm
));
984 assert_param(IS_RTC_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
== RTC_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
);
1076 tickstart
= HAL_GetTick();
1078 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1079 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
1081 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1083 /* Enable the write protection for RTC registers */
1084 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1086 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1088 /* Process Unlocked */
1095 hrtc
->Instance
->ALRMAR
= (uint32_t)tmpreg
;
1096 /* Configure the Alarm A Sub Second register */
1097 hrtc
->Instance
->ALRMASSR
= subsecondtmpreg
;
1098 /* Configure the Alarm state: Enable Alarm */
1099 __HAL_RTC_ALARMA_ENABLE(hrtc
);
1100 /* Configure the Alarm interrupt */
1101 __HAL_RTC_ALARM_ENABLE_IT(hrtc
,RTC_IT_ALRA
);
1105 /* Disable the Alarm B interrupt */
1106 __HAL_RTC_ALARMB_DISABLE(hrtc
);
1108 /* Clear flag alarm B */
1109 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRBF
);
1112 tickstart
= HAL_GetTick();
1114 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1115 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
1117 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1119 /* Enable the write protection for RTC registers */
1120 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1122 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1124 /* Process Unlocked */
1131 hrtc
->Instance
->ALRMBR
= (uint32_t)tmpreg
;
1132 /* Configure the Alarm B Sub Second register */
1133 hrtc
->Instance
->ALRMBSSR
= subsecondtmpreg
;
1134 /* Configure the Alarm state: Enable Alarm */
1135 __HAL_RTC_ALARMB_ENABLE(hrtc
);
1136 /* Configure the Alarm interrupt */
1137 __HAL_RTC_ALARM_ENABLE_IT(hrtc
, RTC_IT_ALRB
);
1140 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1141 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1143 EXTI
->RTSR
|= RTC_EXTI_LINE_ALARM_EVENT
;
1145 /* Enable the write protection for RTC registers */
1146 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1148 hrtc
->State
= HAL_RTC_STATE_READY
;
1150 /* Process Unlocked */
1157 * @brief Deactive the specified RTC Alarm
1158 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1159 * the configuration information for RTC.
1160 * @param Alarm: Specifies the Alarm.
1161 * This parameter can be one of the following values:
1162 * @arg RTC_ALARM_A: AlarmA
1163 * @arg RTC_ALARM_B: AlarmB
1164 * @retval HAL status
1166 HAL_StatusTypeDef
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef
*hrtc
, uint32_t Alarm
)
1168 uint32_t tickstart
= 0;
1170 /* Check the parameters */
1171 assert_param(IS_RTC_ALARM(Alarm
));
1173 /* Process Locked */
1176 hrtc
->State
= HAL_RTC_STATE_BUSY
;
1178 /* Disable the write protection for RTC registers */
1179 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
1181 if(Alarm
== RTC_ALARM_A
)
1184 __HAL_RTC_ALARMA_DISABLE(hrtc
);
1186 /* In case of interrupt mode is used, the interrupt source must disabled */
1187 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRA
);
1190 tickstart
= HAL_GetTick();
1192 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1193 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
1195 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1197 /* Enable the write protection for RTC registers */
1198 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1200 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1202 /* Process Unlocked */
1212 __HAL_RTC_ALARMB_DISABLE(hrtc
);
1214 /* In case of interrupt mode is used, the interrupt source must disabled */
1215 __HAL_RTC_ALARM_DISABLE_IT(hrtc
,RTC_IT_ALRB
);
1218 tickstart
= HAL_GetTick();
1220 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1221 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
1223 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1225 /* Enable the write protection for RTC registers */
1226 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1228 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1230 /* Process Unlocked */
1237 /* Enable the write protection for RTC registers */
1238 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1240 hrtc
->State
= HAL_RTC_STATE_READY
;
1242 /* Process Unlocked */
1249 * @brief Gets the RTC Alarm value and masks.
1250 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1251 * the configuration information for RTC.
1252 * @param sAlarm: Pointer to Date structure
1253 * @param Alarm: Specifies the Alarm.
1254 * This parameter can be one of the following values:
1255 * @arg RTC_ALARM_A: AlarmA
1256 * @arg RTC_ALARM_B: AlarmB
1257 * @param Format: Specifies the format of the entered parameters.
1258 * This parameter can be one of the following values:
1259 * @arg RTC_FORMAT_BIN: Binary data format
1260 * @arg RTC_FORMAT_BCD: BCD data format
1261 * @retval HAL status
1263 HAL_StatusTypeDef
HAL_RTC_GetAlarm(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Alarm
, uint32_t Format
)
1265 uint32_t tmpreg
= 0, subsecondtmpreg
= 0;
1267 /* Check the parameters */
1268 assert_param(IS_RTC_FORMAT(Format
));
1269 assert_param(IS_RTC_ALARM(Alarm
));
1271 if(Alarm
== RTC_ALARM_A
)
1274 sAlarm
->Alarm
= RTC_ALARM_A
;
1276 tmpreg
= (uint32_t)(hrtc
->Instance
->ALRMAR
);
1277 subsecondtmpreg
= (uint32_t)((hrtc
->Instance
->ALRMASSR
) & RTC_ALRMASSR_SS
);
1281 sAlarm
->Alarm
= RTC_ALARM_B
;
1283 tmpreg
= (uint32_t)(hrtc
->Instance
->ALRMBR
);
1284 subsecondtmpreg
= (uint32_t)((hrtc
->Instance
->ALRMBSSR
) & RTC_ALRMBSSR_SS
);
1287 /* Fill the structure with the read parameters */
1288 sAlarm
->AlarmTime
.Hours
= (uint32_t)((tmpreg
& (RTC_ALRMAR_HT
| RTC_ALRMAR_HU
)) >> 16);
1289 sAlarm
->AlarmTime
.Minutes
= (uint32_t)((tmpreg
& (RTC_ALRMAR_MNT
| RTC_ALRMAR_MNU
)) >> 8);
1290 sAlarm
->AlarmTime
.Seconds
= (uint32_t)(tmpreg
& (RTC_ALRMAR_ST
| RTC_ALRMAR_SU
));
1291 sAlarm
->AlarmTime
.TimeFormat
= (uint32_t)((tmpreg
& RTC_ALRMAR_PM
) >> 16);
1292 sAlarm
->AlarmTime
.SubSeconds
= (uint32_t) subsecondtmpreg
;
1293 sAlarm
->AlarmDateWeekDay
= (uint32_t)((tmpreg
& (RTC_ALRMAR_DT
| RTC_ALRMAR_DU
)) >> 24);
1294 sAlarm
->AlarmDateWeekDaySel
= (uint32_t)(tmpreg
& RTC_ALRMAR_WDSEL
);
1295 sAlarm
->AlarmMask
= (uint32_t)(tmpreg
& RTC_ALARMMASK_ALL
);
1297 if(Format
== RTC_FORMAT_BIN
)
1299 sAlarm
->AlarmTime
.Hours
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
1300 sAlarm
->AlarmTime
.Minutes
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
);
1301 sAlarm
->AlarmTime
.Seconds
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
);
1302 sAlarm
->AlarmDateWeekDay
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1309 * @brief This function handles Alarm interrupt request.
1310 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1311 * the configuration information for RTC.
1314 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef
* hrtc
)
1316 if(__HAL_RTC_ALARM_GET_IT(hrtc
, RTC_IT_ALRA
))
1318 /* Get the status of the Interrupt */
1319 if((uint32_t)(hrtc
->Instance
->CR
& RTC_IT_ALRA
) != (uint32_t)RESET
)
1321 /* AlarmA callback */
1322 HAL_RTC_AlarmAEventCallback(hrtc
);
1324 /* Clear the Alarm interrupt pending bit */
1325 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
,RTC_FLAG_ALRAF
);
1329 if(__HAL_RTC_ALARM_GET_IT(hrtc
, RTC_IT_ALRB
))
1331 /* Get the status of the Interrupt */
1332 if((uint32_t)(hrtc
->Instance
->CR
& RTC_IT_ALRB
) != (uint32_t)RESET
)
1334 /* AlarmB callback */
1335 HAL_RTCEx_AlarmBEventCallback(hrtc
);
1337 /* Clear the Alarm interrupt pending bit */
1338 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
,RTC_FLAG_ALRBF
);
1342 /* Clear the EXTI's line Flag for RTC Alarm */
1343 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1345 /* Change RTC state */
1346 hrtc
->State
= HAL_RTC_STATE_READY
;
1350 * @brief Alarm A callback.
1351 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1352 * the configuration information for RTC.
1355 __weak
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef
*hrtc
)
1357 /* Prevent unused argument(s) compilation warning */
1360 /* NOTE : This function Should not be modified, when the callback is needed,
1361 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1366 * @brief This function handles AlarmA Polling request.
1367 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1368 * the configuration information for RTC.
1369 * @param Timeout: Timeout duration
1370 * @retval HAL status
1372 HAL_StatusTypeDef
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef
*hrtc
, uint32_t Timeout
)
1374 uint32_t tickstart
= 0;
1377 tickstart
= HAL_GetTick();
1379 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAF
) == RESET
)
1381 if(Timeout
!= HAL_MAX_DELAY
)
1383 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
1385 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1391 /* Clear the Alarm interrupt pending bit */
1392 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRAF
);
1394 /* Change RTC state */
1395 hrtc
->State
= HAL_RTC_STATE_READY
;
1404 /** @defgroup RTC_Group4 Peripheral Control functions
1405 * @brief Peripheral Control functions
1408 ===============================================================================
1409 ##### Peripheral Control functions #####
1410 ===============================================================================
1412 This subsection provides functions allowing to
1413 (+) Wait for RTC Time and Date Synchronization
1420 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1421 * synchronized with RTC APB clock.
1422 * @note The RTC Resynchronization mode is write protected, use the
1423 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1424 * @note To read the calendar through the shadow registers after Calendar
1425 * initialization, calendar update or after wakeup from low power modes
1426 * the software must first clear the RSF flag.
1427 * The software must then wait until it is set again before reading
1428 * the calendar, which means that the calendar registers have been
1429 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1430 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1431 * the configuration information for RTC.
1432 * @retval HAL status
1434 HAL_StatusTypeDef
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef
* hrtc
)
1436 uint32_t tickstart
= 0;
1438 /* Clear RSF flag */
1439 hrtc
->Instance
->ISR
&= (uint32_t)RTC_RSF_MASK
;
1442 tickstart
= HAL_GetTick();
1444 /* Wait the registers to be synchronised */
1445 while((hrtc
->Instance
->ISR
& RTC_ISR_RSF
) == (uint32_t)RESET
)
1447 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1460 /** @defgroup RTC_Group5 Peripheral State functions
1461 * @brief Peripheral State functions
1464 ===============================================================================
1465 ##### Peripheral State functions #####
1466 ===============================================================================
1468 This subsection provides functions allowing to
1475 * @brief Returns the RTC state.
1476 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1477 * the configuration information for RTC.
1480 HAL_RTCStateTypeDef
HAL_RTC_GetState(RTC_HandleTypeDef
* hrtc
)
1490 * @brief Enters the RTC Initialization mode.
1491 * @note The RTC Initialization mode is write protected, use the
1492 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1493 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
1494 * the configuration information for RTC.
1495 * @retval HAL status
1497 HAL_StatusTypeDef
RTC_EnterInitMode(RTC_HandleTypeDef
* hrtc
)
1499 uint32_t tickstart
= 0;
1501 /* Check if the Initialization mode is set */
1502 if((hrtc
->Instance
->ISR
& RTC_ISR_INITF
) == (uint32_t)RESET
)
1504 /* Set the Initialization mode */
1505 hrtc
->Instance
->ISR
= (uint32_t)RTC_INIT_MASK
;
1508 tickstart
= HAL_GetTick();
1510 /* Wait till RTC is in INIT state and if Time out is reached exit */
1511 while((hrtc
->Instance
->ISR
& RTC_ISR_INITF
) == (uint32_t)RESET
)
1513 if((HAL_GetTick() - tickstart
) > RTC_TIMEOUT_VALUE
)
1525 * @brief Converts a 2 digit decimal to BCD format.
1526 * @param Value: Byte to be converted
1527 * @retval Converted byte
1529 uint8_t RTC_ByteToBcd2(uint8_t Value
)
1531 uint32_t bcdhigh
= 0;
1539 return ((uint8_t)(bcdhigh
<< 4) | Value
);
1543 * @brief Converts from 2 digit BCD to Binary.
1544 * @param Value: BCD value to be converted
1545 * @retval Converted word
1547 uint8_t RTC_Bcd2ToByte(uint8_t Value
)
1550 tmp
= ((uint8_t)(Value
& (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1551 return (tmp
+ (Value
& (uint8_t)0x0F));
1558 #endif /* HAL_RTC_MODULE_ENABLED */
1567 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/