STM32F439xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_rtc.c 00004 * @author MCD Application Team 00005 * @brief RTC HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Real Time Clock (RTC) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + RTC Time and Date functions 00010 * + RTC Alarm functions 00011 * + Peripheral Control functions 00012 * + Peripheral State functions 00013 * 00014 @verbatim 00015 ============================================================================== 00016 ##### Backup Domain Operating Condition ##### 00017 ============================================================================== 00018 [..] The real-time clock (RTC), the RTC backup registers, and the backup 00019 SRAM (BKP SRAM) can be powered from the VBAT voltage when the main 00020 VDD supply is powered off. 00021 To retain the content of the RTC backup registers, backup SRAM, and supply 00022 the RTC when VDD is turned off, VBAT pin can be connected to an optional 00023 standby voltage supplied by a battery or by another source. 00024 00025 [..] To allow the RTC operating even when the main digital supply (VDD) is turned 00026 off, the VBAT pin powers the following blocks: 00027 (#) The RTC 00028 (#) The LSE oscillator 00029 (#) The backup SRAM when the low power backup regulator is enabled 00030 (#) PC13 to PC15 I/Os, plus PI8 I/O (when available) 00031 00032 [..] When the backup domain is supplied by VDD (analog switch connected to VDD), 00033 the following pins are available: 00034 (#) PC14 and PC15 can be used as either GPIO or LSE pins 00035 (#) PC13 can be used as a GPIO or as the RTC_AF1 pin 00036 (#) PI8 can be used as a GPIO or as the RTC_AF2 pin 00037 00038 [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT 00039 because VDD is not present), the following pins are available: 00040 (#) PC14 and PC15 can be used as LSE pins only 00041 (#) PC13 can be used as the RTC_AF1 pin 00042 (#) PI8 can be used as the RTC_AF2 pin 00043 00044 ##### Backup Domain Reset ##### 00045 ================================================================== 00046 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register 00047 to their reset values. The BKPSRAM is not affected by this reset. The only 00048 way to reset the BKPSRAM is through the Flash interface by requesting 00049 a protection level change from 1 to 0. 00050 [..] A backup domain reset is generated when one of the following events occurs: 00051 (#) Software reset, triggered by setting the BDRST bit in the 00052 RCC Backup domain control register (RCC_BDCR). 00053 (#) VDD or VBAT power on, if both supplies have previously been powered off. 00054 00055 ##### Backup Domain Access ##### 00056 ================================================================== 00057 [..] After reset, the backup domain (RTC registers, RTC backup data 00058 registers and backup SRAM) is protected against possible unwanted write 00059 accesses. 00060 [..] To enable access to the RTC Domain and RTC registers, proceed as follows: 00061 (+) Enable the Power Controller (PWR) APB1 interface clock using the 00062 __HAL_RCC_PWR_CLK_ENABLE() function. 00063 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function. 00064 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function. 00065 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function. 00066 00067 00068 ##### How to use this driver ##### 00069 ================================================================== 00070 [..] 00071 (+) Enable the RTC domain access (see description in the section above). 00072 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour 00073 format using the HAL_RTC_Init() function. 00074 00075 *** Time and Date configuration *** 00076 =================================== 00077 [..] 00078 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime() 00079 and HAL_RTC_SetDate() functions. 00080 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions. 00081 00082 *** Alarm configuration *** 00083 =========================== 00084 [..] 00085 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function. 00086 You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function. 00087 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function. 00088 00089 ##### RTC and low power modes ##### 00090 ================================================================== 00091 [..] The MCU can be woken up from a low power mode by an RTC alternate 00092 function. 00093 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B), 00094 RTC wake-up, RTC tamper event detection and RTC time stamp event detection. 00095 These RTC alternate functions can wake up the system from the Stop and 00096 Standby low power modes. 00097 [..] The system can also wake up from low power modes without depending 00098 on an external interrupt (Auto-wake-up mode), by using the RTC alarm 00099 or the RTC wake-up events. 00100 [..] The RTC provides a programmable time base for waking up from the 00101 Stop or Standby mode at regular intervals. 00102 Wake-up from STOP and STANDBY modes is possible only when the RTC clock source 00103 is LSE or LSI. 00104 00105 @endverbatim 00106 ****************************************************************************** 00107 * @attention 00108 * 00109 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00110 * 00111 * Redistribution and use in source and binary forms, with or without modification, 00112 * are permitted provided that the following conditions are met: 00113 * 1. Redistributions of source code must retain the above copyright notice, 00114 * this list of conditions and the following disclaimer. 00115 * 2. Redistributions in binary form must reproduce the above copyright notice, 00116 * this list of conditions and the following disclaimer in the documentation 00117 * and/or other materials provided with the distribution. 00118 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00119 * may be used to endorse or promote products derived from this software 00120 * without specific prior written permission. 00121 * 00122 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00123 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00124 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00125 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00126 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00127 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00128 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00129 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00130 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00131 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00132 * 00133 ****************************************************************************** 00134 */ 00135 00136 /* Includes ------------------------------------------------------------------*/ 00137 #include "stm32f4xx_hal.h" 00138 00139 /** @addtogroup STM32F4xx_HAL_Driver 00140 * @{ 00141 */ 00142 00143 /** @defgroup RTC RTC 00144 * @brief RTC HAL module driver 00145 * @{ 00146 */ 00147 00148 #ifdef HAL_RTC_MODULE_ENABLED 00149 00150 /* Private typedef -----------------------------------------------------------*/ 00151 /* Private define ------------------------------------------------------------*/ 00152 /* Private macro -------------------------------------------------------------*/ 00153 /* Private variables ---------------------------------------------------------*/ 00154 /* Private function prototypes -----------------------------------------------*/ 00155 /* Private functions ---------------------------------------------------------*/ 00156 00157 /** @defgroup RTC_Exported_Functions RTC Exported Functions 00158 * @{ 00159 */ 00160 00161 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions 00162 * @brief Initialization and Configuration functions 00163 * 00164 @verbatim 00165 =============================================================================== 00166 ##### Initialization and de-initialization functions ##### 00167 =============================================================================== 00168 [..] This section provides functions allowing to initialize and configure the 00169 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable 00170 RTC registers Write protection, enter and exit the RTC initialization mode, 00171 RTC registers synchronization check and reference clock detection enable. 00172 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base. 00173 It is split into 2 programmable prescalers to minimize power consumption. 00174 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler. 00175 (++) When both prescalers are used, it is recommended to configure the 00176 asynchronous prescaler to a high value to minimize power consumption. 00177 (#) All RTC registers are Write protected. Writing to the RTC registers 00178 is enabled by writing a key into the Write Protection register, RTC_WPR. 00179 (#) To configure the RTC Calendar, user application should enter 00180 initialization mode. In this mode, the calendar counter is stopped 00181 and its value can be updated. When the initialization sequence is 00182 complete, the calendar restarts counting after 4 RTCCLK cycles. 00183 (#) To read the calendar through the shadow registers after Calendar 00184 initialization, calendar update or after wake-up from low power modes 00185 the software must first clear the RSF flag. The software must then 00186 wait until it is set again before reading the calendar, which means 00187 that the calendar registers have been correctly copied into the 00188 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function 00189 implements the above software sequence (RSF clear and RSF check). 00190 00191 @endverbatim 00192 * @{ 00193 */ 00194 00195 /** 00196 * @brief Initializes the RTC peripheral 00197 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00198 * the configuration information for RTC. 00199 * @retval HAL status 00200 */ 00201 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc) 00202 { 00203 /* Check the RTC peripheral state */ 00204 if(hrtc == NULL) 00205 { 00206 return HAL_ERROR; 00207 } 00208 00209 /* Check the parameters */ 00210 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat)); 00211 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv)); 00212 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv)); 00213 assert_param (IS_RTC_OUTPUT(hrtc->Init.OutPut)); 00214 assert_param (IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity)); 00215 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType)); 00216 00217 if(hrtc->State == HAL_RTC_STATE_RESET) 00218 { 00219 /* Allocate lock resource and initialize it */ 00220 hrtc->Lock = HAL_UNLOCKED; 00221 /* Initialize RTC MSP */ 00222 HAL_RTC_MspInit(hrtc); 00223 } 00224 00225 /* Set RTC state */ 00226 hrtc->State = HAL_RTC_STATE_BUSY; 00227 00228 /* Disable the write protection for RTC registers */ 00229 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00230 00231 /* Set Initialization mode */ 00232 if(RTC_EnterInitMode(hrtc) != HAL_OK) 00233 { 00234 /* Enable the write protection for RTC registers */ 00235 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00236 00237 /* Set RTC state */ 00238 hrtc->State = HAL_RTC_STATE_ERROR; 00239 00240 return HAL_ERROR; 00241 } 00242 else 00243 { 00244 /* Clear RTC_CR FMT, OSEL and POL Bits */ 00245 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL)); 00246 /* Set RTC_CR register */ 00247 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity); 00248 00249 /* Configure the RTC PRER */ 00250 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv); 00251 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16U); 00252 00253 /* Exit Initialization mode */ 00254 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; 00255 00256 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00257 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET) 00258 { 00259 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK) 00260 { 00261 /* Enable the write protection for RTC registers */ 00262 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00263 00264 hrtc->State = HAL_RTC_STATE_ERROR; 00265 00266 return HAL_ERROR; 00267 } 00268 } 00269 00270 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE; 00271 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType); 00272 00273 /* Enable the write protection for RTC registers */ 00274 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00275 00276 /* Set RTC state */ 00277 hrtc->State = HAL_RTC_STATE_READY; 00278 00279 return HAL_OK; 00280 } 00281 } 00282 00283 /** 00284 * @brief DeInitializes the RTC peripheral 00285 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00286 * the configuration information for RTC. 00287 * @note This function doesn't reset the RTC Backup Data registers. 00288 * @retval HAL status 00289 */ 00290 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc) 00291 { 00292 uint32_t tickstart = 0U; 00293 00294 /* Set RTC state */ 00295 hrtc->State = HAL_RTC_STATE_BUSY; 00296 00297 /* Disable the write protection for RTC registers */ 00298 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00299 00300 /* Set Initialization mode */ 00301 if(RTC_EnterInitMode(hrtc) != HAL_OK) 00302 { 00303 /* Enable the write protection for RTC registers */ 00304 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00305 00306 /* Set RTC state */ 00307 hrtc->State = HAL_RTC_STATE_ERROR; 00308 00309 return HAL_ERROR; 00310 } 00311 else 00312 { 00313 /* Reset TR, DR and CR registers */ 00314 hrtc->Instance->TR = 0x00000000U; 00315 hrtc->Instance->DR = 0x00002101U; 00316 /* Reset All CR bits except CR[2:0] */ 00317 hrtc->Instance->CR &= 0x00000007U; 00318 00319 /* Get tick */ 00320 tickstart = HAL_GetTick(); 00321 00322 /* Wait till WUTWF flag is set and if Time out is reached exit */ 00323 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET) 00324 { 00325 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 00326 { 00327 /* Enable the write protection for RTC registers */ 00328 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00329 00330 /* Set RTC state */ 00331 hrtc->State = HAL_RTC_STATE_TIMEOUT; 00332 00333 return HAL_TIMEOUT; 00334 } 00335 } 00336 00337 /* Reset all RTC CR register bits */ 00338 hrtc->Instance->CR &= 0x00000000U; 00339 hrtc->Instance->WUTR = 0x0000FFFFU; 00340 hrtc->Instance->PRER = 0x007F00FFU; 00341 hrtc->Instance->CALIBR = 0x00000000U; 00342 hrtc->Instance->ALRMAR = 0x00000000U; 00343 hrtc->Instance->ALRMBR = 0x00000000U; 00344 hrtc->Instance->SHIFTR = 0x00000000U; 00345 hrtc->Instance->CALR = 0x00000000U; 00346 hrtc->Instance->ALRMASSR = 0x00000000U; 00347 hrtc->Instance->ALRMBSSR = 0x00000000U; 00348 00349 /* Reset ISR register and exit initialization mode */ 00350 hrtc->Instance->ISR = 0x00000000U; 00351 00352 /* Reset Tamper and alternate functions configuration register */ 00353 hrtc->Instance->TAFCR = 0x00000000U; 00354 00355 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00356 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET) 00357 { 00358 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK) 00359 { 00360 /* Enable the write protection for RTC registers */ 00361 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00362 00363 hrtc->State = HAL_RTC_STATE_ERROR; 00364 00365 return HAL_ERROR; 00366 } 00367 } 00368 } 00369 00370 /* Enable the write protection for RTC registers */ 00371 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00372 00373 /* De-Initialize RTC MSP */ 00374 HAL_RTC_MspDeInit(hrtc); 00375 00376 hrtc->State = HAL_RTC_STATE_RESET; 00377 00378 /* Release Lock */ 00379 __HAL_UNLOCK(hrtc); 00380 00381 return HAL_OK; 00382 } 00383 00384 /** 00385 * @brief Initializes the RTC MSP. 00386 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00387 * the configuration information for RTC. 00388 * @retval None 00389 */ 00390 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc) 00391 { 00392 /* Prevent unused argument(s) compilation warning */ 00393 UNUSED(hrtc); 00394 /* NOTE : This function Should not be modified, when the callback is needed, 00395 the HAL_RTC_MspInit could be implemented in the user file 00396 */ 00397 } 00398 00399 /** 00400 * @brief DeInitializes the RTC MSP. 00401 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00402 * the configuration information for RTC. 00403 * @retval None 00404 */ 00405 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc) 00406 { 00407 /* Prevent unused argument(s) compilation warning */ 00408 UNUSED(hrtc); 00409 /* NOTE : This function Should not be modified, when the callback is needed, 00410 the HAL_RTC_MspDeInit could be implemented in the user file 00411 */ 00412 } 00413 00414 /** 00415 * @} 00416 */ 00417 00418 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions 00419 * @brief RTC Time and Date functions 00420 * 00421 @verbatim 00422 =============================================================================== 00423 ##### RTC Time and Date functions ##### 00424 =============================================================================== 00425 00426 [..] This section provides functions allowing to configure Time and Date features 00427 00428 @endverbatim 00429 * @{ 00430 */ 00431 00432 /** 00433 * @brief Sets RTC current time. 00434 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00435 * the configuration information for RTC. 00436 * @param sTime Pointer to Time structure 00437 * @param Format Specifies the format of the entered parameters. 00438 * This parameter can be one of the following values: 00439 * @arg RTC_FORMAT_BIN: Binary data format 00440 * @arg RTC_FORMAT_BCD: BCD data format 00441 * @retval HAL status 00442 */ 00443 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 00444 { 00445 uint32_t tmpreg = 0U; 00446 00447 /* Check the parameters */ 00448 assert_param(IS_RTC_FORMAT(Format)); 00449 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving)); 00450 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation)); 00451 00452 /* Process Locked */ 00453 __HAL_LOCK(hrtc); 00454 00455 hrtc->State = HAL_RTC_STATE_BUSY; 00456 00457 if(Format == RTC_FORMAT_BIN) 00458 { 00459 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET) 00460 { 00461 assert_param(IS_RTC_HOUR12(sTime->Hours)); 00462 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat)); 00463 } 00464 else 00465 { 00466 sTime->TimeFormat = 0x00U; 00467 assert_param(IS_RTC_HOUR24(sTime->Hours)); 00468 } 00469 assert_param(IS_RTC_MINUTES(sTime->Minutes)); 00470 assert_param(IS_RTC_SECONDS(sTime->Seconds)); 00471 00472 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \ 00473 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \ 00474 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \ 00475 (((uint32_t)sTime->TimeFormat) << 16U)); 00476 } 00477 else 00478 { 00479 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET) 00480 { 00481 tmpreg = RTC_Bcd2ToByte(sTime->Hours); 00482 assert_param(IS_RTC_HOUR12(tmpreg)); 00483 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat)); 00484 } 00485 else 00486 { 00487 sTime->TimeFormat = 0x00U; 00488 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours))); 00489 } 00490 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes))); 00491 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds))); 00492 tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \ 00493 ((uint32_t)(sTime->Minutes) << 8U) | \ 00494 ((uint32_t)sTime->Seconds) | \ 00495 ((uint32_t)(sTime->TimeFormat) << 16U)); 00496 } 00497 00498 /* Disable the write protection for RTC registers */ 00499 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00500 00501 /* Set Initialization mode */ 00502 if(RTC_EnterInitMode(hrtc) != HAL_OK) 00503 { 00504 /* Enable the write protection for RTC registers */ 00505 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00506 00507 /* Set RTC state */ 00508 hrtc->State = HAL_RTC_STATE_ERROR; 00509 00510 /* Process Unlocked */ 00511 __HAL_UNLOCK(hrtc); 00512 00513 return HAL_ERROR; 00514 } 00515 else 00516 { 00517 /* Set the RTC_TR register */ 00518 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK); 00519 00520 /* Clear the bits to be configured */ 00521 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK; 00522 00523 /* Configure the RTC_CR register */ 00524 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation); 00525 00526 /* Exit Initialization mode */ 00527 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; 00528 00529 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00530 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET) 00531 { 00532 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK) 00533 { 00534 /* Enable the write protection for RTC registers */ 00535 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00536 00537 hrtc->State = HAL_RTC_STATE_ERROR; 00538 00539 /* Process Unlocked */ 00540 __HAL_UNLOCK(hrtc); 00541 00542 return HAL_ERROR; 00543 } 00544 } 00545 00546 /* Enable the write protection for RTC registers */ 00547 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00548 00549 hrtc->State = HAL_RTC_STATE_READY; 00550 00551 __HAL_UNLOCK(hrtc); 00552 00553 return HAL_OK; 00554 } 00555 } 00556 00557 /** 00558 * @brief Gets RTC current time. 00559 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00560 * the configuration information for RTC. 00561 * @param sTime Pointer to Time structure 00562 * @param Format Specifies the format of the entered parameters. 00563 * This parameter can be one of the following values: 00564 * @arg RTC_FORMAT_BIN: Binary data format 00565 * @arg RTC_FORMAT_BCD: BCD data format 00566 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds 00567 * value in second fraction ratio with time unit following generic formula: 00568 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit 00569 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS 00570 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 00571 * in the higher-order calendar shadow registers to ensure consistency between the time and date values. 00572 * Reading RTC current time locks the values in calendar shadow registers until current date is read. 00573 * @retval HAL status 00574 */ 00575 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 00576 { 00577 uint32_t tmpreg = 0U; 00578 00579 /* Check the parameters */ 00580 assert_param(IS_RTC_FORMAT(Format)); 00581 00582 /* Get subseconds structure field from the corresponding register */ 00583 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR); 00584 00585 /* Get SecondFraction structure field from the corresponding register field*/ 00586 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S); 00587 00588 /* Get the TR register */ 00589 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK); 00590 00591 /* Fill the structure fields with the read parameters */ 00592 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U); 00593 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U); 00594 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU)); 00595 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U); 00596 00597 /* Check the input parameters format */ 00598 if(Format == RTC_FORMAT_BIN) 00599 { 00600 /* Convert the time structure parameters to Binary format */ 00601 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours); 00602 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes); 00603 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds); 00604 } 00605 00606 return HAL_OK; 00607 } 00608 00609 /** 00610 * @brief Sets RTC current date. 00611 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00612 * the configuration information for RTC. 00613 * @param sDate Pointer to date structure 00614 * @param Format specifies the format of the entered parameters. 00615 * This parameter can be one of the following values: 00616 * @arg RTC_FORMAT_BIN: Binary data format 00617 * @arg RTC_FORMAT_BCD: BCD data format 00618 * @retval HAL status 00619 */ 00620 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) 00621 { 00622 uint32_t datetmpreg = 0U; 00623 00624 /* Check the parameters */ 00625 assert_param(IS_RTC_FORMAT(Format)); 00626 00627 /* Process Locked */ 00628 __HAL_LOCK(hrtc); 00629 00630 hrtc->State = HAL_RTC_STATE_BUSY; 00631 00632 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U)) 00633 { 00634 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU); 00635 } 00636 00637 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay)); 00638 00639 if(Format == RTC_FORMAT_BIN) 00640 { 00641 assert_param(IS_RTC_YEAR(sDate->Year)); 00642 assert_param(IS_RTC_MONTH(sDate->Month)); 00643 assert_param(IS_RTC_DATE(sDate->Date)); 00644 00645 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \ 00646 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \ 00647 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \ 00648 ((uint32_t)sDate->WeekDay << 13U)); 00649 } 00650 else 00651 { 00652 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year))); 00653 assert_param(IS_RTC_MONTH(datetmpreg)); 00654 assert_param(IS_RTC_DATE(datetmpreg)); 00655 00656 datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \ 00657 (((uint32_t)sDate->Month) << 8U) | \ 00658 ((uint32_t)sDate->Date) | \ 00659 (((uint32_t)sDate->WeekDay) << 13U)); 00660 } 00661 00662 /* Disable the write protection for RTC registers */ 00663 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00664 00665 /* Set Initialization mode */ 00666 if(RTC_EnterInitMode(hrtc) != HAL_OK) 00667 { 00668 /* Enable the write protection for RTC registers */ 00669 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00670 00671 /* Set RTC state*/ 00672 hrtc->State = HAL_RTC_STATE_ERROR; 00673 00674 /* Process Unlocked */ 00675 __HAL_UNLOCK(hrtc); 00676 00677 return HAL_ERROR; 00678 } 00679 else 00680 { 00681 /* Set the RTC_DR register */ 00682 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK); 00683 00684 /* Exit Initialization mode */ 00685 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; 00686 00687 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00688 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET) 00689 { 00690 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK) 00691 { 00692 /* Enable the write protection for RTC registers */ 00693 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00694 00695 hrtc->State = HAL_RTC_STATE_ERROR; 00696 00697 /* Process Unlocked */ 00698 __HAL_UNLOCK(hrtc); 00699 00700 return HAL_ERROR; 00701 } 00702 } 00703 00704 /* Enable the write protection for RTC registers */ 00705 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00706 00707 hrtc->State = HAL_RTC_STATE_READY ; 00708 00709 /* Process Unlocked */ 00710 __HAL_UNLOCK(hrtc); 00711 00712 return HAL_OK; 00713 } 00714 } 00715 00716 /** 00717 * @brief Gets RTC current date. 00718 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00719 * the configuration information for RTC. 00720 * @param sDate Pointer to Date structure 00721 * @param Format Specifies the format of the entered parameters. 00722 * This parameter can be one of the following values: 00723 * @arg RTC_FORMAT_BIN: Binary data format 00724 * @arg RTC_FORMAT_BCD: BCD data format 00725 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 00726 * in the higher-order calendar shadow registers to ensure consistency between the time and date values. 00727 * Reading RTC current time locks the values in calendar shadow registers until Current date is read. 00728 * @retval HAL status 00729 */ 00730 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) 00731 { 00732 uint32_t datetmpreg = 0U; 00733 00734 /* Check the parameters */ 00735 assert_param(IS_RTC_FORMAT(Format)); 00736 00737 /* Get the DR register */ 00738 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK); 00739 00740 /* Fill the structure fields with the read parameters */ 00741 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U); 00742 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U); 00743 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU)); 00744 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U); 00745 00746 /* Check the input parameters format */ 00747 if(Format == RTC_FORMAT_BIN) 00748 { 00749 /* Convert the date structure parameters to Binary format */ 00750 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year); 00751 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month); 00752 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date); 00753 } 00754 return HAL_OK; 00755 } 00756 00757 /** 00758 * @} 00759 */ 00760 00761 /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions 00762 * @brief RTC Alarm functions 00763 * 00764 @verbatim 00765 =============================================================================== 00766 ##### RTC Alarm functions ##### 00767 =============================================================================== 00768 00769 [..] This section provides functions allowing to configure Alarm feature 00770 00771 @endverbatim 00772 * @{ 00773 */ 00774 /** 00775 * @brief Sets the specified RTC Alarm. 00776 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00777 * the configuration information for RTC. 00778 * @param sAlarm Pointer to Alarm structure 00779 * @param Format Specifies the format of the entered parameters. 00780 * This parameter can be one of the following values: 00781 * @arg RTC_FORMAT_BIN: Binary data format 00782 * @arg RTC_FORMAT_BCD: BCD data format 00783 * @retval HAL status 00784 */ 00785 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format) 00786 { 00787 uint32_t tickstart = 0U; 00788 uint32_t tmpreg = 0U, subsecondtmpreg = 0U; 00789 00790 /* Check the parameters */ 00791 assert_param(IS_RTC_FORMAT(Format)); 00792 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 00793 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 00794 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 00795 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds)); 00796 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask)); 00797 00798 /* Process Locked */ 00799 __HAL_LOCK(hrtc); 00800 00801 hrtc->State = HAL_RTC_STATE_BUSY; 00802 00803 if(Format == RTC_FORMAT_BIN) 00804 { 00805 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET) 00806 { 00807 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours)); 00808 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 00809 } 00810 else 00811 { 00812 sAlarm->AlarmTime.TimeFormat = 0x00U; 00813 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours)); 00814 } 00815 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes)); 00816 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds)); 00817 00818 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 00819 { 00820 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay)); 00821 } 00822 else 00823 { 00824 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay)); 00825 } 00826 00827 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \ 00828 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \ 00829 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \ 00830 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \ 00831 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \ 00832 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 00833 ((uint32_t)sAlarm->AlarmMask)); 00834 } 00835 else 00836 { 00837 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET) 00838 { 00839 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours); 00840 assert_param(IS_RTC_HOUR12(tmpreg)); 00841 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 00842 } 00843 else 00844 { 00845 sAlarm->AlarmTime.TimeFormat = 0x00U; 00846 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 00847 } 00848 00849 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes))); 00850 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds))); 00851 00852 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 00853 { 00854 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay); 00855 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg)); 00856 } 00857 else 00858 { 00859 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay); 00860 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg)); 00861 } 00862 00863 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \ 00864 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \ 00865 ((uint32_t) sAlarm->AlarmTime.Seconds) | \ 00866 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \ 00867 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \ 00868 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 00869 ((uint32_t)sAlarm->AlarmMask)); 00870 } 00871 00872 /* Configure the Alarm A or Alarm B Sub Second registers */ 00873 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask)); 00874 00875 /* Disable the write protection for RTC registers */ 00876 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00877 00878 /* Configure the Alarm register */ 00879 if(sAlarm->Alarm == RTC_ALARM_A) 00880 { 00881 /* Disable the Alarm A interrupt */ 00882 __HAL_RTC_ALARMA_DISABLE(hrtc); 00883 00884 /* In case of interrupt mode is used, the interrupt source must disabled */ 00885 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA); 00886 00887 /* Get tick */ 00888 tickstart = HAL_GetTick(); 00889 00890 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */ 00891 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET) 00892 { 00893 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 00894 { 00895 /* Enable the write protection for RTC registers */ 00896 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00897 00898 hrtc->State = HAL_RTC_STATE_TIMEOUT; 00899 00900 /* Process Unlocked */ 00901 __HAL_UNLOCK(hrtc); 00902 00903 return HAL_TIMEOUT; 00904 } 00905 } 00906 00907 hrtc->Instance->ALRMAR = (uint32_t)tmpreg; 00908 /* Configure the Alarm A Sub Second register */ 00909 hrtc->Instance->ALRMASSR = subsecondtmpreg; 00910 /* Configure the Alarm state: Enable Alarm */ 00911 __HAL_RTC_ALARMA_ENABLE(hrtc); 00912 } 00913 else 00914 { 00915 /* Disable the Alarm B interrupt */ 00916 __HAL_RTC_ALARMB_DISABLE(hrtc); 00917 00918 /* In case of interrupt mode is used, the interrupt source must disabled */ 00919 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB); 00920 00921 /* Get tick */ 00922 tickstart = HAL_GetTick(); 00923 00924 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */ 00925 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET) 00926 { 00927 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 00928 { 00929 /* Enable the write protection for RTC registers */ 00930 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00931 00932 hrtc->State = HAL_RTC_STATE_TIMEOUT; 00933 00934 /* Process Unlocked */ 00935 __HAL_UNLOCK(hrtc); 00936 00937 return HAL_TIMEOUT; 00938 } 00939 } 00940 00941 hrtc->Instance->ALRMBR = (uint32_t)tmpreg; 00942 /* Configure the Alarm B Sub Second register */ 00943 hrtc->Instance->ALRMBSSR = subsecondtmpreg; 00944 /* Configure the Alarm state: Enable Alarm */ 00945 __HAL_RTC_ALARMB_ENABLE(hrtc); 00946 } 00947 00948 /* Enable the write protection for RTC registers */ 00949 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00950 00951 /* Change RTC state */ 00952 hrtc->State = HAL_RTC_STATE_READY; 00953 00954 /* Process Unlocked */ 00955 __HAL_UNLOCK(hrtc); 00956 00957 return HAL_OK; 00958 } 00959 00960 /** 00961 * @brief Sets the specified RTC Alarm with Interrupt 00962 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 00963 * the configuration information for RTC. 00964 * @param sAlarm Pointer to Alarm structure 00965 * @param Format Specifies the format of the entered parameters. 00966 * This parameter can be one of the following values: 00967 * @arg RTC_FORMAT_BIN: Binary data format 00968 * @arg RTC_FORMAT_BCD: BCD data format 00969 * @retval HAL status 00970 */ 00971 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format) 00972 { 00973 uint32_t tmpreg = 0U, subsecondtmpreg = 0U; 00974 __IO uint32_t count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U) ; 00975 00976 /* Check the parameters */ 00977 assert_param(IS_RTC_FORMAT(Format)); 00978 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 00979 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 00980 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 00981 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds)); 00982 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask)); 00983 00984 /* Process Locked */ 00985 __HAL_LOCK(hrtc); 00986 00987 hrtc->State = HAL_RTC_STATE_BUSY; 00988 00989 if(Format == RTC_FORMAT_BIN) 00990 { 00991 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET) 00992 { 00993 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours)); 00994 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 00995 } 00996 else 00997 { 00998 sAlarm->AlarmTime.TimeFormat = 0x00U; 00999 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours)); 01000 } 01001 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes)); 01002 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds)); 01003 01004 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01005 { 01006 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay)); 01007 } 01008 else 01009 { 01010 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay)); 01011 } 01012 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \ 01013 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \ 01014 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \ 01015 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \ 01016 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \ 01017 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01018 ((uint32_t)sAlarm->AlarmMask)); 01019 } 01020 else 01021 { 01022 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET) 01023 { 01024 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours); 01025 assert_param(IS_RTC_HOUR12(tmpreg)); 01026 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01027 } 01028 else 01029 { 01030 sAlarm->AlarmTime.TimeFormat = 0x00U; 01031 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01032 } 01033 01034 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes))); 01035 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds))); 01036 01037 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01038 { 01039 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay); 01040 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg)); 01041 } 01042 else 01043 { 01044 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay); 01045 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg)); 01046 } 01047 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \ 01048 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \ 01049 ((uint32_t) sAlarm->AlarmTime.Seconds) | \ 01050 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \ 01051 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \ 01052 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01053 ((uint32_t)sAlarm->AlarmMask)); 01054 } 01055 /* Configure the Alarm A or Alarm B Sub Second registers */ 01056 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask)); 01057 01058 /* Disable the write protection for RTC registers */ 01059 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01060 01061 /* Configure the Alarm register */ 01062 if(sAlarm->Alarm == RTC_ALARM_A) 01063 { 01064 /* Disable the Alarm A interrupt */ 01065 __HAL_RTC_ALARMA_DISABLE(hrtc); 01066 01067 /* Clear flag alarm A */ 01068 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF); 01069 01070 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */ 01071 do 01072 { 01073 if (count-- == 0U) 01074 { 01075 /* Enable the write protection for RTC registers */ 01076 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01077 01078 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01079 01080 /* Process Unlocked */ 01081 __HAL_UNLOCK(hrtc); 01082 01083 return HAL_TIMEOUT; 01084 } 01085 } 01086 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET); 01087 01088 hrtc->Instance->ALRMAR = (uint32_t)tmpreg; 01089 /* Configure the Alarm A Sub Second register */ 01090 hrtc->Instance->ALRMASSR = subsecondtmpreg; 01091 /* Configure the Alarm state: Enable Alarm */ 01092 __HAL_RTC_ALARMA_ENABLE(hrtc); 01093 /* Configure the Alarm interrupt */ 01094 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA); 01095 } 01096 else 01097 { 01098 /* Disable the Alarm B interrupt */ 01099 __HAL_RTC_ALARMB_DISABLE(hrtc); 01100 01101 /* Clear flag alarm B */ 01102 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF); 01103 01104 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */ 01105 do 01106 { 01107 if (count-- == 0U) 01108 { 01109 /* Enable the write protection for RTC registers */ 01110 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01111 01112 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01113 01114 /* Process Unlocked */ 01115 __HAL_UNLOCK(hrtc); 01116 01117 return HAL_TIMEOUT; 01118 } 01119 } 01120 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET); 01121 01122 hrtc->Instance->ALRMBR = (uint32_t)tmpreg; 01123 /* Configure the Alarm B Sub Second register */ 01124 hrtc->Instance->ALRMBSSR = subsecondtmpreg; 01125 /* Configure the Alarm state: Enable Alarm */ 01126 __HAL_RTC_ALARMB_ENABLE(hrtc); 01127 /* Configure the Alarm interrupt */ 01128 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB); 01129 } 01130 01131 /* RTC Alarm Interrupt Configuration: EXTI configuration */ 01132 __HAL_RTC_ALARM_EXTI_ENABLE_IT(); 01133 01134 EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT; 01135 01136 /* Enable the write protection for RTC registers */ 01137 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01138 01139 hrtc->State = HAL_RTC_STATE_READY; 01140 01141 /* Process Unlocked */ 01142 __HAL_UNLOCK(hrtc); 01143 01144 return HAL_OK; 01145 } 01146 01147 /** 01148 * @brief Deactivate the specified RTC Alarm 01149 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01150 * the configuration information for RTC. 01151 * @param Alarm Specifies the Alarm. 01152 * This parameter can be one of the following values: 01153 * @arg RTC_ALARM_A: AlarmA 01154 * @arg RTC_ALARM_B: AlarmB 01155 * @retval HAL status 01156 */ 01157 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm) 01158 { 01159 uint32_t tickstart = 0U; 01160 01161 /* Check the parameters */ 01162 assert_param(IS_RTC_ALARM(Alarm)); 01163 01164 /* Process Locked */ 01165 __HAL_LOCK(hrtc); 01166 01167 hrtc->State = HAL_RTC_STATE_BUSY; 01168 01169 /* Disable the write protection for RTC registers */ 01170 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01171 01172 if(Alarm == RTC_ALARM_A) 01173 { 01174 /* AlarmA */ 01175 __HAL_RTC_ALARMA_DISABLE(hrtc); 01176 01177 /* In case of interrupt mode is used, the interrupt source must disabled */ 01178 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA); 01179 01180 /* Get tick */ 01181 tickstart = HAL_GetTick(); 01182 01183 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */ 01184 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET) 01185 { 01186 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 01187 { 01188 /* Enable the write protection for RTC registers */ 01189 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01190 01191 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01192 01193 /* Process Unlocked */ 01194 __HAL_UNLOCK(hrtc); 01195 01196 return HAL_TIMEOUT; 01197 } 01198 } 01199 } 01200 else 01201 { 01202 /* AlarmB */ 01203 __HAL_RTC_ALARMB_DISABLE(hrtc); 01204 01205 /* In case of interrupt mode is used, the interrupt source must disabled */ 01206 __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB); 01207 01208 /* Get tick */ 01209 tickstart = HAL_GetTick(); 01210 01211 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */ 01212 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET) 01213 { 01214 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 01215 { 01216 /* Enable the write protection for RTC registers */ 01217 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01218 01219 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01220 01221 /* Process Unlocked */ 01222 __HAL_UNLOCK(hrtc); 01223 01224 return HAL_TIMEOUT; 01225 } 01226 } 01227 } 01228 /* Enable the write protection for RTC registers */ 01229 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01230 01231 hrtc->State = HAL_RTC_STATE_READY; 01232 01233 /* Process Unlocked */ 01234 __HAL_UNLOCK(hrtc); 01235 01236 return HAL_OK; 01237 } 01238 01239 /** 01240 * @brief Gets the RTC Alarm value and masks. 01241 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01242 * the configuration information for RTC. 01243 * @param sAlarm Pointer to Date structure 01244 * @param Alarm Specifies the Alarm. 01245 * This parameter can be one of the following values: 01246 * @arg RTC_ALARM_A: AlarmA 01247 * @arg RTC_ALARM_B: AlarmB 01248 * @param Format Specifies the format of the entered parameters. 01249 * This parameter can be one of the following values: 01250 * @arg RTC_FORMAT_BIN: Binary data format 01251 * @arg RTC_FORMAT_BCD: BCD data format 01252 * @retval HAL status 01253 */ 01254 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format) 01255 { 01256 uint32_t tmpreg = 0U, subsecondtmpreg = 0U; 01257 01258 /* Check the parameters */ 01259 assert_param(IS_RTC_FORMAT(Format)); 01260 assert_param(IS_RTC_ALARM(Alarm)); 01261 01262 if(Alarm == RTC_ALARM_A) 01263 { 01264 /* AlarmA */ 01265 sAlarm->Alarm = RTC_ALARM_A; 01266 01267 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR); 01268 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS); 01269 } 01270 else 01271 { 01272 sAlarm->Alarm = RTC_ALARM_B; 01273 01274 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR); 01275 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS); 01276 } 01277 01278 /* Fill the structure with the read parameters */ 01279 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U); 01280 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U); 01281 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)); 01282 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U); 01283 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg; 01284 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24U); 01285 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL); 01286 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL); 01287 01288 if(Format == RTC_FORMAT_BIN) 01289 { 01290 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours); 01291 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes); 01292 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds); 01293 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay); 01294 } 01295 01296 return HAL_OK; 01297 } 01298 01299 /** 01300 * @brief This function handles Alarm interrupt request. 01301 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01302 * the configuration information for RTC. 01303 * @retval None 01304 */ 01305 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc) 01306 { 01307 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA)) 01308 { 01309 /* Get the status of the Interrupt */ 01310 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET) 01311 { 01312 /* AlarmA callback */ 01313 HAL_RTC_AlarmAEventCallback(hrtc); 01314 01315 /* Clear the Alarm interrupt pending bit */ 01316 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF); 01317 } 01318 } 01319 01320 if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB)) 01321 { 01322 /* Get the status of the Interrupt */ 01323 if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET) 01324 { 01325 /* AlarmB callback */ 01326 HAL_RTCEx_AlarmBEventCallback(hrtc); 01327 01328 /* Clear the Alarm interrupt pending bit */ 01329 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF); 01330 } 01331 } 01332 01333 /* Clear the EXTI's line Flag for RTC Alarm */ 01334 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG(); 01335 01336 /* Change RTC state */ 01337 hrtc->State = HAL_RTC_STATE_READY; 01338 } 01339 01340 /** 01341 * @brief Alarm A callback. 01342 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01343 * the configuration information for RTC. 01344 * @retval None 01345 */ 01346 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) 01347 { 01348 /* Prevent unused argument(s) compilation warning */ 01349 UNUSED(hrtc); 01350 /* NOTE : This function Should not be modified, when the callback is needed, 01351 the HAL_RTC_AlarmAEventCallback could be implemented in the user file 01352 */ 01353 } 01354 01355 /** 01356 * @brief This function handles AlarmA Polling request. 01357 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01358 * the configuration information for RTC. 01359 * @param Timeout Timeout duration 01360 * @retval HAL status 01361 */ 01362 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout) 01363 { 01364 uint32_t tickstart = 0U; 01365 01366 /* Get tick */ 01367 tickstart = HAL_GetTick(); 01368 01369 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET) 01370 { 01371 if(Timeout != HAL_MAX_DELAY) 01372 { 01373 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) 01374 { 01375 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01376 return HAL_TIMEOUT; 01377 } 01378 } 01379 } 01380 01381 /* Clear the Alarm interrupt pending bit */ 01382 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF); 01383 01384 /* Change RTC state */ 01385 hrtc->State = HAL_RTC_STATE_READY; 01386 01387 return HAL_OK; 01388 } 01389 01390 /** 01391 * @} 01392 */ 01393 01394 /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions 01395 * @brief Peripheral Control functions 01396 * 01397 @verbatim 01398 =============================================================================== 01399 ##### Peripheral Control functions ##### 01400 =============================================================================== 01401 [..] 01402 This subsection provides functions allowing to 01403 (+) Wait for RTC Time and Date Synchronization 01404 01405 @endverbatim 01406 * @{ 01407 */ 01408 01409 /** 01410 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are 01411 * synchronized with RTC APB clock. 01412 * @note The RTC Resynchronization mode is write protected, use the 01413 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function. 01414 * @note To read the calendar through the shadow registers after Calendar 01415 * initialization, calendar update or after wake-up from low power modes 01416 * the software must first clear the RSF flag. 01417 * The software must then wait until it is set again before reading 01418 * the calendar, which means that the calendar registers have been 01419 * correctly copied into the RTC_TR and RTC_DR shadow registers. 01420 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01421 * the configuration information for RTC. 01422 * @retval HAL status 01423 */ 01424 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc) 01425 { 01426 uint32_t tickstart = 0U; 01427 01428 /* Clear RSF flag */ 01429 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK; 01430 01431 /* Get tick */ 01432 tickstart = HAL_GetTick(); 01433 01434 /* Wait the registers to be synchronised */ 01435 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET) 01436 { 01437 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 01438 { 01439 return HAL_TIMEOUT; 01440 } 01441 } 01442 01443 return HAL_OK; 01444 } 01445 01446 /** 01447 * @} 01448 */ 01449 01450 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions 01451 * @brief Peripheral State functions 01452 * 01453 @verbatim 01454 =============================================================================== 01455 ##### Peripheral State functions ##### 01456 =============================================================================== 01457 [..] 01458 This subsection provides functions allowing to 01459 (+) Get RTC state 01460 01461 @endverbatim 01462 * @{ 01463 */ 01464 /** 01465 * @brief Returns the RTC state. 01466 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01467 * the configuration information for RTC. 01468 * @retval HAL state 01469 */ 01470 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc) 01471 { 01472 return hrtc->State; 01473 } 01474 01475 /** 01476 * @} 01477 */ 01478 01479 /** 01480 * @brief Enters the RTC Initialization mode. 01481 * @note The RTC Initialization mode is write protected, use the 01482 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function. 01483 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains 01484 * the configuration information for RTC. 01485 * @retval HAL status 01486 */ 01487 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc) 01488 { 01489 uint32_t tickstart = 0U; 01490 01491 /* Check if the Initialization mode is set */ 01492 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET) 01493 { 01494 /* Set the Initialization mode */ 01495 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK; 01496 01497 /* Get tick */ 01498 tickstart = HAL_GetTick(); 01499 01500 /* Wait till RTC is in INIT state and if Time out is reached exit */ 01501 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET) 01502 { 01503 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE) 01504 { 01505 return HAL_TIMEOUT; 01506 } 01507 } 01508 } 01509 01510 return HAL_OK; 01511 } 01512 01513 01514 /** 01515 * @brief Converts a 2 digit decimal to BCD format. 01516 * @param Value Byte to be converted 01517 * @retval Converted byte 01518 */ 01519 uint8_t RTC_ByteToBcd2(uint8_t Value) 01520 { 01521 uint32_t bcdhigh = 0U; 01522 01523 while(Value >= 10U) 01524 { 01525 bcdhigh++; 01526 Value -= 10U; 01527 } 01528 01529 return ((uint8_t)(bcdhigh << 4U) | Value); 01530 } 01531 01532 /** 01533 * @brief Converts from 2 digit BCD to Binary. 01534 * @param Value BCD value to be converted 01535 * @retval Converted word 01536 */ 01537 uint8_t RTC_Bcd2ToByte(uint8_t Value) 01538 { 01539 uint32_t tmp = 0U; 01540 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10; 01541 return (tmp + (Value & (uint8_t)0x0F)); 01542 } 01543 01544 /** 01545 * @} 01546 */ 01547 01548 #endif /* HAL_RTC_MODULE_ENABLED */ 01549 /** 01550 * @} 01551 */ 01552 01553 /** 01554 * @} 01555 */ 01556 01557 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/