2 ******************************************************************************
3 * @file stm32f3xx_hal_rtc.c
4 * @author MCD Application Team
5 * @brief RTC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Real-Time Clock (RTC) peripheral:
9 * + Calendar (Time and Date) configuration
10 * + Alarms (Alarm A and Alarm B) configuration
11 * + WakeUp Timer configuration
12 * + TimeStamp configuration
13 * + Tampers configuration
14 * + Backup Data Registers configuration
15 * + RTC Tamper and TimeStamp Pins Selection
16 * + Interrupts and flags management
20 ===============================================================================
21 ##### RTC Operating Condition #####
22 ===============================================================================
23 [..] The real-time clock (RTC) and the RTC backup registers can be powered
24 from the VBAT voltage when the main VDD supply is powered off.
25 To retain the content of the RTC backup registers and supply the RTC
26 when VDD is turned off, VBAT pin can be connected to an optional
27 standby voltage supplied by a battery or by another source.
29 [..] To allow the RTC to operate even when the main digital supply (VDD)
30 is turned off, the VBAT pin powers the following blocks:
32 (#) The LSE oscillator
33 (#) PC13 to PC15 I/Os (when available)
35 [..] When the backup domain is supplied by VDD (analog switch connected
36 to VDD), the following functions are available:
37 (#) PC14 and PC15 can be used as either GPIO or LSE pins
38 (#) PC13 can be used as a GPIO or as the RTC_OUT pin
40 [..] When the backup domain is supplied by VBAT (analog switch connected
41 to VBAT because VDD is not present), the following functions are available:
42 (#) PC14 and PC15 can be used as LSE pins only
43 (#) PC13 can be used as the RTC_OUT pin
45 ##### Backup Domain Reset #####
46 ===============================================================================
47 [..] The backup domain reset sets all RTC registers and the RCC_BDCR
48 register to their reset values.
49 A backup domain reset is generated when one of the following events
51 (#) Software reset, triggered by setting the BDRST bit in the
52 RCC Backup domain control register (RCC_BDCR).
53 (#) VDD or VBAT power on, if both supplies have previously been
56 ##### Backup Domain Access #####
57 ===================================================================
58 [..] After reset, the backup domain (RTC registers, RTC backup data
59 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 RTC 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
89 HAL_RTC_SetAlarm_IT() function.
90 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
92 *** RTC Wakeup configuration ***
93 ================================
95 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
96 function. You can also configure the RTC Wakeup timer with interrupt mode
97 using the HAL_RTC_SetWakeUpTimer_IT() function.
98 (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
101 *** TimeStamp configuration ***
102 ===============================
104 (+) Configure the RTC_AF trigger and enables the RTC TimeStamp using the
105 HAL_RTC_SetTimeStamp() function. You can also configure the RTC TimeStamp with
106 interrupt mode using the HAL_RTC_SetTimeStamp_IT() function.
107 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
110 *** Tamper configuration ***
111 ============================
113 (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
114 or Level according to the Tamper filter (if equal to 0 Edge else Level)
115 value, sampling frequency, precharge or discharge and Pull-UP using the
116 HAL_RTC_SetTamper() function. You can configure RTC Tamper with interrupt
117 mode using HAL_RTC_SetTamper_IT() function.
119 *** Backup Data Registers configuration ***
120 ===========================================
122 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
124 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
128 ##### RTC and low power modes #####
129 ===================================================================
130 [..] The MCU can be woken up from a low power mode by an RTC alternate
132 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
133 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
134 These RTC alternate functions can wake up the system from the Stop and
135 Standby low power modes.
136 [..] The system can also wake up from low power modes without depending
137 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
138 or the RTC wakeup events.
139 [..] The RTC provides a programmable time base for waking up from the
140 Stop or Standby mode at regular intervals.
141 Wakeup from STOP and Standby modes is possible only when the RTC clock source
146 ******************************************************************************
149 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
151 * Redistribution and use in source and binary forms, with or without modification,
152 * are permitted provided that the following conditions are met:
153 * 1. Redistributions of source code must retain the above copyright notice,
154 * this list of conditions and the following disclaimer.
155 * 2. Redistributions in binary form must reproduce the above copyright notice,
156 * this list of conditions and the following disclaimer in the documentation
157 * and/or other materials provided with the distribution.
158 * 3. Neither the name of STMicroelectronics nor the names of its contributors
159 * may be used to endorse or promote products derived from this software
160 * without specific prior written permission.
162 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
163 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
165 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
166 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
167 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
168 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
169 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
170 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
171 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
173 ******************************************************************************
176 /* Includes ------------------------------------------------------------------*/
177 #include "stm32f3xx_hal.h"
179 /** @addtogroup STM32F3xx_HAL_Driver
184 * @brief RTC HAL module driver
188 #ifdef HAL_RTC_MODULE_ENABLED
190 /* Private typedef -----------------------------------------------------------*/
191 /* Private define ------------------------------------------------------------*/
192 /* Private macro -------------------------------------------------------------*/
193 /* Private variables ---------------------------------------------------------*/
194 /* Private function prototypes -----------------------------------------------*/
195 /* Exported functions ---------------------------------------------------------*/
197 /** @addtogroup RTC_Exported_Functions
201 /** @addtogroup RTC_Exported_Functions_Group1
202 * @brief Initialization and Configuration functions
205 ===============================================================================
206 ##### Initialization and de-initialization functions #####
207 ===============================================================================
208 [..] This section provides functions allowing to initialize and configure the
209 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
210 RTC registers Write protection, enter and exit the RTC initialization mode,
211 RTC registers synchronization check and reference clock detection enable.
212 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
213 It is split into 2 programmable prescalers to minimize power consumption.
214 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
215 (++) When both prescalers are used, it is recommended to configure the
216 asynchronous prescaler to a high value to minimize power consumption.
217 (#) All RTC registers are Write protected. Writing to the RTC registers
218 is enabled by writing a key into the Write Protection register, RTC_WPR.
219 (#) To configure the RTC Calendar, user application should enter
220 initialization mode. In this mode, the calendar counter is stopped
221 and its value can be updated. When the initialization sequence is
222 complete, the calendar restarts counting after 4 RTCCLK cycles.
223 (#) To read the calendar through the shadow registers after Calendar
224 initialization, calendar update or after wakeup from low power modes
225 the software must first clear the RSF flag. The software must then
226 wait until it is set again before reading the calendar, which means
227 that the calendar registers have been correctly copied into the
228 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
229 implements the above software sequence (RSF clear and RSF check).
236 * @brief Initialize the RTC according to the specified parameters
237 * in the RTC_InitTypeDef structure and initialize the associated handle.
238 * @param hrtc RTC handle
241 HAL_StatusTypeDef
HAL_RTC_Init(RTC_HandleTypeDef
*hrtc
)
243 /* Check the RTC peripheral state */
249 /* Check the parameters */
250 assert_param(IS_RTC_ALL_INSTANCE(hrtc
->Instance
));
251 assert_param(IS_RTC_HOUR_FORMAT(hrtc
->Init
.HourFormat
));
252 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc
->Init
.AsynchPrediv
));
253 assert_param(IS_RTC_SYNCH_PREDIV(hrtc
->Init
.SynchPrediv
));
254 assert_param(IS_RTC_OUTPUT(hrtc
->Init
.OutPut
));
255 assert_param(IS_RTC_OUTPUT_POL(hrtc
->Init
.OutPutPolarity
));
256 assert_param(IS_RTC_OUTPUT_TYPE(hrtc
->Init
.OutPutType
));
258 if(hrtc
->State
== HAL_RTC_STATE_RESET
)
260 /* Allocate lock resource and initialize it */
261 hrtc
->Lock
= HAL_UNLOCKED
;
263 /* Initialize RTC MSP */
264 HAL_RTC_MspInit(hrtc
);
268 hrtc
->State
= HAL_RTC_STATE_BUSY
;
270 /* Disable the write protection for RTC registers */
271 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
273 /* Set Initialization mode */
274 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
276 /* Enable the write protection for RTC registers */
277 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
280 hrtc
->State
= HAL_RTC_STATE_ERROR
;
286 /* Clear RTC_CR FMT, OSEL and POL Bits */
287 hrtc
->Instance
->CR
&= ((uint32_t)~(RTC_CR_FMT
| RTC_CR_OSEL
| RTC_CR_POL
));
288 /* Set RTC_CR register */
289 hrtc
->Instance
->CR
|= (uint32_t)(hrtc
->Init
.HourFormat
| hrtc
->Init
.OutPut
| hrtc
->Init
.OutPutPolarity
);
291 /* Configure the RTC PRER */
292 hrtc
->Instance
->PRER
= (uint32_t)(hrtc
->Init
.SynchPrediv
);
293 hrtc
->Instance
->PRER
|= (uint32_t)(hrtc
->Init
.AsynchPrediv
<< 16U);
295 /* Exit Initialization mode */
296 hrtc
->Instance
->ISR
&= (uint32_t)~RTC_ISR_INIT
;
298 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
299 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
301 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
303 /* Enable the write protection for RTC registers */
304 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
306 hrtc
->State
= HAL_RTC_STATE_ERROR
;
312 hrtc
->Instance
->TAFCR
&= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE
;
313 hrtc
->Instance
->TAFCR
|= (uint32_t)(hrtc
->Init
.OutPutType
);
315 /* Enable the write protection for RTC registers */
316 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
319 hrtc
->State
= HAL_RTC_STATE_READY
;
326 * @brief DeInitialize the RTC peripheral.
327 * @param hrtc RTC handle
328 * @note This function doesn't reset the RTC Backup Data registers.
331 HAL_StatusTypeDef
HAL_RTC_DeInit(RTC_HandleTypeDef
*hrtc
)
333 uint32_t tickstart
= 0U;
335 /* Check the parameters */
336 assert_param(IS_RTC_ALL_INSTANCE(hrtc
->Instance
));
339 hrtc
->State
= HAL_RTC_STATE_BUSY
;
341 /* Disable the write protection for RTC registers */
342 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
344 /* Set Initialization mode */
345 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
347 /* Enable the write protection for RTC registers */
348 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
351 hrtc
->State
= HAL_RTC_STATE_ERROR
;
357 /* Reset TR, DR and CR registers */
358 hrtc
->Instance
->TR
= 0x00000000U
;
359 hrtc
->Instance
->DR
= (RTC_DR_WDU_0
| RTC_DR_MU_0
| RTC_DR_DU_0
);
360 /* Reset All CR bits except CR[2:0] */
361 hrtc
->Instance
->CR
&= RTC_CR_WUCKSEL
;
363 tickstart
= HAL_GetTick();
365 /* Wait till WUTWF flag is set and if Time out is reached exit */
366 while(((hrtc
->Instance
->ISR
) & RTC_ISR_WUTWF
) == (uint32_t)RESET
)
368 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
370 /* Enable the write protection for RTC registers */
371 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
374 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
380 /* Reset all RTC CR register bits */
381 hrtc
->Instance
->CR
&= 0x00000000U
;
382 hrtc
->Instance
->WUTR
= RTC_WUTR_WUT
;
383 hrtc
->Instance
->PRER
= ((uint32_t)(RTC_PRER_PREDIV_A
| 0x000000FFU
));
384 hrtc
->Instance
->ALRMAR
= 0x00000000U
;
385 hrtc
->Instance
->ALRMBR
= 0x00000000U
;
386 hrtc
->Instance
->SHIFTR
= 0x00000000U
;
387 hrtc
->Instance
->CALR
= 0x00000000U
;
388 hrtc
->Instance
->ALRMASSR
= 0x00000000U
;
389 hrtc
->Instance
->ALRMBSSR
= 0x00000000U
;
391 /* Reset ISR register and exit initialization mode */
392 hrtc
->Instance
->ISR
= 0x00000000U
;
394 /* Reset Tamper and alternate functions configuration register */
395 hrtc
->Instance
->TAFCR
= 0x00000000U
;
397 /* If RTC_CR_BYPSHAD bit = 0U, wait for synchro else this check is not needed */
398 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
400 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
402 /* Enable the write protection for RTC registers */
403 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
405 hrtc
->State
= HAL_RTC_STATE_ERROR
;
412 /* Enable the write protection for RTC registers */
413 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
415 /* De-Initialize RTC MSP */
416 HAL_RTC_MspDeInit(hrtc
);
418 hrtc
->State
= HAL_RTC_STATE_RESET
;
427 * @brief Initialize the RTC MSP.
428 * @param hrtc RTC handle
431 __weak
void HAL_RTC_MspInit(RTC_HandleTypeDef
* hrtc
)
433 /* Prevent unused argument(s) compilation warning */
436 /* NOTE : This function should not be modified, when the callback is needed,
437 the HAL_RTC_MspInit could be implemented in the user file
442 * @brief DeInitialize the RTC MSP.
443 * @param hrtc RTC handle
446 __weak
void HAL_RTC_MspDeInit(RTC_HandleTypeDef
* hrtc
)
448 /* Prevent unused argument(s) compilation warning */
451 /* NOTE : This function should not be modified, when the callback is needed,
452 the HAL_RTC_MspDeInit could be implemented in the user file
460 /** @addtogroup RTC_Exported_Functions_Group2
461 * @brief RTC Time and Date functions
464 ===============================================================================
465 ##### RTC Time and Date functions #####
466 ===============================================================================
468 [..] This section provides functions allowing to configure Time and Date features
475 * @brief Set RTC current time.
476 * @param hrtc RTC handle
477 * @param sTime Pointer to Time structure
478 * @param Format Specifies the format of the entered parameters.
479 * This parameter can be one of the following values:
480 * @arg RTC_FORMAT_BIN: Binary data format
481 * @arg RTC_FORMAT_BCD: BCD data format
484 HAL_StatusTypeDef
HAL_RTC_SetTime(RTC_HandleTypeDef
*hrtc
, RTC_TimeTypeDef
*sTime
, uint32_t Format
)
486 uint32_t tmpreg
= 0U;
488 /* Check the parameters */
489 assert_param(IS_RTC_FORMAT(Format
));
490 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime
->DayLightSaving
));
491 assert_param(IS_RTC_STORE_OPERATION(sTime
->StoreOperation
));
496 hrtc
->State
= HAL_RTC_STATE_BUSY
;
498 if(Format
== RTC_FORMAT_BIN
)
500 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
502 assert_param(IS_RTC_HOUR12(sTime
->Hours
));
503 assert_param(IS_RTC_HOURFORMAT12(sTime
->TimeFormat
));
507 sTime
->TimeFormat
= 0x00U
;
508 assert_param(IS_RTC_HOUR24(sTime
->Hours
));
510 assert_param(IS_RTC_MINUTES(sTime
->Minutes
));
511 assert_param(IS_RTC_SECONDS(sTime
->Seconds
));
513 tmpreg
= (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime
->Hours
) << 16U) | \
514 ((uint32_t)RTC_ByteToBcd2(sTime
->Minutes
) << 8U) | \
515 ((uint32_t)RTC_ByteToBcd2(sTime
->Seconds
)) | \
516 (((uint32_t)sTime
->TimeFormat
) << 16U));
520 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
522 tmpreg
= RTC_Bcd2ToByte(sTime
->Hours
);
523 assert_param(IS_RTC_HOUR12(tmpreg
));
524 assert_param(IS_RTC_HOURFORMAT12(sTime
->TimeFormat
));
528 sTime
->TimeFormat
= 0x00U
;
529 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime
->Hours
)));
531 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime
->Minutes
)));
532 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime
->Seconds
)));
533 tmpreg
= (((uint32_t)(sTime
->Hours
) << 16U) | \
534 ((uint32_t)(sTime
->Minutes
) << 8U) | \
535 ((uint32_t)sTime
->Seconds
) | \
536 ((uint32_t)(sTime
->TimeFormat
) << 16U));
539 /* Disable the write protection for RTC registers */
540 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
542 /* Set Initialization mode */
543 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
545 /* Enable the write protection for RTC registers */
546 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
549 hrtc
->State
= HAL_RTC_STATE_ERROR
;
551 /* Process Unlocked */
558 /* Set the RTC_TR register */
559 hrtc
->Instance
->TR
= (uint32_t)(tmpreg
& RTC_TR_RESERVED_MASK
);
561 /* Clear the bits to be configured */
562 hrtc
->Instance
->CR
&= ((uint32_t)~RTC_CR_BCK
);
564 /* Configure the RTC_CR register */
565 hrtc
->Instance
->CR
|= (uint32_t)(sTime
->DayLightSaving
| sTime
->StoreOperation
);
567 /* Exit Initialization mode */
568 hrtc
->Instance
->ISR
&= ((uint32_t)~RTC_ISR_INIT
);
570 /* If CR_BYPSHAD bit = 0U, wait for synchro else this check is not needed */
571 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
573 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
575 /* Enable the write protection for RTC registers */
576 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
578 hrtc
->State
= HAL_RTC_STATE_ERROR
;
580 /* Process Unlocked */
587 /* Enable the write protection for RTC registers */
588 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
590 hrtc
->State
= HAL_RTC_STATE_READY
;
599 * @brief Get RTC current time.
600 * @param hrtc RTC handle
601 * @param sTime Pointer to Time 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
606 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
607 * value in second fraction ratio with time unit following generic formula:
608 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
609 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
610 * @note Call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
611 * in the higher-order calendar shadow registers.
614 HAL_StatusTypeDef
HAL_RTC_GetTime(RTC_HandleTypeDef
*hrtc
, RTC_TimeTypeDef
*sTime
, uint32_t Format
)
616 uint32_t tmpreg
= 0U;
618 /* Check the parameters */
619 assert_param(IS_RTC_FORMAT(Format
));
621 /* Get subseconds structure field from the corresponding register*/
622 sTime
->SubSeconds
= (uint32_t)(hrtc
->Instance
->SSR
);
624 /* Get SecondFraction structure field from the corresponding register field*/
625 sTime
->SecondFraction
= (uint32_t)(hrtc
->Instance
->PRER
& RTC_PRER_PREDIV_S
);
627 /* Get the TR register */
628 tmpreg
= (uint32_t)(hrtc
->Instance
->TR
& RTC_TR_RESERVED_MASK
);
630 /* Fill the structure fields with the read parameters */
631 sTime
->Hours
= (uint8_t)((tmpreg
& (RTC_TR_HT
| RTC_TR_HU
)) >> 16U);
632 sTime
->Minutes
= (uint8_t)((tmpreg
& (RTC_TR_MNT
| RTC_TR_MNU
)) >>8U);
633 sTime
->Seconds
= (uint8_t)(tmpreg
& (RTC_TR_ST
| RTC_TR_SU
));
634 sTime
->TimeFormat
= (uint8_t)((tmpreg
& (RTC_TR_PM
)) >> 16U);
636 /* Check the input parameters format */
637 if(Format
== RTC_FORMAT_BIN
)
639 /* Convert the time structure parameters to Binary format */
640 sTime
->Hours
= (uint8_t)RTC_Bcd2ToByte(sTime
->Hours
);
641 sTime
->Minutes
= (uint8_t)RTC_Bcd2ToByte(sTime
->Minutes
);
642 sTime
->Seconds
= (uint8_t)RTC_Bcd2ToByte(sTime
->Seconds
);
649 * @brief Set RTC current date.
650 * @param hrtc RTC handle
651 * @param sDate Pointer to date structure
652 * @param Format specifies the format of the entered parameters.
653 * This parameter can be one of the following values:
654 * @arg RTC_FORMAT_BIN: Binary data format
655 * @arg RTC_FORMAT_BCD: BCD data format
658 HAL_StatusTypeDef
HAL_RTC_SetDate(RTC_HandleTypeDef
*hrtc
, RTC_DateTypeDef
*sDate
, uint32_t Format
)
660 uint32_t datetmpreg
= 0U;
662 /* Check the parameters */
663 assert_param(IS_RTC_FORMAT(Format
));
668 hrtc
->State
= HAL_RTC_STATE_BUSY
;
670 if((Format
== RTC_FORMAT_BIN
) && ((sDate
->Month
& 0x10U
) == 0x10U
))
672 sDate
->Month
= (uint8_t)((sDate
->Month
& (uint8_t)~(0x10U
)) + (uint8_t)0x0AU
);
675 assert_param(IS_RTC_WEEKDAY(sDate
->WeekDay
));
677 if(Format
== RTC_FORMAT_BIN
)
679 assert_param(IS_RTC_YEAR(sDate
->Year
));
680 assert_param(IS_RTC_MONTH(sDate
->Month
));
681 assert_param(IS_RTC_DATE(sDate
->Date
));
683 datetmpreg
= (((uint32_t)RTC_ByteToBcd2(sDate
->Year
) << 16U) | \
684 ((uint32_t)RTC_ByteToBcd2(sDate
->Month
) << 8U) | \
685 ((uint32_t)RTC_ByteToBcd2(sDate
->Date
)) | \
686 ((uint32_t)sDate
->WeekDay
<< 13U));
690 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate
->Year
)));
691 datetmpreg
= RTC_Bcd2ToByte(sDate
->Month
);
692 assert_param(IS_RTC_MONTH(datetmpreg
));
693 datetmpreg
= RTC_Bcd2ToByte(sDate
->Date
);
694 assert_param(IS_RTC_DATE(datetmpreg
));
696 datetmpreg
= ((((uint32_t)sDate
->Year
) << 16U) | \
697 (((uint32_t)sDate
->Month
) << 8U) | \
698 ((uint32_t)sDate
->Date
) | \
699 (((uint32_t)sDate
->WeekDay
) << 13U));
702 /* Disable the write protection for RTC registers */
703 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
705 /* Set Initialization mode */
706 if(RTC_EnterInitMode(hrtc
) != HAL_OK
)
708 /* Enable the write protection for RTC registers */
709 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
712 hrtc
->State
= HAL_RTC_STATE_ERROR
;
714 /* Process Unlocked */
721 /* Set the RTC_DR register */
722 hrtc
->Instance
->DR
= (uint32_t)(datetmpreg
& RTC_DR_RESERVED_MASK
);
724 /* Exit Initialization mode */
725 hrtc
->Instance
->ISR
&= ((uint32_t)~RTC_ISR_INIT
);
727 /* If CR_BYPSHAD bit = 0U, wait for synchro else this check is not needed */
728 if((hrtc
->Instance
->CR
& RTC_CR_BYPSHAD
) == RESET
)
730 if(HAL_RTC_WaitForSynchro(hrtc
) != HAL_OK
)
732 /* Enable the write protection for RTC registers */
733 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
735 hrtc
->State
= HAL_RTC_STATE_ERROR
;
737 /* Process Unlocked */
744 /* Enable the write protection for RTC registers */
745 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
747 hrtc
->State
= HAL_RTC_STATE_READY
;
749 /* Process Unlocked */
757 * @brief Get RTC current date.
758 * @param hrtc RTC handle
759 * @param sDate Pointer to Date structure
760 * @param Format Specifies the format of the entered parameters.
761 * This parameter can be one of the following values:
762 * @arg RTC_FORMAT_BIN : Binary data format
763 * @arg RTC_FORMAT_BCD : BCD data format
766 HAL_StatusTypeDef
HAL_RTC_GetDate(RTC_HandleTypeDef
*hrtc
, RTC_DateTypeDef
*sDate
, uint32_t Format
)
768 uint32_t datetmpreg
= 0U;
770 /* Check the parameters */
771 assert_param(IS_RTC_FORMAT(Format
));
773 /* Get the DR register */
774 datetmpreg
= (uint32_t)(hrtc
->Instance
->DR
& RTC_DR_RESERVED_MASK
);
776 /* Fill the structure fields with the read parameters */
777 sDate
->Year
= (uint8_t)((datetmpreg
& (RTC_DR_YT
| RTC_DR_YU
)) >> 16U);
778 sDate
->Month
= (uint8_t)((datetmpreg
& (RTC_DR_MT
| RTC_DR_MU
)) >> 8U);
779 sDate
->Date
= (uint8_t)(datetmpreg
& (RTC_DR_DT
| RTC_DR_DU
));
780 sDate
->WeekDay
= (uint8_t)((datetmpreg
& (RTC_DR_WDU
)) >> 13U);
782 /* Check the input parameters format */
783 if(Format
== RTC_FORMAT_BIN
)
785 /* Convert the date structure parameters to Binary format */
786 sDate
->Year
= (uint8_t)RTC_Bcd2ToByte(sDate
->Year
);
787 sDate
->Month
= (uint8_t)RTC_Bcd2ToByte(sDate
->Month
);
788 sDate
->Date
= (uint8_t)RTC_Bcd2ToByte(sDate
->Date
);
797 /** @addtogroup RTC_Exported_Functions_Group3
798 * @brief RTC Alarm functions
801 ===============================================================================
802 ##### RTC Alarm functions #####
803 ===============================================================================
805 [..] This section provides functions allowing to configure Alarm feature
811 * @brief Set the specified RTC Alarm.
812 * @param hrtc RTC handle
813 * @param sAlarm Pointer to Alarm structure
814 * @param Format Specifies the format of the entered parameters.
815 * This parameter can be one of the following values:
816 * @arg RTC_FORMAT_BIN: Binary data format
817 * @arg RTC_FORMAT_BCD: BCD data format
820 HAL_StatusTypeDef
HAL_RTC_SetAlarm(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Format
)
822 uint32_t tickstart
= 0U;
823 uint32_t tmpreg
= 0U, subsecondtmpreg
= 0U;
825 /* Check the parameters */
826 assert_param(IS_RTC_FORMAT(Format
));
827 assert_param(IS_RTC_ALARM(sAlarm
->Alarm
));
828 assert_param(IS_RTC_ALARM_MASK(sAlarm
->AlarmMask
));
829 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm
->AlarmDateWeekDaySel
));
830 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm
->AlarmTime
.SubSeconds
));
831 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm
->AlarmSubSecondMask
));
836 hrtc
->State
= HAL_RTC_STATE_BUSY
;
838 if(Format
== RTC_FORMAT_BIN
)
840 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
842 assert_param(IS_RTC_HOUR12(sAlarm
->AlarmTime
.Hours
));
843 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
847 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
848 assert_param(IS_RTC_HOUR24(sAlarm
->AlarmTime
.Hours
));
850 assert_param(IS_RTC_MINUTES(sAlarm
->AlarmTime
.Minutes
));
851 assert_param(IS_RTC_SECONDS(sAlarm
->AlarmTime
.Seconds
));
853 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
855 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm
->AlarmDateWeekDay
));
859 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm
->AlarmDateWeekDay
));
862 tmpreg
= (((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Hours
) << 16U) | \
863 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
864 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Seconds
)) | \
865 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
866 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmDateWeekDay
) << 24U) | \
867 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
868 ((uint32_t)sAlarm
->AlarmMask
));
872 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
874 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
875 assert_param(IS_RTC_HOUR12(tmpreg
));
876 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
880 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
881 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
)));
884 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
)));
885 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
)));
887 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
889 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
890 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg
));
894 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
895 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg
));
898 tmpreg
= (((uint32_t)(sAlarm
->AlarmTime
.Hours
) << 16U) | \
899 ((uint32_t)(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
900 ((uint32_t) sAlarm
->AlarmTime
.Seconds
) | \
901 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
902 ((uint32_t)(sAlarm
->AlarmDateWeekDay
) << 24U) | \
903 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
904 ((uint32_t)sAlarm
->AlarmMask
));
907 /* Configure the Alarm A or Alarm B Sub Second registers */
908 subsecondtmpreg
= (uint32_t)((uint32_t)(sAlarm
->AlarmTime
.SubSeconds
) | (uint32_t)(sAlarm
->AlarmSubSecondMask
));
910 /* Disable the write protection for RTC registers */
911 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
913 /* Configure the Alarm register */
914 if(sAlarm
->Alarm
== RTC_ALARM_A
)
916 /* Disable the Alarm A interrupt */
917 __HAL_RTC_ALARMA_DISABLE(hrtc
);
919 /* In case of interrupt mode is used, the interrupt source must disabled */
920 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRA
);
922 tickstart
= HAL_GetTick();
923 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
924 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
926 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
928 /* Enable the write protection for RTC registers */
929 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
931 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
933 /* Process Unlocked */
940 hrtc
->Instance
->ALRMAR
= (uint32_t)tmpreg
;
941 /* Configure the Alarm A Sub Second register */
942 hrtc
->Instance
->ALRMASSR
= subsecondtmpreg
;
943 /* Configure the Alarm state: Enable Alarm */
944 __HAL_RTC_ALARMA_ENABLE(hrtc
);
948 /* Disable the Alarm B interrupt */
949 __HAL_RTC_ALARMB_DISABLE(hrtc
);
951 /* In case of interrupt mode is used, the interrupt source must disabled */
952 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRB
);
954 tickstart
= HAL_GetTick();
955 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
956 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
958 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
960 /* Enable the write protection for RTC registers */
961 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
963 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
965 /* Process Unlocked */
972 hrtc
->Instance
->ALRMBR
= (uint32_t)tmpreg
;
973 /* Configure the Alarm B Sub Second register */
974 hrtc
->Instance
->ALRMBSSR
= subsecondtmpreg
;
975 /* Configure the Alarm state: Enable Alarm */
976 __HAL_RTC_ALARMB_ENABLE(hrtc
);
979 /* Enable the write protection for RTC registers */
980 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
982 /* Change RTC state */
983 hrtc
->State
= HAL_RTC_STATE_READY
;
985 /* Process Unlocked */
992 * @brief Set the specified RTC Alarm with Interrupt.
993 * @param hrtc RTC handle
994 * @param sAlarm Pointer to Alarm structure
995 * @param Format Specifies the format of the entered parameters.
996 * This parameter can be one of the following values:
997 * @arg RTC_FORMAT_BIN: Binary data format
998 * @arg RTC_FORMAT_BCD: BCD data format
999 * @note The Alarm register can only be written when the corresponding Alarm
1000 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
1001 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1002 * @retval HAL status
1004 HAL_StatusTypeDef
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Format
)
1006 uint32_t tickstart
= 0U;
1007 uint32_t tmpreg
= 0U, subsecondtmpreg
= 0U;
1009 /* Check the parameters */
1010 assert_param(IS_RTC_FORMAT(Format
));
1011 assert_param(IS_RTC_ALARM(sAlarm
->Alarm
));
1012 assert_param(IS_RTC_ALARM_MASK(sAlarm
->AlarmMask
));
1013 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm
->AlarmDateWeekDaySel
));
1014 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm
->AlarmTime
.SubSeconds
));
1015 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm
->AlarmSubSecondMask
));
1017 /* Process Locked */
1020 hrtc
->State
= HAL_RTC_STATE_BUSY
;
1022 if(Format
== RTC_FORMAT_BIN
)
1024 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
1026 assert_param(IS_RTC_HOUR12(sAlarm
->AlarmTime
.Hours
));
1027 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
1031 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
1032 assert_param(IS_RTC_HOUR24(sAlarm
->AlarmTime
.Hours
));
1034 assert_param(IS_RTC_MINUTES(sAlarm
->AlarmTime
.Minutes
));
1035 assert_param(IS_RTC_SECONDS(sAlarm
->AlarmTime
.Seconds
));
1037 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
1039 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm
->AlarmDateWeekDay
));
1043 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm
->AlarmDateWeekDay
));
1045 tmpreg
= (((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Hours
) << 16U) | \
1046 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
1047 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmTime
.Seconds
)) | \
1048 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
1049 ((uint32_t)RTC_ByteToBcd2(sAlarm
->AlarmDateWeekDay
) << 24U) | \
1050 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
1051 ((uint32_t)sAlarm
->AlarmMask
));
1055 if((hrtc
->Instance
->CR
& RTC_CR_FMT
) != (uint32_t)RESET
)
1057 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
1058 assert_param(IS_RTC_HOUR12(tmpreg
));
1059 assert_param(IS_RTC_HOURFORMAT12(sAlarm
->AlarmTime
.TimeFormat
));
1063 sAlarm
->AlarmTime
.TimeFormat
= 0x00U
;
1064 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
)));
1067 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
)));
1068 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
)));
1070 if(sAlarm
->AlarmDateWeekDaySel
== RTC_ALARMDATEWEEKDAYSEL_DATE
)
1072 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1073 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg
));
1077 tmpreg
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1078 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg
));
1080 tmpreg
= (((uint32_t)(sAlarm
->AlarmTime
.Hours
) << 16U) | \
1081 ((uint32_t)(sAlarm
->AlarmTime
.Minutes
) << 8U) | \
1082 ((uint32_t) sAlarm
->AlarmTime
.Seconds
) | \
1083 ((uint32_t)(sAlarm
->AlarmTime
.TimeFormat
) << 16U) | \
1084 ((uint32_t)(sAlarm
->AlarmDateWeekDay
) << 24U) | \
1085 ((uint32_t)sAlarm
->AlarmDateWeekDaySel
) | \
1086 ((uint32_t)sAlarm
->AlarmMask
));
1088 /* Configure the Alarm A or Alarm B Sub Second registers */
1089 subsecondtmpreg
= (uint32_t)((uint32_t)(sAlarm
->AlarmTime
.SubSeconds
) | (uint32_t)(sAlarm
->AlarmSubSecondMask
));
1091 /* Disable the write protection for RTC registers */
1092 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
1094 /* Configure the Alarm register */
1095 if(sAlarm
->Alarm
== RTC_ALARM_A
)
1097 /* Disable the Alarm A interrupt */
1098 __HAL_RTC_ALARMA_DISABLE(hrtc
);
1100 /* Clear flag alarm A */
1101 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRAF
);
1103 tickstart
= HAL_GetTick();
1104 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1105 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
1107 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
1109 /* Enable the write protection for RTC registers */
1110 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1112 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1114 /* Process Unlocked */
1121 hrtc
->Instance
->ALRMAR
= (uint32_t)tmpreg
;
1122 /* Configure the Alarm A Sub Second register */
1123 hrtc
->Instance
->ALRMASSR
= subsecondtmpreg
;
1124 /* Configure the Alarm state: Enable Alarm */
1125 __HAL_RTC_ALARMA_ENABLE(hrtc
);
1126 /* Configure the Alarm interrupt */
1127 __HAL_RTC_ALARM_ENABLE_IT(hrtc
,RTC_IT_ALRA
);
1131 /* Disable the Alarm B interrupt */
1132 __HAL_RTC_ALARMB_DISABLE(hrtc
);
1134 /* Clear flag alarm B */
1135 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRBF
);
1137 tickstart
= HAL_GetTick();
1138 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1139 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
1141 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
1143 /* Enable the write protection for RTC registers */
1144 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1146 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1148 /* Process Unlocked */
1155 hrtc
->Instance
->ALRMBR
= (uint32_t)tmpreg
;
1156 /* Configure the Alarm B Sub Second register */
1157 hrtc
->Instance
->ALRMBSSR
= subsecondtmpreg
;
1158 /* Configure the Alarm state: Enable Alarm */
1159 __HAL_RTC_ALARMB_ENABLE(hrtc
);
1160 /* Configure the Alarm interrupt */
1161 __HAL_RTC_ALARM_ENABLE_IT(hrtc
, RTC_IT_ALRB
);
1164 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1165 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1167 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1169 /* Enable the write protection for RTC registers */
1170 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1172 hrtc
->State
= HAL_RTC_STATE_READY
;
1174 /* Process Unlocked */
1181 * @brief Deactivate the specified RTC Alarm.
1182 * @param hrtc RTC handle
1183 * @param Alarm Specifies the Alarm.
1184 * This parameter can be one of the following values:
1185 * @arg RTC_ALARM_A : AlarmA
1186 * @arg RTC_ALARM_B : AlarmB
1187 * @retval HAL status
1189 HAL_StatusTypeDef
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef
*hrtc
, uint32_t Alarm
)
1191 uint32_t tickstart
= 0U;
1193 /* Check the parameters */
1194 assert_param(IS_RTC_ALARM(Alarm
));
1196 /* Process Locked */
1199 hrtc
->State
= HAL_RTC_STATE_BUSY
;
1201 /* Disable the write protection for RTC registers */
1202 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc
);
1204 if(Alarm
== RTC_ALARM_A
)
1207 __HAL_RTC_ALARMA_DISABLE(hrtc
);
1209 /* In case of interrupt mode is used, the interrupt source must disabled */
1210 __HAL_RTC_ALARM_DISABLE_IT(hrtc
, RTC_IT_ALRA
);
1212 tickstart
= HAL_GetTick();
1214 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1215 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAWF
) == RESET
)
1217 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
1219 /* Enable the write protection for RTC registers */
1220 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1222 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1224 /* Process Unlocked */
1234 __HAL_RTC_ALARMB_DISABLE(hrtc
);
1236 /* In case of interrupt mode is used, the interrupt source must disabled */
1237 __HAL_RTC_ALARM_DISABLE_IT(hrtc
,RTC_IT_ALRB
);
1239 tickstart
= HAL_GetTick();
1241 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1242 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBWF
) == RESET
)
1244 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
1246 /* Enable the write protection for RTC registers */
1247 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1249 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1251 /* Process Unlocked */
1258 /* Enable the write protection for RTC registers */
1259 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc
);
1261 hrtc
->State
= HAL_RTC_STATE_READY
;
1263 /* Process Unlocked */
1270 * @brief Get the RTC Alarm value and masks.
1271 * @param hrtc RTC handle
1272 * @param sAlarm Pointer to Date structure
1273 * @param Alarm Specifies the Alarm.
1274 * This parameter can be one of the following values:
1275 * @arg RTC_ALARM_A: AlarmA
1276 * @arg RTC_ALARM_B: AlarmB
1277 * @param Format Specifies the format of the entered parameters.
1278 * This parameter can be one of the following values:
1279 * @arg RTC_FORMAT_BIN: Binary data format
1280 * @arg RTC_FORMAT_BCD: BCD data format
1281 * @retval HAL status
1283 HAL_StatusTypeDef
HAL_RTC_GetAlarm(RTC_HandleTypeDef
*hrtc
, RTC_AlarmTypeDef
*sAlarm
, uint32_t Alarm
, uint32_t Format
)
1285 uint32_t tmpreg
= 0U, subsecondtmpreg
= 0U;
1287 /* Check the parameters */
1288 assert_param(IS_RTC_FORMAT(Format
));
1289 assert_param(IS_RTC_ALARM(Alarm
));
1291 if(Alarm
== RTC_ALARM_A
)
1294 sAlarm
->Alarm
= RTC_ALARM_A
;
1296 tmpreg
= (uint32_t)(hrtc
->Instance
->ALRMAR
);
1297 subsecondtmpreg
= (uint32_t)((hrtc
->Instance
->ALRMASSR
) & RTC_ALRMASSR_SS
);
1301 sAlarm
->Alarm
= RTC_ALARM_B
;
1303 tmpreg
= (uint32_t)(hrtc
->Instance
->ALRMBR
);
1304 subsecondtmpreg
= (uint32_t)((hrtc
->Instance
->ALRMBSSR
) & RTC_ALRMBSSR_SS
);
1307 /* Fill the structure with the read parameters */
1308 sAlarm
->AlarmTime
.Hours
= (uint32_t)((tmpreg
& (RTC_ALRMAR_HT
| RTC_ALRMAR_HU
)) >> 16U);
1309 sAlarm
->AlarmTime
.Minutes
= (uint32_t)((tmpreg
& (RTC_ALRMAR_MNT
| RTC_ALRMAR_MNU
)) >> 8U);
1310 sAlarm
->AlarmTime
.Seconds
= (uint32_t)(tmpreg
& (RTC_ALRMAR_ST
| RTC_ALRMAR_SU
));
1311 sAlarm
->AlarmTime
.TimeFormat
= (uint32_t)((tmpreg
& RTC_ALRMAR_PM
) >> 16U);
1312 sAlarm
->AlarmTime
.SubSeconds
= (uint32_t) subsecondtmpreg
;
1313 sAlarm
->AlarmDateWeekDay
= (uint32_t)((tmpreg
& (RTC_ALRMAR_DT
| RTC_ALRMAR_DU
)) >> 24U);
1314 sAlarm
->AlarmDateWeekDaySel
= (uint32_t)(tmpreg
& RTC_ALRMAR_WDSEL
);
1315 sAlarm
->AlarmMask
= (uint32_t)(tmpreg
& RTC_ALARMMASK_ALL
);
1317 if(Format
== RTC_FORMAT_BIN
)
1319 sAlarm
->AlarmTime
.Hours
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Hours
);
1320 sAlarm
->AlarmTime
.Minutes
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Minutes
);
1321 sAlarm
->AlarmTime
.Seconds
= RTC_Bcd2ToByte(sAlarm
->AlarmTime
.Seconds
);
1322 sAlarm
->AlarmDateWeekDay
= RTC_Bcd2ToByte(sAlarm
->AlarmDateWeekDay
);
1329 * @brief Handle Alarm interrupt request.
1330 * @param hrtc RTC handle
1333 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef
* hrtc
)
1335 /* Get the AlarmA interrupt source enable status */
1336 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc
, RTC_IT_ALRA
) != RESET
)
1338 /* Get the pending status of the AlarmA Interrupt */
1339 if(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAF
) != RESET
)
1341 /* AlarmA callback */
1342 HAL_RTC_AlarmAEventCallback(hrtc
);
1344 /* Clear the AlarmA interrupt pending bit */
1345 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
,RTC_FLAG_ALRAF
);
1349 /* Get the AlarmB interrupt source enable status */
1350 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc
, RTC_IT_ALRB
) != RESET
)
1352 /* Get the pending status of the AlarmB Interrupt */
1353 if(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRBF
) != RESET
)
1355 /* AlarmB callback */
1356 HAL_RTCEx_AlarmBEventCallback(hrtc
);
1358 /* Clear the AlarmB interrupt pending bit */
1359 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
,RTC_FLAG_ALRBF
);
1363 /* Clear the EXTI's line Flag for RTC Alarm */
1364 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1366 /* Change RTC state */
1367 hrtc
->State
= HAL_RTC_STATE_READY
;
1371 * @brief Alarm A callback.
1372 * @param hrtc RTC handle
1375 __weak
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef
*hrtc
)
1377 /* Prevent unused argument(s) compilation warning */
1380 /* NOTE : This function should not be modified, when the callback is needed,
1381 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1386 * @brief Handle AlarmA Polling request.
1387 * @param hrtc RTC handle
1388 * @param Timeout Timeout duration
1389 * @retval HAL status
1391 HAL_StatusTypeDef
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef
*hrtc
, uint32_t Timeout
)
1394 uint32_t tickstart
= HAL_GetTick();
1396 while(__HAL_RTC_ALARM_GET_FLAG(hrtc
, RTC_FLAG_ALRAF
) == RESET
)
1398 if(Timeout
!= HAL_MAX_DELAY
)
1400 if((Timeout
== 0U) || ((HAL_GetTick()-tickstart
) > Timeout
))
1402 hrtc
->State
= HAL_RTC_STATE_TIMEOUT
;
1408 /* Clear the Alarm interrupt pending bit */
1409 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc
, RTC_FLAG_ALRAF
);
1411 /* Change RTC state */
1412 hrtc
->State
= HAL_RTC_STATE_READY
;
1421 /** @@addtogroup RTC_Exported_Functions_Group4 Peripheral Control functions
1422 * @brief Peripheral Control functions
1425 ===============================================================================
1426 ##### Peripheral Control functions #####
1427 ===============================================================================
1429 This subsection provides functions allowing to
1430 (+) Wait for RTC Time and Date Synchronization
1437 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1438 * synchronized with RTC APB clock.
1439 * @note The RTC Resynchronization mode is write protected, use the
1440 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1441 * @note To read the calendar through the shadow registers after Calendar
1442 * initialization, calendar update or after wakeup from low power modes
1443 * the software must first clear the RSF flag.
1444 * The software must then wait until it is set again before reading
1445 * the calendar, which means that the calendar registers have been
1446 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1447 * @param hrtc RTC handle
1448 * @retval HAL status
1450 HAL_StatusTypeDef
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef
* hrtc
)
1452 uint32_t tickstart
= 0U;
1454 /* Clear RSF flag */
1455 hrtc
->Instance
->ISR
&= (uint32_t)RTC_RSF_MASK
;
1457 tickstart
= HAL_GetTick();
1459 /* Wait the registers to be synchronised */
1460 while((hrtc
->Instance
->ISR
& RTC_ISR_RSF
) == (uint32_t)RESET
)
1462 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
1475 /** @@addtogroup RTC_Exported_Functions_Group5 Peripheral State functions
1476 * @brief Peripheral State functions
1479 ===============================================================================
1480 ##### Peripheral State functions #####
1481 ===============================================================================
1483 This subsection provides functions allowing to
1490 * @brief Return the RTC handle state.
1491 * @param hrtc RTC handle
1494 HAL_RTCStateTypeDef
HAL_RTC_GetState(RTC_HandleTypeDef
* hrtc
)
1496 /* Return RTC handle state */
1508 /** @@addtogroup RTC_Private_Functions RTC Private Functions
1513 * @brief Enter the RTC Initialization mode.
1514 * @note The RTC Initialization mode is write protected, use the
1515 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1516 * @param hrtc RTC handle
1517 * @retval An ErrorStatus enumeration value:
1518 * - HAL_OK : RTC is in Init mode
1519 * - HAL_TIMEOUT : RTC is not in Init mode and in Timeout
1521 HAL_StatusTypeDef
RTC_EnterInitMode(RTC_HandleTypeDef
* hrtc
)
1523 uint32_t tickstart
= 0U;
1525 /* Check if the Initialization mode is set */
1526 if((hrtc
->Instance
->ISR
& RTC_ISR_INITF
) == (uint32_t)RESET
)
1528 /* Set the Initialization mode */
1529 hrtc
->Instance
->ISR
= (uint32_t)RTC_INIT_MASK
;
1531 tickstart
= HAL_GetTick();
1532 /* Wait till RTC is in INIT state and if Time out is reached exit */
1533 while((hrtc
->Instance
->ISR
& RTC_ISR_INITF
) == (uint32_t)RESET
)
1535 if((HAL_GetTick()-tickstart
) > RTC_TIMEOUT_VALUE
)
1547 * @brief Convert a 2 digit decimal to BCD format.
1548 * @param Value Byte to be converted
1549 * @retval Converted byte
1551 uint8_t RTC_ByteToBcd2(uint8_t Value
)
1553 uint32_t bcdhigh
= 0U;
1561 return ((uint8_t)(bcdhigh
<< 4U) | Value
);
1565 * @brief Convert from 2 digit BCD to Binary.
1566 * @param Value BCD value to be converted
1567 * @retval Converted word
1569 uint8_t RTC_Bcd2ToByte(uint8_t Value
)
1572 tmp
= ((uint8_t)(Value
& (uint8_t)0xF0U
) >> (uint8_t)0x4U
) * 10U;
1573 return (tmp
+ (Value
& (uint8_t)0x0FU
));
1579 #endif /* HAL_RTC_MODULE_ENABLED */
1590 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/