STM32L486xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_ll_rtc.c 00004 * @author MCD Application Team 00005 * @brief RTC LL module driver. 00006 ****************************************************************************** 00007 * @attention 00008 * 00009 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 1. Redistributions of source code must retain the above copyright notice, 00014 * this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright notice, 00016 * this list of conditions and the following disclaimer in the documentation 00017 * and/or other materials provided with the distribution. 00018 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00019 * may be used to endorse or promote products derived from this software 00020 * without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00023 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00025 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00026 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00027 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00028 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00030 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 * 00033 ****************************************************************************** 00034 */ 00035 #if defined(USE_FULL_LL_DRIVER) 00036 00037 /* Includes ------------------------------------------------------------------*/ 00038 #include "stm32l4xx_ll_rtc.h" 00039 #include "stm32l4xx_ll_cortex.h" 00040 #ifdef USE_FULL_ASSERT 00041 #include "stm32_assert.h" 00042 #else 00043 #define assert_param(expr) ((void)0U) 00044 #endif 00045 00046 /** @addtogroup STM32L4xx_LL_Driver 00047 * @{ 00048 */ 00049 00050 #if defined(RTC) 00051 00052 /** @addtogroup RTC_LL 00053 * @{ 00054 */ 00055 00056 /* Private types -------------------------------------------------------------*/ 00057 /* Private variables ---------------------------------------------------------*/ 00058 /* Private constants ---------------------------------------------------------*/ 00059 /** @addtogroup RTC_LL_Private_Constants 00060 * @{ 00061 */ 00062 /* Default values used for prescaler */ 00063 #define RTC_ASYNCH_PRESC_DEFAULT 0x0000007FU 00064 #define RTC_SYNCH_PRESC_DEFAULT 0x000000FFU 00065 00066 /* Values used for timeout */ 00067 #define RTC_INITMODE_TIMEOUT 1000U /* 1s when tick set to 1ms */ 00068 #define RTC_SYNCHRO_TIMEOUT 1000U /* 1s when tick set to 1ms */ 00069 /** 00070 * @} 00071 */ 00072 00073 /* Private macros ------------------------------------------------------------*/ 00074 /** @addtogroup RTC_LL_Private_Macros 00075 * @{ 00076 */ 00077 00078 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \ 00079 || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM)) 00080 00081 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FU) 00082 00083 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FFFU) 00084 00085 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \ 00086 || ((__VALUE__) == LL_RTC_FORMAT_BCD)) 00087 00088 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \ 00089 || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM)) 00090 00091 #define IS_LL_RTC_HOUR12(__HOUR__) (((__HOUR__) > 0U) && ((__HOUR__) <= 12U)) 00092 #define IS_LL_RTC_HOUR24(__HOUR__) ((__HOUR__) <= 23U) 00093 #define IS_LL_RTC_MINUTES(__MINUTES__) ((__MINUTES__) <= 59U) 00094 #define IS_LL_RTC_SECONDS(__SECONDS__) ((__SECONDS__) <= 59U) 00095 00096 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \ 00097 || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \ 00098 || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \ 00099 || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \ 00100 || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \ 00101 || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \ 00102 || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY)) 00103 00104 #define IS_LL_RTC_DAY(__DAY__) (((__DAY__) >= 1U) && ((__DAY__) <= 31U)) 00105 00106 #define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \ 00107 || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \ 00108 || ((__VALUE__) == LL_RTC_MONTH_MARCH) \ 00109 || ((__VALUE__) == LL_RTC_MONTH_APRIL) \ 00110 || ((__VALUE__) == LL_RTC_MONTH_MAY) \ 00111 || ((__VALUE__) == LL_RTC_MONTH_JUNE) \ 00112 || ((__VALUE__) == LL_RTC_MONTH_JULY) \ 00113 || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \ 00114 || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \ 00115 || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \ 00116 || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \ 00117 || ((__VALUE__) == LL_RTC_MONTH_DECEMBER)) 00118 00119 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U) 00120 00121 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \ 00122 || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \ 00123 || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \ 00124 || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \ 00125 || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \ 00126 || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL)) 00127 00128 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \ 00129 || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \ 00130 || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \ 00131 || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \ 00132 || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \ 00133 || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL)) 00134 00135 00136 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \ 00137 ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY)) 00138 00139 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \ 00140 ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY)) 00141 00142 00143 /** 00144 * @} 00145 */ 00146 /* Private function prototypes -----------------------------------------------*/ 00147 /* Exported functions --------------------------------------------------------*/ 00148 /** @addtogroup RTC_LL_Exported_Functions 00149 * @{ 00150 */ 00151 00152 /** @addtogroup RTC_LL_EF_Init 00153 * @{ 00154 */ 00155 00156 /** 00157 * @brief De-Initializes the RTC registers to their default reset values. 00158 * @note This function doesn't reset the RTC Clock source and RTC Backup Data 00159 * registers. 00160 * @param RTCx RTC Instance 00161 * @retval An ErrorStatus enumeration value: 00162 * - SUCCESS: RTC registers are de-initialized 00163 * - ERROR: RTC registers are not de-initialized 00164 */ 00165 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx) 00166 { 00167 ErrorStatus status = ERROR; 00168 00169 /* Check the parameter */ 00170 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00171 00172 /* Disable the write protection for RTC registers */ 00173 LL_RTC_DisableWriteProtection(RTCx); 00174 00175 /* Set Initialization mode */ 00176 if (LL_RTC_EnterInitMode(RTCx) != ERROR) 00177 { 00178 /* Reset TR, DR and CR registers */ 00179 LL_RTC_WriteReg(RTCx, TR, 0x00000000U); 00180 #if defined(RTC_WAKEUP_SUPPORT) 00181 LL_RTC_WriteReg(RTCx, WUTR, RTC_WUTR_WUT); 00182 #endif /* RTC_WAKEUP_SUPPORT */ 00183 LL_RTC_WriteReg(RTCx, DR , (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0)); 00184 /* Reset All CR bits except CR[2:0] */ 00185 #if defined(RTC_WAKEUP_SUPPORT) 00186 LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL)); 00187 #else 00188 LL_RTC_WriteReg(RTCx, CR, 0x00000000U); 00189 #endif /* RTC_WAKEUP_SUPPORT */ 00190 LL_RTC_WriteReg(RTCx, PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT)); 00191 LL_RTC_WriteReg(RTCx, ALRMAR, 0x00000000U); 00192 LL_RTC_WriteReg(RTCx, ALRMBR, 0x00000000U); 00193 LL_RTC_WriteReg(RTCx, SHIFTR, 0x00000000U); 00194 LL_RTC_WriteReg(RTCx, CALR, 0x00000000U); 00195 LL_RTC_WriteReg(RTCx, ALRMASSR, 0x00000000U); 00196 LL_RTC_WriteReg(RTCx, ALRMBSSR, 0x00000000U); 00197 00198 /* Reset ISR register and exit initialization mode */ 00199 LL_RTC_WriteReg(RTCx, ISR, 0x00000000U); 00200 00201 /* Reset Tamper and alternate functions configuration register */ 00202 LL_RTC_WriteReg(RTCx, TAMPCR, 0x00000000U); 00203 00204 /* Reset Option register */ 00205 LL_RTC_WriteReg(RTCx, OR, 0x00000000U); 00206 00207 /* Wait till the RTC RSF flag is set */ 00208 status = LL_RTC_WaitForSynchro(RTCx); 00209 } 00210 00211 /* Enable the write protection for RTC registers */ 00212 LL_RTC_EnableWriteProtection(RTCx); 00213 00214 return status; 00215 } 00216 00217 /** 00218 * @brief Initializes the RTC registers according to the specified parameters 00219 * in RTC_InitStruct. 00220 * @param RTCx RTC Instance 00221 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains 00222 * the configuration information for the RTC peripheral. 00223 * @note The RTC Prescaler register is write protected and can be written in 00224 * initialization mode only. 00225 * @retval An ErrorStatus enumeration value: 00226 * - SUCCESS: RTC registers are initialized 00227 * - ERROR: RTC registers are not initialized 00228 */ 00229 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct) 00230 { 00231 ErrorStatus status = ERROR; 00232 00233 /* Check the parameters */ 00234 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00235 assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat)); 00236 assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler)); 00237 assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler)); 00238 00239 /* Disable the write protection for RTC registers */ 00240 LL_RTC_DisableWriteProtection(RTCx); 00241 00242 /* Set Initialization mode */ 00243 if (LL_RTC_EnterInitMode(RTCx) != ERROR) 00244 { 00245 /* Set Hour Format */ 00246 LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat); 00247 00248 /* Configure Synchronous and Asynchronous prescaler factor */ 00249 LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler); 00250 LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler); 00251 00252 /* Exit Initialization mode */ 00253 LL_RTC_DisableInitMode(RTCx); 00254 00255 status = SUCCESS; 00256 } 00257 /* Enable the write protection for RTC registers */ 00258 LL_RTC_EnableWriteProtection(RTCx); 00259 00260 return status; 00261 } 00262 00263 /** 00264 * @brief Set each @ref LL_RTC_InitTypeDef field to default value. 00265 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized. 00266 * @retval None 00267 */ 00268 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct) 00269 { 00270 /* Set RTC_InitStruct fields to default values */ 00271 RTC_InitStruct->HourFormat = LL_RTC_HOURFORMAT_24HOUR; 00272 RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT; 00273 RTC_InitStruct->SynchPrescaler = RTC_SYNCH_PRESC_DEFAULT; 00274 } 00275 00276 /** 00277 * @brief Set the RTC current time. 00278 * @param RTCx RTC Instance 00279 * @param RTC_Format This parameter can be one of the following values: 00280 * @arg @ref LL_RTC_FORMAT_BIN 00281 * @arg @ref LL_RTC_FORMAT_BCD 00282 * @param RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains 00283 * the time configuration information for the RTC. 00284 * @retval An ErrorStatus enumeration value: 00285 * - SUCCESS: RTC Time register is configured 00286 * - ERROR: RTC Time register is not configured 00287 */ 00288 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct) 00289 { 00290 ErrorStatus status = ERROR; 00291 00292 /* Check the parameters */ 00293 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00294 assert_param(IS_LL_RTC_FORMAT(RTC_Format)); 00295 00296 if (RTC_Format == LL_RTC_FORMAT_BIN) 00297 { 00298 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR) 00299 { 00300 assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours)); 00301 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat)); 00302 } 00303 else 00304 { 00305 RTC_TimeStruct->TimeFormat = 0x00U; 00306 assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours)); 00307 } 00308 assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes)); 00309 assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds)); 00310 } 00311 else 00312 { 00313 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR) 00314 { 00315 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours))); 00316 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat)); 00317 } 00318 else 00319 { 00320 RTC_TimeStruct->TimeFormat = 0x00U; 00321 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours))); 00322 } 00323 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes))); 00324 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds))); 00325 } 00326 00327 /* Disable the write protection for RTC registers */ 00328 LL_RTC_DisableWriteProtection(RTCx); 00329 00330 /* Set Initialization mode */ 00331 if (LL_RTC_EnterInitMode(RTCx) != ERROR) 00332 { 00333 /* Check the input parameters format */ 00334 if (RTC_Format != LL_RTC_FORMAT_BIN) 00335 { 00336 LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours, 00337 RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds); 00338 } 00339 else 00340 { 00341 LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours), 00342 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes), 00343 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds)); 00344 } 00345 00346 /* Exit Initialization mode */ 00347 LL_RTC_DisableInitMode(RTC); 00348 00349 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00350 if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U) 00351 { 00352 status = LL_RTC_WaitForSynchro(RTCx); 00353 } 00354 else 00355 { 00356 status = SUCCESS; 00357 } 00358 } 00359 /* Enable the write protection for RTC registers */ 00360 LL_RTC_EnableWriteProtection(RTCx); 00361 00362 return status; 00363 } 00364 00365 /** 00366 * @brief Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec). 00367 * @param RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized. 00368 * @retval None 00369 */ 00370 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct) 00371 { 00372 /* Time = 00h:00min:00sec */ 00373 RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24; 00374 RTC_TimeStruct->Hours = 0U; 00375 RTC_TimeStruct->Minutes = 0U; 00376 RTC_TimeStruct->Seconds = 0U; 00377 } 00378 00379 /** 00380 * @brief Set the RTC current date. 00381 * @param RTCx RTC Instance 00382 * @param RTC_Format This parameter can be one of the following values: 00383 * @arg @ref LL_RTC_FORMAT_BIN 00384 * @arg @ref LL_RTC_FORMAT_BCD 00385 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains 00386 * the date configuration information for the RTC. 00387 * @retval An ErrorStatus enumeration value: 00388 * - SUCCESS: RTC Day register is configured 00389 * - ERROR: RTC Day register is not configured 00390 */ 00391 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct) 00392 { 00393 ErrorStatus status = ERROR; 00394 00395 /* Check the parameters */ 00396 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00397 assert_param(IS_LL_RTC_FORMAT(RTC_Format)); 00398 00399 if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U)) 00400 { 00401 RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t)~(0x10U)) + 0x0AU; 00402 } 00403 if (RTC_Format == LL_RTC_FORMAT_BIN) 00404 { 00405 assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year)); 00406 assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month)); 00407 assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day)); 00408 } 00409 else 00410 { 00411 assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year))); 00412 assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month))); 00413 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day))); 00414 } 00415 assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay)); 00416 00417 /* Disable the write protection for RTC registers */ 00418 LL_RTC_DisableWriteProtection(RTCx); 00419 00420 /* Set Initialization mode */ 00421 if (LL_RTC_EnterInitMode(RTCx) != ERROR) 00422 { 00423 /* Check the input parameters format */ 00424 if (RTC_Format != LL_RTC_FORMAT_BIN) 00425 { 00426 LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year); 00427 } 00428 else 00429 { 00430 LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day), 00431 __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year)); 00432 } 00433 00434 /* Exit Initialization mode */ 00435 LL_RTC_DisableInitMode(RTC); 00436 00437 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00438 if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U) 00439 { 00440 status = LL_RTC_WaitForSynchro(RTCx); 00441 } 00442 else 00443 { 00444 status = SUCCESS; 00445 } 00446 } 00447 /* Enable the write protection for RTC registers */ 00448 LL_RTC_EnableWriteProtection(RTCx); 00449 00450 return status; 00451 } 00452 00453 /** 00454 * @brief Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00) 00455 * @param RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized. 00456 * @retval None 00457 */ 00458 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct) 00459 { 00460 /* Monday, January 01 xx00 */ 00461 RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY; 00462 RTC_DateStruct->Day = 1U; 00463 RTC_DateStruct->Month = LL_RTC_MONTH_JANUARY; 00464 RTC_DateStruct->Year = 0U; 00465 } 00466 00467 /** 00468 * @brief Set the RTC Alarm A. 00469 * @note The Alarm register can only be written when the corresponding Alarm 00470 * is disabled (Use @ref LL_RTC_ALMA_Disable function). 00471 * @param RTCx RTC Instance 00472 * @param RTC_Format This parameter can be one of the following values: 00473 * @arg @ref LL_RTC_FORMAT_BIN 00474 * @arg @ref LL_RTC_FORMAT_BCD 00475 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that 00476 * contains the alarm configuration parameters. 00477 * @retval An ErrorStatus enumeration value: 00478 * - SUCCESS: ALARMA registers are configured 00479 * - ERROR: ALARMA registers are not configured 00480 */ 00481 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct) 00482 { 00483 /* Check the parameters */ 00484 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00485 assert_param(IS_LL_RTC_FORMAT(RTC_Format)); 00486 assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask)); 00487 assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel)); 00488 00489 if (RTC_Format == LL_RTC_FORMAT_BIN) 00490 { 00491 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR) 00492 { 00493 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours)); 00494 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat)); 00495 } 00496 else 00497 { 00498 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U; 00499 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours)); 00500 } 00501 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes)); 00502 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds)); 00503 00504 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) 00505 { 00506 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay)); 00507 } 00508 else 00509 { 00510 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay)); 00511 } 00512 } 00513 else 00514 { 00515 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR) 00516 { 00517 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours))); 00518 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat)); 00519 } 00520 else 00521 { 00522 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U; 00523 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours))); 00524 } 00525 00526 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes))); 00527 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds))); 00528 00529 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) 00530 { 00531 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay))); 00532 } 00533 else 00534 { 00535 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay))); 00536 } 00537 } 00538 00539 /* Disable the write protection for RTC registers */ 00540 LL_RTC_DisableWriteProtection(RTCx); 00541 00542 /* Select weekday selection */ 00543 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) 00544 { 00545 /* Set the date for ALARM */ 00546 LL_RTC_ALMA_DisableWeekday(RTCx); 00547 if (RTC_Format != LL_RTC_FORMAT_BIN) 00548 { 00549 LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay); 00550 } 00551 else 00552 { 00553 LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay)); 00554 } 00555 } 00556 else 00557 { 00558 /* Set the week day for ALARM */ 00559 LL_RTC_ALMA_EnableWeekday(RTCx); 00560 LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay); 00561 } 00562 00563 /* Configure the Alarm register */ 00564 if (RTC_Format != LL_RTC_FORMAT_BIN) 00565 { 00566 LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours, 00567 RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds); 00568 } 00569 else 00570 { 00571 LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, 00572 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours), 00573 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes), 00574 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds)); 00575 } 00576 /* Set ALARM mask */ 00577 LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask); 00578 00579 /* Enable the write protection for RTC registers */ 00580 LL_RTC_EnableWriteProtection(RTCx); 00581 00582 return SUCCESS; 00583 } 00584 00585 /** 00586 * @brief Set the RTC Alarm B. 00587 * @note The Alarm register can only be written when the corresponding Alarm 00588 * is disabled (@ref LL_RTC_ALMB_Disable function). 00589 * @param RTCx RTC Instance 00590 * @param RTC_Format This parameter can be one of the following values: 00591 * @arg @ref LL_RTC_FORMAT_BIN 00592 * @arg @ref LL_RTC_FORMAT_BCD 00593 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that 00594 * contains the alarm configuration parameters. 00595 * @retval An ErrorStatus enumeration value: 00596 * - SUCCESS: ALARMB registers are configured 00597 * - ERROR: ALARMB registers are not configured 00598 */ 00599 ErrorStatus LL_RTC_ALMB_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct) 00600 { 00601 /* Check the parameters */ 00602 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00603 assert_param(IS_LL_RTC_FORMAT(RTC_Format)); 00604 assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct->AlarmMask)); 00605 assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel)); 00606 00607 if (RTC_Format == LL_RTC_FORMAT_BIN) 00608 { 00609 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR) 00610 { 00611 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours)); 00612 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat)); 00613 } 00614 else 00615 { 00616 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U; 00617 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours)); 00618 } 00619 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes)); 00620 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds)); 00621 00622 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) 00623 { 00624 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay)); 00625 } 00626 else 00627 { 00628 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay)); 00629 } 00630 } 00631 else 00632 { 00633 if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR) 00634 { 00635 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours))); 00636 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat)); 00637 } 00638 else 00639 { 00640 RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U; 00641 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours))); 00642 } 00643 00644 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes))); 00645 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds))); 00646 00647 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) 00648 { 00649 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay))); 00650 } 00651 else 00652 { 00653 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay))); 00654 } 00655 } 00656 00657 /* Disable the write protection for RTC registers */ 00658 LL_RTC_DisableWriteProtection(RTCx); 00659 00660 /* Select weekday selection */ 00661 if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) 00662 { 00663 /* Set the date for ALARM */ 00664 LL_RTC_ALMB_DisableWeekday(RTCx); 00665 if (RTC_Format != LL_RTC_FORMAT_BIN) 00666 { 00667 LL_RTC_ALMB_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay); 00668 } 00669 else 00670 { 00671 LL_RTC_ALMB_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay)); 00672 } 00673 } 00674 else 00675 { 00676 /* Set the week day for ALARM */ 00677 LL_RTC_ALMB_EnableWeekday(RTCx); 00678 LL_RTC_ALMB_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay); 00679 } 00680 00681 /* Configure the Alarm register */ 00682 if (RTC_Format != LL_RTC_FORMAT_BIN) 00683 { 00684 LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours, 00685 RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds); 00686 } 00687 else 00688 { 00689 LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, 00690 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours), 00691 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes), 00692 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds)); 00693 } 00694 /* Set ALARM mask */ 00695 LL_RTC_ALMB_SetMask(RTCx, RTC_AlarmStruct->AlarmMask); 00696 00697 /* Enable the write protection for RTC registers */ 00698 LL_RTC_EnableWriteProtection(RTCx); 00699 00700 return SUCCESS; 00701 } 00702 00703 /** 00704 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec / 00705 * Day = 1st day of the month/Mask = all fields are masked). 00706 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized. 00707 * @retval None 00708 */ 00709 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct) 00710 { 00711 /* Alarm Time Settings : Time = 00h:00mn:00sec */ 00712 RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM; 00713 RTC_AlarmStruct->AlarmTime.Hours = 0U; 00714 RTC_AlarmStruct->AlarmTime.Minutes = 0U; 00715 RTC_AlarmStruct->AlarmTime.Seconds = 0U; 00716 00717 /* Alarm Day Settings : Day = 1st day of the month */ 00718 RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE; 00719 RTC_AlarmStruct->AlarmDateWeekDay = 1U; 00720 00721 /* Alarm Masks Settings : Mask = all fields are not masked */ 00722 RTC_AlarmStruct->AlarmMask = LL_RTC_ALMA_MASK_NONE; 00723 } 00724 00725 /** 00726 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec / 00727 * Day = 1st day of the month/Mask = all fields are masked). 00728 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized. 00729 * @retval None 00730 */ 00731 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct) 00732 { 00733 /* Alarm Time Settings : Time = 00h:00mn:00sec */ 00734 RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMB_TIME_FORMAT_AM; 00735 RTC_AlarmStruct->AlarmTime.Hours = 0U; 00736 RTC_AlarmStruct->AlarmTime.Minutes = 0U; 00737 RTC_AlarmStruct->AlarmTime.Seconds = 0U; 00738 00739 /* Alarm Day Settings : Day = 1st day of the month */ 00740 RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMB_DATEWEEKDAYSEL_DATE; 00741 RTC_AlarmStruct->AlarmDateWeekDay = 1U; 00742 00743 /* Alarm Masks Settings : Mask = all fields are not masked */ 00744 RTC_AlarmStruct->AlarmMask = LL_RTC_ALMB_MASK_NONE; 00745 } 00746 00747 /** 00748 * @brief Enters the RTC Initialization mode. 00749 * @note The RTC Initialization mode is write protected, use the 00750 * @ref LL_RTC_DisableWriteProtection before calling this function. 00751 * @param RTCx RTC Instance 00752 * @retval An ErrorStatus enumeration value: 00753 * - SUCCESS: RTC is in Init mode 00754 * - ERROR: RTC is not in Init mode 00755 */ 00756 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx) 00757 { 00758 __IO uint32_t timeout = RTC_INITMODE_TIMEOUT; 00759 ErrorStatus status = SUCCESS; 00760 uint32_t tmp = 0U; 00761 00762 /* Check the parameter */ 00763 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00764 00765 /* Check if the Initialization mode is set */ 00766 if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U) 00767 { 00768 /* Set the Initialization mode */ 00769 LL_RTC_EnableInitMode(RTCx); 00770 00771 /* Wait till RTC is in INIT state and if Time out is reached exit */ 00772 tmp = LL_RTC_IsActiveFlag_INIT(RTCx); 00773 while ((timeout != 0U) && (tmp != 1U)) 00774 { 00775 if (LL_SYSTICK_IsActiveCounterFlag() == 1U) 00776 { 00777 timeout --; 00778 } 00779 tmp = LL_RTC_IsActiveFlag_INIT(RTCx); 00780 if (timeout == 0U) 00781 { 00782 status = ERROR; 00783 } 00784 } 00785 } 00786 return status; 00787 } 00788 00789 /** 00790 * @brief Exit the RTC Initialization mode. 00791 * @note When the initialization sequence is complete, the calendar restarts 00792 * counting after 4 RTCCLK cycles. 00793 * @note The RTC Initialization mode is write protected, use the 00794 * @ref LL_RTC_DisableWriteProtection before calling this function. 00795 * @param RTCx RTC Instance 00796 * @retval An ErrorStatus enumeration value: 00797 * - SUCCESS: RTC exited from in Init mode 00798 * - ERROR: Not applicable 00799 */ 00800 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx) 00801 { 00802 /* Check the parameter */ 00803 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00804 00805 /* Disable initialization mode */ 00806 LL_RTC_DisableInitMode(RTCx); 00807 00808 return SUCCESS; 00809 } 00810 00811 /** 00812 * @brief Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are 00813 * synchronized with RTC APB clock. 00814 * @note The RTC Resynchronization mode is write protected, use the 00815 * @ref LL_RTC_DisableWriteProtection before calling this function. 00816 * @note To read the calendar through the shadow registers after Calendar 00817 * initialization, calendar update or after wakeup from low power modes 00818 * the software must first clear the RSF flag. 00819 * The software must then wait until it is set again before reading 00820 * the calendar, which means that the calendar registers have been 00821 * correctly copied into the RTC_TR and RTC_DR shadow registers. 00822 * @param RTCx RTC Instance 00823 * @retval An ErrorStatus enumeration value: 00824 * - SUCCESS: RTC registers are synchronised 00825 * - ERROR: RTC registers are not synchronised 00826 */ 00827 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx) 00828 { 00829 __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT; 00830 ErrorStatus status = SUCCESS; 00831 uint32_t tmp = 0U; 00832 00833 /* Check the parameter */ 00834 assert_param(IS_RTC_ALL_INSTANCE(RTCx)); 00835 00836 /* Clear RSF flag */ 00837 LL_RTC_ClearFlag_RS(RTCx); 00838 00839 /* Wait the registers to be synchronised */ 00840 tmp = LL_RTC_IsActiveFlag_RS(RTCx); 00841 while ((timeout != 0U) && (tmp != 0U)) 00842 { 00843 if (LL_SYSTICK_IsActiveCounterFlag() == 1U) 00844 { 00845 timeout--; 00846 } 00847 tmp = LL_RTC_IsActiveFlag_RS(RTCx); 00848 if (timeout == 0U) 00849 { 00850 status = ERROR; 00851 } 00852 } 00853 00854 if (status != ERROR) 00855 { 00856 timeout = RTC_SYNCHRO_TIMEOUT; 00857 tmp = LL_RTC_IsActiveFlag_RS(RTCx); 00858 while ((timeout != 0U) && (tmp != 1U)) 00859 { 00860 if (LL_SYSTICK_IsActiveCounterFlag() == 1U) 00861 { 00862 timeout--; 00863 } 00864 tmp = LL_RTC_IsActiveFlag_RS(RTCx); 00865 if (timeout == 0U) 00866 { 00867 status = ERROR; 00868 } 00869 } 00870 } 00871 00872 return (status); 00873 } 00874 00875 /** 00876 * @} 00877 */ 00878 00879 /** 00880 * @} 00881 */ 00882 00883 /** 00884 * @} 00885 */ 00886 00887 #endif /* defined(RTC) */ 00888 00889 /** 00890 * @} 00891 */ 00892 00893 #endif /* USE_FULL_LL_DRIVER */ 00894 00895 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/