2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_rtc.c
\r
4 * @author MCD Application Team
\r
6 * @date 24-March-2015
\r
7 * @brief RTC HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the Real Time Clock (RTC) peripheral:
\r
10 * + Initialization and de-initialization functions
\r
11 * + RTC Time and Date functions
\r
12 * + RTC Alarm functions
\r
13 * + Peripheral Control functions
\r
14 * + Peripheral State functions
\r
17 ==============================================================================
\r
18 ##### Backup Domain Operating Condition #####
\r
19 ==============================================================================
\r
20 [..] The real-time clock (RTC), the RTC backup registers, and the backup
\r
21 SRAM (BKP SRAM) can be powered from the VBAT voltage when the main
\r
22 VDD supply is powered off.
\r
23 To retain the content of the RTC backup registers, backup SRAM, and supply
\r
24 the RTC when VDD is turned off, VBAT pin can be connected to an optional
\r
25 standby voltage supplied by a battery or by another source.
\r
27 [..] To allow the RTC operating even when the main digital supply (VDD) is turned
\r
28 off, the VBAT pin powers the following blocks:
\r
30 (#) The LSE oscillator
\r
31 (#) The backup SRAM when the low power backup regulator is enabled
\r
32 (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
\r
34 [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
\r
35 the following pins are available:
\r
36 (#) PC14 and PC15 can be used as either GPIO or LSE pins
\r
37 (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
\r
38 (#) PI8 can be used as a GPIO or as the RTC_AF2 pin
\r
40 [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
\r
41 because VDD is not present), the following pins are available:
\r
42 (#) PC14 and PC15 can be used as LSE pins only
\r
43 (#) PC13 can be used as the RTC_AF1 pin
\r
44 (#) PI8 can be used as the RTC_AF2 pin
\r
45 (#) PC1 can be used as the RTC_AF3 pin
\r
47 ##### Backup Domain Reset #####
\r
48 ==================================================================
\r
49 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
\r
50 to their reset values. The BKPSRAM is not affected by this reset. The only
\r
51 way to reset the BKPSRAM is through the Flash interface by requesting
\r
52 a protection level change from 1 to 0.
\r
53 [..] A backup domain reset is generated when one of the following events occurs:
\r
54 (#) Software reset, triggered by setting the BDRST bit in the
\r
55 RCC Backup domain control register (RCC_BDCR).
\r
56 (#) VDD or VBAT power on, if both supplies have previously been powered off.
\r
58 ##### Backup Domain Access #####
\r
59 ==================================================================
\r
60 [..] After reset, the backup domain (RTC registers, RTC backup data
\r
61 registers and backup SRAM) is protected against possible unwanted write
\r
63 [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
\r
64 (+) Enable the Power Controller (PWR) APB1 interface clock using the
\r
65 __PWR_CLK_ENABLE() function.
\r
66 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
\r
67 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
\r
68 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
\r
71 ##### How to use this driver #####
\r
72 ==================================================================
\r
74 (+) Enable the RTC domain access (see description in the section above).
\r
75 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
\r
76 format using the HAL_RTC_Init() function.
\r
78 *** Time and Date configuration ***
\r
79 ===================================
\r
81 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
\r
82 and HAL_RTC_SetDate() functions.
\r
83 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
\r
85 *** Alarm configuration ***
\r
86 ===========================
\r
88 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
\r
89 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
\r
90 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
\r
92 ##### RTC and low power modes #####
\r
93 ==================================================================
\r
94 [..] The MCU can be woken up from a low power mode by an RTC alternate
\r
96 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
\r
97 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
\r
98 These RTC alternate functions can wake up the system from the Stop and
\r
99 Standby low power modes.
\r
100 [..] The system can also wake up from low power modes without depending
\r
101 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
\r
102 or the RTC wakeup events.
\r
103 [..] The RTC provides a programmable time base for waking up from the
\r
104 Stop or Standby mode at regular intervals.
\r
105 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
\r
109 ******************************************************************************
\r
112 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
114 * Redistribution and use in source and binary forms, with or without modification,
\r
115 * are permitted provided that the following conditions are met:
\r
116 * 1. Redistributions of source code must retain the above copyright notice,
\r
117 * this list of conditions and the following disclaimer.
\r
118 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
119 * this list of conditions and the following disclaimer in the documentation
\r
120 * and/or other materials provided with the distribution.
\r
121 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
122 * may be used to endorse or promote products derived from this software
\r
123 * without specific prior written permission.
\r
125 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
126 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
127 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
128 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
129 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
130 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
131 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
132 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
133 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
134 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
136 ******************************************************************************
\r
139 /* Includes ------------------------------------------------------------------*/
\r
140 #include "stm32f7xx_hal.h"
\r
142 /** @addtogroup STM32F7xx_HAL_Driver
\r
146 /** @defgroup RTC RTC
\r
147 * @brief RTC HAL module driver
\r
151 #ifdef HAL_RTC_MODULE_ENABLED
\r
153 /* Private typedef -----------------------------------------------------------*/
\r
154 /* Private define ------------------------------------------------------------*/
\r
155 /* Private macro -------------------------------------------------------------*/
\r
156 /* Private variables ---------------------------------------------------------*/
\r
157 /* Private function prototypes -----------------------------------------------*/
\r
158 /* Private functions ---------------------------------------------------------*/
\r
160 /** @defgroup RTC_Exported_Functions RTC Exported Functions
\r
164 /** @defgroup RTC_Group1 Initialization and de-initialization functions
\r
165 * @brief Initialization and Configuration functions
\r
168 ===============================================================================
\r
169 ##### Initialization and de-initialization functions #####
\r
170 ===============================================================================
\r
171 [..] This section provides functions allowing to initialize and configure the
\r
172 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
\r
173 RTC registers Write protection, enter and exit the RTC initialization mode,
\r
174 RTC registers synchronization check and reference clock detection enable.
\r
175 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
\r
176 It is split into 2 programmable prescalers to minimize power consumption.
\r
177 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
\r
178 (++) When both prescalers are used, it is recommended to configure the
\r
179 asynchronous prescaler to a high value to minimize power consumption.
\r
180 (#) All RTC registers are Write protected. Writing to the RTC registers
\r
181 is enabled by writing a key into the Write Protection register, RTC_WPR.
\r
182 (#) To configure the RTC Calendar, user application should enter
\r
183 initialization mode. In this mode, the calendar counter is stopped
\r
184 and its value can be updated. When the initialization sequence is
\r
185 complete, the calendar restarts counting after 4 RTCCLK cycles.
\r
186 (#) To read the calendar through the shadow registers after Calendar
\r
187 initialization, calendar update or after wakeup from low power modes
\r
188 the software must first clear the RSF flag. The software must then
\r
189 wait until it is set again before reading the calendar, which means
\r
190 that the calendar registers have been correctly copied into the
\r
191 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
\r
192 implements the above software sequence (RSF clear and RSF check).
\r
199 * @brief Initializes the RTC peripheral
\r
200 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
201 * the configuration information for RTC.
\r
202 * @retval HAL status
\r
204 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
\r
206 /* Check the RTC peripheral state */
\r
212 /* Check the parameters */
\r
213 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
\r
214 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
\r
215 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
\r
216 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
\r
217 assert_param (IS_RTC_OUTPUT(hrtc->Init.OutPut));
\r
218 assert_param (IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
\r
219 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
\r
221 if(hrtc->State == HAL_RTC_STATE_RESET)
\r
223 /* Initialize RTC MSP */
\r
224 HAL_RTC_MspInit(hrtc);
\r
227 /* Set RTC state */
\r
228 hrtc->State = HAL_RTC_STATE_BUSY;
\r
230 /* Disable the write protection for RTC registers */
\r
231 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
233 /* Set Initialization mode */
\r
234 if(RTC_EnterInitMode(hrtc) != HAL_OK)
\r
236 /* Enable the write protection for RTC registers */
\r
237 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
239 /* Set RTC state */
\r
240 hrtc->State = HAL_RTC_STATE_ERROR;
\r
246 /* Clear RTC_CR FMT, OSEL and POL Bits */
\r
247 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
\r
248 /* Set RTC_CR register */
\r
249 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
\r
251 /* Configure the RTC PRER */
\r
252 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
\r
253 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
\r
255 /* Exit Initialization mode */
\r
256 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
\r
258 hrtc->Instance->OR &= (uint32_t)~RTC_OR_ALARMTYPE;
\r
259 hrtc->Instance->OR |= (uint32_t)(hrtc->Init.OutPutType);
\r
261 /* Enable the write protection for RTC registers */
\r
262 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
264 /* Set RTC state */
\r
265 hrtc->State = HAL_RTC_STATE_READY;
\r
272 * @brief DeInitializes the RTC peripheral
\r
273 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
274 * the configuration information for RTC.
\r
275 * @note This function doesn't reset the RTC Backup Data registers.
\r
276 * @retval HAL status
\r
278 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
\r
280 uint32_t tickstart = 0;
\r
282 /* Check the parameters */
\r
283 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
\r
285 /* Set RTC state */
\r
286 hrtc->State = HAL_RTC_STATE_BUSY;
\r
288 /* Disable the write protection for RTC registers */
\r
289 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
291 /* Set Initialization mode */
\r
292 if(RTC_EnterInitMode(hrtc) != HAL_OK)
\r
294 /* Enable the write protection for RTC registers */
\r
295 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
297 /* Set RTC state */
\r
298 hrtc->State = HAL_RTC_STATE_ERROR;
\r
304 /* Reset TR, DR and CR registers */
\r
305 hrtc->Instance->TR = (uint32_t)0x00000000;
\r
306 hrtc->Instance->DR = (uint32_t)0x00002101;
\r
307 /* Reset All CR bits except CR[2:0] */
\r
308 hrtc->Instance->CR &= (uint32_t)0x00000007;
\r
311 tickstart = HAL_GetTick();
\r
313 /* Wait till WUTWF flag is set and if Time out is reached exit */
\r
314 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
\r
316 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
318 /* Enable the write protection for RTC registers */
\r
319 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
321 /* Set RTC state */
\r
322 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
324 return HAL_TIMEOUT;
\r
328 /* Reset all RTC CR register bits */
\r
329 hrtc->Instance->CR &= (uint32_t)0x00000000;
\r
330 hrtc->Instance->WUTR = (uint32_t)0x0000FFFF;
\r
331 hrtc->Instance->PRER = (uint32_t)0x007F00FF;
\r
332 hrtc->Instance->ALRMAR = (uint32_t)0x00000000;
\r
333 hrtc->Instance->ALRMBR = (uint32_t)0x00000000;
\r
334 hrtc->Instance->SHIFTR = (uint32_t)0x00000000;
\r
335 hrtc->Instance->CALR = (uint32_t)0x00000000;
\r
336 hrtc->Instance->ALRMASSR = (uint32_t)0x00000000;
\r
337 hrtc->Instance->ALRMBSSR = (uint32_t)0x00000000;
\r
339 /* Reset ISR register and exit initialization mode */
\r
340 hrtc->Instance->ISR = (uint32_t)0x00000000;
\r
342 /* Reset Tamper and alternate functions configuration register */
\r
343 hrtc->Instance->TAMPCR = 0x00000000;
\r
345 /* Reset Option register */
\r
346 hrtc->Instance->OR = 0x00000000;
\r
348 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
\r
349 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
\r
351 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
\r
353 /* Enable the write protection for RTC registers */
\r
354 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
356 hrtc->State = HAL_RTC_STATE_ERROR;
\r
363 /* Enable the write protection for RTC registers */
\r
364 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
366 /* De-Initialize RTC MSP */
\r
367 HAL_RTC_MspDeInit(hrtc);
\r
369 hrtc->State = HAL_RTC_STATE_RESET;
\r
372 __HAL_UNLOCK(hrtc);
\r
378 * @brief Initializes the RTC MSP.
\r
379 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
380 * the configuration information for RTC.
\r
383 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
\r
385 /* NOTE : This function Should not be modified, when the callback is needed,
\r
386 the HAL_RTC_MspInit could be implemented in the user file
\r
391 * @brief DeInitializes the RTC MSP.
\r
392 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
393 * the configuration information for RTC.
\r
396 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
\r
398 /* NOTE : This function Should not be modified, when the callback is needed,
\r
399 the HAL_RTC_MspDeInit could be implemented in the user file
\r
407 /** @defgroup RTC_Group2 RTC Time and Date functions
\r
408 * @brief RTC Time and Date functions
\r
411 ===============================================================================
\r
412 ##### RTC Time and Date functions #####
\r
413 ===============================================================================
\r
415 [..] This section provides functions allowing to configure Time and Date features
\r
422 * @brief Sets RTC current time.
\r
423 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
424 * the configuration information for RTC.
\r
425 * @param sTime: Pointer to Time structure
\r
426 * @param Format: Specifies the format of the entered parameters.
\r
427 * This parameter can be one of the following values:
\r
428 * @arg FORMAT_BIN: Binary data format
\r
429 * @arg FORMAT_BCD: BCD data format
\r
430 * @retval HAL status
\r
432 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
\r
434 uint32_t tmpreg = 0;
\r
436 /* Check the parameters */
\r
437 assert_param(IS_RTC_FORMAT(Format));
\r
438 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
\r
439 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
\r
441 /* Process Locked */
\r
444 hrtc->State = HAL_RTC_STATE_BUSY;
\r
446 if(Format == RTC_FORMAT_BIN)
\r
448 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
\r
450 assert_param(IS_RTC_HOUR12(sTime->Hours));
\r
451 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
\r
455 sTime->TimeFormat = 0x00;
\r
456 assert_param(IS_RTC_HOUR24(sTime->Hours));
\r
458 assert_param(IS_RTC_MINUTES(sTime->Minutes));
\r
459 assert_param(IS_RTC_SECONDS(sTime->Seconds));
\r
461 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
\r
462 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
\r
463 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
\r
464 (((uint32_t)sTime->TimeFormat) << 16));
\r
468 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
\r
470 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
\r
471 assert_param(IS_RTC_HOUR12(tmpreg));
\r
472 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
\r
476 sTime->TimeFormat = 0x00;
\r
477 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
\r
479 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
\r
480 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
\r
481 tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
\r
482 ((uint32_t)(sTime->Minutes) << 8) | \
\r
483 ((uint32_t)sTime->Seconds) | \
\r
484 ((uint32_t)(sTime->TimeFormat) << 16));
\r
487 /* Disable the write protection for RTC registers */
\r
488 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
490 /* Set Initialization mode */
\r
491 if(RTC_EnterInitMode(hrtc) != HAL_OK)
\r
493 /* Enable the write protection for RTC registers */
\r
494 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
496 /* Set RTC state */
\r
497 hrtc->State = HAL_RTC_STATE_ERROR;
\r
499 /* Process Unlocked */
\r
500 __HAL_UNLOCK(hrtc);
\r
506 /* Set the RTC_TR register */
\r
507 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
\r
509 /* Clear the bits to be configured */
\r
510 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
\r
512 /* Configure the RTC_CR register */
\r
513 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
\r
515 /* Exit Initialization mode */
\r
516 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
\r
518 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
\r
519 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
\r
521 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
\r
523 /* Enable the write protection for RTC registers */
\r
524 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
526 hrtc->State = HAL_RTC_STATE_ERROR;
\r
528 /* Process Unlocked */
\r
529 __HAL_UNLOCK(hrtc);
\r
535 /* Enable the write protection for RTC registers */
\r
536 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
538 hrtc->State = HAL_RTC_STATE_READY;
\r
540 __HAL_UNLOCK(hrtc);
\r
547 * @brief Gets RTC current time.
\r
548 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
549 * the configuration information for RTC.
\r
550 * @param sTime: Pointer to Time structure
\r
551 * @param Format: Specifies the format of the entered parameters.
\r
552 * This parameter can be one of the following values:
\r
553 * @arg FORMAT_BIN: Binary data format
\r
554 * @arg FORMAT_BCD: BCD data format
\r
555 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
\r
556 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
\r
557 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
\r
558 * @retval HAL status
\r
560 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
\r
562 uint32_t tmpreg = 0;
\r
564 /* Check the parameters */
\r
565 assert_param(IS_RTC_FORMAT(Format));
\r
567 /* Get subseconds values from the correspondent registers*/
\r
568 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
\r
570 /* Get the TR register */
\r
571 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
\r
573 /* Fill the structure fields with the read parameters */
\r
574 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
\r
575 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
\r
576 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
\r
577 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
\r
579 /* Check the input parameters format */
\r
580 if(Format == RTC_FORMAT_BIN)
\r
582 /* Convert the time structure parameters to Binary format */
\r
583 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
\r
584 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
\r
585 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
\r
592 * @brief Sets RTC current date.
\r
593 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
594 * the configuration information for RTC.
\r
595 * @param sDate: Pointer to date structure
\r
596 * @param Format: specifies the format of the entered parameters.
\r
597 * This parameter can be one of the following values:
\r
598 * @arg FORMAT_BIN: Binary data format
\r
599 * @arg FORMAT_BCD: BCD data format
\r
600 * @retval HAL status
\r
602 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
\r
604 uint32_t datetmpreg = 0;
\r
606 /* Check the parameters */
\r
607 assert_param(IS_RTC_FORMAT(Format));
\r
609 /* Process Locked */
\r
612 hrtc->State = HAL_RTC_STATE_BUSY;
\r
614 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
\r
616 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
\r
619 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
\r
621 if(Format == RTC_FORMAT_BIN)
\r
623 assert_param(IS_RTC_YEAR(sDate->Year));
\r
624 assert_param(IS_RTC_MONTH(sDate->Month));
\r
625 assert_param(IS_RTC_DATE(sDate->Date));
\r
627 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
\r
628 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
\r
629 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
\r
630 ((uint32_t)sDate->WeekDay << 13));
\r
634 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
\r
635 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
\r
636 assert_param(IS_RTC_MONTH(datetmpreg));
\r
637 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
\r
638 assert_param(IS_RTC_DATE(datetmpreg));
\r
640 datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
\r
641 (((uint32_t)sDate->Month) << 8) | \
\r
642 ((uint32_t)sDate->Date) | \
\r
643 (((uint32_t)sDate->WeekDay) << 13));
\r
646 /* Disable the write protection for RTC registers */
\r
647 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
649 /* Set Initialization mode */
\r
650 if(RTC_EnterInitMode(hrtc) != HAL_OK)
\r
652 /* Enable the write protection for RTC registers */
\r
653 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
656 hrtc->State = HAL_RTC_STATE_ERROR;
\r
658 /* Process Unlocked */
\r
659 __HAL_UNLOCK(hrtc);
\r
665 /* Set the RTC_DR register */
\r
666 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
\r
668 /* Exit Initialization mode */
\r
669 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
\r
671 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
\r
672 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
\r
674 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
\r
676 /* Enable the write protection for RTC registers */
\r
677 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
679 hrtc->State = HAL_RTC_STATE_ERROR;
\r
681 /* Process Unlocked */
\r
682 __HAL_UNLOCK(hrtc);
\r
688 /* Enable the write protection for RTC registers */
\r
689 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
691 hrtc->State = HAL_RTC_STATE_READY ;
\r
693 /* Process Unlocked */
\r
694 __HAL_UNLOCK(hrtc);
\r
701 * @brief Gets RTC current date.
\r
702 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
703 * the configuration information for RTC.
\r
704 * @param sDate: Pointer to Date structure
\r
705 * @param Format: Specifies the format of the entered parameters.
\r
706 * This parameter can be one of the following values:
\r
707 * @arg FORMAT_BIN: Binary data format
\r
708 * @arg FORMAT_BCD: BCD data format
\r
709 * @retval HAL status
\r
711 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
\r
713 uint32_t datetmpreg = 0;
\r
715 /* Check the parameters */
\r
716 assert_param(IS_RTC_FORMAT(Format));
\r
718 /* Get the DR register */
\r
719 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
\r
721 /* Fill the structure fields with the read parameters */
\r
722 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
\r
723 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
\r
724 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
\r
725 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
\r
727 /* Check the input parameters format */
\r
728 if(Format == RTC_FORMAT_BIN)
\r
730 /* Convert the date structure parameters to Binary format */
\r
731 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
\r
732 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
\r
733 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
\r
742 /** @defgroup RTC_Group3 RTC Alarm functions
\r
743 * @brief RTC Alarm functions
\r
746 ===============================================================================
\r
747 ##### RTC Alarm functions #####
\r
748 ===============================================================================
\r
750 [..] This section provides functions allowing to configure Alarm feature
\r
756 * @brief Sets the specified RTC Alarm.
\r
757 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
758 * the configuration information for RTC.
\r
759 * @param sAlarm: Pointer to Alarm structure
\r
760 * @param Format: Specifies the format of the entered parameters.
\r
761 * This parameter can be one of the following values:
\r
762 * @arg FORMAT_BIN: Binary data format
\r
763 * @arg FORMAT_BCD: BCD data format
\r
764 * @retval HAL status
\r
766 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
\r
768 uint32_t tickstart = 0;
\r
769 uint32_t tmpreg = 0, subsecondtmpreg = 0;
\r
771 /* Check the parameters */
\r
772 assert_param(IS_RTC_FORMAT(Format));
\r
773 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
\r
774 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
\r
775 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
\r
776 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
\r
777 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
\r
779 /* Process Locked */
\r
782 hrtc->State = HAL_RTC_STATE_BUSY;
\r
784 if(Format == RTC_FORMAT_BIN)
\r
786 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
\r
788 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
\r
789 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
\r
793 sAlarm->AlarmTime.TimeFormat = 0x00;
\r
794 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
\r
796 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
\r
797 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
\r
799 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
\r
801 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
\r
805 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
\r
808 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
\r
809 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
\r
810 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
\r
811 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
\r
812 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
\r
813 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
\r
814 ((uint32_t)sAlarm->AlarmMask));
\r
818 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
\r
820 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
\r
821 assert_param(IS_RTC_HOUR12(tmpreg));
\r
822 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
\r
826 sAlarm->AlarmTime.TimeFormat = 0x00;
\r
827 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
\r
830 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
\r
831 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
\r
833 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
\r
835 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
\r
836 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
\r
840 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
\r
841 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
\r
844 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
\r
845 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
\r
846 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
\r
847 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
\r
848 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
\r
849 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
\r
850 ((uint32_t)sAlarm->AlarmMask));
\r
853 /* Configure the Alarm A or Alarm B Sub Second registers */
\r
854 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
\r
856 /* Disable the write protection for RTC registers */
\r
857 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
859 /* Configure the Alarm register */
\r
860 if(sAlarm->Alarm == RTC_ALARM_A)
\r
862 /* Disable the Alarm A interrupt */
\r
863 __HAL_RTC_ALARMA_DISABLE(hrtc);
\r
865 /* In case of interrupt mode is used, the interrupt source must disabled */
\r
866 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
\r
869 tickstart = HAL_GetTick();
\r
871 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
\r
872 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
\r
874 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
876 /* Enable the write protection for RTC registers */
\r
877 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
879 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
881 /* Process Unlocked */
\r
882 __HAL_UNLOCK(hrtc);
\r
884 return HAL_TIMEOUT;
\r
888 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
\r
889 /* Configure the Alarm A Sub Second register */
\r
890 hrtc->Instance->ALRMASSR = subsecondtmpreg;
\r
891 /* Configure the Alarm state: Enable Alarm */
\r
892 __HAL_RTC_ALARMA_ENABLE(hrtc);
\r
896 /* Disable the Alarm B interrupt */
\r
897 __HAL_RTC_ALARMB_DISABLE(hrtc);
\r
899 /* In case of interrupt mode is used, the interrupt source must disabled */
\r
900 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
\r
903 tickstart = HAL_GetTick();
\r
905 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
\r
906 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
\r
908 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
910 /* Enable the write protection for RTC registers */
\r
911 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
913 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
915 /* Process Unlocked */
\r
916 __HAL_UNLOCK(hrtc);
\r
918 return HAL_TIMEOUT;
\r
922 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
\r
923 /* Configure the Alarm B Sub Second register */
\r
924 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
\r
925 /* Configure the Alarm state: Enable Alarm */
\r
926 __HAL_RTC_ALARMB_ENABLE(hrtc);
\r
929 /* Enable the write protection for RTC registers */
\r
930 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
932 /* Change RTC state */
\r
933 hrtc->State = HAL_RTC_STATE_READY;
\r
935 /* Process Unlocked */
\r
936 __HAL_UNLOCK(hrtc);
\r
942 * @brief Sets the specified RTC Alarm with Interrupt
\r
943 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
944 * the configuration information for RTC.
\r
945 * @param sAlarm: Pointer to Alarm structure
\r
946 * @param Format: Specifies the format of the entered parameters.
\r
947 * This parameter can be one of the following values:
\r
948 * @arg FORMAT_BIN: Binary data format
\r
949 * @arg FORMAT_BCD: BCD data format
\r
950 * @note The Alarm register can only be written when the corresponding Alarm
\r
951 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
\r
952 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
\r
953 * @retval HAL status
\r
955 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
\r
957 uint32_t tickstart = 0;
\r
958 uint32_t tmpreg = 0, subsecondtmpreg = 0;
\r
960 /* Check the parameters */
\r
961 assert_param(IS_RTC_FORMAT(Format));
\r
962 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
\r
963 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
\r
964 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
\r
965 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
\r
966 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
\r
968 /* Process Locked */
\r
971 hrtc->State = HAL_RTC_STATE_BUSY;
\r
973 if(Format == RTC_FORMAT_BIN)
\r
975 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
\r
977 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
\r
978 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
\r
982 sAlarm->AlarmTime.TimeFormat = 0x00;
\r
983 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
\r
985 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
\r
986 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
\r
988 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
\r
990 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
\r
994 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
\r
996 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
\r
997 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
\r
998 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
\r
999 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
\r
1000 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
\r
1001 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
\r
1002 ((uint32_t)sAlarm->AlarmMask));
\r
1006 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
\r
1008 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
\r
1009 assert_param(IS_RTC_HOUR12(tmpreg));
\r
1010 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
\r
1014 sAlarm->AlarmTime.TimeFormat = 0x00;
\r
1015 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
\r
1018 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
\r
1019 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
\r
1021 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
\r
1023 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
\r
1024 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
\r
1028 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
\r
1029 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
\r
1031 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
\r
1032 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
\r
1033 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
\r
1034 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
\r
1035 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
\r
1036 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
\r
1037 ((uint32_t)sAlarm->AlarmMask));
\r
1039 /* Configure the Alarm A or Alarm B Sub Second registers */
\r
1040 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
\r
1042 /* Disable the write protection for RTC registers */
\r
1043 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
1045 /* Configure the Alarm register */
\r
1046 if(sAlarm->Alarm == RTC_ALARM_A)
\r
1048 /* Disable the Alarm A interrupt */
\r
1049 __HAL_RTC_ALARMA_DISABLE(hrtc);
\r
1051 /* Clear flag alarm A */
\r
1052 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
\r
1055 tickstart = HAL_GetTick();
\r
1057 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
\r
1058 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
\r
1060 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
1062 /* Enable the write protection for RTC registers */
\r
1063 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
1065 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
1067 /* Process Unlocked */
\r
1068 __HAL_UNLOCK(hrtc);
\r
1070 return HAL_TIMEOUT;
\r
1074 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
\r
1075 /* Configure the Alarm A Sub Second register */
\r
1076 hrtc->Instance->ALRMASSR = subsecondtmpreg;
\r
1077 /* Configure the Alarm state: Enable Alarm */
\r
1078 __HAL_RTC_ALARMA_ENABLE(hrtc);
\r
1079 /* Configure the Alarm interrupt */
\r
1080 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
\r
1084 /* Disable the Alarm B interrupt */
\r
1085 __HAL_RTC_ALARMB_DISABLE(hrtc);
\r
1087 /* Clear flag alarm B */
\r
1088 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
\r
1091 tickstart = HAL_GetTick();
\r
1093 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
\r
1094 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
\r
1096 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
1098 /* Enable the write protection for RTC registers */
\r
1099 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
1101 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
1103 /* Process Unlocked */
\r
1104 __HAL_UNLOCK(hrtc);
\r
1106 return HAL_TIMEOUT;
\r
1110 hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
\r
1111 /* Configure the Alarm B Sub Second register */
\r
1112 hrtc->Instance->ALRMBSSR = subsecondtmpreg;
\r
1113 /* Configure the Alarm state: Enable Alarm */
\r
1114 __HAL_RTC_ALARMB_ENABLE(hrtc);
\r
1115 /* Configure the Alarm interrupt */
\r
1116 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
\r
1119 /* RTC Alarm Interrupt Configuration: EXTI configuration */
\r
1120 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
\r
1122 EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
\r
1124 /* Enable the write protection for RTC registers */
\r
1125 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
1127 hrtc->State = HAL_RTC_STATE_READY;
\r
1129 /* Process Unlocked */
\r
1130 __HAL_UNLOCK(hrtc);
\r
1136 * @brief Deactive the specified RTC Alarm
\r
1137 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1138 * the configuration information for RTC.
\r
1139 * @param Alarm: Specifies the Alarm.
\r
1140 * This parameter can be one of the following values:
\r
1141 * @arg RTC_ALARM_A: AlarmA
\r
1142 * @arg RTC_ALARM_B: AlarmB
\r
1143 * @retval HAL status
\r
1145 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
\r
1147 uint32_t tickstart = 0;
\r
1149 /* Check the parameters */
\r
1150 assert_param(IS_RTC_ALARM(Alarm));
\r
1152 /* Process Locked */
\r
1155 hrtc->State = HAL_RTC_STATE_BUSY;
\r
1157 /* Disable the write protection for RTC registers */
\r
1158 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
\r
1160 if(Alarm == RTC_ALARM_A)
\r
1163 __HAL_RTC_ALARMA_DISABLE(hrtc);
\r
1165 /* In case of interrupt mode is used, the interrupt source must disabled */
\r
1166 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
\r
1169 tickstart = HAL_GetTick();
\r
1171 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
\r
1172 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
\r
1174 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
1176 /* Enable the write protection for RTC registers */
\r
1177 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
1179 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
1181 /* Process Unlocked */
\r
1182 __HAL_UNLOCK(hrtc);
\r
1184 return HAL_TIMEOUT;
\r
1191 __HAL_RTC_ALARMB_DISABLE(hrtc);
\r
1193 /* In case of interrupt mode is used, the interrupt source must disabled */
\r
1194 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
\r
1197 tickstart = HAL_GetTick();
\r
1199 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
\r
1200 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
\r
1202 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
1204 /* Enable the write protection for RTC registers */
\r
1205 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
1207 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
1209 /* Process Unlocked */
\r
1210 __HAL_UNLOCK(hrtc);
\r
1212 return HAL_TIMEOUT;
\r
1216 /* Enable the write protection for RTC registers */
\r
1217 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
\r
1219 hrtc->State = HAL_RTC_STATE_READY;
\r
1221 /* Process Unlocked */
\r
1222 __HAL_UNLOCK(hrtc);
\r
1228 * @brief Gets the RTC Alarm value and masks.
\r
1229 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1230 * the configuration information for RTC.
\r
1231 * @param sAlarm: Pointer to Date structure
\r
1232 * @param Alarm: Specifies the Alarm.
\r
1233 * This parameter can be one of the following values:
\r
1234 * @arg RTC_ALARM_A: AlarmA
\r
1235 * @arg RTC_ALARM_B: AlarmB
\r
1236 * @param Format: Specifies the format of the entered parameters.
\r
1237 * This parameter can be one of the following values:
\r
1238 * @arg FORMAT_BIN: Binary data format
\r
1239 * @arg FORMAT_BCD: BCD data format
\r
1240 * @retval HAL status
\r
1242 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
\r
1244 uint32_t tmpreg = 0, subsecondtmpreg = 0;
\r
1246 /* Check the parameters */
\r
1247 assert_param(IS_RTC_FORMAT(Format));
\r
1248 assert_param(IS_RTC_ALARM(Alarm));
\r
1250 if(Alarm == RTC_ALARM_A)
\r
1253 sAlarm->Alarm = RTC_ALARM_A;
\r
1255 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
\r
1256 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
\r
1260 sAlarm->Alarm = RTC_ALARM_B;
\r
1262 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
\r
1263 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
\r
1266 /* Fill the structure with the read parameters */
\r
1267 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
\r
1268 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
\r
1269 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
\r
1270 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
\r
1271 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
\r
1272 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
\r
1273 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
\r
1274 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
\r
1276 if(Format == RTC_FORMAT_BIN)
\r
1278 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
\r
1279 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
\r
1280 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
\r
1281 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
\r
1288 * @brief This function handles Alarm interrupt request.
\r
1289 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1290 * the configuration information for RTC.
\r
1293 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
\r
1295 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
\r
1297 /* Get the status of the Interrupt */
\r
1298 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
\r
1300 /* AlarmA callback */
\r
1301 HAL_RTC_AlarmAEventCallback(hrtc);
\r
1303 /* Clear the Alarm interrupt pending bit */
\r
1304 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
\r
1308 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
\r
1310 /* Get the status of the Interrupt */
\r
1311 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
\r
1313 /* AlarmB callback */
\r
1314 HAL_RTCEx_AlarmBEventCallback(hrtc);
\r
1316 /* Clear the Alarm interrupt pending bit */
\r
1317 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
\r
1321 /* Clear the EXTI's line Flag for RTC Alarm */
\r
1322 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
\r
1324 /* Change RTC state */
\r
1325 hrtc->State = HAL_RTC_STATE_READY;
\r
1329 * @brief Alarm A callback.
\r
1330 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1331 * the configuration information for RTC.
\r
1334 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
\r
1336 /* NOTE : This function Should not be modified, when the callback is needed,
\r
1337 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
\r
1342 * @brief This function handles AlarmA Polling request.
\r
1343 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1344 * the configuration information for RTC.
\r
1345 * @param Timeout: Timeout duration
\r
1346 * @retval HAL status
\r
1348 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
\r
1350 uint32_t tickstart = 0;
\r
1353 tickstart = HAL_GetTick();
\r
1355 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
\r
1357 if(Timeout != HAL_MAX_DELAY)
\r
1359 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
\r
1361 hrtc->State = HAL_RTC_STATE_TIMEOUT;
\r
1362 return HAL_TIMEOUT;
\r
1367 /* Clear the Alarm interrupt pending bit */
\r
1368 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
\r
1370 /* Change RTC state */
\r
1371 hrtc->State = HAL_RTC_STATE_READY;
\r
1380 /** @defgroup RTC_Group4 Peripheral Control functions
\r
1381 * @brief Peripheral Control functions
\r
1384 ===============================================================================
\r
1385 ##### Peripheral Control functions #####
\r
1386 ===============================================================================
\r
1388 This subsection provides functions allowing to
\r
1389 (+) Wait for RTC Time and Date Synchronization
\r
1396 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
\r
1397 * synchronized with RTC APB clock.
\r
1398 * @note The RTC Resynchronization mode is write protected, use the
\r
1399 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
\r
1400 * @note To read the calendar through the shadow registers after Calendar
\r
1401 * initialization, calendar update or after wakeup from low power modes
\r
1402 * the software must first clear the RSF flag.
\r
1403 * The software must then wait until it is set again before reading
\r
1404 * the calendar, which means that the calendar registers have been
\r
1405 * correctly copied into the RTC_TR and RTC_DR shadow registers.
\r
1406 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1407 * the configuration information for RTC.
\r
1408 * @retval HAL status
\r
1410 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
\r
1412 uint32_t tickstart = 0;
\r
1414 /* Clear RSF flag */
\r
1415 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
\r
1418 tickstart = HAL_GetTick();
\r
1420 /* Wait the registers to be synchronised */
\r
1421 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
\r
1423 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
1425 return HAL_TIMEOUT;
\r
1436 /** @defgroup RTC_Group5 Peripheral State functions
\r
1437 * @brief Peripheral State functions
\r
1440 ===============================================================================
\r
1441 ##### Peripheral State functions #####
\r
1442 ===============================================================================
\r
1444 This subsection provides functions allowing to
\r
1451 * @brief Returns the RTC state.
\r
1452 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1453 * the configuration information for RTC.
\r
1454 * @retval HAL state
\r
1456 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
\r
1458 return hrtc->State;
\r
1466 * @brief Enters the RTC Initialization mode.
\r
1467 * @note The RTC Initialization mode is write protected, use the
\r
1468 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
\r
1469 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
\r
1470 * the configuration information for RTC.
\r
1471 * @retval HAL status
\r
1473 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
\r
1475 uint32_t tickstart = 0;
\r
1477 /* Check if the Initialization mode is set */
\r
1478 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
\r
1480 /* Set the Initialization mode */
\r
1481 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
\r
1484 tickstart = HAL_GetTick();
\r
1486 /* Wait till RTC is in INIT state and if Time out is reached exit */
\r
1487 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
\r
1489 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
\r
1491 return HAL_TIMEOUT;
\r
1501 * @brief Converts a 2 digit decimal to BCD format.
\r
1502 * @param Value: Byte to be converted
\r
1503 * @retval Converted byte
\r
1505 uint8_t RTC_ByteToBcd2(uint8_t Value)
\r
1507 uint32_t bcdhigh = 0;
\r
1509 while(Value >= 10)
\r
1515 return ((uint8_t)(bcdhigh << 4) | Value);
\r
1519 * @brief Converts from 2 digit BCD to Binary.
\r
1520 * @param Value: BCD value to be converted
\r
1521 * @retval Converted word
\r
1523 uint8_t RTC_Bcd2ToByte(uint8_t Value)
\r
1526 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
\r
1527 return (tmp + (Value & (uint8_t)0x0F));
\r
1534 #endif /* HAL_RTC_MODULE_ENABLED */
\r
1543 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r