📄 stm32f4xx_rtc.c
字号:
{
status = ERROR;
}
else
{
if (NewState != DISABLE)
{
/* Enable the RTC reference clock detection */
RTC->CR |= RTC_CR_REFCKON;
}
else
{
/* Disable the RTC reference clock detection */
RTC->CR &= ~RTC_CR_REFCKON;
}
/* Exit Initialization mode */
RTC_ExitInitMode();
status = SUCCESS;
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
/**
* @brief Enables or Disables the Bypass Shadow feature.
* @note When the Bypass Shadow is enabled the calendar value are taken
* directly from the Calendar counter.
* @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->CR |= (uint8_t)RTC_CR_BYPSHAD;
}
else
{
/* Reset the BYPSHAD bit */
RTC->CR &= (uint8_t)~RTC_CR_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 Set the RTC current time.
* @param RTC_Format: specifies the format of the entered parameters.
* This parameter can be one of the following values:
* @arg RTC_Format_BIN: Binary data format
* @arg RTC_Format_BCD: BCD data format
* @param RTC_TimeStruct: pointer to a 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(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
{
uint32_t tmpreg = 0;
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
if (RTC_Format == RTC_Format_BIN)
{
if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
{
assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
}
else
{
RTC_TimeStruct->RTC_H12 = 0x00;
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
{
if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
{
tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
assert_param(IS_RTC_HOUR12(tmpreg));
assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
}
else
{
RTC_TimeStruct->RTC_H12 = 0x00;
assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
}
assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
}
/* Check the input parameters format */
if (RTC_Format != RTC_Format_BIN)
{
tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
((uint32_t)(RTC_TimeStruct->RTC_H12) << 16));
}
else
{
tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
(((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
}
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
}
else
{
/* Set the RTC_TR register */
RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
/* Exit Initialization mode */
RTC_ExitInitMode();
if(RTC_WaitForSynchro() == ERROR)
{
status = ERROR;
}
else
{
status = SUCCESS;
}
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
/**
* @brief Fills each RTC_TimeStruct member with its default value
* (Time = 00h:00min:00sec).
* @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be
* initialized.
* @retval None
*/
void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
{
/* Time = 00h:00min:00sec */
RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
RTC_TimeStruct->RTC_Hours = 0;
RTC_TimeStruct->RTC_Minutes = 0;
RTC_TimeStruct->RTC_Seconds = 0;
}
/**
* @brief Get the RTC current Time.
* @param RTC_Format: specifies the format of the returned parameters.
* This parameter can be one of the following values:
* @arg RTC_Format_BIN: Binary data format
* @arg RTC_Format_BCD: BCD data format
* @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will
* contain the returned current time configuration.
* @retval None
*/
void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
/* Get the RTC_TR register */
tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
/* Fill the structure fields with the read parameters */
RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
/* Check the input parameters format */
if (RTC_Format == RTC_Format_BIN)
{
/* Convert the structure parameters to Binary format */
RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);
}
}
/**
* @brief Gets the RTC current Calendar Subseconds value.
* @note This function freeze the Time and Date registers after reading the
* SSR register.
* @param None
* @retval RTC current Calendar Subseconds value.
*/
uint32_t RTC_GetSubSecond(void)
{
uint32_t tmpreg = 0;
/* Get subseconds values from the correspondent registers*/
tmpreg = (uint32_t)(RTC->SSR);
/* Read DR register to unfroze calendar registers */
(void) (RTC->DR);
return (tmpreg);
}
/**
* @brief Set the RTC current date.
* @param RTC_Format: specifies the format of the entered parameters.
* This parameter can be one of the following values:
* @arg RTC_Format_BIN: Binary data format
* @arg RTC_Format_BCD: BCD data format
* @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
* the date configuration information for the RTC.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC Date register is configured
* - ERROR: RTC Date register is not configured
*/
ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
{
uint32_t tmpreg = 0;
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
{
RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
}
if (RTC_Format == RTC_Format_BIN)
{
assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
}
else
{
assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
assert_param(IS_RTC_MONTH(tmpreg));
tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
assert_param(IS_RTC_DATE(tmpreg));
}
assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
/* Check the input parameters format */
if (RTC_Format != RTC_Format_BIN)
{
tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
(((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
((uint32_t)RTC_DateStruct->RTC_Date) | \
(((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13));
}
else
{
tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
}
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
}
else
{
/* Set the RTC_DR register */
RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
/* Exit Initialization mode */
RTC_ExitInitMode();
if(RTC_WaitForSynchro() == ERROR)
{
status = ERROR;
}
else
{
status = SUCCESS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -