📄 stm32f0xx_rtc.c
字号:
* is disabled (Use the RTC_AlarmCmd(DISABLE)).
* @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_Alarm: specifies the alarm to be configured.
* This parameter can be one of the following values:
* @arg RTC_Alarm_A: to select Alarm A
* @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that
* contains the alarm configuration parameters.
* @retval None
*/
void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
assert_param(IS_RTC_ALARM(RTC_Alarm));
assert_param(IS_RTC_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
if (RTC_Format == RTC_Format_BIN)
{
if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
{
assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
}
else
{
RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
}
assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
{
assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
}
else
{
assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
}
}
else
{
if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
{
tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
assert_param(IS_RTC_HOUR12(tmpreg));
assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
}
else
{
RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
}
assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
{
tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
}
else
{
tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
}
}
/* Check the input parameters format */
if (RTC_Format != RTC_Format_BIN)
{
tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
}
else
{
tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
}
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Configure the Alarm register */
RTC->ALRMAR = (uint32_t)tmpreg;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Fills each RTC_AlarmStruct member with its default value
* (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
* all fields are masked).
* @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
* will be initialized.
* @retval None
*/
void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
{
/* Alarm Time Settings : Time = 00h:00mn:00sec */
RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
/* Alarm Date Settings : Date = 1st day of the month */
RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
/* Alarm Masks Settings : Mask = all fields are not masked */
RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
}
/**
* @brief Get the RTC Alarm value and masks.
* @param RTC_Format: specifies the format of the output 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_Alarm: specifies the alarm to be read.
* This parameter can be one of the following values:
* @arg RTC_Alarm_A: to select Alarm A
* @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will
* contains the output alarm configuration values.
* @retval None
*/
void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
assert_param(IS_RTC_ALARM(RTC_Alarm));
/* Get the RTC_ALRMAR register */
tmpreg = (uint32_t)(RTC->ALRMAR);
/* Fill the structure with the read parameters */
RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
RTC_ALRMAR_HU)) >> 16);
RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
RTC_ALRMAR_MNU)) >> 8);
RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
RTC_ALRMAR_SU));
RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
if (RTC_Format == RTC_Format_BIN)
{
RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
RTC_AlarmTime.RTC_Hours);
RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
RTC_AlarmTime.RTC_Minutes);
RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
RTC_AlarmTime.RTC_Seconds);
RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
}
}
/**
* @brief Enables or disables the specified RTC Alarm.
* @param RTC_Alarm: specifies the alarm to be configured.
* This parameter can be any combination of the following values:
* @arg RTC_Alarm_A: to select Alarm A
* @param NewState: new state of the specified alarm.
* This parameter can be: ENABLE or DISABLE.
* @retval An ErrorStatus enumeration value:
* - SUCCESS: RTC Alarm is enabled/disabled
* - ERROR: RTC Alarm is not enabled/disabled
*/
ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
{
__IO uint32_t alarmcounter = 0x00;
uint32_t alarmstatus = 0x00;
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Configure the Alarm state */
if (NewState != DISABLE)
{
RTC->CR |= (uint32_t)RTC_Alarm;
status = SUCCESS;
}
else
{
/* Disable the Alarm in RTC_CR register */
RTC->CR &= (uint32_t)~RTC_Alarm;
/* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
do
{
alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
alarmcounter++;
} while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
{
status = ERROR;
}
else
{
status = SUCCESS;
}
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
/**
* @brief Configure the RTC AlarmA/B Subseconds value and mask.
* @note This function is performed only when the Alarm is disabled.
* @param RTC_Alarm: specifies the alarm to be configured.
* This parameter can be one of the following values:
* @arg RTC_Alarm_A: to select Alarm A
* @param RTC_AlarmSubSecondValue: specifies the Subseconds value.
* This parameter can be a value from 0 to 0x00007FFF.
* @param RTC_AlarmSubSecondMask: specifies the Subseconds Mask.
* This parameter can be any combination of the following values:
* @arg RTC_AlarmSubSecondMask_All: All Alarm SS fields are masked.
* There is no comparison on sub seconds for Alarm.
* @arg RTC_AlarmSubSecondMask_SS14_1: SS[14:1] are don't care in Alarm comparison.
* Only SS[0] is compared
* @arg RTC_AlarmSubSecondMask_SS14_2: SS[14:2] are don't care in Alarm comparison.
* Only SS[1:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_3: SS[14:3] are don't care in Alarm comparison.
* Only SS[2:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_4: SS[14:4] are don't care in Alarm comparison.
* Only SS[3:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_5: SS[14:5] are don't care in Alarm comparison.
* Only SS[4:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_6: SS[14:6] are don't care in Alarm comparison.
* Only SS[5:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_7: SS[14:7] are don't care in Alarm comparison.
* Only SS[6:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_8: SS[14:8] are don't care in Alarm comparison.
* Only SS[7:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_9: SS[14:9] are don't care in Alarm comparison.
* Only SS[8:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_10: SS[14:10] are don't care in Alarm comparison.
* Only SS[9:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_11: SS[14:11] are don't care in Alarm comparison.
* Only SS[10:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_12: SS[14:12] are don't care in Alarm comparison.
* Only SS[11:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14_13: SS[14:13] are don't care in Alarm comparison.
* Only SS[12:0] are compared
* @arg RTC_AlarmSubSecondMask_SS14: SS[14] is don't care in Alarm comparison.
* Only SS[13:0] are compared
* @arg RTC_AlarmSubSecondMask_None: SS[14:0] are compared and must match
* to activate alarm
* @retval None
*/
void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint8_t RTC_AlarmSubSecondMask)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_RTC_ALARM(RTC_Alarm));
assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(RTC_AlarmSubSecondMask));
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Configure the Alarm A or Alarm B SubSecond registers */
tmpreg = (uint32_t) (((uint32_t)(RTC_AlarmSubSecondValue)) | ((uint32_t)(RTC_AlarmSubSecondMask) << 24));
/* Configure the AlarmA SubSecond register */
RTC->ALRMASSR = tmpreg;
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
}
/**
* @brief Gets the RTC Alarm Subseconds value.
* @param RTC_Alarm: specifies the alarm to be read.
* This parameter can be one of the following values:
* @arg RTC_Alarm_A: to select Alarm A
* @param None
* @retval RTC Alarm Subseconds value.
*/
uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
{
uint32_t tmpreg = 0;
/* Get the RTC_ALRMAR register */
tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
return (tmpreg);
}
/**
* @}
*/
/** @defgroup RTC_Group4 Daylight Saving configuration functions
* @brief Daylight Saving configuration functions
*
@verbatim
===============================================================================
##### WakeUp Timer configuration functions #####
===============================================================================
[..] This section provide functions allowing to program and read the RTC WakeUp.
This section provide functions allowing to configure the RTC DayLight Saving.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -