2 ******************************************************************************
3 * @file stm32l0xx_hal_rtc.c
4 * @author MCD Application Team
6 * @date 06-February-2015
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/de-initialization functions
11 * + I/O operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ==============================================================================
17 ##### Backup Domain Operating Condition #####
18 ==============================================================================
19 [..] As long as the supply voltage remains in the operating range,
20 the RTC never stops, regardless of the device status (Run mode,
21 low power modes or under reset).
23 ##### Backup Domain Reset #####
24 ==================================================================
25 [..] The backup domain reset sets all RTC registers and the RCC_CSR register
26 to their reset values.
27 [..] A backup domain reset is generated when one of the following events occurs:
28 (+) Software reset, triggered by setting the RTCRST bit in the
29 RCC Control Status register (RCC_CSR).
30 (+) Power reset (BOR/POR/PDR).
32 ##### Backup Domain Access #####
33 ==================================================================
34 [..] After reset, the backup domain (RTC registers and RTC backup data registers)
35 is protected against possible unwanted write accesses.
36 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
37 (+) Enable the Power Controller (PWR) APB1 interface clock using the
38 __HAL_RCC_PWR_CLK_ENABLE() function.
39 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
40 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
41 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
44 ##### How to use RTC Driver #####
45 ===================================================================
47 (+) Enable the RTC domain access (see description in the section above).
48 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
49 format using the HAL_RTC_Init() function.
51 *** Time and Date configuration ***
52 ===================================
54 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
55 and HAL_RTC_SetDate() functions.
56 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
58 *** Alarm configuration ***
59 ===========================
61 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
62 You can also configure the RTC Alarm with interrupt mode using the
63 HAL_RTC_SetAlarm_IT() function.
64 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
66 *** RTC Wakeup configuration ***
67 ================================
69 (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
70 function. You can also configure the RTC Wakeup timer with interrupt mode
71 using the HAL_RTC_SetWakeUpTimer_IT() function.
72 (+) To read the RTC WakeUp Counter register, use the HAL_RTC_GetWakeUpTimer()
75 *** Outputs configuration ***
76 =============================
77 [..] The RTC has 2 different outputs:
78 (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
80 To output the selected RTC signal, use the HAL_RTC_Init() function.
81 (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
82 To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
83 (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB14) managed on
85 (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
86 automatically configured in output alternate function.
88 *** Smooth digital Calibration configuration ***
89 ================================================
91 (+) Configure the RTC Original Digital Calibration Value and the corresponding
92 calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
95 *** TimeStamp configuration ***
96 ===============================
98 (+) Enables the RTC TimeStamp using the HAL_RTC_SetTimeStamp() function.
99 You can also configure the RTC TimeStamp with interrupt mode using the
100 HAL_RTC_SetTimeStamp_IT() function.
101 (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
104 *** Tamper configuration ***
105 ============================
107 (+) Enable the RTC Tamper and Configure the Tamper filter count, trigger Edge
108 or Level according to the Tamper filter (if equal to 0 Edge else Level)
109 value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
110 Pull-UP using the HAL_RTC_SetTamper() function. You can configure RTC Tamper
111 with interrupt mode using HAL_RTC_SetTamper_IT() function.
112 (+) The default configuration of the Tamper erases the backup registers. To avoid
113 erase, enable the NoErase field on the RTC_TAMPCR register.
115 *** Backup Data Registers configuration ***
116 ===========================================
118 (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
120 (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
122 (+) The backup registers are reset when a tamper detection event occurs
125 ##### RTC and low power modes #####
126 ==================================================================
127 [..] The MCU can be woken up from a low power mode by an RTC alternate
129 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
130 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
131 These RTC alternate functions can wake up the system from the Stop and
132 Standby low power modes.
133 [..] The system can also wake up from low power modes without depending
134 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
135 or the RTC wakeup events.
136 [..] The RTC provides a programmable time base for waking up from the
137 Stop or Standby mode at regular intervals.
138 Wakeup from STOP and Standby modes is possible only when the RTC clock source
142 ******************************************************************************
145 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
147 * Redistribution and use in source and binary forms, with or without modification,
148 * are permitted provided that the following conditions are met:
149 * 1. Redistributions of source code must retain the above copyright notice,
150 * this list of conditions and the following disclaimer.
151 * 2. Redistributions in binary form must reproduce the above copyright notice,
152 * this list of conditions and the following disclaimer in the documentation
153 * and/or other materials provided with the distribution.
154 * 3. Neither the name of STMicroelectronics nor the names of its contributors
155 * may be used to endorse or promote products derived from this software
156 * without specific prior written permission.
158 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
159 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
161 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
162 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
163 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
164 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
165 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
166 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
167 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
169 ******************************************************************************
172 /* Includes ------------------------------------------------------------------*/
173 #include "stm32l0xx_hal.h"
175 /** @addtogroup STM32L0xx_HAL_Driver
180 * @brief RTC HAL module driver
184 #ifdef HAL_RTC_MODULE_ENABLED
186 /* Private typedef -----------------------------------------------------------*/
187 /* Private define ------------------------------------------------------------*/
188 /* Private macro -------------------------------------------------------------*/
189 /* Private variables ---------------------------------------------------------*/
190 /* Private function prototypes -----------------------------------------------*/
191 /* Exported functions --------------------------------------------------------*/
193 /** @addtogroup RTC_Exported_Functions
197 /** @addtogroup RTC_Exported_Functions_Group1
198 * @brief Initialization and Configuration functions
201 ===============================================================================
202 ##### Initialization and de-initialization functions #####
203 ===============================================================================
204 [..] This section provides functions allowing to initialize and configure the
205 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
206 RTC registers Write protection, enter and exit the RTC initialization mode,
207 RTC registers synchronization check and reference clock detection enable.
208 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
209 It is split into 2 programmable prescalers to minimize power consumption.
210 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
211 (++) When both prescalers are used, it is recommended to configure the
212 asynchronous prescaler to a high value to minimize power consumption.
213 (#) All RTC registers are Write protected. Writing to the RTC registers
214 is enabled by writing a key into the Write Protection register, RTC_WPR.
215 (#) To configure the RTC Calendar, user application should enter
216 initialization mode. In this mode, the calendar counter is stopped
217 and its value can be updated. When the initialization sequence is
218 complete, the calendar restarts counting after 4 RTCCLK cycles.
219 (#) To read the calendar through the shadow registers after Calendar
220 initialization, calendar update or after wakeup from low power modes
221 the software must first clear the RSF flag. The software must then
222 wait until it is set again before reading the calendar, which means
223 that the calendar registers have been correctly copied into the
224 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
225 implements the above software sequence (RSF clear and RSF check).
232 * @brief Initializes the RTC peripheral
233 * @param hrtc: RTC handle
236 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
238 /* Check the RTC peripheral state */
244 /* Check the parameters */
245 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
246 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
247 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
248 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
249 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
250 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
251 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
252 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
254 if(hrtc->State == HAL_RTC_STATE_RESET)
256 /* Initialize RTC MSP */
257 HAL_RTC_MspInit(hrtc);
261 hrtc->State = HAL_RTC_STATE_BUSY;
263 /* Disable the write protection for RTC registers */
264 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
266 /* Set Initialization mode */
267 if(RTC_EnterInitMode(hrtc) != HAL_OK)
269 /* Enable the write protection for RTC registers */
270 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
273 hrtc->State = HAL_RTC_STATE_ERROR;
279 /* Clear RTC_CR FMT, OSEL and POL Bits */
280 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
281 /* Set RTC_CR register */
282 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
284 /* Configure the RTC PRER */
285 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
286 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
288 /* Exit Initialization mode */
289 hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
291 hrtc->Instance->OR &= (uint32_t)~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
292 hrtc->Instance->OR |= (uint32_t)(hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
294 /* Enable the write protection for RTC registers */
295 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
298 hrtc->State = HAL_RTC_STATE_READY;
305 * @brief DeInitializes the RTC peripheral
306 * @param hrtc: RTC handle
307 * @note This function doesn't reset the RTC Backup Data registers.
310 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
312 uint32_t tickstart = 0;
314 /* Check the parameters */
315 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
318 hrtc->State = HAL_RTC_STATE_BUSY;
320 /* Disable the write protection for RTC registers */
321 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
323 /* Set Initialization mode */
324 if(RTC_EnterInitMode(hrtc) != HAL_OK)
326 /* Enable the write protection for RTC registers */
327 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
330 hrtc->State = HAL_RTC_STATE_ERROR;
336 /* Reset TR, DR and CR registers */
337 hrtc->Instance->TR = (uint32_t)0x00000000;
338 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
339 /* Reset All CR bits except CR[2:0] */
340 hrtc->Instance->CR &= RTC_CR_WUCKSEL;
342 tickstart = HAL_GetTick();
344 /* Wait till WUTWF flag is set and if Time out is reached exit */
345 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
347 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
349 /* Enable the write protection for RTC registers */
350 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
353 hrtc->State = HAL_RTC_STATE_TIMEOUT;
359 /* Reset all RTC CR register bits */
360 hrtc->Instance->CR &= (uint32_t)0x00000000;
361 hrtc->Instance->WUTR = RTC_WUTR_WUT;
362 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FF));
363 hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
364 hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
365 hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
366 hrtc->Instance->CALR = (uint32_t)0x00000000;
367 hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
368 hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
370 /* Reset ISR register and exit initialization mode */
371 hrtc->Instance->ISR = (uint32_t)0x00000000;
373 /* Reset Tamper configuration register */
374 hrtc->Instance->TAMPCR = 0x00000000;
376 /* Reset Option register */
377 hrtc->Instance->OR = 0x00000000;
379 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
380 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
382 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
384 /* Enable the write protection for RTC registers */
385 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
387 hrtc->State = HAL_RTC_STATE_ERROR;
394 /* Enable the write protection for RTC registers */
395 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
397 /* De-Initialize RTC MSP */
398 HAL_RTC_MspDeInit(hrtc);
400 hrtc->State = HAL_RTC_STATE_RESET;
409 * @brief Initializes the RTC MSP.
410 * @param hrtc: RTC handle
413 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
415 /* NOTE : This function Should not be modified, when the callback is needed,
416 the HAL_RTC_MspInit could be implenetd in the user file
421 * @brief DeInitializes the RTC MSP.
422 * @param hrtc: RTC handle
425 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
427 /* NOTE : This function Should not be modified, when the callback is needed,
428 the HAL_RTC_MspDeInit could be implenetd in the user file
436 /** @addtogroup RTC_Exported_Functions_Group2
437 * @brief RTC Time and Date functions
440 ===============================================================================
441 ##### RTC Time and Date functions #####
442 ===============================================================================
444 [..] This section provide functions allowing to configure Time and Date features
451 * @brief Sets RTC current time.
452 * @param hrtc: RTC handle
453 * @param sTime: Pointer to Time structure
454 * @param Format: Specifies the format of the entered parameters.
455 * This parameter can be one of the following values:
456 * @arg RTC_FORMAT_BIN: Binary data format
457 * @arg RTC_FORMAT_BCD
461 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
465 /* Check the parameters */
466 assert_param(IS_RTC_FORMAT(Format));
467 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
468 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
473 hrtc->State = HAL_RTC_STATE_BUSY;
475 if(Format == RTC_FORMAT_BIN)
477 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
479 assert_param(IS_RTC_HOUR12(sTime->Hours));
480 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
484 sTime->TimeFormat = 0x00;
485 assert_param(IS_RTC_HOUR24(sTime->Hours));
487 assert_param(IS_RTC_MINUTES(sTime->Minutes));
488 assert_param(IS_RTC_SECONDS(sTime->Seconds));
490 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
491 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
492 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
493 (((uint32_t)sTime->TimeFormat) << 16));
497 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
499 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
500 assert_param(IS_RTC_HOUR12(tmpreg));
501 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
505 sTime->TimeFormat = 0x00;
506 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
508 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
509 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
510 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
511 ((uint32_t)(sTime->Minutes) << 8) | \
512 ((uint32_t)sTime->Seconds) | \
513 ((uint32_t)(sTime->TimeFormat) << 16));
516 /* Disable the write protection for RTC registers */
517 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
519 /* Set Initialization mode */
520 if(RTC_EnterInitMode(hrtc) != HAL_OK)
522 /* Enable the write protection for RTC registers */
523 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
526 hrtc->State = HAL_RTC_STATE_ERROR;
528 /* Process Unlocked */
535 /* Set the RTC_TR register */
536 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
538 /* Clear the bits to be configured */
539 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BCK);
541 /* Configure the RTC_CR register */
542 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
544 /* Exit Initialization mode */
545 hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
547 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
548 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
550 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
552 /* Enable the write protection for RTC registers */
553 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
555 hrtc->State = HAL_RTC_STATE_ERROR;
557 /* Process Unlocked */
564 /* Enable the write protection for RTC registers */
565 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
567 hrtc->State = HAL_RTC_STATE_READY;
576 * @brief Gets RTC current time.
577 * @param hrtc: RTC handle
578 * @param sTime: Pointer to Time structure
579 * @param Format: Specifies the format of the entered parameters.
580 * This parameter can be one of the following values:
581 * @arg RTC_FORMAT_BIN: Binary data format
582 * @arg RTC_FORMAT_BCD
584 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values in the
585 * higher-order calendar shadow registers.
586 * Reading RTC current time locks the values in calendar shadow registers until current
587 * date is read to ensure consistency between the time and date values.
590 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
594 /* Check the parameters */
595 assert_param(IS_RTC_FORMAT(Format));
597 /* Get subseconds values from the correspondent registers*/
598 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
600 /* Get the TR register */
601 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
603 /* Fill the structure fields with the read parameters */
604 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
605 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
606 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
607 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
609 /* Check the input parameters format */
610 if(Format == RTC_FORMAT_BIN)
612 /* Convert the time structure parameters to Binary format */
613 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
614 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
615 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
622 * @brief Sets RTC current date.
623 * @param hrtc: RTC handle
624 * @param sDate: Pointer to date structure
625 * @param Format: specifies the format of the entered parameters.
626 * This parameter can be one of the following values:
627 * @arg RTC_FORMAT_BIN: Binary data format
628 * @arg RTC_FORMAT_BCD
632 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
634 uint32_t datetmpreg = 0;
636 /* Check the parameters */
637 assert_param(IS_RTC_FORMAT(Format));
642 hrtc->State = HAL_RTC_STATE_BUSY;
644 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
646 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
649 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
651 if(Format == RTC_FORMAT_BIN)
653 assert_param(IS_RTC_YEAR(sDate->Year));
654 assert_param(IS_RTC_MONTH(sDate->Month));
655 assert_param(IS_RTC_DATE(sDate->Date));
657 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
658 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
659 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
660 ((uint32_t)sDate->WeekDay << 13));
664 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
665 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
666 assert_param(IS_RTC_MONTH(datetmpreg));
667 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
668 assert_param(IS_RTC_DATE(datetmpreg));
670 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
671 (((uint32_t)sDate->Month) << 8) | \
672 ((uint32_t)sDate->Date) | \
673 (((uint32_t)sDate->WeekDay) << 13));
676 /* Disable the write protection for RTC registers */
677 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
679 /* Set Initialization mode */
680 if(RTC_EnterInitMode(hrtc) != HAL_OK)
682 /* Enable the write protection for RTC registers */
683 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
686 hrtc->State = HAL_RTC_STATE_ERROR;
688 /* Process Unlocked */
695 /* Set the RTC_DR register */
696 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
698 /* Exit Initialization mode */
699 hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
701 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
702 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
704 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
706 /* Enable the write protection for RTC registers */
707 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
709 hrtc->State = HAL_RTC_STATE_ERROR;
711 /* Process Unlocked */
718 /* Enable the write protection for RTC registers */
719 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
721 hrtc->State = HAL_RTC_STATE_READY ;
723 /* Process Unlocked */
731 * @brief Gets RTC current date.
732 * @param hrtc: RTC handle
733 * @param sDate: Pointer to Date structure
734 * @param Format: Specifies the format of the entered parameters.
735 * This parameter can be one of the following values:
736 * @arg RTC_FORMAT_BIN: Binary data format
737 * @arg RTC_FORMAT_BCD
741 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
743 uint32_t datetmpreg = 0;
745 /* Check the parameters */
746 assert_param(IS_RTC_FORMAT(Format));
748 /* Get the DR register */
749 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
751 /* Fill the structure fields with the read parameters */
752 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
753 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
754 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
755 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
757 /* Check the input parameters format */
758 if(Format == RTC_FORMAT_BIN)
760 /* Convert the date structure parameters to Binary format */
761 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
762 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
763 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
772 /** @addtogroup RTC_Exported_Functions_Group3
773 * @brief RTC Alarm functions
776 ===============================================================================
777 ##### RTC Alarm functions #####
778 ===============================================================================
780 [..] This section provides functions allowing to configure Alarm feature
786 * @brief Sets the specified RTC Alarm.
787 * @param hrtc: RTC handle
788 * @param sAlarm: Pointer to Alarm structure
789 * @param Format: Specifies the format of the entered parameters.
790 * This parameter can be one of the following values:
791 * @arg RTC_FORMAT_BIN: Binary data format
792 * @arg RTC_FORMAT_BCD
796 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
798 uint32_t tickstart = 0;
799 uint32_t tmpreg = 0, subsecondtmpreg = 0;
801 /* Check the parameters */
802 assert_param(IS_RTC_FORMAT(Format));
803 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
804 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
805 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
806 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
807 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
812 hrtc->State = HAL_RTC_STATE_BUSY;
814 if(Format == RTC_FORMAT_BIN)
816 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
818 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
819 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
823 sAlarm->AlarmTime.TimeFormat = 0x00;
824 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
826 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
827 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
829 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
831 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
835 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
838 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
839 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
840 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
841 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
842 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
843 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
844 ((uint32_t)sAlarm->AlarmMask));
848 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
850 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
851 assert_param(IS_RTC_HOUR12(tmpreg));
852 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
856 sAlarm->AlarmTime.TimeFormat = 0x00;
857 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
860 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
861 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
863 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
865 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
866 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
870 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
871 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
874 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
875 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
876 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
877 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
878 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
879 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
880 ((uint32_t)sAlarm->AlarmMask));
883 /* Configure the Alarm A or Alarm B Sub Second registers */
884 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
886 /* Disable the write protection for RTC registers */
887 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
889 /* Configure the Alarm register */
890 if(sAlarm->Alarm == RTC_ALARM_A)
892 /* Disable the Alarm A interrupt */
893 __HAL_RTC_ALARMA_DISABLE(hrtc);
895 /* In case of interrupt mode is used, the interrupt source must disabled */
896 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
898 tickstart = HAL_GetTick();
899 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
900 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
902 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
904 /* Enable the write protection for RTC registers */
905 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
907 hrtc->State = HAL_RTC_STATE_TIMEOUT;
909 /* Process Unlocked */
916 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
917 /* Configure the Alarm A Sub Second register */
918 hrtc->Instance->ALRMASSR = subsecondtmpreg;
919 /* Configure the Alarm state: Enable Alarm */
920 __HAL_RTC_ALARMA_ENABLE(hrtc);
924 /* Disable the Alarm B interrupt */
925 __HAL_RTC_ALARMB_DISABLE(hrtc);
927 /* In case of interrupt mode is used, the interrupt source must disabled */
928 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
930 tickstart = HAL_GetTick();
931 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
932 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
934 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
936 /* Enable the write protection for RTC registers */
937 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
939 hrtc->State = HAL_RTC_STATE_TIMEOUT;
941 /* Process Unlocked */
948 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
949 /* Configure the Alarm B Sub Second register */
950 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
951 /* Configure the Alarm state: Enable Alarm */
952 __HAL_RTC_ALARMB_ENABLE(hrtc);
955 /* Enable the write protection for RTC registers */
956 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
958 /* Change RTC state */
959 hrtc->State = HAL_RTC_STATE_READY;
961 /* Process Unlocked */
968 * @brief Sets the specified RTC Alarm with Interrupt
969 * @param hrtc: RTC handle
970 * @param sAlarm: Pointer to Alarm structure
971 * @param Format: Specifies the format of the entered parameters.
972 * This parameter can be one of the following values:
973 * @arg RTC_FORMAT_BIN: Binary data format
974 * @arg RTC_FORMAT_BCD
976 * @note The Alarm register can only be written when the corresponding Alarm
977 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
978 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
981 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
983 uint32_t tickstart = 0;
984 uint32_t tmpreg = 0, subsecondtmpreg = 0;
986 /* Check the parameters */
987 assert_param(IS_RTC_FORMAT(Format));
988 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
989 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
990 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
991 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
992 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
997 hrtc->State = HAL_RTC_STATE_BUSY;
999 if(Format == RTC_FORMAT_BIN)
1001 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1003 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1004 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1008 sAlarm->AlarmTime.TimeFormat = 0x00;
1009 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1011 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1012 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1014 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1016 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1020 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1022 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
1023 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
1024 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
1025 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1026 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
1027 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1028 ((uint32_t)sAlarm->AlarmMask));
1032 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
1034 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1035 assert_param(IS_RTC_HOUR12(tmpreg));
1036 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1040 sAlarm->AlarmTime.TimeFormat = 0x00;
1041 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1044 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1045 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1047 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1049 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1050 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1054 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1055 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1057 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
1058 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
1059 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
1060 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
1061 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
1062 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1063 ((uint32_t)sAlarm->AlarmMask));
1065 /* Configure the Alarm A or Alarm B Sub Second registers */
1066 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1068 /* Disable the write protection for RTC registers */
1069 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1071 /* Configure the Alarm register */
1072 if(sAlarm->Alarm == RTC_ALARM_A)
1074 /* Disable the Alarm A interrupt */
1075 __HAL_RTC_ALARMA_DISABLE(hrtc);
1077 /* Clear flag alarm A */
1078 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1080 tickstart = HAL_GetTick();
1081 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1082 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1084 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1086 /* Enable the write protection for RTC registers */
1087 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1089 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1091 /* Process Unlocked */
1098 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1099 /* Configure the Alarm A Sub Second register */
1100 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1101 /* Configure the Alarm state: Enable Alarm */
1102 __HAL_RTC_ALARMA_ENABLE(hrtc);
1103 /* Configure the Alarm interrupt */
1104 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1108 /* Disable the Alarm B interrupt */
1109 __HAL_RTC_ALARMB_DISABLE(hrtc);
1111 /* Clear flag alarm B */
1112 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1114 tickstart = HAL_GetTick();
1115 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1116 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1118 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1120 /* Enable the write protection for RTC registers */
1121 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1123 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1125 /* Process Unlocked */
1132 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1133 /* Configure the Alarm B Sub Second register */
1134 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1135 /* Configure the Alarm state: Enable Alarm */
1136 __HAL_RTC_ALARMB_ENABLE(hrtc);
1137 /* Configure the Alarm interrupt */
1138 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1141 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1142 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1144 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1146 /* Enable the write protection for RTC registers */
1147 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1149 hrtc->State = HAL_RTC_STATE_READY;
1151 /* Process Unlocked */
1158 * @brief Deactive the specified RTC Alarm
1159 * @param hrtc: RTC handle
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);
1189 tickstart = HAL_GetTick();
1191 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1192 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1194 if( (HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1196 /* Enable the write protection for RTC registers */
1197 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1199 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1201 /* Process Unlocked */
1211 __HAL_RTC_ALARMB_DISABLE(hrtc);
1213 /* In case of interrupt mode is used, the interrupt source must disabled */
1214 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
1216 tickstart = HAL_GetTick();
1218 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1219 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
1221 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1223 /* Enable the write protection for RTC registers */
1224 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1226 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1228 /* Process Unlocked */
1235 /* Enable the write protection for RTC registers */
1236 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1238 hrtc->State = HAL_RTC_STATE_READY;
1240 /* Process Unlocked */
1247 * @brief Gets the RTC Alarm value and masks.
1248 * @param hrtc: RTC handle
1249 * @param sAlarm: Pointer to Date structure
1250 * @param Alarm: Specifies the Alarm.
1251 * This parameter can be one of the following values:
1252 * @arg RTC_ALARM_A: AlarmA
1253 * @arg RTC_ALARM_B: AlarmB
1254 * @param Format: Specifies the format of the entered parameters.
1255 * This parameter can be one of the following values:
1256 * @arg RTC_FORMAT_BIN: Binary data format
1257 * @arg RTC_FORMAT_BCD
1259 * @retval HAL status
1261 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1263 uint32_t tmpreg = 0, subsecondtmpreg = 0;
1265 /* Check the parameters */
1266 assert_param(IS_RTC_FORMAT(Format));
1267 assert_param(IS_RTC_ALARM(Alarm));
1269 if(Alarm == RTC_ALARM_A)
1272 sAlarm->Alarm = RTC_ALARM_A;
1274 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1275 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1277 /* Fill the structure with the read parameters */
1278 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
1279 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
1280 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1281 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1282 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1283 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1284 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1285 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1289 sAlarm->Alarm = RTC_ALARM_B;
1291 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1292 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1294 /* Fill the structure with the read parameters */
1295 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> 16);
1296 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> 8);
1297 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU));
1298 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMBR_PM) >> 16);
1299 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1300 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> 24);
1301 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1302 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1305 if(Format == RTC_FORMAT_BIN)
1307 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1308 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1309 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1310 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1317 * @brief This function handles Alarm interrupt request.
1318 * @param hrtc: RTC handle
1321 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1323 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
1325 /* Get the status of the Interrupt */
1326 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
1328 /* AlarmA callback */
1329 HAL_RTC_AlarmAEventCallback(hrtc);
1331 /* Clear the Alarm interrupt pending bit */
1332 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
1336 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
1338 /* Get the status of the Interrupt */
1339 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
1341 /* AlarmB callback */
1342 HAL_RTCEx_AlarmBEventCallback(hrtc);
1344 /* Clear the Alarm interrupt pending bit */
1345 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
1349 /* Clear the EXTI's line Flag for RTC Alarm */
1350 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1352 /* Change RTC state */
1353 hrtc->State = HAL_RTC_STATE_READY;
1357 * @brief Alarm A callback.
1358 * @param hrtc: RTC handle
1361 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1363 /* NOTE : This function Should not be modified, when the callback is needed,
1364 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1369 * @brief This function handles AlarmA Polling request.
1370 * @param hrtc: RTC handle
1371 * @param Timeout: Timeout duration
1372 * @retval HAL status
1374 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1377 uint32_t 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 /** @addtogroup RTC_Exported_Functions_Group4
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: RTC handle
1431 * @retval HAL status
1433 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1435 uint32_t tickstart = 0;
1437 /* Clear RSF flag */
1438 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1440 tickstart = HAL_GetTick();
1442 /* Wait the registers to be synchronised */
1443 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1445 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1458 /** @addtogroup RTC_Exported_Functions_Group5
1459 * @brief Peripheral State functions
1462 ===============================================================================
1463 ##### Peripheral State functions #####
1464 ===============================================================================
1466 This subsection provides functions allowing to
1473 * @brief Returns the RTC state.
1474 * @param hrtc: RTC handle
1477 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1489 /** @addtogroup RTC_Private_Functions
1493 * @brief Enters the RTC Initialization mode.
1494 * @note The RTC Initialization mode is write protected, use the
1495 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1496 * @param hrtc: RTC handle
1497 * @retval HAL status
1499 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1501 uint32_t tickstart = 0;
1503 /* Check if the Initialization mode is set */
1504 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1506 /* Set the Initialization mode */
1507 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1509 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));
1557 #endif /* HAL_RTC_MODULE_ENABLED */
1567 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/