STM32L443xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_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/de-initialization functions 00009 * + Calendar (Time and Date) configuration 00010 * + Alarms (Alarm A and Alarm B) configuration 00011 * + WakeUp Timer configuration 00012 * + TimeStamp configuration 00013 * + Tampers configuration 00014 * + Backup Data Registers configuration 00015 * + RTC Tamper and TimeStamp Pins Selection 00016 * + Interrupts and flags management 00017 * 00018 ****************************************************************************** 00019 * @attention 00020 * 00021 * Copyright (c) 2017 STMicroelectronics. 00022 * All rights reserved. 00023 * 00024 * This software is licensed under terms that can be found in the LICENSE file 00025 * in the root directory of this software component. 00026 * If no LICENSE file comes with this software, it is provided AS-IS. 00027 * 00028 ****************************************************************************** 00029 @verbatim 00030 =============================================================================== 00031 ##### RTC Operating Condition ##### 00032 =============================================================================== 00033 [..] The real-time clock (RTC) and the RTC backup registers can be powered 00034 from the VBAT voltage when the main VDD supply is powered off. 00035 To retain the content of the RTC backup registers and supply the RTC 00036 when VDD is turned off, VBAT pin can be connected to an optional 00037 standby voltage supplied by a battery or by another source. 00038 00039 ##### Backup Domain Reset ##### 00040 =============================================================================== 00041 [..] The backup domain reset sets all RTC registers and the RCC_BDCR register 00042 to their reset values. 00043 A backup domain reset is generated when one of the following events occurs: 00044 (#) Software reset, triggered by setting the BDRST bit in the 00045 RCC Backup domain control register (RCC_BDCR). 00046 (#) VDD or VBAT power on, if both supplies have previously been powered off. 00047 (#) Tamper detection event resets all data backup registers. 00048 00049 ##### Backup Domain Access ##### 00050 ================================================================== 00051 [..] After reset, the backup domain (RTC registers and RTC backup data registers) 00052 is protected against possible unwanted write accesses. 00053 [..] To enable access to the RTC Domain and RTC registers, proceed as follows: 00054 (+) Enable the Power Controller (PWR) APB1 interface clock using the 00055 __HAL_RCC_PWR_CLK_ENABLE() function. 00056 (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function. 00057 (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function. 00058 (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function. 00059 00060 [..] To enable access to the RTC Domain and RTC registers, proceed as follows: 00061 (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for 00062 PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32) 00063 (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro. 00064 00065 ##### How to use RTC Driver ##### 00066 =================================================================== 00067 [..] 00068 (+) Enable the RTC domain access (see description in the section above). 00069 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour 00070 format using the HAL_RTC_Init() function. 00071 00072 *** Time and Date configuration *** 00073 =================================== 00074 [..] 00075 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime() 00076 and HAL_RTC_SetDate() functions. 00077 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions. 00078 00079 *** Alarm configuration *** 00080 =========================== 00081 [..] 00082 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function. 00083 You can also configure the RTC Alarm with interrupt mode using the 00084 HAL_RTC_SetAlarm_IT() function. 00085 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function. 00086 00087 ##### RTC and low power modes ##### 00088 ================================================================== 00089 [..] The MCU can be woken up from a low power mode by an RTC alternate 00090 function. 00091 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B), 00092 RTC wakeup, RTC tamper event detection and RTC time stamp event detection. 00093 These RTC alternate functions can wake up the system from the Stop and 00094 Standby low power modes. 00095 [..] The system can also wake up from low power modes without depending 00096 on an external interrupt (Auto-wakeup mode), by using the RTC alarm 00097 or the RTC wakeup events. 00098 [..] The RTC provides a programmable time base for waking up from the 00099 Stop or Standby mode at regular intervals. 00100 Wakeup from STOP and STANDBY modes is possible only when the RTC clock source 00101 is LSE or LSI. 00102 00103 *** Callback registration *** 00104 ============================================= 00105 00106 [..] 00107 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or 00108 not defined, the callback registration feature is not available and all callbacks 00109 are set to the corresponding weak functions. This is the recommended configuration 00110 in order to optimize memory/code consumption footprint/performances. 00111 00112 [..] 00113 The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1 00114 allows the user to configure dynamically the driver callbacks. 00115 Use Function HAL_RTC_RegisterCallback() to register an interrupt callback. 00116 00117 [..] 00118 Function HAL_RTC_RegisterCallback() allows to register following callbacks: 00119 (+) AlarmAEventCallback : RTC Alarm A Event callback. 00120 (+) AlarmBEventCallback : RTC Alarm B Event callback. 00121 (+) TimeStampEventCallback : RTC TimeStamp Event callback. 00122 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback. 00123 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00124 (+) SSRUEventCallback : RTC SSRU Event callback. 00125 #endif 00126 (+) Tamper1EventCallback : RTC Tamper 1 Event callback. 00127 (+) Tamper2EventCallback : RTC Tamper 2 Event callback. 00128 (+) Tamper3EventCallback : RTC Tamper 3 Event callback. 00129 (+) MspInitCallback : RTC MspInit callback. 00130 (+) MspDeInitCallback : RTC MspDeInit callback. 00131 This function takes as parameters the HAL peripheral handle, the Callback ID 00132 and a pointer to the user callback function. 00133 00134 [..] 00135 Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default 00136 weak function. 00137 HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00138 and the Callback ID. 00139 This function allows to reset following callbacks: 00140 (+) AlarmAEventCallback : RTC Alarm A Event callback. 00141 (+) AlarmBEventCallback : RTC Alarm B Event callback. 00142 (+) TimeStampEventCallback : RTC TimeStamp Event callback. 00143 (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback. 00144 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00145 (+) SSRUEventCallback : RTC SSRU Event callback. 00146 #endif 00147 (+) Tamper1EventCallback : RTC Tamper 1 Event callback. 00148 (+) Tamper2EventCallback : RTC Tamper 2 Event callback. 00149 (+) Tamper3EventCallback : RTC Tamper 3 Event callback. 00150 (+) MspInitCallback : RTC MspInit callback. 00151 (+) MspDeInitCallback : RTC MspDeInit callback. 00152 00153 [..] 00154 By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET, 00155 all callbacks are set to the corresponding weak functions : 00156 examples AlarmAEventCallback(), TimeStampEventCallback(). 00157 Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function 00158 in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null 00159 (not registered beforehand). 00160 If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit() 00161 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) 00162 00163 [..] 00164 Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only. 00165 Exception done MspInit/MspDeInit that can be registered/unregistered 00166 in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state, 00167 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. 00168 In that case first register the MspInit/MspDeInit user callbacks 00169 using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit() 00170 or HAL_RTC_Init() function. 00171 00172 [..] 00173 When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or 00174 not defined, the callback registration feature is not available and all callbacks 00175 are set to the corresponding weak functions. 00176 00177 @endverbatim 00178 ****************************************************************************** 00179 */ 00180 00181 /* Includes ------------------------------------------------------------------*/ 00182 #include "stm32l4xx_hal.h" 00183 00184 /** @addtogroup STM32L4xx_HAL_Driver 00185 * @{ 00186 */ 00187 00188 00189 /** @addtogroup RTC 00190 * @brief RTC HAL module driver 00191 * @{ 00192 */ 00193 00194 #ifdef HAL_RTC_MODULE_ENABLED 00195 00196 /* Private typedef -----------------------------------------------------------*/ 00197 /* Private define ------------------------------------------------------------*/ 00198 /* Private macro -------------------------------------------------------------*/ 00199 /* Private variables ---------------------------------------------------------*/ 00200 /* Private function prototypes -----------------------------------------------*/ 00201 /* Exported functions --------------------------------------------------------*/ 00202 00203 /** @addtogroup RTC_Exported_Functions 00204 * @{ 00205 */ 00206 00207 /** @addtogroup RTC_Exported_Functions_Group1 00208 * @brief Initialization and Configuration functions 00209 * 00210 @verbatim 00211 =============================================================================== 00212 ##### Initialization and de-initialization functions ##### 00213 =============================================================================== 00214 [..] This section provides functions allowing to initialize and configure the 00215 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable 00216 RTC registers Write protection, enter and exit the RTC initialization mode, 00217 RTC registers synchronization check and reference clock detection enable. 00218 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base. 00219 It is split into 2 programmable prescalers to minimize power consumption. 00220 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler. 00221 (++) When both prescalers are used, it is recommended to configure the 00222 asynchronous prescaler to a high value to minimize power consumption. 00223 (#) All RTC registers are Write protected. Writing to the RTC registers 00224 is enabled by writing a key into the Write Protection register, RTC_WPR. 00225 (#) To configure the RTC Calendar, user application should enter 00226 initialization mode. In this mode, the calendar counter is stopped 00227 and its value can be updated. When the initialization sequence is 00228 complete, the calendar restarts counting after 4 RTCCLK cycles. 00229 (#) To read the calendar through the shadow registers after Calendar 00230 initialization, calendar update or after wakeup from low power modes 00231 the software must first clear the RSF flag. The software must then 00232 wait until it is set again before reading the calendar, which means 00233 that the calendar registers have been correctly copied into the 00234 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function 00235 implements the above software sequence (RSF clear and RSF check). 00236 00237 @endverbatim 00238 * @{ 00239 */ 00240 00241 /** 00242 * @brief Initialize the RTC peripheral 00243 * @param hrtc RTC handle 00244 * @retval HAL status 00245 */ 00246 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc) 00247 { 00248 HAL_StatusTypeDef status = HAL_ERROR; 00249 00250 /* Check the RTC peripheral state */ 00251 if (hrtc != NULL) 00252 { 00253 /* Check the parameters */ 00254 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance)); 00255 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat)); 00256 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv)); 00257 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv)); 00258 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut)); 00259 assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap)); 00260 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity)); 00261 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType)); 00262 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00263 assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp)); 00264 #endif 00265 00266 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00267 assert_param(IS_RTC_BINARY_MODE(hrtc->Init.BinMode)); 00268 assert_param(IS_RTC_BINARY_MIX_BCDU(hrtc->Init.BinMixBcdU)); 00269 #endif 00270 00271 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 00272 if (hrtc->State == HAL_RTC_STATE_RESET) 00273 { 00274 /* Allocate lock resource and initialize it */ 00275 hrtc->Lock = HAL_UNLOCKED; 00276 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */ 00277 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */ 00278 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */ 00279 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */ 00280 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00281 hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback; /* Legacy weak SSRUEventCallback */ 00282 #endif 00283 00284 #if defined(RTC_TAMPER1_SUPPORT) 00285 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */ 00286 #endif /* RTC_TAMPER1_SUPPORT */ 00287 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */ 00288 #if defined(RTC_TAMPER3_SUPPORT) 00289 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */ 00290 #endif /* RTC_TAMPER3_SUPPORT */ 00291 00292 if (hrtc->MspInitCallback == NULL) 00293 { 00294 hrtc->MspInitCallback = HAL_RTC_MspInit; 00295 } 00296 /* Init the low level hardware */ 00297 hrtc->MspInitCallback(hrtc); 00298 00299 if (hrtc->MspDeInitCallback == NULL) 00300 { 00301 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit; 00302 } 00303 } 00304 #else /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */ 00305 if (hrtc->State == HAL_RTC_STATE_RESET) 00306 { 00307 /* Allocate lock resource and initialize it */ 00308 hrtc->Lock = HAL_UNLOCKED; 00309 00310 /* Initialize RTC MSP */ 00311 HAL_RTC_MspInit(hrtc); 00312 } 00313 #endif /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */ 00314 00315 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00316 /* Process TAMP ip offset from RTC one */ 00317 hrtc->TampOffset = (TAMP_BASE - RTC_BASE); 00318 #endif 00319 /* Set RTC state */ 00320 hrtc->State = HAL_RTC_STATE_BUSY; 00321 00322 /* Disable the write protection for RTC registers */ 00323 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00324 00325 /* Enter Initialization mode */ 00326 status = RTC_EnterInitMode(hrtc); 00327 00328 if (status == HAL_OK) 00329 { 00330 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00331 /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */ 00332 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE); 00333 #else 00334 /* Clear RTC_CR FMT, OSEL and POL Bits */ 00335 hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL); 00336 #endif 00337 /* Set RTC_CR register */ 00338 hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity); 00339 00340 /* Configure the RTC PRER */ 00341 hrtc->Instance->PRER = (hrtc->Init.SynchPrediv); 00342 hrtc->Instance->PRER |= (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos); 00343 00344 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00345 /* Configure the Binary mode */ 00346 MODIFY_REG(RTC->ICSR, RTC_ICSR_BIN | RTC_ICSR_BCDU, hrtc->Init.BinMode | hrtc->Init.BinMixBcdU); 00347 #endif 00348 00349 /* Exit Initialization mode */ 00350 status = RTC_ExitInitMode(hrtc); 00351 00352 if (status == HAL_OK) 00353 { 00354 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00355 hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN); 00356 hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap); 00357 #else 00358 hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP); 00359 hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap); 00360 #endif 00361 00362 /* Enable the write protection for RTC registers */ 00363 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00364 00365 hrtc->State = HAL_RTC_STATE_READY; 00366 } 00367 } 00368 } 00369 00370 return status; 00371 } 00372 00373 /** 00374 * @brief DeInitialize the RTC peripheral. 00375 * @note This function does not reset the RTC Backup Data registers. 00376 * @param hrtc RTC handle 00377 * @retval HAL status 00378 */ 00379 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc) 00380 { 00381 HAL_StatusTypeDef status = HAL_ERROR; 00382 00383 /* Check the RTC peripheral state */ 00384 if (hrtc != NULL) 00385 { 00386 /* Check the parameters */ 00387 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance)); 00388 00389 /* Set RTC state */ 00390 hrtc->State = HAL_RTC_STATE_BUSY; 00391 00392 /* Disable the write protection for RTC registers */ 00393 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00394 00395 /* Enter Initialization mode */ 00396 status = RTC_EnterInitMode(hrtc); 00397 00398 if (status == HAL_OK) 00399 { 00400 /* Reset all RTC CR register bits */ 00401 hrtc->Instance->TR = 0x00000000U; 00402 hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0)); 00403 hrtc->Instance->CR &= 0x00000000U; 00404 00405 hrtc->Instance->WUTR = RTC_WUTR_WUT; 00406 hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU)); 00407 hrtc->Instance->ALRMAR = 0x00000000U; 00408 hrtc->Instance->ALRMBR = 0x00000000U; 00409 hrtc->Instance->SHIFTR = 0x00000000U; 00410 hrtc->Instance->CALR = 0x00000000U; 00411 hrtc->Instance->ALRMASSR = 0x00000000U; 00412 hrtc->Instance->ALRMBSSR = 0x00000000U; 00413 00414 /* Exit initialization mode */ 00415 status = RTC_ExitInitMode(hrtc); 00416 00417 00418 if (status == HAL_OK) 00419 { 00420 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00421 /* Reset TAMP registers */ 00422 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR1 = 0xFFFF0000U; 00423 ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR2 = 0x00000000U; 00424 #else 00425 /* Reset Tamper configuration register */ 00426 hrtc->Instance->TAMPCR = 0x00000000U; 00427 00428 /* Reset Option register */ 00429 hrtc->Instance->OR = 0x00000000U; 00430 #endif 00431 00432 /* Enable the write protection for RTC registers */ 00433 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00434 00435 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 00436 if (hrtc->MspDeInitCallback == NULL) 00437 { 00438 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit; 00439 } 00440 00441 /* DeInit the low level hardware: CLOCK, NVIC.*/ 00442 hrtc->MspDeInitCallback(hrtc); 00443 #else 00444 /* De-Initialize RTC MSP */ 00445 HAL_RTC_MspDeInit(hrtc); 00446 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */ 00447 00448 hrtc->State = HAL_RTC_STATE_RESET; 00449 00450 /* Release Lock */ 00451 __HAL_UNLOCK(hrtc); 00452 } 00453 } 00454 } 00455 00456 return status; 00457 } 00458 00459 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 00460 /** 00461 * @brief Register a User RTC Callback 00462 * To be used instead of the weak predefined callback 00463 * @param hrtc RTC handle 00464 * @param CallbackID ID of the callback to be registered 00465 * This parameter can be one of the following values: 00466 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID 00467 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID 00468 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID 00469 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID 00470 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID 00471 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID 00472 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID 00473 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID 00474 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID 00475 * @param pCallback pointer to the Callback function 00476 * @retval HAL status 00477 */ 00478 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback) 00479 { 00480 HAL_StatusTypeDef status = HAL_OK; 00481 00482 if (pCallback == NULL) 00483 { 00484 return HAL_ERROR; 00485 } 00486 00487 /* Process locked */ 00488 __HAL_LOCK(hrtc); 00489 00490 if (HAL_RTC_STATE_READY == hrtc->State) 00491 { 00492 switch (CallbackID) 00493 { 00494 case HAL_RTC_ALARM_A_EVENT_CB_ID : 00495 hrtc->AlarmAEventCallback = pCallback; 00496 break; 00497 00498 case HAL_RTC_ALARM_B_EVENT_CB_ID : 00499 hrtc->AlarmBEventCallback = pCallback; 00500 break; 00501 00502 case HAL_RTC_TIMESTAMP_EVENT_CB_ID : 00503 hrtc->TimeStampEventCallback = pCallback; 00504 break; 00505 00506 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID : 00507 hrtc->WakeUpTimerEventCallback = pCallback; 00508 break; 00509 00510 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00511 case HAL_RTC_SSRU_EVENT_CB_ID : 00512 hrtc->SSRUEventCallback = pCallback; 00513 break; 00514 #endif 00515 00516 #if defined(RTC_TAMPER1_SUPPORT) 00517 case HAL_RTC_TAMPER1_EVENT_CB_ID : 00518 hrtc->Tamper1EventCallback = pCallback; 00519 break; 00520 #endif /* RTC_TAMPER1_SUPPORT */ 00521 00522 case HAL_RTC_TAMPER2_EVENT_CB_ID : 00523 hrtc->Tamper2EventCallback = pCallback; 00524 break; 00525 00526 #if defined(RTC_TAMPER3_SUPPORT) 00527 case HAL_RTC_TAMPER3_EVENT_CB_ID : 00528 hrtc->Tamper3EventCallback = pCallback; 00529 break; 00530 #endif /* RTC_TAMPER3_SUPPORT */ 00531 00532 case HAL_RTC_MSPINIT_CB_ID : 00533 hrtc->MspInitCallback = pCallback; 00534 break; 00535 00536 case HAL_RTC_MSPDEINIT_CB_ID : 00537 hrtc->MspDeInitCallback = pCallback; 00538 break; 00539 00540 default : 00541 /* Return error status */ 00542 status = HAL_ERROR; 00543 break; 00544 } 00545 } 00546 else if (HAL_RTC_STATE_RESET == hrtc->State) 00547 { 00548 switch (CallbackID) 00549 { 00550 case HAL_RTC_MSPINIT_CB_ID : 00551 hrtc->MspInitCallback = pCallback; 00552 break; 00553 00554 case HAL_RTC_MSPDEINIT_CB_ID : 00555 hrtc->MspDeInitCallback = pCallback; 00556 break; 00557 00558 default : 00559 /* Return error status */ 00560 status = HAL_ERROR; 00561 break; 00562 } 00563 } 00564 else 00565 { 00566 /* Return error status */ 00567 status = HAL_ERROR; 00568 } 00569 00570 /* Release Lock */ 00571 __HAL_UNLOCK(hrtc); 00572 00573 return status; 00574 } 00575 00576 /** 00577 * @brief Unregister an RTC Callback 00578 * RTC callback is redirected to the weak predefined callback 00579 * @param hrtc RTC handle 00580 * @param CallbackID ID of the callback to be unregistered 00581 * This parameter can be one of the following values: 00582 * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID 00583 * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID 00584 * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID 00585 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00586 * @arg @ref HAL_RTC_SSRU_EVENT_CB_ID SSRU Callback ID 00587 #endif 00588 * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID 00589 * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID 00590 * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID 00591 * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID 00592 * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID 00593 * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID 00594 * @retval HAL status 00595 */ 00596 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID) 00597 { 00598 HAL_StatusTypeDef status = HAL_OK; 00599 00600 /* Process locked */ 00601 __HAL_LOCK(hrtc); 00602 00603 if (HAL_RTC_STATE_READY == hrtc->State) 00604 { 00605 switch (CallbackID) 00606 { 00607 case HAL_RTC_ALARM_A_EVENT_CB_ID : 00608 hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */ 00609 break; 00610 00611 case HAL_RTC_ALARM_B_EVENT_CB_ID : 00612 hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */ 00613 break; 00614 00615 case HAL_RTC_TIMESTAMP_EVENT_CB_ID : 00616 hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */ 00617 break; 00618 00619 case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID : 00620 hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */ 00621 break; 00622 00623 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00624 case HAL_RTC_SSRU_EVENT_CB_ID : 00625 hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback; /* Legacy weak SSRUEventCallback */ 00626 break; 00627 #endif 00628 00629 #if defined(RTC_TAMPER1_SUPPORT) 00630 case HAL_RTC_TAMPER1_EVENT_CB_ID : 00631 hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */ 00632 break; 00633 #endif /* RTC_TAMPER1_SUPPORT */ 00634 00635 case HAL_RTC_TAMPER2_EVENT_CB_ID : 00636 hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */ 00637 break; 00638 00639 #if defined(RTC_TAMPER3_SUPPORT) 00640 case HAL_RTC_TAMPER3_EVENT_CB_ID : 00641 hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */ 00642 break; 00643 #endif /* RTC_TAMPER3_SUPPORT */ 00644 00645 case HAL_RTC_MSPINIT_CB_ID : 00646 hrtc->MspInitCallback = HAL_RTC_MspInit; 00647 break; 00648 00649 case HAL_RTC_MSPDEINIT_CB_ID : 00650 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit; 00651 break; 00652 00653 default : 00654 /* Return error status */ 00655 status = HAL_ERROR; 00656 break; 00657 } 00658 } 00659 else if (HAL_RTC_STATE_RESET == hrtc->State) 00660 { 00661 switch (CallbackID) 00662 { 00663 case HAL_RTC_MSPINIT_CB_ID : 00664 hrtc->MspInitCallback = HAL_RTC_MspInit; 00665 break; 00666 00667 case HAL_RTC_MSPDEINIT_CB_ID : 00668 hrtc->MspDeInitCallback = HAL_RTC_MspDeInit; 00669 break; 00670 00671 default : 00672 /* Return error status */ 00673 status = HAL_ERROR; 00674 break; 00675 } 00676 } 00677 else 00678 { 00679 /* Return error status */ 00680 status = HAL_ERROR; 00681 } 00682 00683 /* Release Lock */ 00684 __HAL_UNLOCK(hrtc); 00685 00686 return status; 00687 } 00688 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */ 00689 00690 /** 00691 * @brief Initialize the RTC MSP. 00692 * @param hrtc RTC handle 00693 * @retval None 00694 */ 00695 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc) 00696 { 00697 /* Prevent unused argument(s) compilation warning */ 00698 UNUSED(hrtc); 00699 00700 /* NOTE : This function should not be modified, when the callback is needed, 00701 the HAL_RTC_MspInit could be implemented in the user file 00702 */ 00703 } 00704 00705 /** 00706 * @brief DeInitialize the RTC MSP. 00707 * @param hrtc RTC handle 00708 * @retval None 00709 */ 00710 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc) 00711 { 00712 /* Prevent unused argument(s) compilation warning */ 00713 UNUSED(hrtc); 00714 00715 /* NOTE : This function should not be modified, when the callback is needed, 00716 the HAL_RTC_MspDeInit could be implemented in the user file 00717 */ 00718 } 00719 00720 /** 00721 * @} 00722 */ 00723 00724 /** @addtogroup RTC_Exported_Functions_Group2 00725 * @brief RTC Time and Date functions 00726 * 00727 @verbatim 00728 =============================================================================== 00729 ##### RTC Time and Date functions ##### 00730 =============================================================================== 00731 00732 [..] This section provides functions allowing to configure Time and Date features 00733 00734 @endverbatim 00735 * @{ 00736 */ 00737 00738 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) 00739 /** 00740 * @brief Set RTC current time. 00741 * @param hrtc RTC handle 00742 * @param sTime Pointer to Time structure 00743 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used and RTC_SSR will be automatically reset to 0xFFFFFFFF 00744 else sTime->SubSeconds is not used and RTC_SSR will be automatically reset to the A 7-bit async prescaler (RTC_PRER_PREDIV_A) 00745 * @param Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds. 00746 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used 00747 * else this parameter can be one of the following values 00748 * @arg RTC_FORMAT_BIN: Binary format 00749 * @arg RTC_FORMAT_BCD: BCD format 00750 * @retval HAL status 00751 */ 00752 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 00753 { 00754 uint32_t tmpreg; 00755 HAL_StatusTypeDef status; 00756 00757 #ifdef USE_FULL_ASSERT 00758 /* Check the parameters depending of the Binary mode with 32-bit free-running counter configuration. */ 00759 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE) 00760 { 00761 /* Check the parameters */ 00762 assert_param(IS_RTC_FORMAT(Format)); 00763 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving)); 00764 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation)); 00765 } 00766 #endif 00767 00768 /* Process Locked */ 00769 __HAL_LOCK(hrtc); 00770 00771 hrtc->State = HAL_RTC_STATE_BUSY; 00772 00773 /* Disable the write protection for RTC registers */ 00774 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00775 00776 /* Enter Initialization mode */ 00777 status = RTC_EnterInitMode(hrtc); 00778 if (status == HAL_OK) 00779 { 00780 /* Check Binary mode ((32-bit free-running counter) */ 00781 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY) 00782 { 00783 if (Format == RTC_FORMAT_BIN) 00784 { 00785 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U) 00786 { 00787 assert_param(IS_RTC_HOUR12(sTime->Hours)); 00788 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat)); 00789 } 00790 else 00791 { 00792 sTime->TimeFormat = 0x00U; 00793 assert_param(IS_RTC_HOUR24(sTime->Hours)); 00794 } 00795 assert_param(IS_RTC_MINUTES(sTime->Minutes)); 00796 assert_param(IS_RTC_SECONDS(sTime->Seconds)); 00797 00798 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \ 00799 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \ 00800 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \ 00801 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos)); 00802 00803 } 00804 else 00805 { 00806 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U) 00807 { 00808 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours))); 00809 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat)); 00810 } 00811 else 00812 { 00813 sTime->TimeFormat = 0x00U; 00814 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours))); 00815 } 00816 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes))); 00817 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds))); 00818 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \ 00819 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \ 00820 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \ 00821 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos)); 00822 } 00823 00824 /* Set the RTC_TR register */ 00825 WRITE_REG(RTC->TR, (tmpreg & RTC_TR_RESERVED_MASK)); 00826 00827 /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */ 00828 CLEAR_BIT(RTC->CR, RTC_CR_BKP); 00829 00830 /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */ 00831 SET_BIT(RTC->CR, (sTime->DayLightSaving | sTime->StoreOperation)); 00832 } 00833 } 00834 00835 /* Exit Initialization mode */ 00836 status = RTC_ExitInitMode(hrtc); 00837 00838 00839 /* Enable the write protection for RTC registers */ 00840 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00841 00842 if (status == HAL_OK) 00843 { 00844 hrtc->State = HAL_RTC_STATE_READY; 00845 } 00846 00847 /* Process Unlocked */ 00848 __HAL_UNLOCK(hrtc); 00849 00850 return status; 00851 } 00852 00853 /** 00854 * @brief Get RTC current time. 00855 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds 00856 * value in second fraction ratio with time unit following generic formula: 00857 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit 00858 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS 00859 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 00860 * in the higher-order calendar shadow registers to ensure consistency between the time and date values. 00861 * Reading RTC current time locks the values in calendar shadow registers until Current date is read 00862 * to ensure consistency between the time and date values. 00863 * @param hrtc RTC handle 00864 * @param sTime 00865 * if Binary mode is RTC_BINARY_ONLY, sTime->SubSeconds only is updated 00866 * else 00867 * Pointer to Time structure with Hours, Minutes and Seconds fields returned 00868 * with input format (BIN or BCD), also SubSeconds field returning the 00869 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler 00870 * factor to be used for second fraction ratio computation. 00871 * @param Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds. 00872 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used 00873 * else this parameter can be one of the following values: 00874 * @arg RTC_FORMAT_BIN: Binary format 00875 * @arg RTC_FORMAT_BCD: BCD format 00876 * @retval HAL status 00877 */ 00878 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 00879 { 00880 uint32_t tmpreg; 00881 00882 UNUSED(hrtc); 00883 /* Get subseconds structure field from the corresponding register*/ 00884 sTime->SubSeconds = READ_REG(RTC->SSR); 00885 00886 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY) 00887 { 00888 /* Check the parameters */ 00889 assert_param(IS_RTC_FORMAT(Format)); 00890 00891 /* Get SecondFraction structure field from the corresponding register field*/ 00892 sTime->SecondFraction = (uint32_t)(READ_REG(RTC->PRER) & RTC_PRER_PREDIV_S); 00893 00894 /* Get the TR register */ 00895 tmpreg = (uint32_t)(READ_REG(RTC->TR) & RTC_TR_RESERVED_MASK); 00896 00897 /* Fill the structure fields with the read parameters */ 00898 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos); 00899 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos); 00900 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos); 00901 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos); 00902 00903 /* Check the input parameters format */ 00904 if (Format == RTC_FORMAT_BIN) 00905 { 00906 /* Convert the time structure parameters to Binary format */ 00907 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours); 00908 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes); 00909 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds); 00910 } 00911 } 00912 00913 return HAL_OK; 00914 } 00915 00916 /** 00917 * @brief Set RTC current date. 00918 * @param hrtc RTC handle 00919 * @param sDate Pointer to date structure 00920 * @param Format Format of sDate->Year, sDate->Month and sDate->Weekday. 00921 * This parameter can be one of the following values: 00922 * @arg RTC_FORMAT_BIN: Binary format 00923 * @arg RTC_FORMAT_BCD: BCD format 00924 * @retval HAL status 00925 */ 00926 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) 00927 { 00928 uint32_t datetmpreg; 00929 HAL_StatusTypeDef status; 00930 00931 /* Check the parameters */ 00932 assert_param(IS_RTC_FORMAT(Format)); 00933 00934 /* Process Locked */ 00935 __HAL_LOCK(hrtc); 00936 00937 hrtc->State = HAL_RTC_STATE_BUSY; 00938 00939 if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U)) 00940 { 00941 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU); 00942 } 00943 00944 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay)); 00945 00946 if (Format == RTC_FORMAT_BIN) 00947 { 00948 assert_param(IS_RTC_YEAR(sDate->Year)); 00949 assert_param(IS_RTC_MONTH(sDate->Month)); 00950 assert_param(IS_RTC_DATE(sDate->Date)); 00951 00952 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \ 00953 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \ 00954 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \ 00955 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos)); 00956 } 00957 else 00958 { 00959 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year))); 00960 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month))); 00961 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date))); 00962 00963 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \ 00964 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \ 00965 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \ 00966 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos)); 00967 } 00968 00969 /* Disable the write protection for RTC registers */ 00970 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 00971 00972 /* Enter Initialization mode */ 00973 status = RTC_EnterInitMode(hrtc); 00974 if (status == HAL_OK) 00975 { 00976 /* Set the RTC_DR register */ 00977 WRITE_REG(RTC->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK)); 00978 00979 /* Exit Initialization mode */ 00980 status = RTC_ExitInitMode(hrtc); 00981 } 00982 00983 /* Enable the write protection for RTC registers */ 00984 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 00985 00986 if (status == HAL_OK) 00987 { 00988 hrtc->State = HAL_RTC_STATE_READY ; 00989 } 00990 00991 /* Process Unlocked */ 00992 __HAL_UNLOCK(hrtc); 00993 00994 return status; 00995 } 00996 00997 /** 00998 * @brief Get RTC current date. 00999 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 01000 * in the higher-order calendar shadow registers to ensure consistency between the time and date values. 01001 * Reading RTC current time locks the values in calendar shadow registers until Current date is read. 01002 * @param hrtc RTC handle 01003 * @param sDate Pointer to Date structure 01004 * @param Format Format of sDate->Year, sDate->Month and sDate->Weekday. 01005 * This parameter can be one of the following values: 01006 * @arg RTC_FORMAT_BIN: Binary format 01007 * @arg RTC_FORMAT_BCD: BCD format 01008 * @retval HAL status 01009 */ 01010 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) 01011 { 01012 uint32_t datetmpreg; 01013 01014 UNUSED(hrtc); 01015 /* Check the parameters */ 01016 assert_param(IS_RTC_FORMAT(Format)); 01017 01018 /* Get the DR register */ 01019 datetmpreg = (uint32_t)(READ_REG(RTC->DR) & RTC_DR_RESERVED_MASK); 01020 01021 /* Fill the structure fields with the read parameters */ 01022 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos); 01023 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos); 01024 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos); 01025 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos); 01026 01027 /* Check the input parameters format */ 01028 if (Format == RTC_FORMAT_BIN) 01029 { 01030 /* Convert the date structure parameters to Binary format */ 01031 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year); 01032 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month); 01033 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date); 01034 } 01035 return HAL_OK; 01036 } 01037 01038 /** 01039 * @} 01040 */ 01041 01042 /** @addtogroup RTC_Exported_Functions_Group3 01043 * @brief RTC Alarm functions 01044 * 01045 @verbatim 01046 =============================================================================== 01047 ##### RTC Alarm functions ##### 01048 =============================================================================== 01049 01050 [..] This section provides functions allowing to configure Alarm feature 01051 01052 @endverbatim 01053 * @{ 01054 */ 01055 /** 01056 * @brief Set the specified RTC Alarm. 01057 * @param hrtc RTC handle 01058 * @param sAlarm Pointer to Alarm structure 01059 * if Binary mode is RTC_BINARY_ONLY, 3 fields only are used 01060 * sAlarm->AlarmTime.SubSeconds 01061 * sAlarm->AlarmSubSecondMask 01062 * sAlarm->BinaryAutoClr 01063 * @param Format of the entered parameters. 01064 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used 01065 * else this parameter can be one of the following values 01066 * @arg RTC_FORMAT_BIN: Binary format 01067 * @arg RTC_FORMAT_BCD: BCD format 01068 * @retval HAL status 01069 */ 01070 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format) 01071 { 01072 uint32_t tmpreg = 0, binaryMode; 01073 01074 __HAL_LOCK(hrtc); 01075 hrtc->State = HAL_RTC_STATE_BUSY; 01076 01077 #ifdef USE_FULL_ASSERT 01078 /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration). */ 01079 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE) 01080 { 01081 assert_param(IS_RTC_FORMAT(Format)); 01082 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 01083 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 01084 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 01085 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds)); 01086 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask)); 01087 } 01088 else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY) 01089 { 01090 assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask)); 01091 assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr)); 01092 } 01093 else /* RTC_BINARY_MIX */ 01094 { 01095 assert_param(IS_RTC_FORMAT(Format)); 01096 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 01097 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 01098 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 01099 /* In Binary Mix Mode, the RTC can not generate an alarm on a match involving all calendar items + the upper SSR bits */ 01100 assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <= (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos))); 01101 } 01102 #endif 01103 01104 /* Get Binary mode (32-bit free-running counter configuration) */ 01105 binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN); 01106 01107 if (binaryMode != RTC_BINARY_ONLY) 01108 { 01109 if (Format == RTC_FORMAT_BIN) 01110 { 01111 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U) 01112 { 01113 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours)); 01114 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01115 } 01116 else 01117 { 01118 sAlarm->AlarmTime.TimeFormat = 0x00U; 01119 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours)); 01120 } 01121 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes)); 01122 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds)); 01123 01124 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01125 { 01126 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay)); 01127 } 01128 else 01129 { 01130 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay)); 01131 } 01132 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01133 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01134 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01135 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01136 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01137 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01138 ((uint32_t)sAlarm->AlarmMask)); 01139 } 01140 else /* format BCD */ 01141 { 01142 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U) 01143 { 01144 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01145 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01146 } 01147 else 01148 { 01149 sAlarm->AlarmTime.TimeFormat = 0x00U; 01150 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01151 } 01152 01153 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes))); 01154 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds))); 01155 01156 #ifdef USE_FULL_ASSERT 01157 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01158 { 01159 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01160 } 01161 else 01162 { 01163 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01164 } 01165 01166 #endif /* USE_FULL_ASSERT */ 01167 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01168 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01169 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01170 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01171 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01172 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01173 ((uint32_t)sAlarm->AlarmMask)); 01174 } 01175 } 01176 01177 /* Disable the write protection for RTC registers */ 01178 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01179 01180 /* Configure the Alarm register */ 01181 if (sAlarm->Alarm == RTC_ALARM_A) 01182 { 01183 /* Disable the Alarm A interrupt */ 01184 /* In case of interrupt mode is used, the interrupt source must disabled */ 01185 CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE)); 01186 01187 /* Clear flag alarm A */ 01188 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF); 01189 01190 if (binaryMode == RTC_BINARY_ONLY) 01191 { 01192 WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr); 01193 } 01194 else 01195 { 01196 WRITE_REG(RTC->ALRMAR, tmpreg); 01197 WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask); 01198 } 01199 01200 WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds); 01201 01202 /* Configure the Alarm state: Enable Alarm */ 01203 SET_BIT(RTC->CR, RTC_CR_ALRAE); 01204 } 01205 else 01206 { 01207 /* Disable the Alarm B interrupt */ 01208 /* In case of interrupt mode is used, the interrupt source must disabled */ 01209 CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE)); 01210 01211 /* Clear flag alarm B */ 01212 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF); 01213 01214 if (binaryMode == RTC_BINARY_ONLY) 01215 { 01216 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr); 01217 } 01218 else 01219 { 01220 WRITE_REG(RTC->ALRMBR, tmpreg); 01221 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask); 01222 } 01223 01224 WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds); 01225 01226 /* Configure the Alarm state: Enable Alarm */ 01227 SET_BIT(RTC->CR, RTC_CR_ALRBE); 01228 } 01229 01230 /* Enable the write protection for RTC registers */ 01231 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01232 01233 /* Change RTC state */ 01234 hrtc->State = HAL_RTC_STATE_READY; 01235 01236 /* Process Unlocked */ 01237 __HAL_UNLOCK(hrtc); 01238 01239 return HAL_OK; 01240 } 01241 01242 /** 01243 * @brief Set the specified RTC Alarm with Interrupt. 01244 * @param hrtc RTC handle 01245 * @param sAlarm Pointer to Alarm structure 01246 * if Binary mode is RTC_BINARY_ONLY, 3 fields only are used 01247 * sAlarm->AlarmTime.SubSeconds 01248 * sAlarm->AlarmSubSecondMask 01249 * sAlarm->BinaryAutoClr 01250 * @param Format Specifies the format of the entered parameters. 01251 * if Binary mode is RTC_BINARY_ONLY, this parameter is not used 01252 * else this parameter can be one of the following values 01253 * @arg RTC_FORMAT_BIN: Binary format 01254 * @arg RTC_FORMAT_BCD: BCD format 01255 * @retval HAL status 01256 */ 01257 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format) 01258 { 01259 uint32_t tmpreg = 0, binaryMode; 01260 01261 /* Process Locked */ 01262 __HAL_LOCK(hrtc); 01263 hrtc->State = HAL_RTC_STATE_BUSY; 01264 01265 #ifdef USE_FULL_ASSERT 01266 /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration). */ 01267 if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE) 01268 { 01269 assert_param(IS_RTC_FORMAT(Format)); 01270 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 01271 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 01272 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 01273 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds)); 01274 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask)); 01275 } 01276 else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY) 01277 { 01278 assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask)); 01279 assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr)); 01280 } 01281 else /* RTC_BINARY_MIX */ 01282 { 01283 assert_param(IS_RTC_FORMAT(Format)); 01284 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 01285 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 01286 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 01287 /* In Binary Mix Mode, the RTC can not generate an alarm on a match involving all calendar items + the upper SSR bits */ 01288 assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <= (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos))); 01289 } 01290 #endif 01291 01292 /* Get Binary mode (32-bit free-running counter configuration) */ 01293 binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN); 01294 01295 if (binaryMode != RTC_BINARY_ONLY) 01296 { 01297 if (Format == RTC_FORMAT_BIN) 01298 { 01299 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U) 01300 { 01301 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours)); 01302 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01303 } 01304 else 01305 { 01306 sAlarm->AlarmTime.TimeFormat = 0x00U; 01307 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours)); 01308 } 01309 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes)); 01310 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds)); 01311 01312 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01313 { 01314 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay)); 01315 } 01316 else 01317 { 01318 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay)); 01319 } 01320 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01321 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01322 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01323 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01324 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01325 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01326 ((uint32_t)sAlarm->AlarmMask)); 01327 } 01328 else /* Format BCD */ 01329 { 01330 if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U) 01331 { 01332 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01333 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01334 } 01335 else 01336 { 01337 sAlarm->AlarmTime.TimeFormat = 0x00U; 01338 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01339 } 01340 01341 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes))); 01342 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds))); 01343 01344 #ifdef USE_FULL_ASSERT 01345 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01346 { 01347 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01348 } 01349 else 01350 { 01351 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01352 } 01353 01354 #endif /* USE_FULL_ASSERT */ 01355 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01356 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01357 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01358 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01359 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01360 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01361 ((uint32_t)sAlarm->AlarmMask)); 01362 01363 } 01364 } 01365 01366 /* Disable the write protection for RTC registers */ 01367 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01368 01369 /* Configure the Alarm register */ 01370 if (sAlarm->Alarm == RTC_ALARM_A) 01371 { 01372 /* Disable the Alarm A interrupt */ 01373 CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE); 01374 /* Clear flag alarm A */ 01375 WRITE_REG(RTC->SCR, RTC_SCR_CALRAF); 01376 01377 if (binaryMode == RTC_BINARY_ONLY) 01378 { 01379 RTC->ALRMASSR = sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr; 01380 } 01381 else 01382 { 01383 WRITE_REG(RTC->ALRMAR, tmpreg); 01384 WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask); 01385 } 01386 01387 WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds); 01388 01389 /* Configure the Alarm interrupt */ 01390 SET_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE); 01391 } 01392 else 01393 { 01394 /* Disable the Alarm B interrupt */ 01395 CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE); 01396 /* Clear flag alarm B */ 01397 WRITE_REG(RTC->SCR, RTC_SCR_CALRBF); 01398 01399 if (binaryMode == RTC_BINARY_ONLY) 01400 { 01401 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr); 01402 } 01403 else 01404 { 01405 WRITE_REG(RTC->ALRMBR, tmpreg); 01406 WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask); 01407 } 01408 01409 WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds); 01410 01411 /* Configure the Alarm interrupt */ 01412 SET_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE); 01413 } 01414 01415 /* RTC Alarm Interrupt Configuration: EXTI configuration */ 01416 __HAL_RTC_ALARM_EXTI_ENABLE_IT(); 01417 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE(); 01418 01419 /* Enable the write protection for RTC registers */ 01420 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01421 01422 hrtc->State = HAL_RTC_STATE_READY; 01423 01424 /* Process Unlocked */ 01425 __HAL_UNLOCK(hrtc); 01426 01427 return HAL_OK; 01428 } 01429 01430 #else /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */ 01431 01432 /** 01433 * @brief Set RTC current time. 01434 * @param hrtc RTC handle 01435 * @param sTime Pointer to Time structure 01436 * @param Format Specifies the format of the entered parameters. 01437 * This parameter can be one of the following values: 01438 * @arg RTC_FORMAT_BIN: Binary data format 01439 * @arg RTC_FORMAT_BCD: BCD data format 01440 * @retval HAL status 01441 */ 01442 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 01443 { 01444 uint32_t tmpreg; 01445 HAL_StatusTypeDef status; 01446 01447 /* Check the parameters */ 01448 assert_param(IS_RTC_FORMAT(Format)); 01449 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving)); 01450 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation)); 01451 01452 /* Process Locked */ 01453 __HAL_LOCK(hrtc); 01454 01455 hrtc->State = HAL_RTC_STATE_BUSY; 01456 01457 /* Disable the write protection for RTC registers */ 01458 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01459 01460 /* Enter Initialization mode */ 01461 status = RTC_EnterInitMode(hrtc); 01462 if (status == HAL_OK) 01463 { 01464 if (Format == RTC_FORMAT_BIN) 01465 { 01466 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U) 01467 { 01468 assert_param(IS_RTC_HOUR12(sTime->Hours)); 01469 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat)); 01470 } 01471 else 01472 { 01473 sTime->TimeFormat = 0x00U; 01474 assert_param(IS_RTC_HOUR24(sTime->Hours)); 01475 } 01476 assert_param(IS_RTC_MINUTES(sTime->Minutes)); 01477 assert_param(IS_RTC_SECONDS(sTime->Seconds)); 01478 01479 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \ 01480 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \ 01481 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \ 01482 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos)); 01483 } 01484 else 01485 { 01486 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U) 01487 { 01488 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours))); 01489 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat)); 01490 } 01491 else 01492 { 01493 sTime->TimeFormat = 0x00U; 01494 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours))); 01495 } 01496 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes))); 01497 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds))); 01498 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \ 01499 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \ 01500 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \ 01501 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos)); 01502 } 01503 01504 /* Set the RTC_TR register */ 01505 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK); 01506 01507 /* Clear the bits to be configured */ 01508 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP); 01509 01510 /* Configure the RTC_CR register */ 01511 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation); 01512 01513 /* Exit Initialization mode */ 01514 status = RTC_ExitInitMode(hrtc); 01515 } 01516 01517 /* Enable the write protection for RTC registers */ 01518 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01519 01520 if (status == HAL_OK) 01521 { 01522 hrtc->State = HAL_RTC_STATE_READY; 01523 } 01524 01525 /* Process Unlocked */ 01526 __HAL_UNLOCK(hrtc); 01527 01528 return status; 01529 } 01530 01531 /** 01532 * @brief Get RTC current time. 01533 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds 01534 * value in second fraction ratio with time unit following generic formula: 01535 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit 01536 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS 01537 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 01538 * in the higher-order calendar shadow registers to ensure consistency between the time and date values. 01539 * Reading RTC current time locks the values in calendar shadow registers until Current date is read 01540 * to ensure consistency between the time and date values. 01541 * @param hrtc RTC handle 01542 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned 01543 * with input format (BIN or BCD), also SubSeconds field returning the 01544 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler 01545 * factor to be used for second fraction ratio computation. 01546 * @param Format Specifies the format of the entered parameters. 01547 * This parameter can be one of the following values: 01548 * @arg RTC_FORMAT_BIN: Binary data format 01549 * @arg RTC_FORMAT_BCD: BCD data format 01550 * @retval HAL status 01551 */ 01552 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) 01553 { 01554 uint32_t tmpreg; 01555 01556 /* Check the parameters */ 01557 assert_param(IS_RTC_FORMAT(Format)); 01558 01559 /* Get subseconds structure field from the corresponding register*/ 01560 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR); 01561 01562 /* Get SecondFraction structure field from the corresponding register field*/ 01563 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S); 01564 01565 /* Get the TR register */ 01566 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK); 01567 01568 /* Fill the structure fields with the read parameters */ 01569 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos); 01570 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos); 01571 sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos); 01572 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos); 01573 01574 /* Check the input parameters format */ 01575 if (Format == RTC_FORMAT_BIN) 01576 { 01577 /* Convert the time structure parameters to Binary format */ 01578 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours); 01579 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes); 01580 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds); 01581 } 01582 01583 return HAL_OK; 01584 } 01585 01586 /** 01587 * @brief Set RTC current date. 01588 * @param hrtc RTC handle 01589 * @param sDate Pointer to date structure 01590 * @param Format specifies the format of the entered parameters. 01591 * This parameter can be one of the following values: 01592 * @arg RTC_FORMAT_BIN: Binary data format 01593 * @arg RTC_FORMAT_BCD: BCD data format 01594 * @retval HAL status 01595 */ 01596 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) 01597 { 01598 uint32_t datetmpreg; 01599 HAL_StatusTypeDef status; 01600 01601 /* Check the parameters */ 01602 assert_param(IS_RTC_FORMAT(Format)); 01603 01604 /* Process Locked */ 01605 __HAL_LOCK(hrtc); 01606 01607 hrtc->State = HAL_RTC_STATE_BUSY; 01608 01609 if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U)) 01610 { 01611 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU); 01612 } 01613 01614 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay)); 01615 01616 if (Format == RTC_FORMAT_BIN) 01617 { 01618 assert_param(IS_RTC_YEAR(sDate->Year)); 01619 assert_param(IS_RTC_MONTH(sDate->Month)); 01620 assert_param(IS_RTC_DATE(sDate->Date)); 01621 01622 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \ 01623 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \ 01624 ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \ 01625 ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos)); 01626 } 01627 else 01628 { 01629 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year))); 01630 assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month))); 01631 assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date))); 01632 01633 datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \ 01634 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \ 01635 (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \ 01636 (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos)); 01637 } 01638 01639 /* Disable the write protection for RTC registers */ 01640 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01641 01642 /* Enter Initialization mode */ 01643 status = RTC_EnterInitMode(hrtc); 01644 if (status == HAL_OK) 01645 { 01646 /* Set the RTC_DR register */ 01647 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK); 01648 01649 /* Exit Initialization mode */ 01650 status = RTC_ExitInitMode(hrtc); 01651 } 01652 01653 /* Enable the write protection for RTC registers */ 01654 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01655 01656 if (status == HAL_OK) 01657 { 01658 hrtc->State = HAL_RTC_STATE_READY ; 01659 } 01660 01661 /* Process Unlocked */ 01662 __HAL_UNLOCK(hrtc); 01663 01664 return status; 01665 } 01666 01667 /** 01668 * @brief Get RTC current date. 01669 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values 01670 * in the higher-order calendar shadow registers to ensure consistency between the time and date values. 01671 * Reading RTC current time locks the values in calendar shadow registers until Current date is read. 01672 * @param hrtc RTC handle 01673 * @param sDate Pointer to Date structure 01674 * @param Format Specifies the format of the entered parameters. 01675 * This parameter can be one of the following values: 01676 * @arg RTC_FORMAT_BIN: Binary data format 01677 * @arg RTC_FORMAT_BCD: BCD data format 01678 * @retval HAL status 01679 */ 01680 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format) 01681 { 01682 uint32_t datetmpreg; 01683 01684 /* Check the parameters */ 01685 assert_param(IS_RTC_FORMAT(Format)); 01686 01687 /* Get the DR register */ 01688 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK); 01689 01690 /* Fill the structure fields with the read parameters */ 01691 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos); 01692 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos); 01693 sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos); 01694 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos); 01695 01696 /* Check the input parameters format */ 01697 if (Format == RTC_FORMAT_BIN) 01698 { 01699 /* Convert the date structure parameters to Binary format */ 01700 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year); 01701 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month); 01702 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date); 01703 } 01704 return HAL_OK; 01705 } 01706 01707 /** 01708 * @} 01709 */ 01710 01711 /** @addtogroup RTC_Exported_Functions_Group3 01712 * @brief RTC Alarm functions 01713 * 01714 @verbatim 01715 =============================================================================== 01716 ##### RTC Alarm functions ##### 01717 =============================================================================== 01718 01719 [..] This section provides functions allowing to configure Alarm feature 01720 01721 @endverbatim 01722 * @{ 01723 */ 01724 /** 01725 * @brief Set the specified RTC Alarm. 01726 * @param hrtc RTC handle 01727 * @param sAlarm Pointer to Alarm structure 01728 * @param Format Specifies the format of the entered parameters. 01729 * This parameter can be one of the following values: 01730 * @arg RTC_FORMAT_BIN: Binary data format 01731 * @arg RTC_FORMAT_BCD: BCD data format 01732 * @retval HAL status 01733 */ 01734 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format) 01735 { 01736 uint32_t tmpreg, subsecondtmpreg; 01737 01738 /* Check the parameters */ 01739 assert_param(IS_RTC_FORMAT(Format)); 01740 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 01741 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 01742 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 01743 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds)); 01744 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask)); 01745 01746 /* Process Locked */ 01747 __HAL_LOCK(hrtc); 01748 01749 hrtc->State = HAL_RTC_STATE_BUSY; 01750 01751 if (Format == RTC_FORMAT_BIN) 01752 { 01753 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U) 01754 { 01755 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours)); 01756 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01757 } 01758 else 01759 { 01760 sAlarm->AlarmTime.TimeFormat = 0x00U; 01761 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours)); 01762 } 01763 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes)); 01764 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds)); 01765 01766 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01767 { 01768 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay)); 01769 } 01770 else 01771 { 01772 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay)); 01773 } 01774 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01775 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01776 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01777 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01778 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01779 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01780 ((uint32_t)sAlarm->AlarmMask)); 01781 } 01782 else 01783 { 01784 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U) 01785 { 01786 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01787 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01788 } 01789 else 01790 { 01791 sAlarm->AlarmTime.TimeFormat = 0x00U; 01792 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01793 } 01794 01795 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes))); 01796 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds))); 01797 01798 #ifdef USE_FULL_ASSERT 01799 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01800 { 01801 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01802 } 01803 else 01804 { 01805 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01806 } 01807 01808 #endif /* USE_FULL_ASSERT */ 01809 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01810 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01811 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01812 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01813 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01814 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01815 ((uint32_t)sAlarm->AlarmMask)); 01816 } 01817 01818 /* Configure the Alarm A or Alarm B Sub Second registers */ 01819 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask)); 01820 01821 /* Disable the write protection for RTC registers */ 01822 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 01823 01824 /* Configure the Alarm register */ 01825 if (sAlarm->Alarm == RTC_ALARM_A) 01826 { 01827 /* Disable the Alarm A interrupt */ 01828 __HAL_RTC_ALARMA_DISABLE(hrtc); 01829 /* Clear flag alarm A */ 01830 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF); 01831 /* In case of interrupt mode is used, the interrupt source must disabled */ 01832 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA); 01833 01834 #if defined (RTC_FLAG_ALRAWF) 01835 uint32_t tickstart = HAL_GetTick(); 01836 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */ 01837 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U) 01838 { 01839 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 01840 { 01841 /* Enable the write protection for RTC registers */ 01842 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01843 01844 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01845 01846 /* Process Unlocked */ 01847 __HAL_UNLOCK(hrtc); 01848 01849 return HAL_TIMEOUT; 01850 } 01851 } 01852 #endif 01853 01854 hrtc->Instance->ALRMAR = (uint32_t)tmpreg; 01855 /* Configure the Alarm A Sub Second register */ 01856 hrtc->Instance->ALRMASSR = subsecondtmpreg; 01857 /* Configure the Alarm state: Enable Alarm */ 01858 __HAL_RTC_ALARMA_ENABLE(hrtc); 01859 } 01860 else 01861 { 01862 /* Disable the Alarm B interrupt */ 01863 __HAL_RTC_ALARMB_DISABLE(hrtc); 01864 /* Clear flag alarm B */ 01865 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF); 01866 /* In case of interrupt mode is used, the interrupt source must disabled */ 01867 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB); 01868 01869 #if defined (RTC_FLAG_ALRBWF) 01870 uint32_t tickstart = HAL_GetTick(); 01871 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */ 01872 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U) 01873 { 01874 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 01875 { 01876 /* Enable the write protection for RTC registers */ 01877 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01878 01879 hrtc->State = HAL_RTC_STATE_TIMEOUT; 01880 01881 /* Process Unlocked */ 01882 __HAL_UNLOCK(hrtc); 01883 01884 return HAL_TIMEOUT; 01885 } 01886 } 01887 #endif 01888 01889 hrtc->Instance->ALRMBR = (uint32_t)tmpreg; 01890 /* Configure the Alarm B Sub Second register */ 01891 hrtc->Instance->ALRMBSSR = subsecondtmpreg; 01892 /* Configure the Alarm state: Enable Alarm */ 01893 __HAL_RTC_ALARMB_ENABLE(hrtc); 01894 } 01895 01896 /* Enable the write protection for RTC registers */ 01897 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 01898 01899 /* Change RTC state */ 01900 hrtc->State = HAL_RTC_STATE_READY; 01901 01902 /* Process Unlocked */ 01903 __HAL_UNLOCK(hrtc); 01904 01905 return HAL_OK; 01906 } 01907 01908 /** 01909 * @brief Set the specified RTC Alarm with Interrupt. 01910 * @note The Alarm register can only be written when the corresponding Alarm 01911 * is disabled (Use the HAL_RTC_DeactivateAlarm()). 01912 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature. 01913 * @param hrtc RTC handle 01914 * @param sAlarm Pointer to Alarm structure 01915 * @param Format Specifies the format of the entered parameters. 01916 * This parameter can be one of the following values: 01917 * @arg RTC_FORMAT_BIN: Binary data format 01918 * @arg RTC_FORMAT_BCD: BCD data format 01919 * @retval HAL status 01920 */ 01921 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format) 01922 { 01923 uint32_t tmpreg, subsecondtmpreg; 01924 01925 /* Check the parameters */ 01926 assert_param(IS_RTC_FORMAT(Format)); 01927 assert_param(IS_RTC_ALARM(sAlarm->Alarm)); 01928 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask)); 01929 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel)); 01930 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds)); 01931 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask)); 01932 01933 /* Process Locked */ 01934 __HAL_LOCK(hrtc); 01935 01936 hrtc->State = HAL_RTC_STATE_BUSY; 01937 01938 if (Format == RTC_FORMAT_BIN) 01939 { 01940 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U) 01941 { 01942 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours)); 01943 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01944 } 01945 else 01946 { 01947 sAlarm->AlarmTime.TimeFormat = 0x00U; 01948 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours)); 01949 } 01950 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes)); 01951 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds)); 01952 01953 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01954 { 01955 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay)); 01956 } 01957 else 01958 { 01959 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay)); 01960 } 01961 01962 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01963 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01964 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 01965 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 01966 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 01967 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 01968 ((uint32_t)sAlarm->AlarmMask)); 01969 } 01970 else 01971 { 01972 if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U) 01973 { 01974 assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01975 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat)); 01976 } 01977 else 01978 { 01979 sAlarm->AlarmTime.TimeFormat = 0x00U; 01980 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours))); 01981 } 01982 01983 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes))); 01984 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds))); 01985 01986 #ifdef USE_FULL_ASSERT 01987 if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE) 01988 { 01989 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01990 } 01991 else 01992 { 01993 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay))); 01994 } 01995 01996 #endif /* USE_FULL_ASSERT */ 01997 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \ 01998 ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \ 01999 ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \ 02000 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \ 02001 ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \ 02002 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \ 02003 ((uint32_t)sAlarm->AlarmMask)); 02004 } 02005 /* Configure the Alarm A or Alarm B Sub Second registers */ 02006 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask)); 02007 02008 /* Disable the write protection for RTC registers */ 02009 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 02010 02011 /* Configure the Alarm register */ 02012 if (sAlarm->Alarm == RTC_ALARM_A) 02013 { 02014 /* Disable the Alarm A interrupt */ 02015 __HAL_RTC_ALARMA_DISABLE(hrtc); 02016 02017 /* Clear flag alarm A */ 02018 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF); 02019 02020 #if defined (RTC_FLAG_ALRAWF) 02021 uint32_t tickstart = HAL_GetTick(); 02022 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */ 02023 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U) 02024 { 02025 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02026 { 02027 /* Enable the write protection for RTC registers */ 02028 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02029 02030 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02031 02032 /* Process Unlocked */ 02033 __HAL_UNLOCK(hrtc); 02034 02035 return HAL_TIMEOUT; 02036 } 02037 } 02038 #endif 02039 02040 hrtc->Instance->ALRMAR = (uint32_t)tmpreg; 02041 /* Configure the Alarm A Sub Second register */ 02042 hrtc->Instance->ALRMASSR = subsecondtmpreg; 02043 /* Configure the Alarm state: Enable Alarm */ 02044 __HAL_RTC_ALARMA_ENABLE(hrtc); 02045 /* Configure the Alarm interrupt */ 02046 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA); 02047 } 02048 else 02049 { 02050 /* Disable the Alarm B interrupt */ 02051 __HAL_RTC_ALARMB_DISABLE(hrtc); 02052 02053 /* Clear flag alarm B */ 02054 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF); 02055 02056 #if defined (RTC_FLAG_ALRBWF) 02057 uint32_t tickstart = HAL_GetTick(); 02058 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */ 02059 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U) 02060 { 02061 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02062 { 02063 /* Enable the write protection for RTC registers */ 02064 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02065 02066 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02067 02068 /* Process Unlocked */ 02069 __HAL_UNLOCK(hrtc); 02070 02071 return HAL_TIMEOUT; 02072 } 02073 } 02074 #endif 02075 02076 hrtc->Instance->ALRMBR = (uint32_t)tmpreg; 02077 /* Configure the Alarm B Sub Second register */ 02078 hrtc->Instance->ALRMBSSR = subsecondtmpreg; 02079 /* Configure the Alarm state: Enable Alarm */ 02080 __HAL_RTC_ALARMB_ENABLE(hrtc); 02081 /* Configure the Alarm interrupt */ 02082 __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB); 02083 } 02084 02085 /* RTC Alarm Interrupt Configuration: EXTI configuration */ 02086 __HAL_RTC_ALARM_EXTI_ENABLE_IT(); 02087 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE(); 02088 02089 /* Enable the write protection for RTC registers */ 02090 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02091 02092 hrtc->State = HAL_RTC_STATE_READY; 02093 02094 /* Process Unlocked */ 02095 __HAL_UNLOCK(hrtc); 02096 02097 return HAL_OK; 02098 } 02099 02100 #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */ 02101 02102 /** 02103 * @brief Deactivate the specified RTC Alarm. 02104 * @param hrtc RTC handle 02105 * @param Alarm Specifies the Alarm. 02106 * This parameter can be one of the following values: 02107 * @arg RTC_ALARM_A: AlarmA 02108 * @arg RTC_ALARM_B: AlarmB 02109 * @retval HAL status 02110 */ 02111 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm) 02112 { 02113 /* Check the parameters */ 02114 assert_param(IS_RTC_ALARM(Alarm)); 02115 02116 /* Process Locked */ 02117 __HAL_LOCK(hrtc); 02118 02119 hrtc->State = HAL_RTC_STATE_BUSY; 02120 02121 /* Disable the write protection for RTC registers */ 02122 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 02123 02124 if (Alarm == RTC_ALARM_A) 02125 { 02126 /* AlarmA */ 02127 #if defined (RTC_ALRMASSR_SSCLR) 02128 CLEAR_BIT(RTC->ALRMASSR, RTC_ALRMASSR_SSCLR); 02129 #endif 02130 02131 __HAL_RTC_ALARMA_DISABLE(hrtc); 02132 02133 /* In case of interrupt mode is used, the interrupt source must disabled */ 02134 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA); 02135 02136 #if defined (RTC_FLAG_ALRAWF) 02137 uint32_t tickstart = HAL_GetTick(); 02138 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */ 02139 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U) 02140 { 02141 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02142 { 02143 /* Enable the write protection for RTC registers */ 02144 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02145 02146 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02147 02148 /* Process Unlocked */ 02149 __HAL_UNLOCK(hrtc); 02150 02151 return HAL_TIMEOUT; 02152 } 02153 } 02154 #endif 02155 } 02156 else 02157 { 02158 /* AlarmB */ 02159 #if defined (RTC_ALRMBSSR_SSCLR) 02160 CLEAR_BIT(RTC->ALRMBSSR, RTC_ALRMASSR_SSCLR); 02161 #endif 02162 02163 __HAL_RTC_ALARMB_DISABLE(hrtc); 02164 02165 /* In case of interrupt mode is used, the interrupt source must disabled */ 02166 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB); 02167 02168 #if defined (RTC_FLAG_ALRBWF) 02169 uint32_t tickstart = HAL_GetTick(); 02170 /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */ 02171 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U) 02172 { 02173 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02174 { 02175 /* Enable the write protection for RTC registers */ 02176 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02177 02178 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02179 02180 /* Process Unlocked */ 02181 __HAL_UNLOCK(hrtc); 02182 02183 return HAL_TIMEOUT; 02184 } 02185 } 02186 #endif 02187 } 02188 /* Enable the write protection for RTC registers */ 02189 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02190 02191 hrtc->State = HAL_RTC_STATE_READY; 02192 02193 /* Process Unlocked */ 02194 __HAL_UNLOCK(hrtc); 02195 02196 return HAL_OK; 02197 } 02198 02199 /** 02200 * @brief Get the RTC Alarm value and masks. 02201 * @param hrtc RTC handle 02202 * @param sAlarm Pointer to Date structure 02203 * @param Alarm Specifies the Alarm. 02204 * This parameter can be one of the following values: 02205 * @arg RTC_ALARM_A: AlarmA 02206 * @arg RTC_ALARM_B: AlarmB 02207 * @param Format Specifies the format of the entered parameters. 02208 * This parameter can be one of the following values: 02209 * @arg RTC_FORMAT_BIN: Binary data format 02210 * @arg RTC_FORMAT_BCD: BCD data format 02211 * @retval HAL status 02212 */ 02213 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format) 02214 { 02215 uint32_t tmpreg, subsecondtmpreg; 02216 02217 /* Check the parameters */ 02218 assert_param(IS_RTC_FORMAT(Format)); 02219 assert_param(IS_RTC_ALARM(Alarm)); 02220 02221 if (Alarm == RTC_ALARM_A) 02222 { 02223 /* AlarmA */ 02224 sAlarm->Alarm = RTC_ALARM_A; 02225 02226 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR); 02227 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS); 02228 02229 /* Fill the structure with the read parameters */ 02230 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos); 02231 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos); 02232 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos); 02233 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos); 02234 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg; 02235 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos); 02236 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL); 02237 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL); 02238 } 02239 else 02240 { 02241 sAlarm->Alarm = RTC_ALARM_B; 02242 02243 tmpreg = (uint32_t)(hrtc->Instance->ALRMBR); 02244 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS); 02245 02246 /* Fill the structure with the read parameters */ 02247 sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos); 02248 sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos); 02249 sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos); 02250 sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos); 02251 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg; 02252 sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos); 02253 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL); 02254 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL); 02255 } 02256 02257 if (Format == RTC_FORMAT_BIN) 02258 { 02259 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours); 02260 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes); 02261 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds); 02262 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay); 02263 } 02264 02265 return HAL_OK; 02266 } 02267 02268 02269 /** 02270 * @brief Daylight Saving Time, Add one hour to the calendar in one single operation 02271 * without going through the initialization procedure. 02272 * @param hrtc RTC handle 02273 * @retval None 02274 */ 02275 void HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc) 02276 { 02277 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 02278 SET_BIT(hrtc->Instance->CR, RTC_CR_ADD1H); 02279 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02280 } 02281 02282 /** 02283 * @brief Daylight Saving Time, Substract one hour from the calendar in one 02284 * single operation without going through the initialization procedure. 02285 * @param hrtc RTC handle 02286 * @retval None 02287 */ 02288 void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc) 02289 { 02290 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 02291 SET_BIT(hrtc->Instance->CR, RTC_CR_SUB1H); 02292 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02293 } 02294 02295 /** 02296 * @brief Daylight Saving Time, Set the store operation bit. 02297 * @note It can be used by the software in order to memorize the DST status. 02298 * @param hrtc RTC handle 02299 * @retval None 02300 */ 02301 void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc) 02302 { 02303 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 02304 SET_BIT(hrtc->Instance->CR, RTC_CR_BKP); 02305 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02306 } 02307 02308 /** 02309 * @brief Daylight Saving Time, Clear the store operation bit. 02310 * @param hrtc RTC handle 02311 * @retval None 02312 */ 02313 void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc) 02314 { 02315 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); 02316 CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP); 02317 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); 02318 } 02319 02320 /** 02321 * @brief Daylight Saving Time, Read the store operation bit. 02322 * @param hrtc RTC handle 02323 * @retval operation see RTC_StoreOperation_Definitions 02324 */ 02325 uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc) 02326 { 02327 return READ_BIT(hrtc->Instance->CR, RTC_CR_BKP); 02328 } 02329 02330 /** 02331 * @brief Handle Alarm interrupt request. 02332 * @param hrtc RTC handle 02333 * @retval None 02334 */ 02335 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc) 02336 { 02337 /* Clear the EXTI's line Flag for RTC Alarm */ 02338 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG(); 02339 02340 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 02341 /* Get interrupt status */ 02342 uint32_t tmp = hrtc->Instance->MISR; 02343 02344 if ((tmp & RTC_MISR_ALRAMF) != 0u) 02345 { 02346 /* Clear the AlarmA interrupt pending bit */ 02347 hrtc->Instance->SCR = RTC_SCR_CALRAF; 02348 02349 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 02350 /* Call Compare Match registered Callback */ 02351 hrtc->AlarmAEventCallback(hrtc); 02352 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */ 02353 HAL_RTC_AlarmAEventCallback(hrtc); 02354 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */ 02355 } 02356 02357 if ((tmp & RTC_MISR_ALRBMF) != 0u) 02358 { 02359 /* Clear the AlarmB interrupt pending bit */ 02360 hrtc->Instance->SCR = RTC_SCR_CALRBF; 02361 02362 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 02363 /* Call Compare Match registered Callback */ 02364 hrtc->AlarmBEventCallback(hrtc); 02365 #else 02366 HAL_RTCEx_AlarmBEventCallback(hrtc); 02367 #endif 02368 } 02369 02370 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */ 02371 02372 /* Get the AlarmA interrupt source enable status */ 02373 if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U) 02374 { 02375 /* Get the pending status of the AlarmA Interrupt */ 02376 if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U) 02377 { 02378 /* Clear the AlarmA interrupt pending bit */ 02379 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF); 02380 02381 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 02382 hrtc->AlarmAEventCallback(hrtc); 02383 #else 02384 HAL_RTC_AlarmAEventCallback(hrtc); 02385 #endif 02386 } 02387 } 02388 02389 /* Get the AlarmB interrupt source enable status */ 02390 if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U) 02391 { 02392 /* Get the pending status of the AlarmB Interrupt */ 02393 if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U) 02394 { 02395 /* Clear the AlarmB interrupt pending bit */ 02396 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF); 02397 02398 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) 02399 hrtc->AlarmBEventCallback(hrtc); 02400 #else 02401 HAL_RTCEx_AlarmBEventCallback(hrtc); 02402 #endif 02403 } 02404 } 02405 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */ 02406 02407 /* Change RTC state */ 02408 hrtc->State = HAL_RTC_STATE_READY; 02409 } 02410 02411 /** 02412 * @brief Alarm A callback. 02413 * @param hrtc RTC handle 02414 * @retval None 02415 */ 02416 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) 02417 { 02418 /* Prevent unused argument(s) compilation warning */ 02419 UNUSED(hrtc); 02420 02421 /* NOTE : This function should not be modified, when the callback is needed, 02422 the HAL_RTC_AlarmAEventCallback could be implemented in the user file 02423 */ 02424 } 02425 02426 /** 02427 * @brief Handle AlarmA Polling request. 02428 * @param hrtc RTC handle 02429 * @param Timeout Timeout duration 02430 * @retval HAL status 02431 */ 02432 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout) 02433 { 02434 02435 uint32_t tickstart = HAL_GetTick(); 02436 02437 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U) 02438 { 02439 if (Timeout != HAL_MAX_DELAY) 02440 { 02441 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 02442 { 02443 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02444 return HAL_TIMEOUT; 02445 } 02446 } 02447 } 02448 02449 /* Clear the Alarm interrupt pending bit */ 02450 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF); 02451 02452 /* Change RTC state */ 02453 hrtc->State = HAL_RTC_STATE_READY; 02454 02455 return HAL_OK; 02456 } 02457 02458 /** 02459 * @} 02460 */ 02461 02462 /** @addtogroup RTC_Exported_Functions_Group4 02463 * @brief Peripheral Control functions 02464 * 02465 @verbatim 02466 =============================================================================== 02467 ##### Peripheral Control functions ##### 02468 =============================================================================== 02469 [..] 02470 This subsection provides functions allowing to 02471 (+) Wait for RTC Time and Date Synchronization 02472 02473 @endverbatim 02474 * @{ 02475 */ 02476 02477 /** 02478 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are 02479 * synchronized with RTC APB clock. 02480 * @note The RTC Resynchronization mode is write protected, use the 02481 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function. 02482 * @note To read the calendar through the shadow registers after Calendar 02483 * initialization, calendar update or after wakeup from low power modes 02484 * the software must first clear the RSF flag. 02485 * The software must then wait until it is set again before reading 02486 * the calendar, which means that the calendar registers have been 02487 * correctly copied into the RTC_TR and RTC_DR shadow registers. 02488 * @param hrtc RTC handle 02489 * @retval HAL status 02490 */ 02491 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc) 02492 { 02493 uint32_t tickstart; 02494 02495 /* Clear RSF flag */ 02496 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 02497 hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK; 02498 #else 02499 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK; 02500 #endif 02501 02502 tickstart = HAL_GetTick(); 02503 02504 /* Wait the registers to be synchronised */ 02505 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 02506 while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U) 02507 #else 02508 while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U) 02509 #endif 02510 { 02511 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02512 { 02513 return HAL_TIMEOUT; 02514 } 02515 } 02516 02517 return HAL_OK; 02518 } 02519 02520 /** 02521 * @} 02522 */ 02523 02524 /** @addtogroup RTC_Exported_Functions_Group5 02525 * @brief Peripheral State functions 02526 * 02527 @verbatim 02528 =============================================================================== 02529 ##### Peripheral State functions ##### 02530 =============================================================================== 02531 [..] 02532 This subsection provides functions allowing to 02533 (+) Get RTC state 02534 02535 @endverbatim 02536 * @{ 02537 */ 02538 /** 02539 * @brief Return the RTC handle state. 02540 * @param hrtc RTC handle 02541 * @retval HAL state 02542 */ 02543 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc) 02544 { 02545 /* Return RTC handle state */ 02546 return hrtc->State; 02547 } 02548 02549 /** 02550 * @} 02551 */ 02552 02553 /** 02554 * @} 02555 */ 02556 02557 /** @addtogroup RTC_Private_Functions 02558 * @{ 02559 */ 02560 /** 02561 * @brief Enter the RTC Initialization mode. 02562 * @note The RTC Initialization mode is write protected, use the 02563 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function. 02564 * @param hrtc RTC handle 02565 * @retval HAL status 02566 */ 02567 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc) 02568 { 02569 uint32_t tickstart; 02570 HAL_StatusTypeDef status = HAL_OK; 02571 02572 /* Check if the Initialization mode is set */ 02573 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) 02574 if ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U) 02575 { 02576 /* Set the Initialization mode */ 02577 SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT); 02578 02579 tickstart = HAL_GetTick(); 02580 /* Wait till RTC is in INIT state and if Time out is reached exit */ 02581 while ((READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT)) 02582 { 02583 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02584 { 02585 status = HAL_TIMEOUT; 02586 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02587 } 02588 } 02589 } 02590 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */ 02591 if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U) 02592 { 02593 /* Set the Initialization mode */ 02594 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK; 02595 02596 tickstart = HAL_GetTick(); 02597 /* Wait till RTC is in INIT state and if Time out is reached exit */ 02598 while ((READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT)) 02599 { 02600 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE) 02601 { 02602 status = HAL_TIMEOUT; 02603 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02604 } 02605 } 02606 } 02607 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */ 02608 02609 return status; 02610 } 02611 02612 /** 02613 * @brief Exit the RTC Initialization mode. 02614 * @param hrtc RTC handle 02615 * @retval HAL status 02616 */ 02617 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc) 02618 { 02619 HAL_StatusTypeDef status = HAL_OK; 02620 02621 /* Exit Initialization mode */ 02622 #if defined(STM32L412xx) || defined(STM32L422xx) || defined(STM32L4P5xx) || defined(STM32L4Q5xx) 02623 CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT); 02624 #else 02625 /* Exit Initialization mode */ 02626 CLEAR_BIT(RTC->ISR, RTC_ISR_INIT); 02627 #endif 02628 02629 /* If CR_BYPSHAD bit = 0, wait for synchro */ 02630 if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U) 02631 { 02632 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK) 02633 { 02634 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02635 status = HAL_TIMEOUT; 02636 } 02637 } 02638 else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry */ 02639 { 02640 /* Clear BYPSHAD bit */ 02641 CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD); 02642 if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK) 02643 { 02644 hrtc->State = HAL_RTC_STATE_TIMEOUT; 02645 status = HAL_TIMEOUT; 02646 } 02647 /* Restore BYPSHAD bit */ 02648 SET_BIT(RTC->CR, RTC_CR_BYPSHAD); 02649 } 02650 02651 return status; 02652 } 02653 02654 02655 02656 /** 02657 * @brief Convert a 2 digit decimal to BCD format. 02658 * @param Value Byte to be converted 02659 * @retval Converted byte 02660 */ 02661 uint8_t RTC_ByteToBcd2(uint8_t Value) 02662 { 02663 uint32_t bcdhigh = 0U; 02664 uint8_t temp = Value; 02665 02666 while (temp >= 10U) 02667 { 02668 bcdhigh++; 02669 temp -= 10U; 02670 } 02671 02672 return ((uint8_t)(bcdhigh << 4U) | temp); 02673 } 02674 02675 /** 02676 * @brief Convert from 2 digit BCD to Binary. 02677 * @param Value BCD value to be converted 02678 * @retval Converted word 02679 */ 02680 uint8_t RTC_Bcd2ToByte(uint8_t Value) 02681 { 02682 uint8_t tmp; 02683 tmp = ((Value & 0xF0U) >> 4U) * 10U; 02684 return (tmp + (Value & 0x0FU)); 02685 } 02686 02687 /** 02688 * @} 02689 */ 02690 02691 #endif /* HAL_RTC_MODULE_ENABLED */ 02692 /** 02693 * @} 02694 */ 02695 02696 /** 02697 * @} 02698 */