📄 stm8l15x_rtc.c
字号:
status = SUCCESS;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
/* return Initialize the RTC registers status*/
return (ErrorStatus)(status);
}
/**
* @brief Fills each RTC_InitStruct member with its default value
* Hour format = 24h / Prescalers configured to their reset values.
* @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
* initialized.
* @retval None
*/
void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
{
/* Initialize the RTC_Hourformat member */
RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
/* Initialize the RTC_AsynchPrediv member */
RTC_InitStruct->RTC_AsynchPrediv = RTC_APRER_RESET_VALUE;
/* Initialize the RTC_SynchPrediv member */
RTC_InitStruct->RTC_SynchPrediv = RTC_SPRERL_RESET_VALUE;
}
/**
* @brief Enables or disables the RTC registers write protection.
* @param NewState: new state of the write protection.
* This parameter can be: ENABLE or DISABLE.
* @note Writing a wrong key reactivates the write protection.
* @note The protection mechanism is not affected by system reset.
* @retval None
*/
void RTC_WriteProtectionCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the write protection for RTC registers */
RTC->WPR = RTC_WPR_EnableKey;
}
else
{
/* Disable the write protection for RTC registers */
RTC->WPR = RTC_WPR_DisableKey1;
RTC->WPR = RTC_WPR_DisableKey2;
}
}
/**
* @brief Enters the RTC Initialization mode.
* @note The RTC Initialization mode is write protected, use the
* RTC_WriteProtectionCmd(DISABLE) before calling this function.
* @param None
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC is in Init mode
* - ERROR: RTC is not in Init mode
*/
ErrorStatus RTC_EnterInitMode(void)
{
ErrorStatus status = ERROR;
uint16_t initfcount = 0;
/* Check if the Initialization mode is set */
if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET)
{
/* Set the Initialization mode */
RTC->ISR1 = (uint8_t)RTC_ISR1_INIT;
/* Wait until INITF flag is set */
while (((RTC->ISR1 & RTC_ISR1_INITF) == RESET) && ( initfcount != INITF_TIMEOUT))
{
initfcount++;
}
}
if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET)
{
status = ERROR;
}
else
{
status = SUCCESS;
}
return (ErrorStatus)status;
}
/**
* @brief Exits the RTC Initialization mode.
* @note When the initialization sequence is complete, the Calendar restarts
* counting after 4 RTCCLK cycles.
* @note The RTC Initialization mode is write protected, use the
* RTC_WriteProtectionCmd(DISABLE) before calling this function.
* @param None
* @retval None
*/
void RTC_ExitInitMode(void)
{
/* Exit Initialization mode */
RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
}
/**
* @brief Waits until the RTC Calendar registers (Time and Date)
* are synchronized with RTC clock.
* @note This function is meaningless when BAYPASS feature is enabled in RTC_CR1
* register.
* @note To read the Calendar through the shadow registers after Calendar
* initialization, Calendar update or after wakeup from low power modes
* the software must first clear the RSF flag.
* The software must then wait until it is set again before reading
* the Calendar (if not yet done), which means that the Calendar registers
* have been correctly copied into the RTC_TRx and RTC_DRx shadow registers.
* @note RTC_SetTime() and RTC_SetDate() functions call RTC_WaitForSynchro() function
* after updating the Calendar. In Run mode, User can use RTC_GetDate(),
* RTC_GetTime() and/or RTC_GetSubSecond() without need to call
* RTC_WaitForSynchro() function. After waking up from low power mode, this
* function must be called before calling RTC_GetDate(), RTC_GetTime() or
* RTC_GetSubSecond() functions.
* @param None
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC registers are synchronized
* - ERROR: RTC registers are not synchronized
*/
ErrorStatus RTC_WaitForSynchro(void)
{
uint16_t rsfcount = 0;
ErrorStatus status = ERROR;
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Clear RSF flag by writing 0 in RSF bit */
RTC->ISR1 &= (uint8_t)~(RTC_ISR1_RSF | RTC_ISR1_INIT);
/* Wait the registers to be synchronised */
while (((RTC->ISR1 & RTC_ISR1_RSF) == RESET) && ( rsfcount != RSF_TIMEOUT))
{
rsfcount++;
}
/* Check if RSF flag occurs*/
if ((RTC->ISR1 & RTC_ISR1_RSF) != RESET)
{
status = SUCCESS;
}
else
{
status = ERROR;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return (ErrorStatus)status;
}
/**
* @brief Enables or Disables the RTC Ratio.
* @param NewState: new state of the Ratio feature.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RTC_RatioCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
if (NewState != DISABLE)
{
/* Set the RATIO bit */
RTC->CR1 |= (uint8_t)RTC_CR1_RATIO;
}
else
{
/* Reset the RATIO bit */
RTC->CR1 &= (uint8_t)~RTC_CR1_RATIO;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Enables or Disables the Bypass Shadow feature.
* @param NewState: new state of the Bypass Shadow feature.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RTC_BypassShadowCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
if (NewState != DISABLE)
{
/* Set the BYPSHAD bit */
RTC->CR1 |= (uint8_t)RTC_CR1_BYPSHAD;
}
else
{
/* Reset the BYPSHAD bit */
RTC->CR1 &= (uint8_t)~RTC_CR1_BYPSHAD;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @}
*/
/** @defgroup RTC_Group2 Time and Date configuration functions
* @brief Time and Date configuration functions
*
@verbatim
===============================================================================
Time and Date configuration functions
===============================================================================
This section provide functions allowing to program and read the RTC Calendar
(Time and Date).
@endverbatim
* @{
*/
/**
* @brief Sets the RTC current time.
* @note After updating the Calendar, this routine clears the RSF flag and waits
* until it is set again (using RTC_WaitForSynchro() function) , which means
* that the Calendar registers have been correctly copied into the RTC_TRx
* and RTC_DRx shadow registers.
* @param RTC_Format: specifies the format of the entered parameters.
* This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
* @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure that
* contains the time configuration information for the RTC
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC Time register is configured
* - ERROR: RTC Time register is not configured
*/
ErrorStatus RTC_SetTime(RTC_Format_TypeDef RTC_Format,
RTC_TimeTypeDef* RTC_TimeStruct)
{
ErrorStatus status = ERROR;
uint8_t temp = 0;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
if (RTC_Format == RTC_Format_BIN)
{
/* Check Hour Format (24h or 12h)*/
if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
{
assert_param(IS_RTC_HOUR12_MAX(RTC_TimeStruct->RTC_Hours));
assert_param(IS_RTC_HOUR12_MIN(RTC_TimeStruct->RTC_Hours));
}
else
{
assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
}
assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
}
else
{
/* Check Hour Format (24h or 12h)*/
if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
{
assert_param(IS_RTC_HOUR12_MAX(Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
assert_param(IS_RTC_HOUR12_MIN(Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
}
else
{
assert_param(IS_RTC_HOUR24(Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
}
assert_param(IS_RTC_MINUTES(Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
assert_param(IS_RTC_SECONDS(Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
}
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
else
{
/* Check Hour Format is 12h)*/
if ((RTC->CR1 & RTC_CR1_FMT) != RESET)
{
assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
temp = RTC_TimeStruct->RTC_H12;
}
else
{
temp = 0;
}
/* Check the input parameters format */
if (RTC_Format != RTC_Format_BIN)
{
RTC->TR1 = (uint8_t)(RTC_TimeStruct->RTC_Seconds);
RTC->TR2 = (uint8_t)(RTC_TimeStruct->RTC_Minutes) ;
RTC->TR3 = (uint8_t)( temp | RTC_TimeStruct->RTC_Hours) ;
}
else
{
RTC->TR1 = (uint8_t)(ByteToBcd2(RTC_TimeStruct->RTC_Seconds));
RTC->TR2 = (uint8_t)(ByteToBcd2(RTC_TimeStruct->RTC_Minutes)) ;
RTC->TR3 = (uint8_t)( temp | ByteToBcd2(RTC_TimeStruct->RTC_Hours));
}
/* Read DR3 register to unfreeze calender registers */
(void)(RTC->DR3);
/* Exit Initialization mode */
RTC->ISR1 &= (uint8_t)~RTC_ISR1_INIT;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
/* if RTC_CR1_BYPSHAD bit = 0, wait for synchro else this check is not needed */
if ((RTC->CR1 & RTC_CR1_BYPSHAD) == RESET)
{
if (RTC_WaitForSynchro() == ERROR)
{
status = ERROR;
}
else
{
status = SUCCESS;
}
}
else
{
status = SUCCESS;
}
}
return (ErrorStatus)status;
}
/**
* @brief Fills each RTC_TimeStruct member with its default value
* (Time = 00h:00min:00sec).
* @param RTC_TimeStruct: pointer to a @ref RTC_TimeTypeDef structure which will be
* initialized.
* @retval None
*/
void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -