5.18 RTC 使用说明
本章说明 N32G41x 标准外设库中 RTC 驱动(实时时钟)的文件组成、42 个公开函数与 5 个示例工程:RTC 在独立的 RTCCLK 上持续走时,提供时间与日期、闹钟 A/B、唤醒定时器、时间戳、夏时令切换、输出引脚与校准等功能,可在低功耗下按设定时刻产生中断或输出信号。RTC 时钟源与寄存器定义请配合用户手册的“实时时钟(RTC)”章节阅读,芯片已知问题以勘误手册为准。示例分析取自 projects/n32g41x_EVAL/examples/RTC/ 下的工程。
5.18.1 相关文件
| 相对路径 | 作用 |
|---|---|
firmware/n32g41x_std_periph_driver/inc/n32g41x_rtc.h |
定义 RTC_InitType、RTC_TimeType、RTC_DateType、RTC_AlarmType 等结构体与全部配置宏,并按复位与初始化、时间与日期等 10 个连续功能区段声明 42 个公开函数 |
firmware/n32g41x_std_periph_driver/src/n32g41x_rtc.c |
实现上述 42 个函数:以 RTC_WRP 的 0xCA、0x53 解锁序列读改写各寄存器,完成日历、闹钟、唤醒、时间戳、校准与中断控制 |
firmware/CMSIS/device/n32g41x.h |
定义 RTC 寄存器结构体指针、各字段掩码与位定义,以及 RTC 时钟源相关宏 |
projects/n32g41x_EVAL/examples/RTC/ |
RTC 示例分类根目录,含 5 个可构建工程:Alarm、AlarmSubsecond、Calendar、RtcAutoWakeUp、TimeStamp |
projects/n32g41x_EVAL/examples/RTC/Calendar/ |
一个典型示例的目录组成:src/main.c 与 src/n32g41x_it.c 为主流程与中断服务,inc/main.h、inc/n32g41x_it.h 为头文件,readme.txt 为说明,另有 MDK-ARM/、EWARM/ 两套工程;读写日期时间的公共辅助函数放在同级的 Common/ 目录 |
projects/n32g41x_EVAL/bsp/ |
板级支持代码:log 串口日志、delay 延时等,被 RTC 示例共同引用 |
5.18.2 API 参考
以下 42 个公开函数按 n32g41x_rtc.h 的声明顺序分为 10 组;每个条目给出功能摘要与 n32g41x_rtc.c 中该函数正上方的注释原文。
复位与初始化
RTC_DeInit、RTC_Init、RTC_StructInit、RTC_EnableWriteProtection、RTC_EnterInitMode、RTC_ExitInitMode、RTC_WaitForSynchro、RTC_EnableRefClock、RTC_EnableBypassShadow
时间与日期配置
RTC_TimeStructInit、RTC_ConfigTime、RTC_GetTime、RTC_GetSubSecond、RTC_DateStructInit、RTC_SetDate、RTC_GetDate
闹钟配置
RTC_SetAlarm、RTC_AlarmStructInit、RTC_GetAlarm、RTC_EnableAlarm、RTC_ConfigAlarmSubSecond、RTC_GetAlarmSubSecond
唤醒定时器
RTC_ConfigWakeUpClock、RTC_SetWakeUpCounter、RTC_GetWakeUpCounter、RTC_EnableWakeUp
夏时令
RTC_ConfigDayLightSaving、RTC_GetStoreOperation
输出引脚配置
RTC_ConfigOutput、RTC_ConfigOutputType
校准
RTC_EnableCalibOutput、RTC_ConfigCalibOutput、RTC_ConfigSmoothCalib
时间戳
RTC_EnableTimeStamp、RTC_GetTimeStamp、RTC_GetTimeStampSubSecond
同步位移
RTC_ConfigSynchroShift
中断与标志管理
RTC_ConfigInt、RTC_GetFlagStatus、RTC_ClrFlag、RTC_GetITStatus、RTC_ClrIntPendingBit
ErrorStatus RTC_DeInit(void)将 RTC 寄存器恢复为默认复位值。
name RTC_DeInit.
fun Deinitializes the RTC registers to their default reset values.
param none
return ErrorStatus
- SUCCESS: RTC registers are deinitialized
- ERROR: RTC registers are not deinitialized
使用提示: 实现先按 0xCA、0x53 解锁写保护,进入初始化模式后把时间、日期、预分频、闹钟 A/B、校准与中断状态寄存器逐一复位,等待寄存器同步后重新上锁。
ErrorStatus RTC_Init(const RTC_InitType *RTC_InitStruct)按 RTC_InitStruct 中的参数初始化 RTC 寄存器。
name RTC_Init.
fun Initializes the RTC registers according to the specified parameters
in RTC_InitStruct.
param RTC_InitStruct : pointer to a RTC_InitType structure that contains
the configuration information for the RTC peripheral.
- RTC_HourFormat
- RTC_24HOUR_FORMAT
- RTC_12HOUR_FORMAT
- RTC_AsynchPrediv the value must not be greater than 0x7F
(7 bit field RTC_PRE.DIVA[6:0])
- RTC_SynchPrediv the value must not be greater than 0x7FFF
(15 bit field RTC_PRE.DIVS[14:0])
return ErrorStatus
- SUCCESS: RTC registers are initialized
- ERROR: RTC registers are not initialized
使用提示: 实现把 RTC_SynchPrediv 写入 RTC_PRE.DIVS[14:0]、把 RTC_AsynchPrediv 写入 RTC_PRE.DIVA[6:0],再按 RTC_HourFormat 写入 RTC_CTRL.HFMT。
void RTC_StructInit(RTC_InitType *RTC_InitStruct)把 RTC_InitStruct 各成员填充为默认值。
name RTC_StructInit.
fun Fills each RTC_InitStruct member with its default value.
param RTC_InitStruct : pointer to a RTC_InitType structure which will be
initialized.
- RTC_HourFormat
- RTC_24HOUR_FORMAT
- RTC_12HOUR_FORMAT
- RTC_AsynchPrediv the value must not be greater than 0x7F
(7 bit field RTC_PRE.DIVA[6:0])
- RTC_SynchPrediv the value must not be greater than 0x7FFF
(15 bit field RTC_PRE.DIVS[14:0])
return none
void RTC_EnableWriteProtection(FunctionalState Cmd)使能或关闭 RTC 寄存器的写保护。
name RTC_EnableWriteProtection.
fun Enable or disable the RTC registers write protection.
param Cmd : new state of the RTC registers write protection
- ENABLE: write protection enabled (a wrong key is written to RTC_WRP)
- DISABLE: write protection disabled (0xCA then 0x53 unlocks the registers)
return none
使用提示:Cmd 为 DISABLE 时依次向 RTC_WRP 写入 0xCA、0x53 解锁,其余情况写入非密钥值重新上锁;其它改写 RTC 寄存器的函数内部都执行同样的解锁与上锁。
ErrorStatus RTC_EnterInitMode(void)进入 RTC 初始化模式。
name RTC_EnterInitMode.
fun Enters the RTC Initialization mode.
param none
return ErrorStatus
- SUCCESS: RTC is in Init mode
- ERROR: RTC is not in Init mode
使用提示: 实现先等待亚秒计数不小于 3 且不等于预分频值,并读取一次日期寄存器,再置位 RTC_INITSTS.INITM;返回 ERROR 表示未进入初始化模式。
void RTC_ExitInitMode(void)初始化序列完成后,日历在 4 个 RTCCLK 周期后重新开始计数。
name RTC_ExitInitMode.
fun When the initialization sequence is complete, the calendar restarts counting after 4 RTCCLK cycles.
param none
return none
ErrorStatus RTC_WaitForSynchro(void)等待 RTC 时间与日期寄存器(RTC_TSH、RTC_DATE)与 RTC APB 时钟同步。
name RTC_WaitForSynchro.
fun Waits until the RTC Time and Date registers (RTC_TSH and RTC_DATE) are synchronized with RTC APB clock.
param none
return ErrorStatus :
- SUCCESS: RTC registers are synchronised
- ERROR: RTC registers are not synchronised
ErrorStatus RTC_EnableRefClock(FunctionalState Cmd)使能或关闭 RTC 参考时钟检测。
name RTC_EnableRefClock.
fun Enables or disables the RTC reference clock detection.
param Cmd : new state of the RTC reference clock detection
- ENABLE: reference clock detection enabled
- DISABLE: reference clock detection disabled
return ErrorStatus
- SUCCESS: reference clock detection configured
- ERROR: the initialization mode could not be entered
void RTC_EnableBypassShadow(FunctionalState Cmd)使能旁路影子寄存器后,日历值直接取自日历计数器。
name RTC_EnableBypassShadow.
fun When the bypass shadow is enabled the calendar values are taken
directly from the calendar counters.
param Cmd : new state of the bypass shadow
- ENABLE: calendar values read directly from the counters
- DISABLE: calendar values read from the shadow registers
return none
使用提示: 使能时把 RTC_CTRL.BYPS 置 1;当 APB1 时钟低于 RTCCLK 的 7 倍时必须使能旁路,否则日历读值不保证正确。
void RTC_TimeStructInit(RTC_TimeType *RTC_TimeStruct)把 RTC_TimeStruct 各成员填充为默认值(时间为 00h:00min:00sec)。
name RTC_TimeStructInit.
fun Fills each RTC_TimeStruct member with its default value
(Time = 00h:00 min:00sec).
param RTC_TimeStruct : RTC_TimeStruct pointer to a RTC_TimeType structure that contains
the time configuration information for the RTC.
- Hours
- if RTC_AM_H12 is select the value in the 0-23 range
- if RTC_PM_H12 is select the value in the 1-12 range
- Minutes the value set in the 0-59 range
- Seconds the value set in the 0-59 range
- H12
- RTC_AM_H12
- RTC_PM_H12
return none
ErrorStatus RTC_ConfigTime(uint32_t RTC_Format, const RTC_TimeType *RTC_TimeStruct)设置 RTC 当前时间。
name RTC_ConfigTime.
fun Set the RTC current Time.
param RTC_Format :
- RTC_FORMAT_BIN
- RTC_FORMAT_BCD
param RTC_TimeStruct : RTC_TimeStruct pointer to a RTC_TimeType structure that contains
the time configuration information for the RTC. The pointer must reference
a valid RTC_TimeType instance, NULL is not accepted by this function.
- Hours
- if RTC_AM_H12 is select the value in the 0-23 range
- if RTC_PM_H12 is select the value in the 1-12 range
- Minutes the value set in the 0-59 range
- Seconds the value set in the 0-59 range
- H12
- RTC_AM_H12
- RTC_PM_H12
return ErrorStatus
void RTC_GetTime(uint32_t RTC_Format, RTC_TimeType *RTC_TimeStruct)获取 RTC 当前时间。
name RTC_GetTime.
fun Get the RTC current Time.
param RTC_Format : specifies the format of the returned parameters.
- RTC_FORMAT_BIN
- RTC_FORMAT_BCD
param RTC_TimeStruct : RTC_TimeStruct pointer to a RTC_TimeType structure.
return none
uint32_t RTC_GetSubSecond(void)获取 RTC 当前日历的亚秒值。
name RTC_GetSubSecond.
fun Gets the RTC current Calendar Subseconds value.
return RTC current Calendar Subseconds value.
void RTC_DateStructInit(RTC_DateType *RTC_DateStruct)把 RTC_DateStruct 各成员填充为默认值(星期一、1 月 01 日 xx00 年)。
name RTC_DateStructInit.
fun Fills each RTC_DateStruct member with its default value (Monday, January 01 xx00).
param RTC_DateStruct : RTC_DateStruct pointer to a RTC_DateType structure that contains
the date configuration information for the RTC.
- WeekDay
- RTC_WEEKDAY_MONDAY
- RTC_WEEKDAY_TUESDAY
- RTC_WEEKDAY_WEDNESDAY
- RTC_WEEKDAY_THURSDAY
- RTC_WEEKDAY_FRIDAY
- RTC_WEEKDAY_SATURDAY
- RTC_WEEKDAY_SUNDAY
- Month
- RTC_MONTH_JANUARY
- RTC_MONTH_FEBRURY
- RTC_MONTH_MARCH
- RTC_MONTH_APRIL
- RTC_MONTH_MAY
- RTC_MONTH_JUNE
- RTC_MONTH_JULY
- RTC_MONTH_AUGUST
- RTC_MONTH_SEPTEMBER
- RTC_MONTH_OCTOBER
- RTC_MONTH_NOVEMBER
- RTC_MONTH_DECEMBER
- Date the value in the 1-31 range
- Year the value in the 0-99 range
return none
ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateType *RTC_DateStruct)设置 RTC 日历当前日期。
name RTC_SetDate.
fun Set RTC Calendar current date.
param RTC_Format : specifies the format of the returned parameters.
- RTC_FORMAT_BIN
- RTC_FORMAT_BCD
param RTC_DateStruct : RTC_DateStruct pointer to a RTC_DateType structure that contains
the date configuration information for the RTC. The pointer must reference
a valid RTC_DateType instance, NULL is not accepted by this function.
- WeekDay
- RTC_WEEKDAY_MONDAY
- RTC_WEEKDAY_TUESDAY
- RTC_WEEKDAY_WEDNESDAY
- RTC_WEEKDAY_THURSDAY
- RTC_WEEKDAY_FRIDAY
- RTC_WEEKDAY_SATURDAY
- RTC_WEEKDAY_SUNDAY
- Month
- RTC_MONTH_JANUARY
- RTC_MONTH_FEBRURY
- RTC_MONTH_MARCH
- RTC_MONTH_APRIL
- RTC_MONTH_MAY
- RTC_MONTH_JUNE
- RTC_MONTH_JULY
- RTC_MONTH_AUGUST
- RTC_MONTH_SEPTEMBER
- RTC_MONTH_OCTOBER
- RTC_MONTH_NOVEMBER
- RTC_MONTH_DECEMBER
- Date the value in the 1-31 range
- Year the value in the 0-99 range
return ErrorStatus
- SUCCESS: RTC Date register is configured
- ERROR: RTCDate register is not configured
void RTC_GetDate(uint32_t RTC_Format, RTC_DateType *RTC_DateStruct)获取 RTC 当前日期。
name RTC_GetDate.
fun Get the RTC current date.
param RTC_Format : specifies the format of the returned parameters.
- RTC_FORMAT_BIN
- RTC_FORMAT_BCD
param RTC_DateStruct : RTC_DateStruct pointer to a RTC_DateType structure that will
contain the returned current date configuration.
return none
void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, const RTC_AlarmType *RTC_AlarmStruct)设置指定的 RTC 闹钟。
name RTC_SetAlarm.
fun Set the specified RTC Alarm.
param RTC_Format : specifies the format of the returned parameters.
- RTC_FORMAT_BIN
- RTC_FORMAT_BCD
param RTC_Alarm : RTC_Alarm specifies the alarm to be configured.
- RTC_A_ALARM
- RTC_B_ALARM
param RTC_AlarmStruct : RTC_AlarmStruct pointer to a RTC_AlarmType structure that
contains the alarm configuration parameters.
- AlarmTime RTC_TimeStruct pointer to a RTC_TimeType structure that contains
the time configuration information for the RTC.
- Hours
- if RTC_AM_H12 is select the value in the 0-23 range
- if RTC_PM_H12 is select the value in the 1-12 range
- Minutes the value set in the 0-59 range
- Seconds the value set in the 0-59 range
- H12
- RTC_AM_H12
- RTC_PM_H12
- AlarmMask
- RTC_ALARMMASK_NONE
- RTC_ALARMMASK_WEEKDAY
- RTC_ALARMMASK_HOURS
- RTC_ALARMMASK_MINUTES
- RTC_ALARMMASK_SECONDS
- RTC_ALARMMASK_ALL
- DateWeekMode
- RTC_ALARM_SEL_WEEKDAY_DATE
- RTC_ALARM_SEL_WEEKDAY_WEEKDAY
- DateWeekValue
- RTC_WEEKDAY_MONDAY
- RTC_WEEKDAY_TUESDAY
- RTC_WEEKDAY_WEDNESDAY
- RTC_WEEKDAY_THURSDAY
- RTC_WEEKDAY_FRIDAY
- RTC_WEEKDAY_SATURDAY
- RTC_WEEKDAY_SUNDAY
return none
void RTC_AlarmStructInit(RTC_AlarmType *RTC_AlarmStruct)把 RTC_AlarmStruct 各成员填充为默认值(时间为 00h:00mn:00sec,日期为当月第 1 天,掩码为不屏蔽任何字段)。
name RTC_AlarmStructInit.
fun Fills each RTC_AlarmStruct member with its default value
(Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
no fields are masked).
param RTC_AlarmStruct : RTC_AlarmStruct pointer to a RTC_AlarmType structure that
contains the alarm configuration parameters.
- AlarmTime RTC_TimeStruct pointer to a RTC_TimeType structure that contains
the time configuration information for the RTC.
- Hours
- if RTC_AM_H12 is select the value in the 0-23 range
- if RTC_PM_H12 is select the value in the 1-12 range
- Minutes the value set in the 0-59 range
- Seconds the value set in the 0-59 range
- H12
- RTC_AM_H12
- RTC_PM_H12
- AlarmMask
- RTC_ALARMMASK_NONE
- RTC_ALARMMASK_WEEKDAY
- RTC_ALARMMASK_HOURS
- RTC_ALARMMASK_MINUTES
- RTC_ALARMMASK_SECONDS
- RTC_ALARMMASK_ALL
- DateWeekMode
- RTC_ALARM_SEL_WEEKDAY_DATE
- RTC_ALARM_SEL_WEEKDAY_WEEKDAY
- DateWeekValue
- RTC_WEEKDAY_MONDAY
- RTC_WEEKDAY_TUESDAY
- RTC_WEEKDAY_WEDNESDAY
- RTC_WEEKDAY_THURSDAY
- RTC_WEEKDAY_FRIDAY
- RTC_WEEKDAY_SATURDAY
- RTC_WEEKDAY_SUNDAY
return none
void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmType *RTC_AlarmStruct)获取 RTC 闹钟的值与掩码。
name RTC_GetAlarm.
fun Get the RTC Alarm value and masks.
param RTC_Format : specifies the format of the output parameters.
- RTC_FORMAT_BIN
- RTC_FORMAT_BCD
param RTC_Alarm : specifies the alarm to be read.
- RTC_A_ALARM
- RTC_B_ALARM
param RTC_AlarmStruct : pointer to a RTC_AlarmType structure that will
contains the output alarm configuration values.
return none
ErrorStatus RTC_EnableAlarm(uint32_t RTC_Alarm, FunctionalState Cmd)使能或关闭指定的 RTC 闹钟。
name RTC_EnableAlarm.
fun Enables or disables the specified RTC Alarm.
param RTC_Alarm : specifies the alarm to be configured.
- RTC_A_ALARM
- RTC_B_ALARM
param Cmd :
- ENABLE
- DISABLE
return ErrorStatus :
- SUCCESS: RTC Alarm set succeed
- ERROR: RTC Alarm set failed
使用提示: 关闭闹钟时实现会等待写标志 ALxAWF 置位,超时仍未置位则返回 ERROR。
void RTC_ConfigAlarmSubSecond(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)配置 RTC 闹钟 A/B 的亚秒值与掩码。
name RTC_ConfigAlarmSubSecond.
fun Configure the RTC AlarmA/B Subseconds value and mask
param RTC_Alarm : specifies the alarm to be configured.
- RTC_A_ALARM : select Alarm A.
- RTC_B_ALARM : select Alarm B.
param RTC_AlarmSubSecondValue : specifies the Subseconds value, the value in the 0-0x00007FFF range
param RTC_AlarmSubSecondMask : specifies the Subseconds Mask.
- RTC_SUBS_MASK_ALL : Alarm SS fields are masked There is no comparison on sub seconds for Alarm.
- RTC_SUBS_MASK_SS14_1 : SS[14:1] are don't care in Alarm comparison Only SS[0] is compared
- RTC_SUBS_MASK_SS14_2 : SS[14:2] are don't care in Alarm comparison Only SS[1:0] are compared
- RTC_SUBS_MASK_SS14_3 : SS[14:3] are don't care in Alarm comparison Only SS[2:0] are compared
- RTC_SUBS_MASK_SS14_4 : SS[14:4] are don't care in Alarm comparison Only SS[3:0] are compared
- RTC_SUBS_MASK_SS14_5 : SS[14:5] are don't care in Alarm comparison Only SS[4:0] are compared
- RTC_SUBS_MASK_SS14_6 : SS[14:6] are don't care in Alarm comparison Only SS[5:0] are compared
- RTC_SUBS_MASK_SS14_7 : SS[14:7] are don't care in Alarm comparison Only SS[6:0] are compared
- RTC_SUBS_MASK_SS14_8 : SS[14:8] are don't care in Alarm comparison Only SS[7:0] are compared
- RTC_SUBS_MASK_SS14_9 : SS[14:9] are don't care in Alarm comparison Only SS[8:0] are compared
- RTC_SUBS_MASK_SS14_10 : SS[14:10] are don't care in Alarm comparison Only SS[9:0] are compared
- RTC_SUBS_MASK_SS14_11 : SS[14:11] are don't care in Alarm comparison Only SS[10:0] are compared
- RTC_SUBS_MASK_SS14_12 : SS[14:12] are don't care in Alarm comparison Only SS[11:0] are compared
- RTC_SUBS_MASK_SS14_13 : SS[14:13] are don't care in Alarm comparison Only SS[12:0] are compared
- RTC_SUBS_MASK_SS14_14 : SS[14] is don't care in Alarm comparison Only SS[13:0] are compared.
- RTC_SUBS_MASK_NONE : SS[14:0] are compared and must match to activate alarm.
return None
uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)获取 RTC 闹钟的亚秒值。
name RTC_GetAlarmSubSecond.
fun Gets the RTC Alarm Subseconds value.
param RTC_Alarm : specifies the alarm to be read.
- RTC_A_ALARM : select Alarm A.
- RTC_B_ALARM : select Alarm B.
return RTC Alarm Subseconds value.
void RTC_ConfigWakeUpClock(RTC_WAKE_UP_CLOCK WakeUpClock)配置 RTC 唤醒时钟源。
name RTC_ConfigWakeUpClock.
fun Configures the RTC Wakeup clock source.
param WakeUpClock : Wakeup Clock source.
- RTC_WKUPCLK_RTCCLK_DIV16 : RTC Wakeup Counter Clock = RTCCLK/16.
- RTC_WKUPCLK_RTCCLK_DIV8 : RTC Wakeup Counter Clock = RTCCLK/8.
- RTC_WKUPCLK_RTCCLK_DIV4 : RTC Wakeup Counter Clock = RTCCLK/4.
- RTC_WKUPCLK_RTCCLK_DIV2 : RTC Wakeup Counter Clock = RTCCLK/2.
- RTC_WKUPCLK_CK_SPRE_16BITS : RTC Wakeup Counter Clock = CK_SPRE.
- RTC_WKUPCLK_CK_SPRE_17BITS : RTC Wakeup Counter Clock = CK_SPRE,and wakeup timer count is 2^16.
return none
void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)配置 RTC 唤醒计数器。
name RTC_SetWakeUpCounter.
fun Configures the RTC Wakeup counter.
param RTC_WakeUpCounter : specifies the WakeUp counter, the value in the 0-0xFFFF range
return none
uint32_t RTC_GetWakeUpCounter(void)返回 RTC 唤醒定时器计数值。
name RTC_GetWakeUpCounter.
fun Returns the RTC WakeUp timer counter value.
param none
return The RTC WakeUp Counter value.
ErrorStatus RTC_EnableWakeUp(FunctionalState Cmd)使能或关闭 RTC 唤醒定时器。
name RTC_EnableWakeUp
fun Enables or disables the RTC WakeUp timer.
param Cmd :
- ENABLE
- DISABLE
return ErrorStatus :
- SUCCESS: RTC wakeup timer configured successfully
- ERROR: RTC wakeup timer WTWF wait timed out
使用提示: 关闭唤醒定时器时实现会等待写标志 WTWF 置位,超时仍未置位则返回 ERROR。
void RTC_ConfigDayLightSaving(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)在当前时间上增加或减少一小时。
name RTC_ConfigDayLightSaving
fun Adds or subtract one hour from the current time.
param RTC_DayLightSaving : the value of hour adjustment.
- RTC_DAYLIGHT_SAVING_SUB1H : Subtract one hour (winter time).
- RTC_DAYLIGHT_SAVING_ADD1H : Add one hour (summer time).
param RTC_StoreOperation : Specifies the value to be written in the BAKP bit
in CTRL register to store the operation.
- RTC_STORE_OPERATION_RESET : BAKP Bit Reset.
- RTC_STORE_OPERATION_SET : BAKP Bit Set.
return none
uint32_t RTC_GetStoreOperation(void)返回 RTC 夏时令的存储操作。
name RTC_GetStoreOperation
fun Returns the RTC Day Light Saving stored operation.
return RTC Day Light Saving stored operation.
- RTC_STORE_OPERATION_RESET
- RTC_STORE_OPERATION_SET
void RTC_ConfigOutput(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)配置 RTC 输出信号源。
name RTC_ConfigOutput
fun Configures the RTC output source.
param RTC_Output : RTC_Output Specifies which signal will be routed to the RTC output.
- RTC_OUTPUT_DIS : No output selected
- RTC_OUTPUT_ALA : signal of AlarmA mapped to output.
- RTC_OUTPUT_ALB : signal of AlarmB mapped to output.
- RTC_OUTPUT_WKUP: signal of WakeUp mapped to output.
param RTC_OutputPolarity : Specifies the polarity of the output signal.
- RTC_OUTPOL_HIGH: The output pin is high when the ALAF/ALBF/WTF flag is set (depending on OUTSEL[1:0]).
- RTC_OUTPOL_LOW : The output pin is low when the ALAF/ALBF/WTF flag is set (depending on OUTSEL[1:0]).
return none
void RTC_ConfigOutputType(uint32_t RTC_OutputType)配置 RTC 输出引脚模式。
name RTC_ConfigOutputType
fun Configures the RTC Output Pin mode.
param RTC_OutputType : specifies the RTC Output (PC13) pin mode.
- RTC_OUTPUT_OPENDRAIN : RTC Output (PC13) is configured in Open Drain mode.
- RTC_OUTPUT_PUSHPULL : RTC Output (PC13) is configured in Push Pull mode.
return none
void RTC_EnableCalibOutput(FunctionalState Cmd)使能或关闭 RTC 校准时钟经相应引脚输出。
name RTC_EnableCalibOutput
fun Enable or disable the RTC calibration clock to be output through the relative pin.
param Cmd :
- ENABLE
- DISABLE
return none
void RTC_ConfigCalibOutput(uint32_t RTC_CalibOutput)配置校准引脚(RTC_CALIB)输出 1Hz 或 256Hz。
name RTC_ConfigCalibOutput
fun Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 256Hz).
param RTC_CalibOutput : Select the Calibration output Selection.
- RTC_CALIB_OUTPUT_256HZ : A signal has a regular waveform at 256Hz.
- RTC_CALIB_OUTPUT_1HZ : A signal has a regular waveform at 1Hz.
return none
ErrorStatus RTC_ConfigSmoothCalib(uint32_t RTC_SmoothCalibPeriod, uint32_t RTC_SmoothCalibPlusPulses, uint32_t RTC_SmoothCalibMinusPulsesValue)配置平滑校准设置。
name RTC_ConfigSmoothCalib
fun Configures the Smooth Calibration Settings.
param RTC_SmoothCalibPeriod : RTC_SmoothCalibPeriod Select the Smooth Calibration Period.
- SMOOTH_CALIB_32SEC : The smooth calibration period is 32s.
- SMOOTH_CALIB_16SEC : The smooth calibration period is 16s.
- SMOOTH_CALIB_8SEC : The smooth calibration period is 8s.
param RTC_SmoothCalibPlusPulses : Set or reset the CP bit.
- RTC_SMOOTH_CALIB_PLUS_PULSES_SET : Add one RTCCLK pulse every 2**11 pulses.
- RTC_SMOOTH_CALIB_PLUS_PULSES_RESET : No RTCCLK pulses are added.
param RTC_SmoothCalibMinusPulsesValue : Set the value of CM[8:0] bits, the value in the 0-0x000001FF range
return ErrorStatus
- SUCCESS : RTC Calib registers are configured
- ERROR : RTC Calib registers are not configured
使用提示: 实现先等待 RECPF 清零,若等待后该标志仍未清零则返回 ERROR,此时不写入校准寄存器。
void RTC_EnableTimeStamp(uint32_t RTC_TimeStampEdge, FunctionalState Cmd)按指定的时间戳引脚触发沿使能或关闭 RTC 时间戳功能。
name RTC_EnableTimeStamp
fun Enables or disables the RTC TimeStamp functionality with the specified time stamp pin stimulating edge.
param RTC_TimeStampEdge : Specifies the pin edge on which the TimeStamp is activated.
- RTC_TIMESTAMP_EDGE_RISING : the Time stamp event occurs on the rising edge of the related pin.
- RTC_TIMESTAMP_EDGE_FALLING : the Time stamp event occurs on the falling edge of the related pin.
param Cmd :
- ENABLE
- DISABLE
return none
void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeType *RTC_StampTimeStruct, RTC_DateType *RTC_StampDateStruct)获取 RTC 时间戳的值与掩码。
name RTC_GetTimeStamp
fun Get the RTC TimeStamp value and masks.
param RTC_Format : specifies the format of the output parameters.
- RTC_FORMAT_BIN : Binary data format
- RTC_FORMAT_BCD : BCD data format
param RTC_StampTimeStruct : pointer to a RTC_TimeType structure that will
contains the TimeStamp time values.
param RTC_StampDateStruct : pointer to a RTC_DateType structure that will
contains the TimeStamp date values.
return none
uint32_t RTC_GetTimeStampSubSecond(void)获取 RTC 时间戳的亚秒值。
name RTC_GetTimeStampSubSecond
fun Get the RTC timestamp Subseconds value.
param none
return RTC current timestamp Subseconds value.
ErrorStatus RTC_ConfigSynchroShift(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)配置同步位移控制设置。
name RTC_ConfigSynchroShift
fun Configures the Synchronization Shift Control Settings.
param RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
- RTC_SHIFT_ADD1S_DISABLE : No effect.
- RTC_SHIFT_ADD1S_ENABLE : Add one second to the clock calendar.
param RTC_ShiftSubFS : Select the number of Second Fractions to Substitute.
This parameter can be any value from 0 to 0x7FFF.
return ErrorStatus :
- SUCCESS : RTC Shift registers are configured
- ERROR : RTC Shift registers are not configured
void RTC_ConfigInt(uint32_t RTC_INT, FunctionalState Cmd)使能或关闭指定的 RTC 中断。
name RTC_ConfigInt
fun Enables or disables the specified RTC interrupts.
param RTC_INT : specifies the RTC interrupt sources to be enabled .
- RTC_INT_TS : TimeStamp interrupt mask.
- RTC_INT_WUT : WakeUp Timer interrupt mask.
- RTC_INT_ALRB : Alarm B interrupt mask.
- RTC_INT_ALRA : Alarm A interrupt mask.
param Cmd :
- ENABLE
- DISABLE
return none
FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)检查指定的 RTC 标志是否置位。
name RTC_GetFlagStatus
fun Checks whether the specified RTC flag is set or not.
param RTC_FLAG : specifies the flag to check.
- RTC_FLAG_RECPF : RECPF flag.
- RTC_FLAG_TISOVF : Time Stamp OverFlow flag.
- RTC_FLAG_TISF : Time Stamp event flag.
- RTC_FLAG_WTF : WakeUp Timer flag.
- RTC_FLAG_ALBF : Alarm B flag.
- RTC_FLAG_ALAF : Alarm A flag.
- RTC_FLAG_INITF : Initialization mode flag.
- RTC_FLAG_RSYF : Registers Synchronized flag.
- RTC_FLAG_INITSF : Registers Configured flag.
- RTC_FLAG_SHOPF : Shift operation pending flag.
- RTC_FLAG_WTWF : WakeUp Timer Write flag.
- RTC_FLAG_ALBWF : Alarm B Write flag.
- RTC_FLAG_ALAWF : Alarm A write flag.
return FlagStatus
- SET : the flag is set
- RESET : the flag is reset
void RTC_ClrFlag(uint32_t RTC_FLAG)清除 RTC 的标志。
name RTC_ClrFlag
fun Clears the RTC's flags.
param RTC_FLAG : RTC_FLAG specifies the RTC flag to clear.
- RTC_FLAG_TISOVF : Time Stamp Overflow flag.
- RTC_FLAG_TISF : Time Stamp event flag.
- RTC_FLAG_WTF : WakeUp Timer flag.
- RTC_FLAG_ALBF : Alarm B flag.
- RTC_FLAG_ALAF : Alarm A flag.
- RTC_FLAG_RSYF : Registers Synchronized flag.
return none
INTStatus RTC_GetITStatus(uint32_t RTC_INT)检查指定的 RTC 中断是否发生。
name RTC_GetITStatus
fun Checks whether the specified RTC interrupt has occurred or not.
param RTC_INT : specifies the RTC interrupt source to check.
- RTC_INT_TS : Timestamp interrupt.
- RTC_INT_WUT : WakeUp Timer interrupt.
- RTC_INT_ALRB : Alarm B interrupt.
- RTC_INT_ALRA : Alarm A interrupt.
return INTStatus
- SET : the flag is set
- RESET : the flag is reset
void RTC_ClrIntPendingBit(uint32_t RTC_INT)清除 RTC 的中断挂起位。
name RTC_ClrIntPendingBit
fun Clears the RTC's interrupt pending bits.
param RTC_INT : specifies the RTC interrupt pending bit to clear.
- RTC_INT_TS : Timestamp interrupt.
- RTC_INT_WUT : WakeUp Timer interrupt
- RTC_INT_ALRB : Alarm B interrupt
- RTC_INT_ALRA : Alarm A interrupt
return none
5.18.3 Demo 示例说明
本节说明 RTC 的 5 个示例:Alarm 与 AlarmSubsecond 演示闹钟中断,Calendar 用一秒一次的校准输出打印日历,RtcAutoWakeUp 演示周期自动唤醒,TimeStamp 记录外部引脚触发时刻。所有示例都以 LSE 作为 RTCCLK,公共的时钟配置与日期时间显示函数放在同级的 Common/ 目录。
5.18.3.1 Alarm 闹钟中断
工程目录: projects/n32g41x_EVAL/examples/RTC/Alarm/
初始化日志串口→选择 LSE 作为 RTC 时钟并配置预分频→按默认值写入日期、时间与闹钟 A→配置 PC13 闹钟输出与 EXTI17 中断→闹钟到点进入中断打印当前时间
| 项目 | 源码解读 |
|---|---|
| 功能 | 用 LSE 驱动 RTC 并写入默认的日期、时间和闹钟 A; 闹钟到点时经 EXTI17 触发中断,在中断里打印当前时间,同时把闹钟 A 信号输出到 PC13。 |
| 源码组成 | src/main.c:EXTI17_RTCAlarm_Configuration 与 main;src/n32g41x_it.c、inc/n32g41x_it.h:RTC_ALARM_IRQHandler 中断服务;inc/main.h:函数声明;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程;日期时间的写入与显示辅助函数在同级 Common/ 目录。 |
| 关键配置 | RTC 时钟源取 RTC_CLK_SRC_TYPE_LSE;闹钟默认掩码由 RTC_ALARMMASK_WEEKDAY、RTC_ALARMMASK_HOURS、RTC_ALARMMASK_MINUTES 按位或而成,只在分钟与秒匹配时触发;输出类型为 RTC_OUTPUT_PUSHPULL,输出源为 RTC_OUTPUT_ALA、极性为 RTC_OUTPOL_HIGH;EXTI17 触发沿为上升沿,中断通道为 RTC_ALARM_IRQn。 |
| 程序与数据路径 | main 先调用 log_init 初始化串口,再用 RTC_DateAndTimeDefaultVale 准备默认值;RTC_CLKSourceConfig 选择 LSE 后,若 RTC_FLAG_INITSF 未置位则依次执行 RTC_DeInit、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate 与 RTC_AlarmRegulate;随后配置 PC13 输出,调用 RTC_ClrIntPendingBit 与 EXTI17_RTCAlarm_Configuration 后进入 while (1) 空循环。 |
| 主要 API | RTC_DateAndTimeDefaultVale、RTC_CLKSourceConfig、RTC_DeInit、RTC_GetFlagStatus、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate、RTC_AlarmRegulate、RTC_ConfigOutputType、RTC_ConfigOutput、RTC_ClrIntPendingBit、EXTI_InitPeripheral、NVIC_Init |
| 验证方法 | 复位运行后串口打印 RTC Init 与初始化结果;到达设定时刻后进入 RTC_ALARM_IRQHandler,串口打印当前日期与时间,PC13 电平随之翻转。 |
| 使用提示 | 串口为 USART1,TX 为 PA9、RX 为 PA10,波特率 115200、8N1; 系统时钟为 HSI 加 PLL,N32G412 为 80 MHz、N32G415 为 96 MHz; 开发环境为 KEIL MDK-ARM V5.34 或 IAR EWARM 8.50.1,芯片支持 N32G412x8L7、N32G412xBL7、N32G415x8L7、N32G415xBL7。 读取日期时间后需读一次 RTC->DATE 以解冻影子寄存器。 |
核心代码解读与移植
示例把默认日期、时间与闹钟一次性写入 RTC,再用闹钟中断验证走时是否正常。
数据路径: main → RTC_DateAndTimeDefaultVale 准备默认结构体 → RTC_CLKSourceConfig 选择 LSE → RTC_DeInit、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate、RTC_AlarmRegulate 写入日历与闹钟 → RTC_ConfigOutput 把闹钟 A 送到 PC13 → 闹钟到点 → RTC_ALARM_IRQHandler → RTC_TimeShow 打印
1. 先准备默认值再选择时钟源。 main 开头调用 RTC_DateAndTimeDefaultVale,用按位或的组合掩码指定闹钟只在分钟与秒匹配时触发,随后用 RTC_CLKSourceConfig 选择 LSE 作为 RTCCLK。
/* RTC date time alarm default value*/
RTC_DateAndTimeDefaultVale(RTC_ALARMMASK_WEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_MINUTES);
/* RTC clock source select */
掩码决定闹钟的触发条件,改动掩码前先确认要让哪几个字段参与比较。
2. 只在首次运行时写入日历与闹钟。 用 RTC_FLAG_INITSF 判断 RTC 是否已被配置过,未配置时按固定顺序完成复位、预分频、日期、时间与闹钟的写入。
if (RTC_GetFlagStatus(RTC_FLAG_INITSF) != SET)
{
RTC_DeInit();
RTC_PrescalerConfig();
/* RTC date time and alarm regulate*/
RTC_DateRegulate();
RTC_TimeRegulate();
RTC_AlarmRegulate(RTC_A_ALARM);
RTC_PrescalerConfig 里把 LSE 分频得到 1Hz 的计数节拍;RTC_AlarmRegulate 返回 SUCCESS 或 ERROR,可据此判断闹钟是否写入成功。
3. 配置闹钟输出并打开中断。 初始化完成后把 PC13 配成推挽输出并选通闹钟 A 信号,清除闹钟 A 的中断挂起位,再打开 EXTI17 中断。
/* Alarm output I/O config */
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
/* Alarm output I/O polarity */
RTC_ConfigOutput(RTC_OUTPUT_ALA, RTC_OUTPOL_HIGH);
/* Alarm out select option*/
RTC_ClrIntPendingBit(RTC_INT_ALRA);
EXTI17_RTCAlarm_Configuration(ENABLE);
对应源码: src/main.c、src/n32g41x_it.c、inc/main.h
5.18.3.2 AlarmSubsecond 亚秒闹钟
工程目录: projects/n32g41x_EVAL/examples/RTC/AlarmSubsecond/
初始化日志串口→选择 LSE 并写入默认日期时间→配置闹钟 A 的亚秒值与掩码→打开闹钟 A 中断并把输出引到 PC13→主循环检测到标志后打印 500ms 提示
| 项目 | 源码解读 |
|---|---|
| 功能 | 在闹钟 A 上进一步比较亚秒,使闹钟每 500 ms 触发一次; 中断置位一个布尔标志,主循环检测到标志后清零并通过串口打印 500ms Alarm Wakeup,同时把闹钟 A 信号输出到 PC13。 |
| 源码组成 | src/main.c:EXTI17_RTCAlarm_Configuration 与 main;src/n32g41x_it.c、inc/n32g41x_it.h:RTC_ALARM_IRQHandler 中断服务,负责置位全局标志;inc/main.h:函数声明;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程;公共辅助函数在同级 Common/ 目录。 |
| 关键配置 | RTC 时钟源取 RTC_CLK_SRC_TYPE_LSE;默认掩码取 RTC_ALARMMASK_ALL,比较全部字段;亚秒值取 128、亚秒掩码取 RTC_SUBS_MASK_SS14_7,对应 500 ms 的 1 / (32768 / 128) 节拍;输出类型为 RTC_OUTPUT_PUSHPULL,输出源为 RTC_OUTPUT_ALA、极性为 RTC_OUTPOL_HIGH。 |
| 程序与数据路径 | main 调用 log_init 后经 RTC_CLKSourceConfig 选择 LSE;首次配置时执行 RTC_DeInit、RTC_PrescalerConfig,再用 RTC_ConfigAlarmSubSecond 写入亚秒值与掩码,随后 RTC_DateRegulate、RTC_TimeRegulate、RTC_AlarmRegulate 写入日历;主循环轮询全局标志 RTC_Time_Out_Flag,置位时清零并打印。 |
| 主要 API | RTC_DateAndTimeDefaultVale、RTC_CLKSourceConfig、RTC_DeInit、RTC_PrescalerConfig、RTC_ConfigAlarmSubSecond、RTC_DateRegulate、RTC_TimeRegulate、RTC_AlarmRegulate、RTC_ConfigOutputType、RTC_ConfigOutput、RTC_ClrIntPendingBit |
| 验证方法 | 复位运行后串口周期性打印 500ms Alarm Wakeup,PC13 输出每 500 ms 翻转一次;调整亚秒值与掩码可改变打印节奏。 |
| 使用提示 | 亚秒掩码 RTC_SUBS_MASK_SS14_7 表示只比较 SS[6:0],比较精度受限;串口为 USART1,TX 为 PA9、RX 为 PA10,波特率 115200、8N1; 系统时钟为 HSI 加 PLL。 |
核心代码解读与移植
示例在分钟闹钟的基础上增加亚秒比较,把闹钟周期缩短到 500 ms,并用一个全局标志把中断事件交给主循环处理。
数据路径: main → RTC_ConfigAlarmSubSecond 写入亚秒值与掩码 → 闹钟 A 每 500 ms 触发 → RTC_ALARM_IRQHandler 置位 RTC_Time_Out_Flag → main 主循环清零标志并 log_info 打印
1. 比较全部字段的默认值。 与 Alarm 示例不同,这里默认掩码取 RTC_ALARMMASK_ALL,让时、分、秒等字段全部参与比较,再靠亚秒细分触发时刻。
/* RTC date time alarm default value*/
RTC_DateAndTimeDefaultVale(RTC_ALARMMASK_ALL);
2. 在初始化里加上亚秒配置。 复位与预分频之后、写入日历之前调用 RTC_ConfigAlarmSubSecond,把亚秒值 128 与掩码 RTC_SUBS_MASK_SS14_7 一次写进闹钟 A 的亚秒寄存器。
RTC_DeInit();
RTC_PrescalerConfig();
/* ALRMASS Value(128) = 500ms / (1 / (32768/128)) */
RTC_ConfigAlarmSubSecond(RTC_A_ALARM, 128, RTC_SUBS_MASK_SS14_7);
注释给出换算关系:RTCCLK 为 32768 Hz,按 128 分频后每个亚秒节拍为 500 ms 的 1 / (32768 / 128)。
3. 主循环消费中断标志。 中断服务只负责置位标志,实际打印放在主循环里,避免在中断中调用串口输出。
while (1)
{
if(RTC_Time_Out_Flag)
{
RTC_Time_Out_Flag=false;
log_info("\r\n 500ms Alarm Wakeup \r\n");
}
}
对应源码: src/main.c、src/n32g41x_it.c、inc/main.h
5.18.3.3 Calendar 日历打印
工程目录: projects/n32g41x_EVAL/examples/RTC/Calendar/
初始化日志串口→选择 LSE 并写入默认日期时间→配置 PA7 为输入并接 EXTI7 中断→使能 PC13 的 1Hz 校准输出→每秒中断一次打印日期与时间
| 项目 | 源码解读 |
|---|---|
| 功能 | 用 RTC 校准输出在 PC13 上产生 1Hz 方波,外部把 PC13 连到 PA7,PA7 的 EXTI7 中断每秒触发一次,在中断里打印当前日期与时间。 |
| 源码组成 | src/main.c:EXTI_PA7_Configuration 与 main;src/n32g41x_it.c、inc/n32g41x_it.h:EXTI5_9_IRQHandler 中断服务;inc/main.h:函数声明;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程;公共辅助函数在同级 Common/ 目录。 |
| 关键配置 | RTC 时钟源取 RTC_CLK_SRC_TYPE_LSE;PA7 初始化为 GPIO_MODE_INPUT 并映射到 EXTI7,触发沿为上升沿,中断通道为 EXTI5_9_IRQn,抢占与子优先级都取 0x01;校准输出取 RTC_CALIB_OUTPUT_1HZ,输出类型为 RTC_OUTPUT_PUSHPULL。 |
| 程序与数据路径 | main 调用 log_init 后经 RTC_CLKSourceConfig 选择 LSE,并在 RTC_FLAG_INITSF 未置位时执行 RTC_DeInit、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate;随后 EXTI_ClrITPendBit 与 EXTI_PA7_Configuration 配置 PA7 中断,再用 RTC_ConfigCalibOutput、RTC_ConfigOutputType、RTC_EnableCalibOutput 打开 PC13 的 1Hz 输出,最后进入 while (1)。 |
| 主要 API | RTC_DateAndTimeDefaultVale、RTC_CLKSourceConfig、RTC_DeInit、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate、RTC_ConfigCalibOutput、RTC_ConfigOutputType、RTC_EnableCalibOutput、GPIO_InitPeripheral、GPIO_ConfigEXTILine、EXTI_InitPeripheral |
| 验证方法 | 先用导线把 PC13 连到 PA7,再复位运行; 串口每秒打印一次日期与时间,断开连线后不再打印。 |
| 使用提示 | 中断服务在读取到 EXTI_LINE7 后打印日历并清除该挂起位; 本例依赖 PC13 与 PA7 的物理连线,换板时需自行连接。 串口为 USART1,TX 为 PA9、RX 为 PA10,波特率 115200、8N1。 |
核心代码解读与移植
示例用 RTC 的校准输出充当秒脉冲源,再借 PA7 的外部中断每秒打印一次日历,是一条不占用定时器的低频时基方案。
数据路径: main → EXTI_PA7_Configuration 配置 PA7 与 EXTI7 → RTC_ConfigCalibOutput、RTC_EnableCalibOutput 让 PC13 输出 1Hz → PC13 信号经外部连线送到 PA7 → EXTI5_9_IRQHandler → RTC_DateShow、RTC_TimeShow 打印
1. 清挂起位并配置 PA7 中断。 打开中断前先清除 EXTI7 的挂起位,再调用 EXTI_PA7_Configuration 完成引脚、EXTI 与 NVIC 的一次性配置。
/* Configure the PA7 pin to generate an EXTI interrupt
in which the calendar value is printed (externally feed
the 1HZ signal output on PC13 to PA7 to produce a 1s EXTI interrupt)*/
EXTI_ClrITPendBit(EXTI_LINE7);
EXTI_PA7_Configuration();
2. 选择 1Hz 校准输出并设为推挽。 校准输出有两档,这里选 1Hz;输出类型必须显式设为推挽或开漏,否则 PC13 上不会出现预期的方波。
/* Calibrate output 1Hz signal */
RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_1HZ);
/* Calibrate output config,push pull */
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
3. 使能校准输出。 末一步调用 RTC_EnableCalibOutput 才真正把校准时钟送到 PC13,随后串口打印一行配置完成提示。
/* Calibrate output enable*/
RTC_EnableCalibOutput(ENABLE);
log_info("\r\n RTC Config end....");
对应源码: src/main.c、src/n32g41x_it.c、inc/main.h
5.18.3.4 RtcAutoWakeUp 自动唤醒
工程目录: projects/n32g41x_EVAL/examples/RTC/RtcAutoWakeUp/
初始化日志串口→选择 LSE 并写入默认日期时间→配置唤醒时钟源与唤醒计数值→打开唤醒中断并把唤醒信号输出到 PC13→每次唤醒进入中断打印当前时间
| 项目 | 源码解读 |
|---|---|
| 功能 | 用 RTC 自动唤醒定时器按固定周期唤醒,唤醒事件经 EXTI19 触发中断打印当前时间,同时把唤醒信号输出到 PC13。 |
| 源码组成 | src/main.c:EXTI19_RTCWKUP_Configuration 与 main;src/n32g41x_it.c、inc/n32g41x_it.h:RTC_WKUP_IRQHandler 中断服务;inc/main.h:函数声明;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程;公共辅助函数在同级 Common/ 目录。 |
| 关键配置 | RTC 时钟源取 RTC_CLK_SRC_TYPE_LSE;唤醒时钟源取 RTC_WKUPCLK_CK_SPRE_16BITS,唤醒计数器取 4,对应每 5 秒唤醒一次;输出类型为 RTC_OUTPUT_PUSHPULL,输出源为 RTC_OUTPUT_WKUP、极性为 RTC_OUTPOL_HIGH;EXTI19 中断通道为 RTC_WKUP_IRQn。 |
| 程序与数据路径 | main 在完成 RTC 初始化后调用 RTC_ConfigWakeUpClock 与 RTC_SetWakeUpCounter 设定周期;接着 EXTI_ClrITPendBit、EXTI19_RTCWKUP_Configuration、RTC_ClrIntPendingBit、RTC_ConfigInt 打开唤醒中断,再用 RTC_ConfigOutputType、RTC_ConfigOutput 与 RTC_EnableWakeUp 使能唤醒并输出。 |
| 主要 API | RTC_DateAndTimeDefaultVale、RTC_CLKSourceConfig、RTC_DeInit、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate、RTC_ConfigWakeUpClock、RTC_SetWakeUpCounter、RTC_ClrIntPendingBit、RTC_ConfigInt、RTC_EnableWakeUp、RTC_ConfigOutput |
| 验证方法 | 复位运行后串口每 5 秒打印一次当前时间,PC13 输出随之翻转; 修改 RTC_SetWakeUpCounter 的取值可观察周期变化。 |
| 使用提示 | 唤醒时钟源取 RTC_WKUPCLK_CK_SPRE_16BITS 时计数节拍与 RTC 秒节拍一致;向唤醒计数器写入 0 会立即产生唤醒事件。 串口为 USART1,TX 为 PA9、RX 为 PA10,波特率 115200、8N1。 |
核心代码解读与移植
示例把 RTC 唤醒定时器配置成周期性时基,用 EXTI19 中断在每次唤醒时打印时间,适合做低功耗下的周期任务触发源。
数据路径: main → RTC_ConfigWakeUpClock、RTC_SetWakeUpCounter 设定唤醒周期 → RTC_ConfigInt 打开 WUT 中断 → 周期到点 → RTC_WKUP_IRQHandler 清 EXTI19 与挂起位 → RTC_TimeShow 打印;RTC_ConfigOutput 把唤醒信号送到 PC13
1. 选择唤醒时钟源与计数值。 唤醒时钟源取秒节拍,计数器取 4,因此计数到 4 时触发一次,配合秒节拍即为每 5 秒唤醒一次。
/* wake up clock select */
RTC_ConfigWakeUpClock(RTC_WKUPCLK_CK_SPRE_16BITS);
/* wake up timer value */
RTC_SetWakeUpCounter(4);
2. 清挂起位、使能中断再启动唤醒。 打开中断时先清 WUT 的挂起位,再使能中断,最后调用 RTC_EnableWakeUp 启动唤醒定时器。
EXTI_ClrITPendBit(EXTI_LINE19);
EXTI19_RTCWKUP_Configuration(ENABLE);
/* Enable the RTC Wakeup Interrupt */
RTC_ClrIntPendingBit(RTC_INT_WUT);
RTC_ConfigInt(RTC_INT_WUT, ENABLE);
RTC_EnableWakeUp(ENABLE);
3. 把唤醒信号输出到 PC13。 输出类型为推挽时 PC13 可直接驱动外部观察;若改为开漏,需要在 PC13 上加外部上拉。
/* Set the output type (open-drain output + push-pull output).
When setting OD, external should be added pull-up resistor to PC13*/
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
RTC_ConfigOutput(RTC_OUTPUT_WKUP, RTC_OUTPOL_HIGH);
RTC_EnableWakeUp(ENABLE);
对应源码: src/main.c、src/n32g41x_it.c、inc/main.h
5.18.3.5 TimeStamp 时间戳
工程目录: projects/n32g41x_EVAL/examples/RTC/TimeStamp/
初始化日志串口→选择 LSE 并写入默认日期时间→配置 PB8 为输入并接时间戳触发源→使能下降沿时间戳与时间戳中断→每次触发进入中断打印触发时刻
| 项目 | 源码解读 |
|---|---|
| 功能 | 把 RTC 时间戳触发源接到 PB8 并配置为下降沿触发,每次 PB8 上出现下降沿时 RTC 记录当前时刻并触发中断,在中断里打印触发瞬间的日期与时间。 |
| 源码组成 | src/main.c:RTC_TimeStampShow、EXTI_PB8_TimeStamp_Configuration、EXTI18_STAMP_IRQn_Configuration 与 main;src/n32g41x_it.c、inc/n32g41x_it.h:RTC_STAMP_LSECSS_IRQHandler 中断服务;inc/main.h:函数声明;readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程;公共辅助函数在同级 Common/ 目录。 |
| 关键配置 | RTC 时钟源取 RTC_CLK_SRC_TYPE_LSE;PB8 初始化为 GPIO_MODE_INPUT,经 EXTI_RTCTimeStampSel 与 GPIO_ConfigEXTILine 接到时间戳;时间戳触发沿取 RTC_TIMESTAMP_EDGE_FALLING;EXTI_LINE18 触发沿取上升沿,中断通道为 RTC_STAMP_LSECSS_IRQn;校准输出为 RTC_CALIB_OUTPUT_1HZ。 |
| 程序与数据路径 | main 完成 RTC 初始化后使能 PC13 的 1Hz 校准输出;随后 EXTI_PB8_TimeStamp_Configuration 配置 PB8,EXTI_ClrITPendBit 与 EXTI18_STAMP_IRQn_Configuration 打开中断,清除 RTC_FLAG_TISF、RTC_FLAG_TISOVF 后经 RTC_EnableTimeStamp、RTC_ConfigInt 使能时间戳。 |
| 主要 API | RTC_DateAndTimeDefaultVale、RTC_CLKSourceConfig、RTC_DeInit、RTC_PrescalerConfig、RTC_DateRegulate、RTC_TimeRegulate、RTC_ConfigCalibOutput、RTC_ConfigOutputType、RTC_EnableCalibOutput、RTC_GetTimeStamp、RTC_ClrFlag、RTC_EnableTimeStamp、RTC_ConfigInt |
| 验证方法 | 先用导线把 PC13 连到 PB8,再复位运行; 串口每秒打印一次时间戳记录到的日期与时间,断开连线后不再触发。 |
| 使用提示 | 中断服务按 RTC_INT_TS 判断事件,读取时间戳后清除 RTC_FLAG_TISF、RTC_FLAG_TISOVF;读取时间戳用 RTC_GetTimeStamp(RTC_FORMAT_BIN, …)。串口为 USART1,TX 为 PA9、RX 为 PA10,波特率 115200、8N1。 |
核心代码解读与移植
示例用 RTC 时间戳功能记录外部引脚上事件发生的时刻,PC13 的 1Hz 输出充当触发源,读取时使用二进制格式。
数据路径: main → EXTI_PB8_TimeStamp_Configuration 把 PB8 接到时间戳 → RTC_EnableTimeStamp、RTC_ConfigInt 使能下降沿时间戳 → PC13 信号经外部连线送到 PB8 → 下降沿触发 → RTC_STAMP_LSECSS_IRQHandler → RTC_GetTimeStamp → RTC_TimeStampShow 打印
1. 把 PB8 接到时间戳触发源。 先按输入模式初始化 PB8,再用 EXTI_RTCTimeStampSel 把 EXTI8 选为时间戳源,最后把 EXTI8 映射到 PB8。
/* Connect EXTI8 to RTC_TimeStamp */
EXTI_RTCTimeStampSel(EXTI_TSSEL_LINE8);
/* Connect EXTI8 Line to PB8 pin */
GPIO_ConfigEXTILine(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE8);
2. 使能下降沿时间戳与中断。 使能前先清除 RTC_FLAG_TISF 与 RTC_FLAG_TISOVF 两个标志,再依次打开时间戳与 RTC_INT_TS 中断。
EXTI_ClrITPendBit(EXTI_LINE18);
EXTI18_STAMP_IRQn_Configuration(ENABLE, 1);
/* clear RTC time stamp flag */
RTC_ClrFlag(RTC_FLAG_TISF);
RTC_ClrFlag(RTC_FLAG_TISOVF);
RTC_EnableTimeStamp(RTC_TIMESTAMP_EDGE_FALLING, ENABLE);
RTC_ConfigInt(RTC_INT_TS, ENABLE);
3. 用 PC13 的 1Hz 输出充当触发源。 使能 PC13 的 1Hz 校准输出,接一根线到 PB8 即可每秒触发一次时间戳。
RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_1HZ);
/* Calibrate output config,push pull */
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
/* Calibrate output enable*/
RTC_EnableCalibOutput(ENABLE);
对应源码: src/main.c、src/n32g41x_it.c、inc/main.h
5.18.4 注意与排障
| 场景 | 检查与建议 | 来源 |
|---|---|---|
| 修改 RTC 寄存器没有生效 | RTC 寄存器默认处于写保护状态,必须先依次向 RTC_WRP 写入 0xCA、0x53 解锁,写完后写入非密钥值重新上锁;驱动中所有会改写 RTC 寄存器的函数内部都会执行这段解锁与上锁序列。 |
用户手册 |
| RTC 时钟源的选择 | RTC 时钟源可选择 LSE、LSI 或 HSE/128,均通过 RCC 的 RTC 时钟选择位配置; 选择 HSE/128 时须保证 APB1 频率不低于 HSE。 |
用户手册 |
| 日历读值不准确 | 当 APB1 时钟低于 RTCCLK 的 7 倍且未使能旁路影子寄存器(BYPS 为 0)时,读日历值须按手册给出的顺序先读时间寄存器再读日期,以冻结影子寄存器; 否则读到的值可能不一致。 |
用户手册 |
| 无法进入初始化模式 | 置位 INITM 前,硬件要求子秒寄存器与预分频器满足手册规定的比较条件,并读取一次日期寄存器; 不满足条件时初始化模式不会生效。 |
用户手册 |
| 秒字段参与闹钟比较 | 当闹钟的秒字段被选中参与比较(对应 MASK 位被复位)时,预分频器 RTC_PRE.DIVS[14:0] 必须大于 3,否则闹钟行为不符合预期。 |
用户手册 |
| 唤醒计数器写入 0 | 向唤醒计数器写入 0 会立即产生一次唤醒事件; 修改唤醒计数器需要在唤醒中断中或先关闭唤醒定时器后再进行。 |
用户手册 |
| 时间戳标志的读写顺序 | 读取时间戳前应先读 RTC_FLAG_TISF 判断事件、读 RTC_FLAG_TISOVF 判断亚秒溢出,读取完成后再清除这两个标志。 |
用户手册 |
| 校准功能的限制 | 平滑校准的 CM[8:0] 只在 DIVA 不小于 3 时生效;校准输出可选 1Hz 或 256Hz,并需将输出映射到 PC13 且使能校准输出。 |
用户手册 |
N32G412、N32G415 系列勘误手册 V1.0.0(2026.6.30)的勘误列表只列出一行 A 版芯片条目,勘误链接列为空,未列出 RTC 相关条目。