STM8L15x Standard Peripherals Drivers
|
stm8l15x_rtc.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8l15x_rtc.c 00004 * @author MCD Application Team 00005 * @version V1.5.0 00006 * @date 13-May-2011 00007 * @brief This file provides firmware functions to manage the following 00008 * functionalities of the Real-Time Clock (RTC) peripheral: 00009 * - Initialization 00010 * - Calendar (Time and Date) configuration 00011 * - Alarm configuration 00012 * - WakeUp Timer configuration 00013 * - Daylight Saving configuration 00014 * - Output pin Configuration 00015 * - Smooth Calibration configuration 00016 * - Tampers configuration 00017 * - Output Type configuration 00018 * - Interrupts and flags management 00019 * 00020 * @verbatim 00021 * 00022 * =================================================================== 00023 * RTC Domain Reset 00024 * =================================================================== 00025 * After power-on reset, the RTC domain (RTC registers) is reset. 00026 * 00027 * =================================================================== 00028 * RTC Operating Condition 00029 * =================================================================== 00030 * As long as the supply voltage remains in the operating range, 00031 * the RTC never stops, regardless of the device status (Run mode, 00032 * low power modes or under reset). 00033 * 00034 * =================================================================== 00035 * RTC Domain Access 00036 * =================================================================== 00037 * After reset, the RTC domain (RTC registers) is protected against 00038 * possible stray write accesses. 00039 * To enable access to the RTC registers, proceed as follows: 00040 * - Select the RTC clock source using the CLK_RTCClockConfig() 00041 * function : HSE, HSI, LSE or LSI. 00042 * CLK_RTCClockConfig(CLK_RTCCLKSource_HSI, CLK_RTCCLKDiv_1) 00043 * - Enable RTC Clock using the CLK_PeripheralClockConfig() function 00044 * : CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE). 00045 * 00046 * =================================================================== 00047 * RTC Driver: how to use it 00048 * =================================================================== 00049 * - Enable the RTC domain access (see description in the section above) 00050 * - Configure the RTC Prescaler (Asynchronous and Synchronous) and 00051 * RTC hour format using the RTC_Init() function. 00052 * 00053 * Time and Date configuration 00054 * =========================== 00055 * - To configure the RTC Calendar (Time and Date) use the RTC_SetTime() 00056 * and RTC_SetDate() functions. 00057 * - To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate() 00058 * functions. 00059 * - Use the RTC_DayLightSavingConfig() function to add or sub 1hour 00060 * to the RTC Calendar. 00061 * 00062 * Alarm configuration 00063 * =================== 00064 * - To configure the RTC Alarm use the RTC_SetAlarm() function. 00065 * - Enable the selected RTC Alarm using the RTC_AlarmCmd() function 00066 * - To read the RTC Alarm, use the RTC_GetAlarm() function. 00067 * 00068 * RTC Wakeup configuration 00069 * ======================== 00070 * - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig() 00071 * function. 00072 * - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter() 00073 * function 00074 * - Enable the RTC WakeUp using the RTC_WakeUpCmd() function 00075 * - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter() 00076 * function. 00077 * 00078 * Outputs configuration 00079 * ===================== 00080 * The RTC has 2 different outputs: 00081 * - AFO_ALARM: this output is used to manage the RTC Alarm and 00082 * WaKeUp signals. 00083 * To output the selected RTC signal on RTC_ALARM pin, use the 00084 * RTC_OutputConfig() function. 00085 * - AFO_CALIB: this output is used to manage the RTC Clock divided 00086 * by 64 (512Hz) signal or divided by 32768 (1Hz) which can be 00087 * configured using RTC_CalibOutputConfig() function. 00088 * To output the RTC Clock on RTC_CALIB pin, use the 00089 * RTC_CalibOutputCmd() function. 00090 * 00091 * Smooth Calibration configuration 00092 * ================================= 00093 * - Configure the RTC Smooth Calibration Value and the corresponding 00094 * sign using the RTC_SmoothCalibConfig() function. 00095 00096 * 00097 * 00098 * Tamper configuration 00099 * ==================== 00100 * - Configure the RTC Tamper Level using the RTC_TamperConfig() 00101 * function. 00102 * - Enable the RTC Tamper using the RTC_TamperCmd() function. 00103 * 00104 * 00105 * =================================================================== 00106 * RTC and low power modes 00107 * =================================================================== 00108 * The MCU can be woken up from a low power mode by an RTC alternate 00109 * function. 00110 * The RTC alternate functions are the RTC alarms, 00111 * RTC wakeup and RTC tamper event detection. 00112 * These RTC alternate functions can wake up the system from the 00113 * lowpower modes. 00114 * The system can also wake up from low power modes without depending 00115 * on an external interrupt (Auto-wakeup mode), by using the RTC alarm 00116 * or the RTC wakeup events. 00117 * The RTC provides a programmable time base for waking up from the 00118 * halt or wfe or wfi modes at regular intervals. 00119 * 00120 * 00121 * @endverbatim 00122 * 00123 ****************************************************************************** 00124 * @attention 00125 * 00126 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 00127 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 00128 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 00129 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 00130 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 00131 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 00132 * 00133 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> 00134 ****************************************************************************** 00135 */ 00136 00137 /* Includes ------------------------------------------------------------------*/ 00138 #include "stm8l15x_rtc.h" 00139 00140 /** @addtogroup STM8L15x_StdPeriph_Driver 00141 * @{ 00142 */ 00143 00144 /** @defgroup RTC 00145 * @brief RTC driver modules 00146 * @{ 00147 */ 00148 00149 /* Private typedef -----------------------------------------------------------*/ 00150 /* Private define ------------------------------------------------------------*/ 00151 #define INIT_TIMEOUT ((uint16_t)0xFFFF) 00152 #define RSF_TIMEOUT ((uint16_t)0xFFFF) 00153 #define INITF_TIMEOUT ((uint16_t)0xFFFF) 00154 #define WUTWF_TIMEOUT ((uint16_t)0xFFFF) 00155 #define ALRAWF_TIMEOUT ((uint16_t)0xFFFF) 00156 #define RECALPF_TIMEOUT ((uint16_t)0xFFFF) 00157 #define SHPF_TIMEOUT ((uint16_t)0xFFFF) 00158 00159 #define TEN_VALUE_BCD ((uint8_t)0x10) 00160 #define TEN_VALUE_BIN ((uint8_t)0x0A) 00161 00162 /* Private macro -------------------------------------------------------------*/ 00163 /* Private variables ---------------------------------------------------------*/ 00164 00165 /** @defgroup RTC_Private_Functions_Prototype 00166 * @{ 00167 */ 00168 static uint8_t ByteToBcd2(uint8_t Value); 00169 static uint8_t Bcd2ToByte(uint8_t Value); 00170 00171 /** 00172 * @} 00173 */ 00174 00175 /** @defgroup RTC_Private_Functions 00176 * @{ 00177 */ 00178 00179 /** @defgroup RTC_Group1 Initialization and Configuration functions 00180 * @brief Initialization and Configuration functions 00181 * 00182 @verbatim 00183 =============================================================================== 00184 Initialization and Configuration functions 00185 =============================================================================== 00186 00187 This section provide functions allowing to initialize and configure the RTC 00188 Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers 00189 Write protection, enter and exit the RTC initialization mode, RTC registers 00190 synchronization check and reference clock detection enable. 00191 00192 1. The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is 00193 split into 2 programmable prescaler to minimize power consumption. 00194 - A 7-bit asynchronous prescaler and A 13-bit or 15-bit synchronous 00195 prescaler depending to density of the mcu package. 00196 - When both prescaler are used, it is recommended to configure the 00197 asynchronous prescaler to a high value to minimize consumption. 00198 00199 2. All RTC registers are Write protected. Writing to the RTC registers 00200 is enabled by writing a key into the Write Protection register, RTC_WPR. 00201 00202 3. To Configure the RTC Calendar, user application should enter initialization 00203 mode. In this mode, the Calendar counter is stopped and its value can be 00204 updated. When the initialization sequence is complete, the Calendar restarts 00205 counting after 4 RTCCLK cycles. 00206 00207 4. To read the Calendar through the shadow registers after Calendar initialization, 00208 Calendar update or after wakeup from low power modes the software must first 00209 clear the RSF flag. The software must then wait until it is set again before 00210 reading the Calendar, which means that the Calendar registers have been 00211 correctly copied into the RTC_TR and RTC_DR shadow registers. 00212 The RTC_WaitForSynchro() function implements the above software sequence 00213 (RSF clear and RSF check). 00214 00215 @endverbatim 00216 * @{ 00217 */ 00218 /** 00219 * @brief Deinitializes the RTC registers to their default reset values. 00220 * @note This function doesn't reset the RTC Clock source. 00221 * @param None 00222 * @retval An ErrorStatus enumeration value: 00223 * - SUCCESS: RTC registers are deinitialized 00224 * - ERROR: RTC registers are not deinitialized 00225 */ 00226 ErrorStatus RTC_DeInit(void) 00227 { 00228 ErrorStatus status = ERROR; 00229 uint16_t wutwfcount = 0; 00230 uint16_t recalpfcount = 0; 00231 00232 /* Disable the write protection for RTC registers */ 00233 RTC->WPR = 0xCA; 00234 RTC->WPR = 0x53; 00235 00236 /* Set Initialization mode */ 00237 if (RTC_EnterInitMode() == ERROR) 00238 { 00239 status = ERROR; 00240 /* Enable the write protection for RTC registers */ 00241 RTC->WPR = 0xFF; 00242 } 00243 else 00244 { 00245 /* Reset TR registers */ 00246 RTC->TR1 = RTC_TR1_RESET_VALUE; 00247 RTC->TR2 = RTC_TR2_RESET_VALUE; 00248 RTC->TR3 = RTC_TR3_RESET_VALUE; 00249 00250 /* Reset DR registers */ 00251 RTC->DR1 = RTC_DR1_RESET_VALUE; 00252 RTC->DR2 = RTC_DR2_RESET_VALUE; 00253 RTC->DR3 = RTC_DR3_RESET_VALUE; 00254 00255 /* Reset SPER & ARPER registers */ 00256 RTC->SPRERH = RTC_SPRERH_RESET_VALUE; 00257 RTC->SPRERL = RTC_SPRERL_RESET_VALUE; 00258 RTC->APRER = RTC_APRER_RESET_VALUE; 00259 00260 RTC->TCR1 = RTC_TCR1_RESET_VALUE; 00261 RTC->TCR2 = RTC_TCR2_RESET_VALUE; 00262 00263 /* Reset All CR1 bits except CR1[2:0] */ 00264 00265 RTC->CR1 = RTC_CR1_RESET_VALUE; 00266 RTC->CR2 = RTC_CR2_RESET_VALUE; 00267 RTC->CR3 = RTC_CR3_RESET_VALUE; 00268 00269 /* Wait till RTC WUTWF flag is set or if Time out is reached exit */ 00270 while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != WUTWF_TIMEOUT)) 00271 { 00272 wutwfcount++; 00273 RTC->ISR1 = 0; 00274 } 00275 00276 if ((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) 00277 { 00278 status = ERROR; 00279 /* Enable the write protection for RTC registers */ 00280 RTC->WPR = 0xFF; 00281 } 00282 else 00283 { 00284 /* Reset All CR1 bits */ 00285 RTC->CR1 = RTC_CR1_RESET_VALUE; 00286 00287 /* Reset WUTR registers */ 00288 RTC->WUTRH = RTC_WUTRH_RESET_VALUE; 00289 RTC->WUTRL = RTC_WUTRL_RESET_VALUE; 00290 00291 /* Reset ALARM registers */ 00292 RTC->ALRMAR1 = RTC_ALRMAR1_RESET_VALUE; 00293 RTC->ALRMAR2 = RTC_ALRMAR2_RESET_VALUE; 00294 RTC->ALRMAR3 = RTC_ALRMAR3_RESET_VALUE; 00295 RTC->ALRMAR4 = RTC_ALRMAR4_RESET_VALUE; 00296 00297 RTC->ALRMASSRH = RTC_ALRMASSRH_RESET_VALUE; 00298 RTC->ALRMASSRL = RTC_ALRMASSRL_RESET_VALUE; 00299 RTC->ALRMASSMSKR = RTC_ALRMASSMSKR_RESET_VALUE; 00300 00301 /* Reset ISR register and exit initialization mode */ 00302 RTC->ISR1 = (uint8_t)0x00; 00303 RTC->ISR2 = RTC_ISR2_RESET_VALUE; 00304 00305 if ((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) 00306 { 00307 while (((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT)) 00308 { 00309 recalpfcount++; 00310 } 00311 } 00312 if ((RTC->ISR1 & RTC_ISR1_RECALPF) == RESET) 00313 { 00314 RTC->CALRH = RTC_CALRH_RESET_VALUE; 00315 RTC->CALRL = RTC_CALRL_RESET_VALUE; 00316 00317 if (RTC_WaitForSynchro() == ERROR) 00318 { 00319 status = ERROR; 00320 } 00321 else 00322 { 00323 status = SUCCESS; 00324 } 00325 } 00326 else 00327 { 00328 status = ERROR; 00329 } 00330 00331 /* Enable the write protection for RTC registers */ 00332 RTC->WPR = 0xFF; 00333 } 00334 } 00335 00336 /* return Deinitialize RTC registers status*/ 00337 return (ErrorStatus)status; 00338 } 00339 00340 00341 00342 /** 00343 * @brief Initializes the RTC registers according to the specified parameters 00344 * in RTC_InitStruct. 00345 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains 00346 * the configuration information for the RTC peripheral. 00347 * @note The RTC Prescaler register is write protected and can be written in 00348 * initialization mode only. 00349 * @retval An ErrorStatus enumeration value: 00350 * - SUCCESS: RTC registers are initialized 00351 * - ERROR: RTC registers are not initialized 00352 */ 00353 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct) 00354 { 00355 ErrorStatus status = ERROR; 00356 00357 /* Check the parameters */ 00358 assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat)); 00359 assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv)); 00360 assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv)); 00361 00362 /* Disable the write protection for RTC registers */ 00363 RTC->WPR = 0xCA; 00364 RTC->WPR = 0x53; 00365 00366 /* Set Initialization mode */ 00367 if (RTC_EnterInitMode() == ERROR) 00368 { 00369 status = ERROR; 00370 } 00371 else 00372 { 00373 /* Clear the bits to be configured first */ 00374 RTC->CR1 &= ((uint8_t)~( RTC_CR1_FMT )); 00375 00376 /* Set RTC_CR1 register */ 00377 RTC->CR1 |= ((uint8_t)(RTC_InitStruct->RTC_HourFormat)); 00378 00379 /* Set Prescalers registers */ 00380 RTC->SPRERH = (uint8_t)(RTC_InitStruct->RTC_SynchPrediv >> 8); 00381 RTC->SPRERL = (uint8_t)(RTC_InitStruct->RTC_SynchPrediv); 00382 RTC->APRER = (uint8_t)(RTC_InitStruct->RTC_AsynchPrediv); 00383 00384 /* Exit Initialization mode */ 00385 RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT; 00386 00387 status = SUCCESS; 00388 } 00389 00390 /* Enable the write protection for RTC registers */ 00391 RTC->WPR = 0xFF; 00392 00393 /* return Initialize the RTC registers status*/ 00394 return (ErrorStatus)(status); 00395 } 00396 00397 /** 00398 * @brief Fills each RTC_InitStruct member with its default value 00399 * Hour format = 24h / Prescalers configured to their reset values. 00400 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be 00401 * initialized. 00402 * @retval None 00403 */ 00404 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct) 00405 { 00406 /* Initialize the RTC_Hourformat member */ 00407 RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24; 00408 00409 /* Initialize the RTC_AsynchPrediv member */ 00410 RTC_InitStruct->RTC_AsynchPrediv = RTC_APRER_RESET_VALUE; 00411 00412 /* Initialize the RTC_SynchPrediv member */ 00413 RTC_InitStruct->RTC_SynchPrediv = RTC_SPRERL_RESET_VALUE; 00414 } 00415 00416 /** 00417 * @brief Enables or disables the RTC registers write protection. 00418 * @param NewState: new state of the write protection. 00419 * This parameter can be: ENABLE or DISABLE. 00420 * @note Writing a wrong key reactivates the write protection. 00421 * @note The protection mechanism is not affected by system reset. 00422 * @retval None 00423 */ 00424 void RTC_WriteProtectionCmd(FunctionalState NewState) 00425 { 00426 /* Check the parameters */ 00427 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00428 00429 if (NewState != DISABLE) 00430 { 00431 /* Enable the write protection for RTC registers */ 00432 RTC->WPR = RTC_WPR_EnableKey; 00433 } 00434 else 00435 { 00436 /* Disable the write protection for RTC registers */ 00437 RTC->WPR = RTC_WPR_DisableKey1; 00438 RTC->WPR = RTC_WPR_DisableKey2; 00439 } 00440 } 00441 00442 00443 /** 00444 * @brief Enters the RTC Initialization mode. 00445 * @note The RTC Initialization mode is write protected, use the 00446 * RTC_WriteProtectionCmd(DISABLE) before calling this function. 00447 * @param None 00448 * @retval An ErrorStatus enumeration value: 00449 * - SUCCESS: RTC is in Init mode 00450 * - ERROR: RTC is not in Init mode 00451 */ 00452 ErrorStatus RTC_EnterInitMode(void) 00453 { 00454 ErrorStatus status = ERROR; 00455 uint16_t initfcount = 0; 00456 00457 /* Check if the Initialization mode is set */ 00458 if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET) 00459 { 00460 /* Set the Initialization mode */ 00461 RTC->ISR1 = (uint8_t)RTC_ISR1_INIT; 00462 00463 /* Wait until INITF flag is set */ 00464 while (((RTC->ISR1 & RTC_ISR1_INITF) == RESET) && ( initfcount != INITF_TIMEOUT)) 00465 { 00466 initfcount++; 00467 } 00468 } 00469 00470 if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET) 00471 { 00472 status = ERROR; 00473 } 00474 else 00475 { 00476 status = SUCCESS; 00477 } 00478 00479 return (ErrorStatus)status; 00480 } 00481 00482 00483 /** 00484 * @brief Exits the RTC Initialization mode. 00485 * @note When the initialization sequence is complete, the Calendar restarts 00486 * counting after 4 RTCCLK cycles. 00487 * @note The RTC Initialization mode is write protected, use the 00488 * RTC_WriteProtectionCmd(DISABLE) before calling this function. 00489 * @param None 00490 * @retval None 00491 */ 00492 void RTC_ExitInitMode(void) 00493 { 00494 /* Exit Initialization mode */ 00495 RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT; 00496 } 00497 00498 00499 /** 00500 * @brief Waits until the RTC Calendar registers (Time and Date) 00501 * are synchronized with RTC clock. 00502 * @note This function is meaningless when BAYPASS feature is enabled in RTC_CR1 00503 * register. 00504 * @note To read the Calendar through the shadow registers after Calendar 00505 * initialization, Calendar update or after wakeup from low power modes 00506 * the software must first clear the RSF flag. 00507 * The software must then wait until it is set again before reading 00508 * the Calendar (if not yet done), which means that the Calendar registers 00509 * have been correctly copied into the RTC_TRx and RTC_DRx shadow registers. 00510 * @note RTC_SetTime() and RTC_SetDate() functions call RTC_WaitForSynchro() function 00511 * after updating the Calendar. In Run mode, User can use RTC_GetDate(), 00512 * RTC_GetTime() and/or RTC_GetSubSecond() without need to call 00513 * RTC_WaitForSynchro() function. After waking up from low power mode, this 00514 * function must be called before calling RTC_GetDate(), RTC_GetTime() or 00515 * RTC_GetSubSecond() functions. 00516 * @param None 00517 * @retval An ErrorStatus enumeration value: 00518 * - SUCCESS: RTC registers are synchronized 00519 * - ERROR: RTC registers are not synchronized 00520 */ 00521 ErrorStatus RTC_WaitForSynchro(void) 00522 { 00523 uint16_t rsfcount = 0; 00524 ErrorStatus status = ERROR; 00525 00526 /* Disable the write protection for RTC registers */ 00527 RTC->WPR = 0xCA; 00528 RTC->WPR = 0x53; 00529 00530 /* Clear RSF flag by writing 0 in RSF bit */ 00531 RTC->ISR1 &= (uint8_t)~(RTC_ISR1_RSF | RTC_ISR1_INIT); 00532 00533 /* Wait the registers to be synchronised */ 00534 while (((RTC->ISR1 & RTC_ISR1_RSF) == RESET) && ( rsfcount != RSF_TIMEOUT)) 00535 { 00536 rsfcount++; 00537 } 00538 00539 /* Check if RSF flag occurs*/ 00540 if ((RTC->ISR1 & RTC_ISR1_RSF) != RESET) 00541 { 00542 status = SUCCESS; 00543 } 00544 else 00545 { 00546 status = ERROR; 00547 } 00548 00549 /* Enable the write protection for RTC registers */ 00550 RTC->WPR = 0xFF; 00551 00552 return (ErrorStatus)status; 00553 } 00554 00555 00556 /** 00557 * @brief Enables or Disables the RTC Ratio. 00558 * @param NewState: new state of the Ratio feature. 00559 * This parameter can be: ENABLE or DISABLE. 00560 * @retval None 00561 */ 00562 void RTC_RatioCmd(FunctionalState NewState) 00563 { 00564 /* Check the parameters */ 00565 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00566 00567 /* Disable the write protection for RTC registers */ 00568 RTC->WPR = 0xCA; 00569 RTC->WPR = 0x53; 00570 00571 if (NewState != DISABLE) 00572 { 00573 /* Set the RATIO bit */ 00574 RTC->CR1 |= (uint8_t)RTC_CR1_RATIO; 00575 } 00576 else 00577 { 00578 /* Reset the RATIO bit */ 00579 RTC->CR1 &= (uint8_t)~RTC_CR1_RATIO; 00580 } 00581 00582 /* Enable the write protection for RTC registers */ 00583 RTC->WPR = 0xFF; 00584 } 00585 00586 /** 00587 * @brief Enables or Disables the Bypass Shadow feature. 00588 * @param NewState: new state of the Bypass Shadow feature. 00589 * This parameter can be: ENABLE or DISABLE. 00590 * @retval None 00591 */ 00592 void RTC_BypassShadowCmd(FunctionalState NewState) 00593 { 00594 /* Check the parameters */ 00595 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00596 00597 /* Disable the write protection for RTC registers */ 00598 RTC->WPR = 0xCA; 00599 RTC->WPR = 0x53; 00600 00601 if (NewState != DISABLE) 00602 { 00603 /* Set the BYPSHAD bit */ 00604 RTC->CR1 |= (uint8_t)RTC_CR1_BYPSHAD; 00605 } 00606 else 00607 { 00608 /* Reset the BYPSHAD bit */ 00609 RTC->CR1 &= (uint8_t)~RTC_CR1_BYPSHAD; 00610 } 00611 00612 /* Enable the write protection for RTC registers */ 00613 RTC->WPR = 0xFF; 00614 } 00615 00616 00617 00618 00619 /** 00620 * @} 00621 */ 00622 00623 /** @defgroup RTC_Group2 Time and Date configuration functions 00624 * @brief Time and Date configuration functions 00625 * 00626 @verbatim 00627 =============================================================================== 00628 Time and Date configuration functions 00629 =============================================================================== 00630 00631 This section provide functions allowing to program and read the RTC Calendar 00632 (Time and Date). 00633 00634 @endverbatim 00635 * @{ 00636 */ 00637 00638 00639 /** 00640 * @brief Sets the RTC current time. 00641 * @note After updating the Calendar, this routine clears the RSF flag and waits 00642 * until it is set again (using RTC_WaitForSynchro() function) , which means 00643 * that the Calendar registers have been correctly copied into the RTC_TRx 00644 * and RTC_DRx shadow registers. 00645 * @param RTC_Format: specifies the format of the entered parameters. 00646 * This parameter can be one of the @ref RTC_Format_TypeDef enumeration. 00647 * @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure that 00648 * contains the time configuration information for the RTC 00649 * @retval An ErrorStatus enumeration value: 00650 * - SUCCESS: RTC Time register is configured 00651 * - ERROR: RTC Time register is not configured 00652 */ 00653 ErrorStatus RTC_SetTime(RTC_Format_TypeDef RTC_Format, 00654 RTC_TimeTypeDef* RTC_TimeStruct) 00655 { 00656 ErrorStatus status = ERROR; 00657 uint8_t temp = 0; 00658 00659 /* Check the parameters */ 00660 assert_param(IS_RTC_FORMAT(RTC_Format)); 00661 00662 if (RTC_Format == RTC_Format_BIN) 00663 { 00664 /* Check Hour Format (24h or 12h)*/ 00665 if ((RTC->CR1 & RTC_CR1_FMT) != RESET) 00666 { 00667 assert_param(IS_RTC_HOUR12_MAX(RTC_TimeStruct->RTC_Hours)); 00668 assert_param(IS_RTC_HOUR12_MIN(RTC_TimeStruct->RTC_Hours)); 00669 } 00670 else 00671 { 00672 assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours)); 00673 } 00674 assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes)); 00675 assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds)); 00676 } 00677 else 00678 { 00679 /* Check Hour Format (24h or 12h)*/ 00680 if ((RTC->CR1 & RTC_CR1_FMT) != RESET) 00681 { 00682 assert_param(IS_RTC_HOUR12_MAX(Bcd2ToByte(RTC_TimeStruct->RTC_Hours))); 00683 assert_param(IS_RTC_HOUR12_MIN(Bcd2ToByte(RTC_TimeStruct->RTC_Hours))); 00684 } 00685 else 00686 { 00687 assert_param(IS_RTC_HOUR24(Bcd2ToByte(RTC_TimeStruct->RTC_Hours))); 00688 } 00689 assert_param(IS_RTC_MINUTES(Bcd2ToByte(RTC_TimeStruct->RTC_Minutes))); 00690 assert_param(IS_RTC_SECONDS(Bcd2ToByte(RTC_TimeStruct->RTC_Seconds))); 00691 } 00692 00693 00694 /* Disable the write protection for RTC registers */ 00695 RTC->WPR = 0xCA; 00696 RTC->WPR = 0x53; 00697 00698 /* Set Initialization mode */ 00699 if (RTC_EnterInitMode() == ERROR) 00700 { 00701 status = ERROR; 00702 /* Enable the write protection for RTC registers */ 00703 RTC->WPR = 0xFF; 00704 } 00705 else 00706 { 00707 /* Check Hour Format is 12h)*/ 00708 if ((RTC->CR1 & RTC_CR1_FMT) != RESET) 00709 { 00710 assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12)); 00711 temp = RTC_TimeStruct->RTC_H12; 00712 } 00713 else 00714 { 00715 temp = 0; 00716 } 00717 /* Check the input parameters format */ 00718 if (RTC_Format != RTC_Format_BIN) 00719 { 00720 RTC->TR1 = (uint8_t)(RTC_TimeStruct->RTC_Seconds); 00721 RTC->TR2 = (uint8_t)(RTC_TimeStruct->RTC_Minutes) ; 00722 RTC->TR3 = (uint8_t)( temp | RTC_TimeStruct->RTC_Hours) ; 00723 } 00724 else 00725 { 00726 RTC->TR1 = (uint8_t)(ByteToBcd2(RTC_TimeStruct->RTC_Seconds)); 00727 RTC->TR2 = (uint8_t)(ByteToBcd2(RTC_TimeStruct->RTC_Minutes)) ; 00728 RTC->TR3 = (uint8_t)( temp | ByteToBcd2(RTC_TimeStruct->RTC_Hours)); 00729 } 00730 /* Read DR3 register to unfreeze calender registers */ 00731 (void)(RTC->DR3); 00732 00733 /* Exit Initialization mode */ 00734 RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT; 00735 00736 /* Enable the write protection for RTC registers */ 00737 RTC->WPR = 0xFF; 00738 00739 /* if RTC_CR1_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00740 if ((RTC->CR1 & RTC_CR1_BYPSHAD) == RESET) 00741 { 00742 if (RTC_WaitForSynchro() == ERROR) 00743 { 00744 status = ERROR; 00745 } 00746 else 00747 { 00748 status = SUCCESS; 00749 } 00750 } 00751 else 00752 { 00753 status = SUCCESS; 00754 } 00755 } 00756 00757 return (ErrorStatus)status; 00758 } 00759 00760 00761 00762 /** 00763 * @brief Fills each RTC_TimeStruct member with its default value 00764 * (Time = 00h:00min:00sec). 00765 * @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure which will be 00766 * initialized. 00767 * @retval None 00768 */ 00769 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct) 00770 { 00771 /* Time = 00h:00min:00sec*/ 00772 RTC_TimeStruct->RTC_H12 = RTC_H12_AM; 00773 RTC_TimeStruct->RTC_Hours = 0; 00774 RTC_TimeStruct->RTC_Minutes = 0; 00775 RTC_TimeStruct->RTC_Seconds = 0; 00776 } 00777 00778 00779 /** 00780 * @brief Gets the RTC current Time. 00781 * @note To read the Calendar after wake up from low power mode, user software 00782 * must first check that the RSF flag is set in RTC_ISR1, using 00783 * RTC_WaitForSynchro() function, which means that the Calendar registers 00784 * have been correctly copied into the shadow registers (RTC_TRx and RTC_DRx). 00785 * @param RTC_Format: specifies the format of the returned parameters. 00786 * This parameter can be one of the @ref RTC_Format_TypeDef enumeration. 00787 * @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure that 00788 * will contain the returned current time configuration. 00789 * @retval None 00790 00791 */ 00792 void RTC_GetTime(RTC_Format_TypeDef RTC_Format, 00793 RTC_TimeTypeDef* RTC_TimeStruct) 00794 { 00795 uint8_t tmpreg = 0; 00796 00797 /* Check the parameters */ 00798 assert_param(IS_RTC_FORMAT(RTC_Format)); 00799 00800 /* Fill the structure fields with the read parameters */ 00801 /* Get RTC seconds */ 00802 RTC_TimeStruct->RTC_Seconds = RTC->TR1; 00803 00804 /* Get RTC Minutes */ 00805 RTC_TimeStruct->RTC_Minutes = RTC->TR2; 00806 00807 /* Get the RTC_TR3 register */ 00808 tmpreg = (uint8_t)RTC->TR3; 00809 00810 /* Read DR3 register to unfreeze calender registers */ 00811 (void) (RTC->DR3) ; 00812 00813 00814 /* Get RTC Hours */ 00815 RTC_TimeStruct->RTC_Hours = (uint8_t)(tmpreg & (uint8_t)~(RTC_TR3_PM)); 00816 00817 /* Get RTC H12 state */ 00818 RTC_TimeStruct->RTC_H12 = (RTC_H12_TypeDef)(tmpreg & RTC_TR3_PM); 00819 00820 /* Check the input parameters format */ 00821 if (RTC_Format == RTC_Format_BIN) 00822 { 00823 /* Convert the structure parameters to Binary format */ 00824 RTC_TimeStruct->RTC_Hours = (uint8_t)Bcd2ToByte(RTC_TimeStruct->RTC_Hours); 00825 RTC_TimeStruct->RTC_Minutes = (uint8_t)Bcd2ToByte(RTC_TimeStruct->RTC_Minutes); 00826 RTC_TimeStruct->RTC_Seconds = (uint8_t)Bcd2ToByte(RTC_TimeStruct->RTC_Seconds); 00827 } 00828 } 00829 00830 /** 00831 * @brief Gets the RTC current Calendar Subseconds value. 00832 * @note To read the Calendar after wake up from low power mode, user software 00833 * must first check that the RSF flag is set in RTC_ISR1, using 00834 * RTC_WaitForSynchro() function, which means that the Calendar registers 00835 * have been correctly copied into the shadow registers (RTC_TRx and RTC_DRx). 00836 * @param None 00837 * @retval RTC current Calendar Subseconds value. 00838 */ 00839 uint16_t RTC_GetSubSecond(void) 00840 { 00841 uint8_t ssrhreg = 0, ssrlreg = 0; 00842 uint16_t ssrreg = 0; 00843 00844 /* Get subseconds values from the correspondent registers*/ 00845 ssrhreg = RTC->SSRH; 00846 ssrlreg = RTC->SSRL; 00847 00848 /* Read DR3 register to unfreeze calender registers */ 00849 (void) (RTC->DR3); 00850 00851 00852 ssrreg = (uint16_t)((uint16_t)((uint16_t)ssrhreg << 8) | (uint16_t)(ssrlreg)); 00853 return (uint16_t)(ssrreg); 00854 } 00855 00856 /** 00857 * @brief Set the RTC current date. 00858 * @note After updating the Calendar, this routine clears the RSF flag and waits 00859 * until it is set again (using RTC_WaitForSynchro() function) , which means 00860 * that the Calendar registers have been correctly copied into the RTC_TRx 00861 * and RTC_DRx shadow registers. 00862 * @param RTC_Format: specifies the format of the entered parameters. 00863 * This parameter can be one of the @ref RTC_Format_TypeDef enumeration. 00864 * @param RTC_DateStruct: pointer to a @ref RTC_TimeTypeDef structure that 00865 * contains the date configuration information for the RTC. 00866 * @retval An ErrorStatus enumeration value: 00867 * - SUCCESS: RTC Date register is configured 00868 * - ERROR: RTC Date register is not configured 00869 */ 00870 ErrorStatus RTC_SetDate(RTC_Format_TypeDef RTC_Format, 00871 RTC_DateTypeDef* RTC_DateStruct) 00872 { 00873 ErrorStatus status = ERROR; 00874 00875 if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & TEN_VALUE_BCD) == TEN_VALUE_BCD)) 00876 { 00877 RTC_DateStruct->RTC_Month = (RTC_Month_TypeDef)((RTC_DateStruct->RTC_Month & (uint8_t)~(TEN_VALUE_BCD)) + TEN_VALUE_BIN); 00878 } 00879 00880 /* Check the parameters */ 00881 assert_param(IS_RTC_FORMAT(RTC_Format)); 00882 if (RTC_Format == RTC_Format_BIN) 00883 { 00884 assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year)); 00885 assert_param(IS_RTC_MONTH_MIN(RTC_DateStruct->RTC_Month)); 00886 assert_param(IS_RTC_MONTH_MAX(RTC_DateStruct->RTC_Month)); 00887 assert_param(IS_RTC_DATE_MIN(RTC_DateStruct->RTC_Date)); 00888 assert_param(IS_RTC_DATE_MAX(RTC_DateStruct->RTC_Date)); 00889 } 00890 else 00891 { 00892 assert_param(IS_RTC_YEAR(Bcd2ToByte(RTC_DateStruct->RTC_Year))); 00893 assert_param(IS_RTC_MONTH_MAX(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Month))); 00894 assert_param(IS_RTC_MONTH_MIN(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Month))); 00895 assert_param(IS_RTC_DATE_MIN(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Date))); 00896 assert_param(IS_RTC_DATE_MAX(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Date))); 00897 } 00898 assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay)); 00899 00900 /* Disable the write protection for RTC registers */ 00901 RTC->WPR = 0xCA; 00902 RTC->WPR = 0x53; 00903 00904 /* Set Initialization mode */ 00905 if (RTC_EnterInitMode() == ERROR) 00906 { 00907 status = ERROR; 00908 /* Enable the write protection for RTC registers */ 00909 RTC->WPR = 0xFF; 00910 } 00911 else 00912 { 00913 (void)(RTC->TR1); 00914 /* Set the RTC_DR registers */ 00915 /* Check the input parameters format */ 00916 if (RTC_Format != RTC_Format_BIN) 00917 { 00918 RTC->DR1 = (uint8_t)(RTC_DateStruct->RTC_Date); 00919 RTC->DR2 = (uint8_t)((RTC_DateStruct->RTC_Month) | (uint8_t)((RTC_DateStruct->RTC_WeekDay) << 5)); 00920 RTC->DR3 = (uint8_t)((RTC_DateStruct->RTC_Year)); 00921 } 00922 else 00923 { 00924 RTC->DR1 = (uint8_t)(ByteToBcd2 ((uint8_t)RTC_DateStruct->RTC_Date)); 00925 RTC->DR2 = (uint8_t)((ByteToBcd2((uint8_t)RTC_DateStruct->RTC_Month)) | (uint8_t)((RTC_DateStruct->RTC_WeekDay) << 5)); 00926 RTC->DR3 = (uint8_t)(ByteToBcd2((uint8_t)RTC_DateStruct->RTC_Year)); 00927 } 00928 00929 /* Exit Initialization mode */ 00930 RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT; 00931 00932 /* Enable the write protection for RTC registers */ 00933 RTC->WPR = 0xFF; 00934 00935 /* if RTC_CR1_BYPSHAD bit = 0, wait for synchro else this check is not needed */ 00936 if ((RTC->CR1 & RTC_CR1_BYPSHAD) == RESET) 00937 { 00938 if (RTC_WaitForSynchro() == ERROR) 00939 { 00940 status = ERROR; 00941 } 00942 else 00943 { 00944 status = SUCCESS; 00945 } 00946 } 00947 else 00948 { 00949 status = SUCCESS; 00950 } 00951 } 00952 00953 return (ErrorStatus)status; 00954 } 00955 /** 00956 * @brief Fills each RTC_DateStruct member with its default value 00957 * (Monday 01 January xx00). 00958 * @param RTC_DateStruct: pointer to a @ref RTC_DateTypeDef structure which will be 00959 * initialized. 00960 * @retval None 00961 */ 00962 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct) 00963 { 00964 /* * (Monday 01 January xx00)*/ 00965 RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday; 00966 RTC_DateStruct->RTC_Date = 1; 00967 RTC_DateStruct->RTC_Month = RTC_Month_January; 00968 RTC_DateStruct->RTC_Year = 0; 00969 } 00970 /** 00971 * @brief Get the RTC current date. 00972 * @note To read the Calendar after wake up from low power mode, user software 00973 * must first check that the RSF flag is set in RTC_ISR1, using 00974 * RTC_WaitForSynchro() function, which means that the Calendar registers 00975 * have been correctly copied into the shadow registers (RTC_TRx and RTC_DRx). 00976 * @param RTC_Format: specifies the format of the returned parameters. 00977 * This parameter can be one of the @ref RTC_Format_TypeDef enumeration. 00978 * @param RTC_DateStruct: pointer to a @ref RTC_DateTypeDef structure that 00979 * will contain the returned current Date configuration. 00980 * @retval None 00981 */ 00982 void RTC_GetDate(RTC_Format_TypeDef RTC_Format, 00983 RTC_DateTypeDef* RTC_DateStruct) 00984 { 00985 uint8_t tmpreg = 0; 00986 00987 /* Check the parameters */ 00988 assert_param(IS_RTC_FORMAT(RTC_Format)); 00989 00990 /* Fill the structure fields with the read parameters */ 00991 (void) (RTC->TR1) ; 00992 RTC_DateStruct->RTC_Date = (uint8_t)(RTC->DR1); 00993 tmpreg = (uint8_t)RTC->DR2; 00994 RTC_DateStruct->RTC_Year = (uint8_t)(RTC->DR3); 00995 00996 00997 RTC_DateStruct->RTC_Month = (RTC_Month_TypeDef)(tmpreg & (uint8_t)(RTC_DR2_MT | RTC_DR2_MU)); 00998 RTC_DateStruct->RTC_WeekDay = (RTC_Weekday_TypeDef)((uint8_t)((uint8_t)tmpreg & (uint8_t)(RTC_DR2_WDU)) >> (uint8_t)5); 00999 01000 /* Check the input parameters format */ 01001 if (RTC_Format == RTC_Format_BIN) 01002 { 01003 /* Convert the structure parameters to Binary format */ 01004 RTC_DateStruct->RTC_Year = (uint8_t)Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Year); 01005 RTC_DateStruct->RTC_Month = (RTC_Month_TypeDef)Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Month); 01006 RTC_DateStruct->RTC_Date = (uint8_t)(Bcd2ToByte((uint8_t)RTC_DateStruct->RTC_Date)); 01007 } 01008 } 01009 01010 /** 01011 * @} 01012 */ 01013 01014 /** @defgroup RTC_Group3 Alarms configuration functions 01015 * @brief Alarms (Alarm A and Alarm B) configuration functions 01016 * 01017 @verbatim 01018 =============================================================================== 01019 Alarms (Alarm A and Alarm B) configuration functions 01020 =============================================================================== 01021 01022 This section provide functions allowing to program and read the RTC Alarms. 01023 01024 @endverbatim 01025 * @{ 01026 */ 01027 01028 /** 01029 * @brief Sets the RTC Alarm configuration. 01030 * @note Before configuring the Alarm settings, the Alarm Unit must be disabled 01031 * (if enabled) using RTC_AlarmCmd(DISABLE) function. 01032 * @param RTC_Format: specifies the format of the entered parameters. 01033 * This parameter can be one of the @ref RTC_Format_TypeDef enumeration. 01034 * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure that 01035 * contains the Alarm configuration information for the RTC. 01036 * @retval None. 01037 */ 01038 void RTC_SetAlarm(RTC_Format_TypeDef RTC_Format, 01039 RTC_AlarmTypeDef* RTC_AlarmStruct) 01040 { 01041 uint8_t tmpreg1 = 0; 01042 uint8_t tmpreg2 = 0; 01043 uint8_t tmpreg3 = 0; 01044 uint8_t tmpreg4 = 0; 01045 01046 /* Check the parameters */ 01047 assert_param(IS_RTC_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask)); 01048 assert_param(IS_RTC_FORMAT(RTC_Format)); 01049 assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12)); 01050 assert_param(IS_RTC_ALARM_DATEWEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel)); 01051 01052 01053 if (RTC_Format == RTC_Format_BIN) 01054 { 01055 /* Check Hour Format (24h or 12h)*/ 01056 if ((RTC->CR1 & RTC_CR1_FMT) != RESET) 01057 { 01058 assert_param(IS_RTC_HOUR12_MAX(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)); 01059 assert_param(IS_RTC_HOUR12_MIN(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)); 01060 } 01061 else 01062 { 01063 assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)); 01064 } 01065 assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)); 01066 assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)); 01067 } 01068 else 01069 { 01070 /* Check Hour Format (24h or 12h)*/ 01071 if ((RTC->CR1 & RTC_CR1_FMT) != RESET) 01072 { 01073 assert_param(IS_RTC_HOUR12_MAX(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours))); 01074 assert_param(IS_RTC_HOUR12_MIN(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours))); 01075 } 01076 else 01077 { 01078 assert_param(IS_RTC_HOUR24(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours))); 01079 } 01080 01081 assert_param(IS_RTC_MINUTES(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes))); 01082 01083 assert_param(IS_RTC_SECONDS(Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds))); 01084 01085 } 01086 01087 /* if Date/Weekday field is not masked */ 01088 if ((RTC_AlarmStruct->RTC_AlarmMask & RTC_AlarmMask_DateWeekDay) == RESET ) 01089 { 01090 if (RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_WeekDay) 01091 { 01092 assert_param(IS_RTC_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay)); 01093 } 01094 else 01095 { 01096 assert_param(IS_RTC_DATE_MIN(RTC_AlarmStruct->RTC_AlarmDateWeekDay)); 01097 assert_param(IS_RTC_DATE_MAX(RTC_AlarmStruct->RTC_AlarmDateWeekDay)); 01098 } 01099 } 01100 01101 /* Disable the write protection for RTC registers */ 01102 RTC->WPR = 0xCA; 01103 RTC->WPR = 0x53; 01104 01105 01106 /* Check the input parameters format & Configure the Alarm register */ 01107 if (RTC_Format != RTC_Format_BIN) 01108 { 01109 tmpreg1 = (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \ 01110 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask) & (uint8_t)RTC_ALRMAR1_MSK1)); 01111 01112 tmpreg2 = (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) | \ 01113 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 1) & (uint8_t)RTC_ALRMAR2_MSK2)); 01114 01115 tmpreg3 = (uint8_t)((uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) | \ 01116 (uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12)) | \ 01117 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 2) & (uint8_t)RTC_ALRMAR3_MSK3)); 01118 01119 tmpreg4 = (uint8_t)((uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) | \ 01120 (uint8_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel)) | \ 01121 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 3) & (uint8_t)RTC_ALRMAR4_MSK4)); 01122 01123 } 01124 else 01125 { 01126 tmpreg1 = (uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \ 01127 (uint8_t)(RTC_AlarmStruct->RTC_AlarmMask & RTC_ALRMAR1_MSK1)); 01128 01129 tmpreg2 = (uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)) | \ 01130 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 1) & (uint8_t)RTC_ALRMAR2_MSK2)); 01131 01132 tmpreg3 = (uint8_t)((uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)) | \ 01133 (uint8_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12)) | \ 01134 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 2) & (uint8_t)RTC_ALRMAR3_MSK3)); 01135 01136 tmpreg4 = (uint8_t)((uint8_t)((ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay)) | \ 01137 (uint8_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel)) | \ 01138 (uint8_t)((uint8_t)(RTC_AlarmStruct->RTC_AlarmMask << 3) & (uint8_t)(RTC_ALRMAR4_MSK4))); 01139 } 01140 01141 /* Configure the Alarm register */ 01142 RTC->ALRMAR1 = tmpreg1; 01143 RTC->ALRMAR2 = tmpreg2; 01144 RTC->ALRMAR3 = tmpreg3; 01145 RTC->ALRMAR4 = tmpreg4; 01146 01147 /* Enable the write protection for RTC registers */ 01148 RTC->WPR = 0xFF; 01149 } 01150 01151 /** 01152 * @brief Fills each RTC_AlarmStruct member with its default value 01153 * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask = 01154 * all fields are masked). 01155 * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which 01156 * will be initialized. 01157 * @retval None 01158 */ 01159 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct) 01160 { 01161 /* Alarm Time Settings : Time = 00h:00mn:00sec */ 01162 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM; 01163 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0; 01164 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0; 01165 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0; 01166 01167 /* Alarm Date Settings : Date = 1st day of the month*/ 01168 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; 01169 RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1; 01170 01171 /* Alarm Masks Settings : Mask = all fields are masked*/ 01172 RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_All; 01173 } 01174 /** 01175 * @brief Gets the RTC Alarm configuration. 01176 * @param RTC_Format: specifies the format of the entered parameters. 01177 * This parameter can be one of the @ref RTC_Format_TypeDef enumeration. 01178 * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure that 01179 * will contain the Alarm configuration information of the RTC. 01180 * @retval None 01181 */ 01182 void RTC_GetAlarm(RTC_Format_TypeDef RTC_Format, 01183 RTC_AlarmTypeDef* RTC_AlarmStruct) 01184 { 01185 uint8_t tmpreg1 = 0; 01186 uint8_t tmpreg2 = 0; 01187 uint8_t tmpreg3 = 0; 01188 uint8_t tmpreg4 = 0; 01189 uint8_t alarmmask = 0; 01190 01191 /* Check the parameters */ 01192 assert_param(IS_RTC_FORMAT(RTC_Format)); 01193 01194 /* Get Alarm registers data */ 01195 tmpreg1 = (uint8_t)RTC->ALRMAR1; 01196 tmpreg2 = (uint8_t)RTC->ALRMAR2; 01197 tmpreg3 = (uint8_t)RTC->ALRMAR3; 01198 tmpreg4 = (uint8_t)RTC->ALRMAR4; 01199 01200 /* Fill the structure with the read parameters */ 01201 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint8_t)((uint8_t)tmpreg1 & (uint8_t)((uint8_t)RTC_ALRMAR1_ST | (uint8_t)RTC_ALRMAR1_SU)); 01202 alarmmask = (uint8_t)(tmpreg1 & RTC_ALRMAR1_MSK1); 01203 01204 /* Fill the structure with the read parameters */ 01205 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint8_t)((uint8_t)tmpreg2 & (uint8_t)((uint8_t)RTC_ALRMAR2_MNT | (uint8_t)RTC_ALRMAR2_MNU)); 01206 alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)(tmpreg2 & RTC_ALRMAR2_MSK2) >> 1)); 01207 01208 /* Fill the structure with the read parameters */ 01209 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint8_t)((uint8_t)tmpreg3 & (uint8_t)((uint8_t)RTC_ALRMAR3_HT | (uint8_t)RTC_ALRMAR3_HU)); 01210 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (RTC_H12_TypeDef)((uint8_t)tmpreg3 & (uint8_t)RTC_ALRMAR3_PM); 01211 alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)((uint8_t)tmpreg3 & (uint8_t)RTC_ALRMAR3_MSK3) >> 2)); 01212 01213 /* Fill the structure with the read parameters */ 01214 RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint8_t)((uint8_t)tmpreg4 & (uint8_t)((uint8_t)RTC_ALRMAR4_DT | (uint8_t)RTC_ALRMAR4_DU)); 01215 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (RTC_AlarmDateWeekDaySel_TypeDef)((uint8_t)tmpreg4 & (uint8_t)RTC_ALRMAR4_WDSEL); 01216 alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)((uint8_t)tmpreg4 & RTC_ALRMAR4_MSK4) >> 3)); 01217 01218 RTC_AlarmStruct->RTC_AlarmMask = alarmmask; 01219 01220 if (RTC_Format == RTC_Format_BIN) 01221 { 01222 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours); 01223 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes); 01224 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds); 01225 RTC_AlarmStruct->RTC_AlarmDateWeekDay = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay); 01226 } 01227 } 01228 01229 /** 01230 * @brief Enables or disables the RTC Alarm. 01231 * @param NewState: new state of the alarm. This parameter can be: ENABLE or DISABLE. 01232 * @retval An ErrorStatus enumeration value: 01233 * - SUCCESS: RTC Alarm is enabled/disabled 01234 * - ERROR: RTC Alarm is not enabled/disabled 01235 */ 01236 ErrorStatus RTC_AlarmCmd(FunctionalState NewState) 01237 { 01238 __IO uint16_t alrawfcount = 0; 01239 ErrorStatus status = ERROR; 01240 uint8_t temp1 = 0; 01241 01242 /* Check the parameters */ 01243 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01244 01245 /* Disable the write protection for RTC registers */ 01246 RTC->WPR = 0xCA; 01247 RTC->WPR = 0x53; 01248 01249 /* Configure the Alarm state */ 01250 if (NewState != DISABLE) 01251 { /*Enable the Alarm*/ 01252 RTC->CR2 |= (uint8_t)(RTC_CR2_ALRAE); 01253 status = SUCCESS; 01254 } 01255 else 01256 { /* Disable the Alarm */ 01257 RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE) ; 01258 01259 /* Wait until ALRxWF flag is set */ 01260 temp1 = (uint8_t)(RTC->ISR1 & RTC_ISR1_ALRAWF); 01261 while ((alrawfcount != ALRAWF_TIMEOUT) && (temp1 == RESET)) 01262 { 01263 alrawfcount++; 01264 } 01265 01266 if ((RTC->ISR1 & RTC_ISR1_ALRAWF) == RESET) 01267 { 01268 status = ERROR; 01269 } 01270 else 01271 { 01272 status = SUCCESS; 01273 } 01274 } 01275 01276 /* Enable the write protection for RTC registers */ 01277 RTC->WPR = 0xFF; 01278 01279 /* Return the status*/ 01280 return (ErrorStatus)status; 01281 } 01282 /** 01283 * @brief Configure the RTC Alarm Subseconds value and mask. 01284 * @param RTC_AlarmSubSecondValue: specifies the Subseconds value. 01285 * This parameter can be a value from 0 to 0x7FFF. 01286 * @param RTC_AlarmSubSecondMask: specifies the Subseconds Mask. 01287 * This parameter can be one of the @ref RTC_AlarmSubSecondMask_TypeDef enumeration. 01288 * @retval An ErrorStatus enumeration value: 01289 * - SUCCESS: Alarm Subseconds value and mask are configured 01290 * - ERROR: Alarm Subseconds value and mask are not configured 01291 */ 01292 ErrorStatus RTC_AlarmSubSecondConfig(uint16_t RTC_AlarmSubSecondValue, 01293 RTC_AlarmSubSecondMask_TypeDef RTC_AlarmSubSecondMask) 01294 { 01295 uint8_t alarmstatus = 0; 01296 ErrorStatus status = ERROR; 01297 01298 /* Check the parameters */ 01299 assert_param(IS_RTC_ALARM_SS_VALUE(RTC_AlarmSubSecondValue)); 01300 assert_param(IS_RTC_ALARM_SS_MASK(RTC_AlarmSubSecondMask)); 01301 01302 /* Disable the write protection for RTC registers */ 01303 RTC->WPR = 0xCA; 01304 RTC->WPR = 0x53; 01305 01306 /* Check if the initialization mode is not set */ 01307 if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET) 01308 { 01309 /* save Alarm status */ 01310 alarmstatus = (uint8_t)(RTC->CR2 | RTC_CR2_ALRAE); 01311 01312 /* Disable the Alarm */ 01313 RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE); 01314 01315 /* Configure the Alarm register */ 01316 RTC->ALRMASSRH = (uint8_t)(RTC_AlarmSubSecondValue >> 8); 01317 RTC->ALRMASSRL = (uint8_t)(RTC_AlarmSubSecondValue); 01318 RTC->ALRMASSMSKR = (uint8_t)RTC_AlarmSubSecondMask; 01319 01320 /* restore the saved Alarm status */ 01321 RTC->CR2 |= alarmstatus; 01322 01323 status = SUCCESS; 01324 } 01325 else 01326 { 01327 status = ERROR; 01328 } 01329 01330 /* Enable the write protection for RTC registers */ 01331 RTC->WPR = 0xFF; 01332 01333 return (ErrorStatus)status; 01334 } 01335 01336 01337 01338 /** 01339 * @} 01340 */ 01341 01342 /** @defgroup RTC_Group4 WakeUp Timer configuration functions 01343 * @brief WakeUp Timer configuration functions 01344 * 01345 @verbatim 01346 =============================================================================== 01347 WakeUp Timer configuration functions 01348 =============================================================================== 01349 01350 This section provide functions allowing to program and read the RTC WakeUp. 01351 01352 @endverbatim 01353 * @{ 01354 */ 01355 /** 01356 * @brief Configures the RTC Wakeup clock source. 01357 * @pre Before configuring the wakeup unit Clock source, the wake up Unit must 01358 * be disabled (if enabled) using RTC_WakeUpCmd(Disable) . 01359 * @param RTC_WakeupClockSrc: specifies the Wakeup clock source, 01360 * this parameter can be one of the @ref RTC_WakeupClockSrc_TypeDef enumeration. 01361 * @retval None 01362 */ 01363 void RTC_WakeUpClockConfig(RTC_WakeUpClock_TypeDef RTC_WakeUpClock) 01364 { 01365 01366 /* Check the parameters */ 01367 assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock)); 01368 01369 /* Disable the write protection for RTC registers */ 01370 RTC->WPR = 0xCA; 01371 RTC->WPR = 0x53; 01372 01373 /* Disable the Wakeup timer in RTC_CR2 register */ 01374 RTC->CR2 &= (uint8_t)~RTC_CR2_WUTE; 01375 01376 /* Clear the Wakeup Timer clock source bits in CR1 register */ 01377 RTC->CR1 &= (uint8_t)~RTC_CR1_WUCKSEL; 01378 01379 /* Configure the clock source */ 01380 RTC->CR1 |= (uint8_t)RTC_WakeUpClock; 01381 01382 /* Enable the write protection for RTC registers */ 01383 RTC->WPR = 0xFF; 01384 } 01385 01386 /** 01387 * @brief Sets the RTC Wakeup counter. 01388 * @note Before configuring the wakeup unit counter, the wake up Unit must be 01389 * disabled (if enabled) using RTC_WakeUpCmd(Disable). 01390 * @param RTC_WakeupCounter: specifies the Wake up counter, 01391 * This parameter can be a value from 0x0000 to 0xFFFF. 01392 * @retval None. 01393 */ 01394 void RTC_SetWakeUpCounter(uint16_t RTC_WakeupCounter) 01395 { 01396 /* Disable the write protection for RTC registers */ 01397 RTC->WPR = 0xCA; 01398 RTC->WPR = 0x53; 01399 01400 01401 /* Configure the Wakeup Timer counter */ 01402 RTC->WUTRH = (uint8_t)(RTC_WakeupCounter >> 8); 01403 RTC->WUTRL = (uint8_t)(RTC_WakeupCounter); 01404 01405 /* Enable the write protection for RTC registers */ 01406 RTC->WPR = 0xFF; 01407 } 01408 01409 /** 01410 * @brief Returns the RTC Wakeup timer counter value. 01411 * @param None. 01412 * @retval RTC Wakeup Counter value. 01413 */ 01414 uint16_t RTC_GetWakeUpCounter(void) 01415 { 01416 uint16_t tmpreg = 0; 01417 01418 /* Get the counter value */ 01419 tmpreg = ((uint16_t)RTC->WUTRH) << 8; 01420 tmpreg |= RTC->WUTRL; 01421 01422 /* return RTC Wakeup Counter value*/ 01423 return (uint16_t)tmpreg; 01424 } 01425 01426 /** 01427 * @brief Enables or Disables the RTC Wakeup Unit. 01428 * @param NewState: new state of the Wakeup Unit. This parameter can 01429 * be: ENABLE or DISABLE. 01430 * @retval An ErrorStatus enumeration value: 01431 * - SUCCESS : RTC Wakeup Unit is enabled/disabled 01432 * - ERROR : RTC Wakeup Unit is not enabled/disabled 01433 */ 01434 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState) 01435 { 01436 ErrorStatus status = ERROR; 01437 uint16_t wutwfcount = 0; 01438 01439 /* Check the parameters */ 01440 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01441 01442 /* Disable the write protection for RTC registers */ 01443 RTC->WPR = 0xCA; 01444 RTC->WPR = 0x53; 01445 01446 if (NewState != DISABLE) 01447 { 01448 /* Enable the Wakeup Timer */ 01449 RTC->CR2 |= (uint8_t)RTC_CR2_WUTE; 01450 01451 status = SUCCESS; 01452 } 01453 else 01454 { 01455 /* Disable the Wakeup Timer */ 01456 RTC->CR2 &= (uint8_t)~RTC_CR2_WUTE; 01457 01458 /* Wait until WUTWF flag is set */ 01459 while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != WUTWF_TIMEOUT)) 01460 { 01461 wutwfcount++; 01462 } 01463 01464 /* Check WUTWF flag is set or not */ 01465 if ((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) 01466 { 01467 status = ERROR; 01468 } 01469 else 01470 { 01471 status = SUCCESS; 01472 } 01473 } 01474 01475 /* Enable the write protection for RTC registers */ 01476 RTC->WPR = 0xFF; 01477 01478 /* Return the status*/ 01479 return (ErrorStatus)status; 01480 } 01481 01482 01483 01484 01485 01486 01487 /** 01488 * @} 01489 */ 01490 01491 /** @defgroup RTC_Group5 Daylight Saving configuration functions 01492 * @brief Daylight Saving configuration functions 01493 * 01494 @verbatim 01495 =============================================================================== 01496 Daylight Saving configuration functions 01497 =============================================================================== 01498 01499 This section provide functions allowing to configure the RTC DayLight Saving. 01500 01501 @endverbatim 01502 * @{ 01503 */ 01504 01505 /** 01506 * @brief Adds or subtracts one hour from the current time depending on 01507 * the daylight saving parameter. 01508 * @param RTC_DayLightSaving: the day light saving Mode 01509 * This parameter can be one of the @ref RTC_DayLightSaving_TypeDef 01510 * enumeration. 01511 * @param RTC_StoreOperation: the day light saving store operation 01512 * This parameter can be one of the @ref RTC_StoreOperation_TypeDef 01513 * enumeration. 01514 * @retval None 01515 */ 01516 void RTC_DayLightSavingConfig(RTC_DayLightSaving_TypeDef RTC_DayLightSaving, 01517 RTC_StoreOperation_TypeDef RTC_StoreOperation) 01518 { 01519 /* Check the parameters */ 01520 assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving)); 01521 assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation)); 01522 01523 /* Disable the write protection for RTC registers */ 01524 RTC->WPR = 0xCA; 01525 RTC->WPR = 0x53; 01526 01527 /* Clear the bits to be configured */ 01528 RTC->CR3 &= (uint8_t)~(RTC_CR3_BCK); 01529 01530 /* Configure the RTC_CR3 register */ 01531 RTC->CR3 |= (uint8_t)((uint8_t)RTC_DayLightSaving | (uint8_t)RTC_StoreOperation); 01532 01533 /* Enable the write protection for RTC registers */ 01534 RTC->WPR = 0xFF; 01535 } 01536 01537 /** 01538 * @brief Returns the stored operation. 01539 * @param None 01540 * @retval the store operation, this parameter can be one of 01541 * the @ref RTC_StoreOperation_TypeDef enumeration. 01542 */ 01543 RTC_StoreOperation_TypeDef RTC_GetStoreOperation(void) 01544 { 01545 /* Return the stored operation*/ 01546 return (RTC_StoreOperation_TypeDef)(RTC->CR3 & RTC_CR3_BCK); 01547 } 01548 01549 01550 01551 01552 01553 01554 /** 01555 * @} 01556 */ 01557 01558 /** @defgroup RTC_Group6 Output pin Configuration function 01559 * @brief Output pin Configuration function 01560 * 01561 @verbatim 01562 =============================================================================== 01563 Output pin Configuration function 01564 =============================================================================== 01565 01566 This section provide functions allowing to configure the RTC Output source. 01567 01568 @endverbatim 01569 * @{ 01570 */ 01571 /** 01572 * @brief Configures the RTC output for the output pin (RTC_ALARM output). 01573 * @param RTC_OutputSel: Specifies which signal will be mapped to the output. 01574 * This parameter can be one parameter from the 01575 * @ref RTC_OutputSel_TypeDef enumeration. 01576 * @param RTC_OutputPolarity: Specifies the polarity of the output signal. 01577 * This parameter can be one parameter from the 01578 * @ref RTC_OutputPolarity_TypeDef enumeration. 01579 * @retval None 01580 */ 01581 void RTC_OutputConfig(RTC_Output_TypeDef RTC_Output, 01582 RTC_OutputPolarity_TypeDef RTC_OutputPolarity) 01583 { 01584 /* Check the parameters */ 01585 assert_param(IS_RTC_OUTPUT_SEL(RTC_Output)); 01586 assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity)); 01587 01588 /* Disable the write protection for RTC registers */ 01589 RTC->WPR = 0xCA; 01590 RTC->WPR = 0x53; 01591 01592 /* Clear the bits to be configured */ 01593 RTC->CR3 &= (uint8_t)~(RTC_CR3_OSEL | RTC_CR3_POL); 01594 01595 /* Configure the output selection and polarity */ 01596 RTC->CR3 |= (uint8_t)((uint8_t)RTC_Output | (uint8_t)RTC_OutputPolarity); 01597 01598 /* Enable the write protection for RTC registers */ 01599 RTC->WPR = 0xFF; 01600 } 01601 01602 01603 01604 01605 /** 01606 * @} 01607 */ 01608 01609 /** @defgroup RTC_Group7 Output pin Configuration function 01610 * @brief Output pin Configuration function 01611 * 01612 @verbatim 01613 =============================================================================== 01614 Output pin Configuration function 01615 =============================================================================== 01616 01617 This section provide functions allowing to configure the RTC Output source. 01618 01619 @endverbatim 01620 * @{ 01621 */ 01622 /** 01623 * @brief Configures the Synchronization Shift Control Settings. 01624 * @param RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar. 01625 * This parameter can be one parameter from the @ref RTC_ShiftAdd1S_TypeDef 01626 * enumeration. 01627 * @param RTC_ShiftSubFS: Select the number of Second Fractions to Substitute. 01628 * This parameter can be one any value from 0 to 0x7FFF. 01629 * @retval An ErrorStatus enumeration value: 01630 * - SUCCESS: RTC Shift registers are configured 01631 * - ERROR: RTC Shift registers are not configured 01632 */ 01633 ErrorStatus RTC_SynchroShiftConfig(RTC_ShiftAdd1S_TypeDef RTC_ShiftAdd1S, 01634 uint16_t RTC_ShiftSubFS) 01635 { 01636 uint8_t shiftrhreg = 0; 01637 ErrorStatus status = ERROR; 01638 uint16_t shpfcount = 0; 01639 01640 /* Check the parameters */ 01641 assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S)); 01642 assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS)); 01643 01644 /* Disable the write protection for RTC registers */ 01645 RTC->WPR = 0xCA; 01646 RTC->WPR = 0x53; 01647 01648 /* Check if a Shift is pending*/ 01649 if ((RTC->ISR1 & RTC_ISR1_SHPF) != RESET) 01650 { 01651 /* wait until the shift is completed*/ 01652 while (((RTC->ISR1 & RTC_ISR1_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT)) 01653 { 01654 shpfcount++; 01655 } 01656 } 01657 01658 /* check if the Shift pending is completed or if there is no Shift operation at all*/ 01659 if ((RTC->ISR1 & RTC_ISR1_SHPF) == RESET) 01660 { 01661 /* Configure the Shift settings */ 01662 shiftrhreg = (uint8_t)((uint8_t)(RTC_ShiftSubFS >> 8) | (uint8_t)(RTC_ShiftAdd1S)); 01663 RTC->SHIFTRH = (uint8_t)(shiftrhreg); 01664 RTC->SHIFTRL = (uint8_t)(RTC_ShiftSubFS); 01665 01666 status = SUCCESS; 01667 } 01668 else 01669 { 01670 status = ERROR; 01671 } 01672 01673 /* Enable the write protection for RTC registers */ 01674 RTC->WPR = 0xFF; 01675 01676 return (ErrorStatus)(status); 01677 } 01678 01679 01680 01681 /** 01682 * @} 01683 */ 01684 01685 /** @defgroup RTC_Group8 Output pin Configuration function 01686 * @brief Output pin Configuration function 01687 * 01688 @verbatim 01689 =============================================================================== 01690 Output pin Configuration function 01691 =============================================================================== 01692 01693 This section provide functions allowing to configure the RTC Output source. 01694 01695 @endverbatim 01696 * @{ 01697 */ 01698 /** 01699 * @brief Configures the Smooth Calibration Settings. 01700 * @param RTC_SmoothCalibPeriod : Select the Smooth Calibration Period. 01701 * This parameter can be one parameter from 01702 * the @ref RTC_SmoothCalibPeriod_TypeDef enumeration. 01703 * @param RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit. 01704 * This parameter can be one parameter from the 01705 * @ref RTC_SmoothCalibPlusPulses_TypeDef enumeration. 01706 * @param RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits. 01707 * This parameter can be one any value from 0 to 0x01FF. 01708 * @retval An ErrorStatus enumeration value: 01709 * - SUCCESS: RTC Calib registers are configured 01710 * - ERROR: RTC Calib registers are not configured 01711 */ 01712 ErrorStatus RTC_SmoothCalibConfig(RTC_SmoothCalibPeriod_TypeDef RTC_SmoothCalibPeriod, 01713 RTC_SmoothCalibPlusPulses_TypeDef RTC_SmoothCalibPlusPulses, 01714 uint16_t RTC_SmouthCalibMinusPulsesValue) 01715 { 01716 ErrorStatus status = ERROR; 01717 uint16_t recalpfcount = 0; 01718 01719 /* Check the parameters */ 01720 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod)); 01721 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses)); 01722 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue)); 01723 01724 /* Disable the write protection for RTC registers */ 01725 RTC->WPR = 0xCA; 01726 RTC->WPR = 0x53; 01727 01728 /* check if a calibration is pending*/ 01729 if ((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) 01730 { 01731 /* wait until the Calibration is completed*/ 01732 while (((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT)) 01733 { 01734 recalpfcount++; 01735 } 01736 } 01737 01738 /* check if the calibration pending is completed or if there is 01739 no calibration operation at all*/ 01740 if ((RTC->ISR1 & RTC_ISR1_RECALPF) == RESET) 01741 { 01742 /* Configure the Smooth calibration settings */ 01743 RTC->CALRH = (uint8_t)((uint8_t)((uint8_t)RTC_SmoothCalibPeriod | \ 01744 (uint8_t)RTC_SmoothCalibPlusPulses) | \ 01745 (uint8_t)((uint16_t)RTC_SmouthCalibMinusPulsesValue >> 8)); 01746 RTC->CALRL = (uint8_t)(RTC_SmouthCalibMinusPulsesValue); 01747 01748 status = SUCCESS; 01749 } 01750 else 01751 { 01752 status = ERROR; 01753 } 01754 01755 /* Enable the write protection for RTC registers */ 01756 RTC->WPR = 0xFF; 01757 01758 return (ErrorStatus)(status); 01759 } 01760 01761 01762 /** 01763 * @} 01764 */ 01765 01766 /** @defgroup RTC_Group9 Digital Calibration configuration functions 01767 * @brief Digital Calibration configuration functions 01768 * 01769 @verbatim 01770 =============================================================================== 01771 Calibration configuration functions 01772 =============================================================================== 01773 01774 @endverbatim 01775 * @{ 01776 */ 01777 /** 01778 * @brief Configure the Calibration Pin-out (RTC_CALIB) Selection (1Hz or 512Hz). 01779 * @param RTC_CalibOutput : Select the Calibration output Selection . 01780 * This parameter can be one parameter from the 01781 * @ref RTC_CalibOutput_TypeDef enumeration. 01782 * @retval None 01783 */ 01784 void RTC_CalibOutputConfig(RTC_CalibOutput_TypeDef RTC_CalibOutput) 01785 { 01786 /* Check the parameters */ 01787 assert_param(IS_RTC_CALOUTPUT_SELECT(RTC_CalibOutput)); 01788 01789 /* Disable the write protection for RTC registers */ 01790 RTC->WPR = 0xCA; 01791 RTC->WPR = 0x53; 01792 01793 if (RTC_CalibOutput != RTC_CalibOutput_512Hz) 01794 { 01795 /* Enable the RTC clock output */ 01796 RTC->CR3 |= (uint8_t)RTC_CR3_COSEL; 01797 } 01798 else 01799 { 01800 /* Disable the RTC clock output */ 01801 RTC->CR3 &= (uint8_t)~RTC_CR3_COSEL; 01802 } 01803 01804 /* Enable the write protection for RTC registers */ 01805 RTC->WPR = 0xFF; 01806 } 01807 01808 /** 01809 * @brief Enables or disables the RTC clock to be output through the relative pin. 01810 * @param NewState: new state of the RTC Calibration output 01811 * This parameter can be: ENABLE or DISABLE. 01812 * @retval None 01813 */ 01814 void RTC_CalibOutputCmd(FunctionalState NewState) 01815 { 01816 /* Check the parameters */ 01817 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01818 01819 /* Disable the write protection for RTC registers */ 01820 RTC->WPR = 0xCA; 01821 RTC->WPR = 0x53; 01822 01823 if (NewState != DISABLE) 01824 { 01825 /* Enable the RTC clock output */ 01826 RTC->CR3 |= (uint8_t)RTC_CR3_COE; 01827 } 01828 else 01829 { 01830 /* Disable the RTC clock output */ 01831 RTC->CR3 &= (uint8_t)~RTC_CR3_COE; 01832 } 01833 01834 /* Enable the write protection for RTC registers */ 01835 RTC->WPR = 0xFF; 01836 } 01837 01838 01839 /** 01840 * @} 01841 */ 01842 01843 /** @defgroup RTC_Group10 Tampers configuration functions 01844 * @brief Tampers configuration functions 01845 * 01846 @verbatim 01847 =============================================================================== 01848 Tampers configuration functions 01849 =============================================================================== 01850 01851 @endverbatim 01852 * @{ 01853 */ 01854 01855 /** 01856 * @brief Configures the Tamper Sensitive Level. 01857 * @param RTC_Tamper: Select the tamper to configure. 01858 * This parameter can be one parameter from the @ref RTC_Tamper_TypeDef 01859 * enumeration. 01860 * @param RTC_TamperLevel: Select the tamper Sensitive Level. 01861 * This parameter can be one parameter from the 01862 * @ref RTC_TamperLevel_TypeDef enumeration. 01863 * @retval None 01864 */ 01865 void RTC_TamperLevelConfig(RTC_Tamper_TypeDef RTC_Tamper, 01866 RTC_TamperLevel_TypeDef RTC_TamperLevel) 01867 { 01868 /* Check the parameters */ 01869 assert_param(IS_RTC_TAMPER((uint8_t)RTC_Tamper)); 01870 assert_param(IS_RTC_TAMPER_LEVEL(RTC_TamperLevel)); 01871 01872 /* Disable the write protection for RTC registers */ 01873 RTC->WPR = 0xCA; 01874 RTC->WPR = 0x53; 01875 01876 if (RTC_TamperLevel != RTC_TamperLevel_Low) 01877 { 01878 /* Enable the selected Tampers */ 01879 RTC->TCR1 |= (uint8_t)(RTC_Tamper << 1); 01880 } 01881 else 01882 { 01883 /* Disable the selected Tampers */ 01884 RTC->TCR1 &= (uint8_t)~(uint8_t)(RTC_Tamper << 1); 01885 } 01886 01887 /* Enable the write protection for RTC registers */ 01888 RTC->WPR = 0xFF; 01889 } 01890 /** 01891 * @brief Configures the Tampers Filter. 01892 * @param RTC_TamperFilter: Specifies the tampers filter. 01893 * This parameter can be one parameter from the 01894 * @ref RTC_TamperFilter_TypeDef enumeration. 01895 * @retval None 01896 */ 01897 void RTC_TamperFilterConfig(RTC_TamperFilter_TypeDef RTC_TamperFilter) 01898 { 01899 01900 /* Check the parameters */ 01901 assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter)); 01902 01903 /* Disable the write protection for RTC registers */ 01904 RTC->WPR = 0xCA; 01905 RTC->WPR = 0x53; 01906 01907 /*clear flags before configuration */ 01908 RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPFLT); 01909 01910 /* Configure the RTC_TCR register */ 01911 RTC->TCR2 |= (uint8_t)RTC_TamperFilter; 01912 01913 /* Enable the write protection for RTC registers */ 01914 RTC->WPR = 0xFF; 01915 01916 } 01917 01918 /** 01919 * @brief Configures the Tampers Sampling Frequency. 01920 * @param RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency. 01921 * This parameter can be one parameter from the 01922 * @ref RTC_TamperSamplingFreq_TypeDef enumeration. 01923 * @retval None 01924 */ 01925 void RTC_TamperSamplingFreqConfig(RTC_TamperSamplingFreq_TypeDef RTC_TamperSamplingFreq) 01926 { 01927 /* Check the parameters */ 01928 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq)); 01929 01930 /* Disable the write protection for RTC registers */ 01931 RTC->WPR = 0xCA; 01932 RTC->WPR = 0x53; 01933 01934 /* Clear flags before configuration */ 01935 RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPFREQ); 01936 01937 /* Configure the RTC_TCR register */ 01938 RTC->TCR2 |= (uint8_t)RTC_TamperSamplingFreq; 01939 01940 /* Enable the write protection for RTC registers */ 01941 RTC->WPR = 0xFF; 01942 } 01943 01944 /** 01945 * @brief Configures the Tampers Pins input Precharge Duration. 01946 * @param RTC_TamperPrechargeDuration: Specifies the Tampers Pins input 01947 * Precharge Duration. 01948 * This parameter can be one parameter from the 01949 * @ref RTC_TamperPrechargeDuration_TypeDef enumeration. 01950 * @retval None 01951 */ 01952 void RTC_TamperPinsPrechargeDuration(RTC_TamperPrechargeDuration_TypeDef RTC_TamperPrechargeDuration) 01953 { 01954 /* Check the parameters */ 01955 assert_param(IS_RTC_TAMPER_PINS_PRECHAR_DURATION(RTC_TamperPrechargeDuration)); 01956 01957 /* Disable the write protection for RTC registers */ 01958 RTC->WPR = 0xCA; 01959 RTC->WPR = 0x53; 01960 01961 /* Clear the tampers pull-up and precharge/discharge duration Old settings*/ 01962 RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPPUDIS | RTC_TCR2_TAMPPRCH); 01963 01964 /* Configure the Tampers Precharge Duration and pull-up New settings*/ 01965 RTC->TCR2 |= (uint8_t)RTC_TamperPrechargeDuration; 01966 01967 /* Enable the write protection for RTC registers */ 01968 RTC->WPR = 0xFF; 01969 } 01970 01971 01972 /** 01973 * @brief Enables or Disables the Tamper detection. 01974 * @param RTC_Tamper: Select the tamper to configure. 01975 * This parameter can be one parameter from the @ref RTC_Tamper_TypeDef 01976 * enumeration. 01977 * @param NewState: new state of the tamper pin. 01978 * This parameter can be: ENABLE or DISABLE. 01979 * @retval None 01980 */ 01981 void RTC_TamperCmd(RTC_Tamper_TypeDef RTC_Tamper, 01982 FunctionalState NewState) 01983 { 01984 01985 /* Check the parameters */ 01986 assert_param(IS_RTC_TAMPER((uint8_t)RTC_Tamper)); 01987 assert_param(IS_FUNCTIONAL_STATE(NewState)); 01988 01989 /* Disable the write protection for RTC registers */ 01990 RTC->WPR = 0xCA; 01991 RTC->WPR = 0x53; 01992 01993 01994 if (NewState != DISABLE) 01995 { 01996 /* Enable the selected Tampers */ 01997 RTC->TCR1 |= (uint8_t)RTC_Tamper; 01998 } 01999 else 02000 { 02001 /* Disable the selected Tampers */ 02002 RTC->TCR1 &= (uint8_t)~RTC_Tamper; 02003 } 02004 02005 02006 /* Enable the write protection for RTC registers */ 02007 RTC->WPR = 0xFF; 02008 } 02009 02010 02011 02012 /** 02013 * @} 02014 */ 02015 02016 /** @defgroup RTC_Group11 Interrupts and flags management functions 02017 * @brief Interrupts and flags management functions 02018 * 02019 @verbatim 02020 =============================================================================== 02021 Interrupts and flags management functions 02022 =============================================================================== 02023 02024 To enable the RTC Alarm interrupt, the following sequence is required: 02025 - Configure the RTC to generate RTC alarm (Alarm A) using the RTC_SetAlarm() 02026 and RTC_AlarmCmd() functions. 02027 02028 To enable the RTC Wakeup interrupt, the following sequence is required: 02029 - Configure the RTC to generate the RTC wakeup timer event using the 02030 RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions. 02031 02032 To enable the RTC Tamper interrupt, the following sequence is required: 02033 - Configure the RTC to detect the RTC tamper event using the 02034 RTC_TamperTriggerConfig() and RTC_TamperCmd() functions. 02035 02036 02037 02038 @endverbatim 02039 * @{ 02040 */ 02041 /** 02042 * @brief Enables or Disables the specified RTC interrupts. 02043 * @param RTC_IT: specifies the RTC interrupt sources to be enabled or disabled. 02044 * This parameter can be any combination of the following values: 02045 * @arg RTC_IT_ALRA: Alarm A interrupt 02046 * @arg RTC_IT_TAMP: Tampers interrupt 02047 * @arg RTC_IT_WUT: WakeUp Timer interrupt 02048 * @param NewState: new state of the specified RTC interrupts. 02049 * This parameter can be: ENABLE or DISABLE. 02050 * @retval None 02051 */ 02052 void RTC_ITConfig(RTC_IT_TypeDef RTC_IT, FunctionalState NewState) 02053 { 02054 /* Check the parameters */ 02055 assert_param(IS_RTC_CONFIG_IT(RTC_IT)); 02056 assert_param(IS_FUNCTIONAL_STATE(NewState)); 02057 02058 /* Disable the write protection for RTC registers */ 02059 RTC->WPR = 0xCA; 02060 RTC->WPR = 0x53; 02061 02062 if (NewState != DISABLE) 02063 { 02064 /* Enable the Interrupts */ 02065 RTC->CR2 |= (uint8_t)((uint16_t)RTC_IT & (uint16_t)0x00F0); 02066 RTC->TCR1 |= (uint8_t)((uint16_t)RTC_IT & RTC_TCR1_TAMPIE); 02067 } 02068 else 02069 { 02070 /* Disable the Interrupts */ 02071 RTC->CR2 &= (uint8_t)~(uint8_t)((uint16_t)RTC_IT & (uint16_t)0x00F0); 02072 RTC->TCR1 &= (uint8_t)~(uint8_t)((uint16_t)RTC_IT & (uint16_t)RTC_TCR1_TAMPIE); 02073 } 02074 02075 /* Enable the write protection for RTC registers */ 02076 RTC->WPR = 0xFF; 02077 } 02078 /** 02079 * @brief Checks whether the specified RTC flag is set or not. 02080 * @param RTC_FLAG: specifies the flag to check. 02081 * This parameter can be one of the @ref RTC_Flag_TypeDef enumeration. 02082 * @retval Status of RTC_FLAG (SET or RESET) 02083 */ 02084 FlagStatus RTC_GetFlagStatus(RTC_Flag_TypeDef RTC_FLAG) 02085 { 02086 FlagStatus flagstatus = RESET; 02087 uint16_t tmpreg1 = 0; 02088 uint16_t tmpreg2 = 0; 02089 02090 /* Check the parameters */ 02091 assert_param(IS_RTC_GET_FLAG(RTC_FLAG)); 02092 02093 /* Get all the flags */ 02094 tmpreg2 = (uint16_t)((uint16_t)RTC->ISR1 << 8); 02095 tmpreg1 = (uint16_t)((uint16_t)((uint16_t)(RTC->ISR2)) | tmpreg2); 02096 02097 /* Return the status of the flag */ 02098 if ((tmpreg1 & (uint16_t)RTC_FLAG) != RESET) 02099 { 02100 flagstatus = SET; 02101 } 02102 else 02103 { 02104 flagstatus = RESET; 02105 } 02106 return (FlagStatus)flagstatus; 02107 } 02108 02109 /** 02110 * @brief Clears the RTC's pending flags. 02111 * @param RTC_FLAG: specifies the RTC flag to clear. 02112 * This parameter can be any combination of the @ref RTC_Flag_TypeDef 02113 * enumeration. 02114 * @retval None 02115 */ 02116 void RTC_ClearFlag(RTC_Flag_TypeDef RTC_FLAG) 02117 { 02118 /* Check the parameters */ 02119 assert_param(IS_RTC_CLEAR_FLAG((uint16_t)RTC_FLAG)); 02120 02121 /* Clear the Flags in the RTC_ISR registers */ 02122 RTC->ISR2 = (uint8_t)~((uint8_t)RTC_FLAG) ; 02123 RTC->ISR1 = (uint8_t)(((uint8_t)~(uint8_t)((uint16_t)RTC_FLAG >> (uint8_t)8)) & ((uint8_t)~(uint8_t)(RTC_ISR1_INIT))); 02124 } 02125 02126 /** 02127 * @brief Checks whether the specified RTC interrupt has occurred or not. 02128 * @param RTC_IT: specifies the RTC interrupt source to check. 02129 * This parameter can be one of the @ref RTC_IT_TypeDef enumeration. 02130 * @retval Status of RTC_IT (SET or RESET). 02131 */ 02132 ITStatus RTC_GetITStatus(RTC_IT_TypeDef RTC_IT) 02133 { 02134 ITStatus itstatus = RESET; 02135 uint8_t enablestatus = 0, tmpreg = 0; 02136 02137 /* Check the parameters */ 02138 assert_param(IS_RTC_GET_IT(RTC_IT)); 02139 02140 /* Get the Interrupt enable Status */ 02141 enablestatus = (uint8_t)(RTC->CR2 & (uint16_t)RTC_IT); 02142 02143 /* Get the Interrupt pending bit */ 02144 tmpreg = (uint8_t)(RTC->ISR2 & (uint8_t)((uint16_t)RTC_IT >> 4)); 02145 02146 /* Get the status of the Interrupt */ 02147 if ((enablestatus != (uint8_t)RESET) && (tmpreg != (uint8_t)RESET)) 02148 { 02149 itstatus = SET; 02150 } 02151 else 02152 { 02153 itstatus = RESET; 02154 } 02155 02156 return (ITStatus)itstatus; 02157 } 02158 02159 /** 02160 * @brief Clears the RTC's interrupt pending bits. 02161 * @param RTC_IT: specifies the RTC interrupt pending bit to clear. 02162 * This parameter can be any combination of the @ref RTC_IT_TypeDef 02163 * enumeration. 02164 * @retval None 02165 */ 02166 void RTC_ClearITPendingBit(RTC_IT_TypeDef RTC_IT) 02167 { 02168 /* Check the parameters */ 02169 assert_param(IS_RTC_CLEAR_IT((uint16_t)RTC_IT)); 02170 02171 /* Clear the interrupt pending bits in the RTC_ISR registers */ 02172 RTC->ISR2 = (uint8_t)~(uint8_t)((uint16_t)RTC_IT >> 4); 02173 } 02174 02175 /** 02176 * @} 02177 */ 02178 02179 02180 02181 02182 02183 02184 02185 02186 02187 /** 02188 * @addtogroup RTC_Private_Functions 02189 * @{ 02190 */ 02191 /** 02192 * @brief Converts a 2 digit decimal to BCD format 02193 * @param Value: Byte to be converted. 02194 * @retval Converted byte 02195 */ 02196 static uint8_t ByteToBcd2(uint8_t Value) 02197 { 02198 uint8_t bcdhigh = 0; 02199 02200 while (Value >= 10) 02201 { 02202 bcdhigh++; 02203 Value -= 10; 02204 } 02205 02206 return (uint8_t)((uint8_t)(bcdhigh << 4) | Value); 02207 } 02208 02209 /** 02210 * @brief Converts from 2 digit BCD to Binary format 02211 * @param Value: BCD value to be converted. 02212 * @retval Converted word 02213 */ 02214 static uint8_t Bcd2ToByte(uint8_t Value) 02215 { 02216 uint8_t tmp = 0; 02217 02218 tmp = (uint8_t)((uint8_t)((uint8_t)(Value & (uint8_t)0xF0) >> 4) * (uint8_t)10); 02219 02220 return (uint8_t)(tmp + (Value & (uint8_t)0x0F)); 02221 } 02222 02223 /** 02224 * @} 02225 */ 02226 02227 /** 02228 * @} 02229 */ 02230 02231 /** 02232 * @} 02233 */ 02234 02235 /** 02236 * @} 02237 */ 02238 02239 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/