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