5.23 RTC Guide
This chapter describes the file layout of the RTC driver in the N32H49x standard peripheral library, its 56 public functions, and the 7 examples Alarm, AlarmSubsecond, Calendar, RtcAutoWakeUp, RtcBKP, Tamper, and TimeStamp. The RTC resides in the backup domain, so enable the PWR and BKP clocks and open backup-domain write access before configuration; read this chapter together with the “Real-time clock (RTC)” chapter of the user manual for register bit definitions, shadow registers, and backup-domain operations.
5.23.1 Related files
| Relative path | Purpose |
|---|---|
firmware/n32h49x_std_periph_driver/inc/n32h49x_rtc.h |
Defines RTC_InitType, RTC_TimeType, RTC_DateType, RTC_AlarmType and other structures, together with macros for clock source, hour format, alarm masks, wake-up clock, calibration, and tamper trigger;declares 56 public functions grouped into 14 sections, from reset and configuration to tamper detection and backup register |
firmware/n32h49x_std_periph_driver/src/n32h49x_rtc.c |
Implements the 56 functions above: init mode and prescaler configuration, time/date/subsecond access, alarm and wake-up timer configuration, output and calibration, timestamp and tamper detection, interrupt and flag management, and backup register access |
projects/n32h49x_EVAL/examples/RTC/ |
RTC example root, containing the 7 examples Alarm, AlarmSubsecond, Calendar, RtcAutoWakeUp, RtcBKP, Tamper, and TimeStamp |
projects/n32h49x_EVAL/examples/RTC/Alarm/ |
Directory composition of this example: MDK-ARM/, EWARM/, inc/, src/, readme.txt |
projects/n32h49x_EVAL/bsp/ |
Board support files shared by several examples: serial log printing and millisecond delay |
5.23.2 API reference
The following 56 public functions are listed in the declaration order of n32h49x_rtc.h in 14 groups; each entry gives a summary and the original comment above the function in n32h49x_rtc.c.
Reset
RTC_DeInit
Initialization and configuration
RTC_Init, RTC_StructInit, RTC_EnableWriteProtection, RTC_EnterInitMode, RTC_ExitInitMode, RTC_WaitForSynchro, RTC_EnableRefClock, RTC_EnableBypassShadow
Time and date
RTC_TimeStructInit, RTC_ConfigTime, RTC_GetTime, RTC_GetSubSecond, RTC_DateStructInit, RTC_SetDate, RTC_GetDate
Output type
RTC_ConfigOutputType
Alarm
RTC_SetAlarm, RTC_AlarmStructInit, RTC_GetAlarm, RTC_EnableAlarm, RTC_ConfigAlarmSubSecond, RTC_GetAlarmSubSecond
Wake-up timer
RTC_ConfigWakeUpClock, RTC_SetWakeUpCounter, RTC_GetWakeUpCounter, RTC_EnableWakeUp
Daylight saving
RTC_ConfigDayLightSaving, RTC_GetStoreOperation
Output pin
RTC_ConfigOutput, RTC_EnableOutput2, RTC_EnableTampOutput
Calibration
RTC_EnableCalibOutput, RTC_ConfigCalibOutput, RTC_ConfigSmoothCalib
Timestamp
RTC_EnableTimeStamp, RTC_GetTimeStamp, RTC_GetTimeStampSubSecond
Synchronization shift
RTC_ConfigSynchroShift
Interrupts and flags
RTC_ConfigInt, RTC_GetFlagStatus, RTC_ClrFlag, RTC_GetITStatus, RTC_ClrIntPendingBit
Tamper detection
RTC_TamperTriggerConfig, RTC_TamperCmd, RTC_TamperFilterConfig, RTC_TamperSamplingFreqConfig, RTC_TamperPinsPrechargeDuration, RTC_TimeStampOnTamperDetectionCmd, RTC_TamperPullUpCmd, RTC_TamperIECmd, RTC_TamperTAMPTSCmd, RTC_EnableTampErase
Backup register
RTC_BKUPRgWrite, RTC_BKUPRgRead
ErrorStatus RTC_DeInit(void)Deinitializes the RTC registers to their default reset values.
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
ErrorStatus RTC_Init(RTC_InitType* RTC_InitStruct)RTC_InitStruct pointer to a RTC_InitType structure that contains the configuration information for the RTC peripheral
name RTC_Init.
fun RTC_InitStruct pointer to a RTC_InitType structure that contains
the configuration information for the RTC peripheral
param RTC_InitStruct
- RTC_HourFormat
- RTC_24HOUR_FORMAT
- RTC_12HOUR_FORMAT
- RTC_AsynchPrediv the value must lower than 0x7F
- RTC_SynchPrediv the value must lower than 0x7FFF
return ErrorStatus
- SUCCESS: RTC registers are deinitialized
- ERROR: RTC registers are not deinitialized
void RTC_StructInit(RTC_InitType* RTC_InitStruct)Fills each RTC_InitStruct member with its default value.
name RTC_StructInit.
fun Fills each RTC_InitStruct member with its default value.
param RTC_InitStruct : RTC_InitStruct pointer to a RTC_InitType structure which will beinitialized.
- RTC_HourFormat
- RTC_24HOUR_FORMAT
- RTC_12HOUR_FORMAT
- RTC_AsynchPrediv the value must lower than 0x7F
- RTC_SynchPrediv the value must lower than 0x7FFF
return none
void RTC_EnableWriteProtection(FunctionalState Cmd)Enable or disable the RTC registers write protection.
name RTC_EnableWriteProtection.
fun Enable or disable the RTC registers write protection.
param Cmd :
- ENABLE
- DISABLE
return none
ErrorStatus RTC_EnterInitMode(void)Enters the RTC Initialization mode.
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
void RTC_ExitInitMode(void)When the initialization sequence is complete, the calendar restarts counting after 4 RTCCLK cycles.
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)Waits until the RTC Time and Date registers (RTC_TSH and RTC_DATE) are synchronized with RTC APB clock.
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)Enables or disables the RTC reference clock detection.
name RTC_EnableRefClock.
fun Enables or disables the RTC reference clock detection.
param Cmd :
- ENABLE
- DISABLE
return none
void RTC_EnableBypassShadow(FunctionalState Cmd)When the Bypass Shadow is enabled the calendar value are taken directly from the Calendar counter
name RTC_EnableBypassShadow.
fun When the Bypass Shadow is enabled the calendar value are taken directly from the Calendar counter
param Cmd :
- ENABLE
- DISABLE
return none
void RTC_TimeStructInit(RTC_TimeType* RTC_TimeStruct)Fills each RTC_TimeStruct member with its default value (Time = 00h:00 min: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, RTC_TimeType* RTC_TimeStruct)Calendar value read from Shadow register.
name RTC_ConfigTime.
fun Calendar value read from Shadow register.
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.When the user does not need to configure this
parameter, note that NULL can be passed in.
- 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
void RTC_GetTime(uint32_t RTC_Format, RTC_TimeType* RTC_TimeStruct)Get the RTC current Time.
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)Gets the RTC current Calendar Subseconds value.
name RTC_GetSubSecond.
fun Gets the RTC current Calendar Subseconds value.
return RTC current Calendar Subseconds value.
void RTC_DateStructInit(RTC_DateType* RTC_DateStruct)Fills each RTC_DateStruct member with its default value (Monday, January 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)Set RTC Calendar current date.
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,note that NULL can be passed in.
- 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)Get the RTC current date.
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_DateType : RTC_DateStruct pointer to a RTC_DateType structure that will
contain the returned current date configuration.
return none
void RTC_ConfigOutputType(uint32_t RTC_OutputType)Configures the RTC Output Pin mode.
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_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmType* RTC_AlarmStruct)Set the specified RTC Alarm.
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)Fills each RTC_AlarmStruct member with its default value (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask = all fields are masked).
name RTC_AlarmStructInit.
fun 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 : 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)Get the RTC Alarm value and masks.
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)Enables or disables the specified RTC Alarm.
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
void RTC_ConfigAlarmSubSecond(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)Configure the RTC AlarmA/B Subseconds value and mask
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)Gets the RTC Alarm Subseconds value.
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(uint32_t RTC_WakeUpClock)Configures the RTC Wakeup clock source.
name RTC_ConfigWakeUpClock.
fun Configures the RTC Wakeup clock source.
param RTC_WakeUpClock : RTC_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)Configures the RTC Wakeup counter.
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)Returns the RTC WakeUp timer counter value.
name RTC_GetWakeUpCounter.
fun Returns the RTC WakeUp timer counter value.
param none
return The RTC WakeUp Counter value.
ErrorStatus RTC_EnableWakeUp(FunctionalState Cmd)Enables or disables the RTC WakeUp timer.
name RTC_EnableWakeUp
fun Enables or disables the RTC WakeUp timer.
param Cmd :
- ENABLE
- DISABLE
return ErrorStatus :
- SUCCESS: RTC wakeup configured succeed
- ERROR: RTC wakeup configured failed
void RTC_ConfigDayLightSaving(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)Adds or substract one hour from the current time.
name RTC_ConfigDayLightSaving
fun Adds or substract one hour from the current time.
param RTC_DayLightSaving : the value of hour adjustment.
- RTC_DAYLIGHT_SAVING_SUB1H : Substract one hour (winter time).
- RTC_DAYLIGHT_SAVING_ADD1H : Add one hour (summer time).
param RTC_StoreOperation : Specifies the value to be written in the BCK bit
in CTRL register to store the operation.
- RTC_STORE_OPERATION_RESET : BCK Bit Reset.
- RTC_STORE_OPERATION_SET : BCK Bit Set.
return none
uint32_t RTC_GetStoreOperation(void)Returns the RTC Day Light Saving stored operation.
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)Configures the RTC output source.
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 ALRAF/ALRBF/WUTF is high (depending on OSEL).
- RTC_OUTPOL_LOW : The output pin is low when the ALRAF/ALRBF/WUTF is high (depending on OSEL).
return none
void RTC_EnableOutput2(FunctionalState Cmd)Enable or disable the RTC output2 .
name RTC_EnableOutput2
fun Enable or disable the RTC output2 .
param Cmd :
- ENABLE
- DISABLE
return none
void RTC_EnableTampOutput(FunctionalState Cmd)Enable or disable the RTC tamper output .
name RTC_EnableTampOutput
fun Enable or disable the RTC tamper output .
param Cmd :
- ENABLE
- DISABLE
return none
void RTC_EnableCalibOutput(FunctionalState Cmd)Enable or disable the RTC calibration clock to be output through the relative pin.
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)Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 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_SmouthCalibMinusPulsesValue)Configures the Smooth Calibration Settings.
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 periode is 32s.
- SMOOTH_CALIB_16SEC : The smooth calibration periode is 16s.
- SMOOTH_CALIB_8SEC : The smooth calibartion periode is 8s.
param RTC_SmoothCalibPlusPulses : Set or reset the CALP bit.
- RTC_SMOOTH_CALIB_PLUS_PULSES_SET : Add one RTCCLK puls every 2**11 pulses.
- RTC_SMOOTH_CALIB_PLUS_PULSES_RESET : No RTCCLK pulses are added.
param RTC_SmouthCalibMinusPulsesValue : Set the value of CALM[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
void RTC_EnableTimeStamp(uint32_t RTC_TimeStampEdge, FunctionalState Cmd)Enables or disables the RTC TimeStamp functionality with the specified time stamp pin stimulating edge.
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)Get the RTC TimeStamp value and masks.
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)Get the RTC timestamp Subseconds value.
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)Configures the Synchronization Shift Control Settings.
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_SUB1S_DISABLE : Add one second to the clock calendar.
- RTC_SHIFT_SUB1S_ENABLE : No effect.
param RTC_ShiftSubFS : Select the number of Second Fractions to Substitute.
This parameter can be one 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)Enables or disables the specified RTC interrupts.
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)Checks whether the specified RTC flag is set or not.
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 : RECALPF event flag.
- RTC_FLAG_TAMP3F : Tamp3 detect flag.
- RTC_FLAG_TAMP2F : Tamp2 detect flag.
- RTC_FLAG_TAMP1F : Tamp1 detect 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)Clears the RTC’s flags.
name RTC_ClrFlag
fun Clears the RTC's flags.
param RTC_FLAG : RTC_FLAG specifies the RTC flag to clear.
- RTC_FLAG_TAMP3F : Tamp3 detect flag.
- RTC_FLAG_TAMP2F : Tamp2 detect flag.
- RTC_FLAG_TAMP1F : Tamp1 detect 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_RSYF : Registers Synchronized flag.
return none
INTStatus RTC_GetITStatus(uint32_t RTC_INT)Checks whether the specified RTC interrupt has occurred or not.
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_TAMP3 : Tamper3 interrupt.
- RTC_INT_TAMP2 : Tamper2 interrupt.
- RTC_INT_TAMP1 : Tamper1 interrupt.
- 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)Clears the RTC’s interrupt pending bits.
name RTC_ClrIntPendingBit
fun Clears the RTC's interrupt pending bits.
param RTC_INT : specifies the RTC interrupt pending bit to clear.
- RTC_INT_TAMP3 : Tamper3 interrupt.
- RTC_INT_TAMP2 : Tamper2 interrupt.
- RTC_INT_TAMP1 : Tamper1 interrupt.
- 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
void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)Configures the select Tamper pin edge.
name RTC_TamperTriggerConfig
fun Configures the select Tamper pin edge.
param RTC_Tamper: Selected tamper pin.
- RTC_TAMPER_1: Select Tamper 1.
- RTC_TAMPER_2: Select Tamper 2.
- RTC_TAMPER_3: Select Tamper 3.
param RTC_TamperTrigger: Specifies the trigger on the tamper pin that stimulates tamper event.
- RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
- RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
- RTC_TamperTrigger_LowLevel: Low Level of the tamper pin causes tamper event.
- RTC_TamperTrigger_HighLevel: High Level of the tamper pin causes tamper event.
return none
void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)Enables or disables the specified Tamper detection.
name RTC_TamperCmd
fun Enables or disables the specified Tamper detection.
param RTC_Tamper: Selected tamper pin.
- RTC_TAMPER_1: Select Tamper 1.
- RTC_TAMPER_2: Select Tamper 2.
- RTC_TAMPER_3: Select Tamper 3.
param NewState :
- ENABLE
- DISABLE
return none
void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)Configures the Tampers Filter.
name RTC_TamperFilterConfig
fun Configures the Tampers Filter.
param RTC_TamperFilter: Selected tamper pin.
- RTC_TamperFilter_Disable: Tamper filter is disabled.
- RTC_TamperFilter_2Sample: Tamper is activated after 2 consecutive samples at the active level.
- RTC_TamperFilter_4Sample: Tamper is activated after 4 consecutive samples at the active level.
- RTC_TamperFilter_8Sample: Tamper is activated after 8 consecutive samples at the active level.
return none
void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)Configures the Tampers Sampling Frequency.
name RTC_TamperSamplingFreqConfig
fun Configures the Tampers Sampling Frequency.
param RTC_TamperSamplingFreq: Selected tamper pin.
- RTC_TamperSamplingFreq_RTCCLK_Div32768
- RTC_TamperSamplingFreq_RTCCLK_Div16384
- RTC_TamperSamplingFreq_RTCCLK_Div8192
- RTC_TamperSamplingFreq_RTCCLK_Div4096
- RTC_TamperSamplingFreq_RTCCLK_Div2048
- RTC_TamperSamplingFreq_RTCCLK_Div1024
- RTC_TamperSamplingFreq_RTCCLK_Div512
- RTC_TamperSamplingFreq_RTCCLK_Div256
return none
void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)Configures the Tampers Pins input Precharge Duration.
name RTC_TamperPinsPrechargeDuration
fun Configures the Tampers Pins input Precharge Duration.
param RTC_TamperPrechargeDuration: Selected tamper pin.
- RTC_TamperPrechargeDuration_1RTCCLK
- RTC_TamperPrechargeDuration_2RTCCLK
- RTC_TamperPrechargeDuration_4RTCCLK
- RTC_TamperPrechargeDuration_8RTCCLK
return none
void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)The timestamp is valid or invalid even the TSEN bit in tamper control register is reset.
name RTC_TimeStampOnTamperDetectionCmd
fun The timestamp is valid or invalid even the TSEN bit in tamper control register is reset.
param NewState :
- ENABLE
- DISABLE
return none
void RTC_TamperPullUpCmd(FunctionalState NewState)Enables or disables the Precharge of Tamper pin.
name RTC_TamperPullUpCmd
fun Enables or disables the Precharge of Tamper pin.
param NewState :
- ENABLE
- DISABLE
return none
void RTC_TamperIECmd(uint32_t TAMPxIE, FunctionalState NewState)Enables or disables the Tamper interrupt.
name RTC_TamperIECmd
fun Enables or disables the Tamper interrupt.
param TAMPxIE: Selected tamper number.
- RTC_TAMPER1_INT
- RTC_TAMPER2_INT
- RTC_TAMPER3_INT
param NewState :
- ENABLE
- DISABLE
return none
void RTC_TamperTAMPTSCmd(FunctionalState NewState)The timestamp is valid or invalid even the TSEN bit in tamper control register is reset.
name RTC_TamperTAMPTSCmd
fun The timestamp is valid or invalid even the TSEN bit in tamper control register is reset.
param NewState :
- ENABLE
- DISABLE
return none
void RTC_EnableTampErase(uint32_t RTC_Tamper_Erase, FunctionalState NewState)Enables or disables the tamper event erase backup register.
name RTC_EnableTampErase
fun Enables or disables the tamper event erase backup register.
param RTC_Tamper: Selected tamper pin.
- RTC_TAMPER_ERASE_1: Select Tamper 1 erase backup register.
- RTC_TAMPER_ERASE_2: Select Tamper 2 erase backup register.
- RTC_TAMPER_ERASE_3: Select Tamper 3 erase backup register.
param NewState :
- ENABLE
- DISABLE
return none
void RTC_BKUPRgWrite(uint8_t register_num, uint32_t Data)Write data to backup register.
name RTC_BKUPRgWrite
fun Write data to backup register.
param register_num: register_num value can be from 1 to 20
param Data : Data write to backup register
return none
uint32_t RTC_BKUPRgRead(uint8_t register_num)Read data from backup register.
name RTC_BKUPRgRead
fun Read data from backup register.
param register_num: register_num value can be from 1 to 20
param Data : Data write to backup register
return none
5.23.3 Demo descriptions
This section describes the 7 RTC examples: Alarm, AlarmSubsecond, Calendar, RtcAutoWakeUp, RtcBKP, Tamper, and TimeStamp; each example gives the project directory, the data flow, an analysis table, and a core-code walkthrough.
5.23.3.1 Alarm alarm interrupt
Example directory: projects/n32h49x_EVAL/examples/RTC/Alarm/
Init serial→write default date, time, and alarm A→select LSE and configure prescaler→enable alarm A and EXTI17→on alarm, the ISR prints the time and toggles PC13
| Item | Source analysis |
|---|---|
| Function | Uses RTC alarm A to raise an interrupt when the configured weekday, date, hour, and minute match; the ISR prints the current time and toggles the PC13 output level. |
| Source files | src/main.c: main flow and the first-config branch;src/User_RTC_Config.c: clock source, calendar, alarm configuration, and display functions;inc/User_RTC_Config.h: declarations of the above functions and structures;src/n32h49x_it.c, inc/n32h49x_it.h: the RTCAlarm_IRQHandler alarm ISR;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | RTC clock source is LSE; sync prescaler SynchPrediv=0xFF, async prescaler AsynchPrediv=0x7F, 24-hour format;default time 4:22:55, date weekday 4, 16 November, year 23; alarm A at 4:23:00, RTC_ALARM_SEL_WEEKDAY_DATE, DateWeekValue=16, mask RTC_ALARMMASK_WEEKDAY, RTC_ALARMMASK_HOURS, and RTC_ALARMMASK_MINUTES;alarm output push-pull, RTC_OUTPUT_ALA, active high;serial port USART1 (PA9/PA10, 115200). |
| Program and data path | main calls RTC_DateAndTimeDefaultVale to fill defaults, then enables the PWR clock and opens backup-domain access;RTC_CLKSourceConfig selects LSE and configures the RTC clock;when backup register 1 is not the sentinel 0xA5A5 it calls RTC_PrescalerConfig, RTC_DateRegulate, RTC_TimeRegulate, and RTC_AlarmRegulate and writes the sentinel back;it then configures the alarm output and calls EXTI17_RTCAlarm_Configuration;when the alarm fires, RTCAlarm_IRQHandler clears the flag and calls RTC_TimeShow. |
| Main APIs | RTC_CLKSourceConfig, RTC_PrescalerConfig, RTC_Init, RTC_SetDate, RTC_ConfigTime, RTC_SetAlarm, RTC_ConfigInt, RTC_EnableAlarm, RTC_ConfigOutput, RTC_BKUPRgWrite |
| Verification | After flashing and reset, the serial port prints the initialization and alarm configuration messages; when the configured time arrives (1 minute interval by default) it prints the current time and toggles PC13. |
| Use note | The example uses the 0xA5A5 sentinel in backup register 1 to avoid reconfiguring the time on every reset;to rewrite the default time and alarm, clear that backup register first; the alarm output pin is PC13 and can only be configured after enabling backup-domain access; the development environment is KEIL MDK-ARM V5.34 or IAR EWARM 8.50.1 on N32H497ZGL7_EVB V1.0. |
Core-code walkthrough and porting
The example uses a backup register as a “first configuration” flag: the date, time, and alarm are written only on the first power-up, and after a reset the running values in the backup domain are reused.
Data path: RTC_DateAndTimeDefaultVale -> RTC_CLKSourceConfig -> RTC_PrescalerConfig -> RTC_DateRegulate/RTC_TimeRegulate -> RTC_AlarmRegulate -> EXTI17_RTCAlarm_Configuration -> RTCAlarm_IRQHandler
1. Use a backup register as the first-config flag. main reads backup register 1 and only reconfigures the calendar and alarm when its content is not 0xA5A5, then writes the sentinel back.
if(SUCCESS==RTC_CLKSourceConfig(RTC_CLK_SRC_TYPE_LSE, true, false))
{
if (USER_WRITE_BKP_DAT1_DATA != RTC_BKUPRgRead(1))
{
RTC_PrescalerConfig();
/* RTC date time and alarm regulate*/
RTC_DateRegulate();
RTC_TimeRegulate();
RTC_AlarmRegulate(RTC_A_ALARM);
RTC_BKUPRgWrite(1, USER_WRITE_BKP_DAT1_DATA);
log_info("\r\n RTC Init Success\r\n");
}
}
2. Configure the prescaler and 24-hour format. RTC_PrescalerConfig writes the async prescaler, sync prescaler, and hour format into RTC_InitType and then calls RTC_Init; the LSE prescaler values are given in the clock configuration function.
void RTC_PrescalerConfig(void)
{
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_24HOUR_FORMAT;
/* Check on RTC init */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
log_info("\r\n //******* RTC Prescaler Config failed **********// \r\n");
}
}
SynchPrediv = 0xFF; // 32.768KHz
AsynchPrediv = 0x7F; // value range: 0-7F
3. Write the alarm and enable its interrupt. RTC_AlarmRegulate first disables the alarm, writes the time, date mode, and mask, calls RTC_SetAlarm, then enables RTC_INT_ALRA and re-enables the alarm.
RTC_AlarmStructure.DateWeekValue = RTC_AlarmDefault.DateWeekValue;
RTC_AlarmStructure.DateWeekMode = RTC_AlarmDefault.DateWeekMode;
RTC_AlarmStructure.AlarmMask = RTC_AlarmDefault.AlarmMask;
/* Configure the RTC Alarm A register */
RTC_SetAlarm(RTC_FORMAT_BIN, RTC_Alarm, &RTC_AlarmStructure);
printf("\n\r>> !! RTC Set Alarm_X success. !! <<\n\r");
if (RTC_Alarm == RTC_A_ALARM)
{
/* Enable the RTC Alarm A Interrupt */
RTC_ConfigInt(RTC_INT_ALRA, ENABLE);
RTC_AlarmShow(1);
}
else
{
/* Enable the RTC Alarm B Interrupt */
RTC_ConfigInt(RTC_INT_ALRB, ENABLE);
RTC_AlarmShow(2);
}
/* Enable the alarm */
RTC_EnableAlarm(RTC_Alarm, ENABLE);
return SUCCESS;
4. The ISR prints the time. When the alarm fires, RTCAlarm_IRQHandler clears the EXTI17 and RTC_INT_ALRA pending bits and prints the current time.
void RTCAlarm_IRQHandler(void)
{
EXTI_ClrITPendBit(EXTI_LINE17);
if (RTC_GetITStatus(RTC_INT_ALRA) != RESET)
{
RTC_ClrIntPendingBit(RTC_INT_ALRA);
RTC_TimeShow();
}
}
Corresponding source files: src/main.c, src/User_RTC_Config.c, src/n32h49x_it.c
5.23.3.2 AlarmSubsecond subsecond alarm
Example directory: projects/n32h49x_EVAL/examples/RTC/AlarmSubsecond/
Init serial→write default date, time, and alarm A→configure alarm A subsecond mask→enable alarm interrupt and EXTI17→on subsecond match, the ISR sets a flag and toggles PC13
| Item | Source analysis |
|---|---|
| Function | Adds a subsecond comparison on top of alarm A: RTC_ConfigAlarmSubSecond sets the subsecond value and mask, and the interrupt fires and prints the time on a subsecond match. |
| Source files | src/main.c: main flow, subsecond configuration, and the main-loop flag check;src/User_RTC_Config.c: clock source, calendar, and alarm configuration functions;inc/User_RTC_Config.h: declarations of the above functions and structures;src/n32h49x_it.c, inc/n32h49x_it.h: the RTCAlarm_IRQHandler ISR;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | RTC clock source is LSE, sync/async prescaler 0xFF/0x7F, 24-hour format;alarm A at 4:23:00; RTC_ConfigAlarmSubSecond(RTC_A_ALARM, 128, RTC_SUBS_MASK_SS14_7) places the subsecond comparison on SS[14:7];alarm output push-pull, RTC_OUTPUT_ALA, active high;serial port USART1 (PA9/PA10, 115200). |
| Program and data path | main is the same as the Alarm example, but the first-config branch additionally calls RTC_ConfigAlarmSubSecond to set the subsecond value and mask;after enabling EXTI17 it enters while(1) and polls RTC_Time_Out_Flag;the ISR prints the time and sets RTC_Time_Out_Flag, and the main loop prints “500ms Alarm Wakeup” when it sees the flag. |
| Main APIs | RTC_CLKSourceConfig, RTC_PrescalerConfig, RTC_SetAlarm, RTC_ConfigAlarmSubSecond, RTC_ConfigInt, RTC_EnableAlarm, RTC_ConfigOutput, RTC_BKUPRgWrite |
| Verification | After reset the serial port prints the configuration; on subsecond matches it prints the current time roughly every 500ms and toggles PC13; the main loop prints “500ms Alarm Wakeup” when it sees the flag. |
| Use note | Subsecond matching requires a prescaler configuration that satisfies the user manual rules on subsecond field validity; the subsecond value 128 corresponds to 500ms, derived from the (32768/128) counting frequency;the rest of the first-config and display logic is the same as the Alarm example. |
Core-code walkthrough and porting
On top of alarm A, RTC_ConfigAlarmSubSecond decides which bit field is used for the subsecond comparison, refining the alarm from second-level to subsecond-level.
Data path: RTC_ConfigAlarmSubSecond -> RTC_DateRegulate/RTC_TimeRegulate -> RTC_AlarmRegulate -> EXTI17_RTCAlarm_Configuration -> RTCAlarm_IRQHandler sets RTC_Time_Out_Flag -> main loop prints
1. Write the subsecond comparison value in the first config. Besides hour, minute, and second, alarm A also configures a subsecond value and mask; the comment gives the relation between 128 and 500ms.
if (USER_WRITE_BKP_DAT1_DATA != RTC_BKUPRgRead(1))
{
RTC_PrescalerConfig();
/* ALRMASS Value(128) = 500ms / (1 / (32768/128)) */
RTC_ConfigAlarmSubSecond(RTC_A_ALARM, 128, RTC_SUBS_MASK_SS14_7);
/* RTC date time and alarm regulate*/
RTC_DateRegulate();
RTC_TimeRegulate();
RTC_AlarmRegulate(RTC_A_ALARM);
RTC_BKUPRgWrite(1, USER_WRITE_BKP_DAT1_DATA);
log_info("\r\n RTC Init Success\r\n");
}
2. The main loop waits for the flag set by the ISR. The ISR only sets a flag; the printing is done in the main loop to avoid long output inside the interrupt.
while (1)
{
if(RTC_Time_Out_Flag)
{
RTC_Time_Out_Flag=false;
log_info("\r\n 500ms Alarm Wakeup \r\n");
}
}
3. The ISR sets a software flag. Compared with the Alarm example, the ISR additionally sets RTC_Time_Out_Flag after printing the time.
void RTCAlarm_IRQHandler(void)
{
EXTI_ClrITPendBit(EXTI_LINE17);
if (RTC_GetITStatus(RTC_INT_ALRA) != RESET)
{
RTC_ClrIntPendingBit(RTC_INT_ALRA);
RTC_TimeShow();
RTC_Time_Out_Flag = true;
}
}
Corresponding source files: src/main.c, src/n32h49x_it.c
5.23.3.3 Calendar calendar
Example directory: projects/n32h49x_EVAL/examples/RTC/Calendar/
Init serial→write default date and time→output a 1Hz calibration signal on PC13→loop the 1Hz signal back to PA7 externally→the EXTI7 ISR prints the date and time
| Item | Source analysis |
|---|---|
| Function | Configures the calendar and outputs a 1Hz calibration signal; the signal is fed back externally into PA7 and the EXTI7 ISR prints the current date and time once per second. |
| Source files | src/main.c: EXTI_PA7_Configuration and the main flow;src/User_RTC_Config.c: clock source, calendar, and prescaler configuration functions;inc/User_RTC_Config.h: declarations of the above functions and structures;src/n32h49x_it.c, inc/n32h49x_it.h: the EXTI9_5_IRQHandler ISR;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | RTC clock source is LSE; the first-config branch calls RTC_DeInit before configuring the prescaler, date, and time;calibration output is RTC_CALIB_OUTPUT_1HZ, push-pull, enabled;PA7 is configured as input and mapped to EXTI_LINE7, rising edge, with NVIC channel EXTI9_5_IRQn;serial port USART1 (PA9/PA10, 115200). |
| Program and data path | main calls RTC_DateAndTimeDefaultVale and enables the PWR and BKP clocks;the first-config branch runs RTC_DeInit, RTC_PrescalerConfig, RTC_DateRegulate, and RTC_TimeRegulate in turn and writes backup register 1;it then calls RTC_ConfigCalibOutput, RTC_ConfigOutputType, and RTC_EnableCalibOutput to output 1Hz;EXTI_PA7_Configuration enables EXTI7;EXTI9_5_IRQHandler checks EXTI_LINE7 and prints the date and time. |
| Main APIs | RTC_DeInit, RTC_CLKSourceConfig, RTC_PrescalerConfig, RTC_SetDate, RTC_ConfigTime, RTC_ConfigCalibOutput, RTC_ConfigOutputType, RTC_EnableCalibOutput, RTC_GetDate, RTC_GetTime |
| Verification | After reset the serial port prints the initialization message; connect the 1Hz calibration signal on PC13 to PA7 and the serial port prints the date and time once per second. |
| Use note | This example needs an external wire between PC13 and PA7 to generate the second interrupt; the first-config branch calls RTC_DeInit before configuring the calendar, unlike the incremental configuration of the Alarm example;the EXTI ISR must clear the EXTI_LINE7 pending bit after reading it. |
Core-code walkthrough and porting
The example routes the RTC 1Hz calibration output to PA7, using an ordinary EXTI interrupt for “print every second” without occupying an RTC interrupt.
Data path: RTC_DeInit -> RTC_PrescalerConfig -> RTC_DateRegulate/RTC_TimeRegulate -> RTC_ConfigCalibOutput/RTC_EnableCalibOutput -> EXTI_PA7_Configuration -> EXTI9_5_IRQHandler -> RTC_DateShow/RTC_TimeShow
1. Configure PA7 as an EXTI input. PA7 is set as input and mapped to EXTI7, rising edge, with NVIC channel EXTI9_5_IRQn.
void EXTI_PA7_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
EXTI_InitType EXTI_InitStructure;
NVIC_InitType NVIC_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
/* Enable the AFIO Clock */
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOA | RCC_APB2_PERIPHEN_AFIO, ENABLE);
/* Select the GPIO - configuration used for floating in */
GPIO_InitStructure.Pin = GPIO_PIN_7;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_ConfigEXTILine(EXTI_LINE_SOURCE7, GPIOA_PORT_SOURCE, GPIO_PIN_SOURCE7);
/* Configure Button EXTI line */
EXTI_InitStructure.EXTI_Line = EXTI_LINE7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitPeripheral(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable and set Button EXTI Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
2. First config and calibration output. The first-config branch resets with RTC_DeInit and writes the calendar, then sets the calibration output to 1Hz and enables it.
if (USER_WRITE_BKP_DAT1_DATA != RTC_BKUPRgRead(1))
{
RTC_DeInit();
RTC_PrescalerConfig();
log_info("\r\n RTC configured....");
/* Adjust time by values entered by the user on the hyperterminal */
RTC_DateRegulate();
RTC_TimeRegulate();
RTC_BKUPRgWrite(1, USER_WRITE_BKP_DAT1_DATA);
log_info("\r\n RTC Init Success\r\n");
}
/* Calibrate output 1Hz signal */
RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_1HZ);
/* Calibrate output config,push pull */
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
/* Calibrate output enable*/
RTC_EnableCalibOutput(ENABLE);
3. The EXTI7 ISR prints the date and time. The ISR checks EXTI_LINE7, prints the date and time, then clears the pending bit.
void EXTI9_5_IRQHandler(void)
{
if (EXTI_GetStatusFlag(EXTI_LINE7) != RESET)
{
/* Display the current Date on the Hyperterminal */
RTC_DateShow();
RTC_TimeShow();
/* Clear the UP Button EXTI line pending bit */
EXTI_ClrITPendBit(EXTI_LINE7);
}
}
Corresponding source files: src/main.c, src/n32h49x_it.c
5.23.3.4 RtcAutoWakeUp periodic wake-up
Example directory: projects/n32h49x_EVAL/examples/RTC/RtcAutoWakeUp/
Init serial→write default date and time→select wake-up clock and counter→enable wake-up interrupt and EXTI20→each wake-up period the ISR prints the time and drives PC13
| Item | Source analysis |
|---|---|
| Function | Uses the RTC wake-up timer to wake automatically at a fixed period; every period RTC_WKUP_IRQHandler prints the current time. |
| Source files | src/main.c: delay, the main flow, and the wake-up configuration;src/User_RTC_Config.c: clock source, calendar, and prescaler configuration functions;inc/User_RTC_Config.h: declarations of the above functions and structures;src/n32h49x_it.c, inc/n32h49x_it.h: the RTC_WKUP_IRQHandler ISR;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | RTC clock source is LSE; wake-up clock is RTC_WKUPCLK_CK_SPRE_16BITS, wake-up counter is 4;wake-up output is RTC_OUTPUT_WKUP, push-pull, active high;EXTI20 is rising-edge and mapped to RTC_WKUP_IRQn;serial port USART1 (PA9/PA10, 115200). |
| Program and data path | In main the first-config branch calls RTC_ConfigWakeUpClock and RTC_SetWakeUpCounter after writing the calendar;it then calls EXTI20_RTCWKUP_Configuration to enable EXTI20, RTC_ConfigInt(RTC_INT_WUT, ENABLE) to enable the wake-up interrupt, and RTC_EnableWakeUp(ENABLE) to start the wake-up timer;finally it configures the RTC_OUTPUT_WKUP output;each wake-up period RTC_WKUP_IRQHandler clears EXTI20 and RTC_INT_WUT and prints the time. |
| Main APIs | RTC_CLKSourceConfig, RTC_PrescalerConfig, RTC_ConfigWakeUpClock, RTC_SetWakeUpCounter, RTC_EnableWakeUp, RTC_ConfigInt, RTC_ConfigOutput, RTC_GetTime |
| Verification | After reset the serial port prints the initialization message; it then periodically prints the current time at the wake-up period and outputs a wake-up pulse on PC13. |
| Use note | The wake-up counter is 4 and, together with CK_SPRE_16BITS, defines the wake-up period;changing the period means adjusting both the wake-up clock and the counter; the main flow has no key or external stimulus, driving itself from the wake-up timer; RTC_EnableWakeUp is called twice in the main flow;the second call ensures both the interrupt and the output stay enabled. |
Core-code walkthrough and porting
The example relies on no external stimulus: it only configures the wake-up clock and counter and lets the RTC wake-up timer generate interrupts periodically.
Data path: RTC_ConfigWakeUpClock -> RTC_SetWakeUpCounter -> EXTI20_RTCWKUP_Configuration -> RTC_ConfigInt -> RTC_EnableWakeUp -> RTC_WKUP_IRQHandler -> RTC_TimeShow
1. Provide a millisecond delay function. The example carries a simple loop-count delay used for clock-stabilization waits and similar cases.
void delay(vu32 nCount)
{
vu32 index = 0;
for (index = (34000 * nCount); index != 0; index--)
{
}
}
2. Configure the wake-up clock and counter. The first-config branch sets the wake-up clock and counter after writing the calendar and writes back the backup-register sentinel.
if (USER_WRITE_BKP_DAT1_DATA != RTC_BKUPRgRead(1))
{
RTC_PrescalerConfig();
/* Adjust time by values entered by the user on the hyperterminal */
RTC_DateRegulate();
RTC_TimeRegulate();
/* wake up clock select */
RTC_ConfigWakeUpClock(RTC_WKUPCLK_CK_SPRE_16BITS);
/* wake up timer value */
RTC_SetWakeUpCounter(4);
RTC_BKUPRgWrite(1, USER_WRITE_BKP_DAT1_DATA);
log_info("\r\n RTC Init Success\r\n");
}
3. Enable the wake-up interrupt and output. After clearing EXTI20 and RTC_INT_WUT it enables EXTI20, the wake-up interrupt, and the wake-up timer, and outputs the wake-up signal on PC13.
EXTI_ClrITPendBit(EXTI_LINE20);
EXTI20_RTCWKUP_Configuration(ENABLE);
/* Enable the RTC Wakeup Interrupt */
RTC_ClrIntPendingBit(RTC_INT_WUT);
RTC_ConfigInt(RTC_INT_WUT, ENABLE);
RTC_EnableWakeUp(ENABLE);
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
RTC_ConfigOutput(RTC_OUTPUT_WKUP, RTC_OUTPOL_HIGH);
RTC_EnableWakeUp(ENABLE);
Corresponding source files: src/main.c, src/User_RTC_Config.c
5.23.3.5 RtcBKP backup register
Example directory: projects/n32h49x_EVAL/examples/RTC/RtcBKP/
Enable the PWR clock and backup-domain access→configure LSE and the RTC clock→write 20 backup registers in turn→read them back and print for comparison
| Item | Source analysis |
|---|---|
| Function | Demonstrates RTC backup-register reads and writes: it writes initial data into 20 backup registers and then reads each back and prints it to verify backup-domain data retention. |
| Source files | src/main.c: User_RTCBKP_Init and the write/read-back loops;inc/User_RTCBKP_Config.h: BKP_DAT1..BKP_DAT20, BACKUP_REGISTER_SIZE, and the extern array declarations;inc/main.h, inc/n32h49x_it.h: common declarations;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | Enables the PWR clock and calls PWR_BackupAccessEnable(ENABLE) to open backup-domain access;configures LSE as the RTC clock source and disables LSI before writing so that only one clock is on; backup register count BACKUP_REGISTER_SIZE is 20;serial port USART1 (PA9/PA10, 115200). |
| Program and data path | main first calls User_RTCBKP_Init to configure the clock and backup-domain access;the first loop writes the test data of all 20 registers with RTC_BKUPRgWrite(i+1, Write_Backup_Datas[i]);the second loop reads them back with RTC_BKUPRgRead(i+1) and prints BKP_DAT%u=%x with printf;the printed result is compared by hand against the written values. |
| Main APIs | RTC_BKUPRgWrite, RTC_BKUPRgRead, RTC_WaitForSynchro |
| Verification | After reset the serial port prints 20 lines of BKP_DATn=value;compare the printed values with the initial values in Write_Backup_Datas. |
| Use note | Backup-register retention during power loss needs VBAT supply; the content is lost only when power is removed; PWR_BackupAccessEnable(ENABLE) must be executed before writing, otherwise the write has no effect;this example has no RTC interrupt and is verified only through serial printing. |
Core-code walkthrough and porting
The example does two things: open backup-domain write access and configure the RTC clock, then write and read back the backup registers in batches.
Data path: User_RTCBKP_Init -> PWR_BackupAccessEnable -> RCC_ConfigRtcClk/RTC_WaitForSynchro -> RTC_BKUPRgWrite -> RTC_BKUPRgRead -> printf
1. Open the backup domain and configure the RTC clock. User_RTCBKP_Init enables the PWR clock, opens backup-domain write access, disables LSI, enables LSE, and selects it as the RTC clock after it stabilizes.
void User_RTCBKP_Init(void)
{
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPHEN_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessEnable(ENABLE);
RCC_EnableRtcClk(DISABLE);
/* Enable the LSE OSC32_IN PC14
LSI is turned off here to ensure that only one clock is turned on */
RCC_EnableLsi(DISABLE);
RCC_ConfigLse(RCC_LSE_ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDF) == RESET){}
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSE);
/* Enable the RTC Clock */
RCC_EnableRtcClk(ENABLE);
RTC_WaitForSynchro();
}
2. Write and read back the backup registers in batches. Two loops perform the write and the read-back and label the register index and content as BKP_DAT%u=%x.
uint8_t i =0;
User_RTCBKP_Init();
/* Initialize USART */
log_init();
log_info("\r\n RTC Init \r\n");
for(i=0;i<BACKUP_REGISTER_SIZE;i++)
{
RTC_BKUPRgWrite(i+1, Write_Backup_Datas[i]);
}
for(i=0;i<BACKUP_REGISTER_SIZE;i++)
{
Read_Backup_Datas[i]=RTC_BKUPRgRead(i+1);
printf("\r\n BKP_DAT%u=%x\r\n",i+1,(unsigned int)Read_Backup_Datas[i]);
}
3. Backup-register count and initial-value definitions. User_RTCBKP_Config.h defines the 20 BKP_DATn index macros and the backup-register count used by the write and read-back loops.
#define BKP_DAT1 ((uint16_t)0x0000)
#define BKP_DAT2 ((uint16_t)0x0004)
#define BKP_DAT3 ((uint16_t)0x0008)
#define BKP_DAT4 ((uint16_t)0x000C)
#define BKP_DAT5 ((uint16_t)0x0010)
#define BKP_DAT6 ((uint16_t)0x0014)
#define BKP_DAT7 ((uint16_t)0x0018)
#define BKP_DAT8 ((uint16_t)0x001C)
#define BKP_DAT9 ((uint16_t)0x0020)
#define BKP_DAT10 ((uint16_t)0x0024)
#define BACKUP_REGISTER_SIZE 20
Corresponding source files: src/main.c, inc/User_RTCBKP_Config.h
5.23.3.6 Tamper tamper detection
Example directory: projects/n32h49x_EVAL/examples/RTC/Tamper/
Init serial→configure LSE and prescaler→configure PC13 as tamper input→set trigger, filter, sampling, and precharge→enable the tamper interrupt→on tamper, the ISR prints a message
| Item | Source analysis |
|---|---|
| Function | Configures RTC tamper channel 1: PC13 is used as the tamper input, and the trigger mode, filter, sampling frequency, and precharge duration are set; the interrupt prints a message on a tamper event. |
| Source files | src/main.c: TamperInputIoInit, TAMPER_INT_Configuration, and the main flow;src/User_RTC_Config.c: clock source and prescaler configuration functions;inc/User_RTC_Config.h: declarations of the above functions and structures;src/n32h49x_it.c, inc/n32h49x_it.h: the RTC_TAMPER_IRQHandler ISR;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | RTC clock source is LSE; tamper input is PC13, configured as floating input; trigger mode RTC_TamperTrigger_HighLevel, filter RTC_TamperFilter_2Sample;sampling frequency RTC_TamperSamplingFreq_RTCCLK_Div256, precharge RTC_TamperPrechargeDuration_1RTCCLK;EXTI19 rising edge mapped to RTC_TAMPER_IRQn. |
| Program and data path | main enables the PWR and BKP clocks and opens backup-domain access, then in the first config sets RTC_PrescalerConfig and the backup-register sentinel after selecting LSE;TamperInputIoInit then sets PC13 as input;it calls RTC_TamperTriggerConfig, RTC_TamperFilterConfig, RTC_TamperSamplingFreqConfig, and RTC_TamperPinsPrechargeDuration for the tamper parameters;TAMPER_INT_Configuration enables EXTI19, and after clearing RTC_INT_TAMP1 it calls RTC_TamperIECmd and RTC_TamperCmd to enable detection. |
| Main APIs | RTC_TamperTriggerConfig, RTC_TamperCmd, RTC_TamperFilterConfig, RTC_TamperSamplingFreqConfig, RTC_TamperPinsPrechargeDuration, RTC_TamperIECmd, RTC_GetITStatus, RTC_ClrIntPendingBit |
| Verification | After reset the serial port prints the initialization message; applying a high level to PC13 triggers a tamper and the serial port prints “RTC Tamper Interrupt”. |
| Use note | Edge or level triggering needs an external pull-up or pull-down to avoid false triggers on a floating pin; clear the RTC_INT_TAMP1 pending bit before enabling tamper detection;the ISR clears both EXTI19 and RTC_INT_TAMP1 after reading the status. |
Core-code walkthrough and porting
Tamper detection requires setting the input pin and trigger parameters first, then enabling both EXTI and the internal RTC tamper interrupt; both interrupt paths must be handled.
Data path: TamperInputIoInit -> RTC_TamperTriggerConfig -> RTC_TamperFilterConfig/RTC_TamperSamplingFreqConfig/RTC_TamperPinsPrechargeDuration -> TAMPER_INT_Configuration -> RTC_TamperIECmd/RTC_TamperCmd -> RTC_TAMPER_IRQHandler
1. Configure the tamper input pin. TamperInputIoInit enables the GPIOC clock and configures PC13 as input.
void TamperInputIoInit(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
/* Enable the GPIO Clock */
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOC, ENABLE);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_13;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
}
2. Configure the tamper parameters and enable detection. The trigger mode, filter sample count, sampling frequency, and precharge duration are set in turn, then EXTI and the tamper interrupt are enabled.
/*Configure Tamper GPIO trigger mode*/
RTC_TamperTriggerConfig(RTC_TAMPER_1, RTC_TamperTrigger_HighLevel);
/*Configure the valib number of scanning times*/
RTC_TamperFilterConfig(RTC_TamperFilter_2Sample);
/*Configure scanning frequence*/
RTC_TamperSamplingFreqConfig(RTC_TamperSamplingFreq_RTCCLK_Div256);
/*Configure precharge duration time*/
RTC_TamperPinsPrechargeDuration(RTC_TamperPrechargeDuration_1RTCCLK);
EXTI_ClrITPendBit(EXTI_LINE19);
/*Configure tamper interruput,EXTI_LINE19*/
TAMPER_INT_Configuration(ENABLE);
RTC_ClrIntPendingBit(RTC_INT_TAMP1);
/*Enable tamper interruput*/
RTC_TamperIECmd(RTC_TAMPER1_INT, ENABLE);
/*Enable tamper1 */
RTC_TamperCmd(RTC_TAMPER_1, ENABLE);
3. The ISR prints a tamper message. On RTC_INT_TAMP1 the ISR clears EXTI19 and RTC_INT_TAMP1 and prints a message.
void RTC_TAMPER_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_INT_TAMP1) != RESET)
{
RTC_ClrIntPendingBit(RTC_INT_TAMP1);
EXTI_ClrITPendBit(EXTI_LINE19);
log_info("RTC Tamper Interrupt");
}
}
Corresponding source files: src/main.c, src/n32h49x_it.c
5.23.3.7 TimeStamp timestamp
Example directory: projects/n32h49x_EVAL/examples/RTC/TimeStamp/
Init serial→write default date and time→output a 1Hz calibration signal on PC13→feed the 1Hz signal into PB8 to trigger a timestamp→the ISR prints the date, time, and timestamp
| Item | Source analysis |
|---|---|
| Function | Uses EXTI8 as the RTC timestamp trigger: the 1Hz calibration signal is fed back from PC13 into PB8, and each trigger records and prints the date, time, and timestamp register content. |
| Source files | src/main.c: EXTI_PB8_TimeStamp_Configuration, EXTI19_TAMPER_IRQn_Configuration, and the main flow;src/User_RTC_Config.c: clock source, calendar, and prescaler configuration functions;inc/User_RTC_Config.h: declarations of the above functions and structures;src/n32h49x_it.c, inc/n32h49x_it.h: the RTC_TAMPER_IRQHandler ISR;readme.txt: features and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | RTC clock source is LSE; calibration output is RTC_CALIB_OUTPUT_1HZ, push-pull, enabled;timestamp trigger source is EXTI8 ( EXTI_RTCTimeStampSel(EXTI_TSSEL_LINE8)) with PB8 as input;timestamp edge is RTC_TIMESTAMP_EDGE_FALLING;EXTI19 rising edge mapped to RTC_TAMPER_IRQn for the timestamp interrupt. |
| Program and data path | In main the first config writes the calendar and the backup-register sentinel;it then configures the 1Hz calibration output; EXTI_PB8_TimeStamp_Configuration maps PB8 to the timestamp trigger source;after clearing RTC_FLAG_TISF and RTC_FLAG_TISOVF it calls RTC_EnableTimeStamp and RTC_ConfigInt(RTC_INT_TS, ENABLE);the timestamp ISR RTC_TAMPER_IRQHandler prints the date, time, and timestamp and clears the overflow flag. |
| Main APIs | RTC_CLKSourceConfig, RTC_PrescalerConfig, RTC_ConfigCalibOutput, RTC_EnableTimeStamp, RTC_ConfigInt, RTC_GetTimeStamp, RTC_GetTimeStampSubSecond, RTC_GetDate, RTC_GetTime |
| Verification | After reset the serial port prints the initialization message; connect the 1Hz calibration signal on PC13 to PB8 and the serial port prints the date, time, and timestamp once per second and reports a valid timestamp. |
| Use note | The timestamp overflow flag must be checked and cleared along with the timestamp flag to avoid misreading stale data; reset the timestamp enable bit before changing the trigger edge; this example uses EXTI8 as the timestamp source and EXTI19 as the interrupt line, so both must be configured separately. |
Core-code walkthrough and porting
The example selects EXTI8 as the timestamp trigger source, records the current calendar from an external pulse on PB8, and reads the timestamp register through the EXTI19 interrupt.
Data path: EXTI_PB8_TimeStamp_Configuration -> EXTI_RTCTimeStampSel -> RTC_EnableTimeStamp -> RTC_ConfigInt -> RTC_TAMPER_IRQHandler -> RTC_GetTimeStamp/RTC_GetTimeStampSubSecond
1. Map PB8 to the timestamp trigger source. EXTI_PB8_TimeStamp_Configuration configures PB8 as input and connects EXTI8 to the RTC timestamp.
void EXTI_PB8_TimeStamp_Configuration()
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOB , ENABLE);
/* Configure PB8 in input mode */
GPIO_InitStructure.Pin = GPIO_PIN_8;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Connect EXTI8 to RTC_TimeStamp */
EXTI_RTCTimeStampSel(EXTI_TSSEL_LINE8);
/* Connect EXTI8 Line to PB8 pin */
GPIO_ConfigEXTILine(EXTI_LINE_SOURCE8, GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE8);
}
2. Configure the calibration output and enable the timestamp. The main flow first outputs the 1Hz calibration signal, connects PB8 to the timestamp source, and after clearing the flags enables the timestamp and its interrupt on the falling edge.
/* Adjust time by values entered by the user on the hyperterminal */
RTC_ConfigCalibOutput(RTC_CALIB_OUTPUT_1HZ);
/* Calibrate output config,push pull */
RTC_ConfigOutputType(RTC_OUTPUT_PUSHPULL);
/* Calibrate output enable*/
RTC_EnableCalibOutput(ENABLE);
/* Configure EXTI PB8 pin connected to RTC TimeStamp
(while externally feeding PB8 with 1HZ signal output from PC13)
*/
EXTI_PB8_TimeStamp_Configuration();
EXTI_ClrITPendBit(EXTI_LINE19);
EXTI19_TAMPER_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. The ISR reads the timestamp register. On RTC_INT_TS the ISR prints the date, time, and timestamp, and clears both the timestamp flag and the overflow flag.
void RTC_TAMPER_IRQHandler(void)
{
EXTI_ClrITPendBit(EXTI_LINE19);
if (RTC_GetITStatus(RTC_INT_TS) != RESET)
{
/* Every interval delay (1s) displays calendar and clock stamp register contents*/
RTC_DateShow();
RTC_TimeShow();
RTC_TimeStampShow();
RTC_ClrFlag(RTC_FLAG_TISF);
RTC_ClrFlag(RTC_FLAG_TISOVF);
}
}
Corresponding source files: src/main.c, src/n32h49x_it.c
5.23.4 Notes and troubleshooting
| Scenario | Check & action | Source |
|---|---|---|
| Backup-domain write protection | RTC registers must be written after opening backup-domain write access, otherwise the write does not take effect; call PWR_BackupAccessEnable(ENABLE) to set the DBKP bit of the PWR control register. |
User manual |
| Entering init mode | Entering init mode requires SS[15:0] of RTC_SUBS to be at least 2 and different from the prescaler DIVS[14:0]; read RTC_DATE once before entering init mode. |
User manual |
| BYPS and prescaler | When BYPS is 0 the APB1 clock must be at least 7 times the RTC clock; when fAPB1 is below 7 times fRTCCLK, BYPS must be set to 1. |
User manual |
| Alarm seconds field | The alarm seconds field can only be compared and triggered when the prescaler DIVS[14:0] is greater than 3. | User manual |
| Wake-up timer | Writing 0 to the wake-up counter immediately sets the wake-up flag and raises one wake-up event, so 0 must not be written by mistake. | User manual |
| Timestamp overflow | After the timestamp flag TISF is detected, check and clear the overflow flag TISOVF as well to avoid using stale overflowed timestamp data. | User manual |
| Tamper interrupt | When the tamper flag TPxMF is already set, do not enable the corresponding tamper interrupt, otherwise the interrupt is entered immediately. | User manual |
| Tamper pin | A tamper pin using edge detection needs an external pull-up or pull-down to avoid false triggers on a floating pin. | User manual |
| Tamper and backup register | Tamper detection and RTC backup-register writes should not happen at the same time; finish the backup-register write before starting tamper detection. |
User manual |
| Reference clock detection | The prescaler DIVS should be configured to 0x00FF when reference clock detection is used. | User manual |
| Timestamp trigger edge | The timestamp enable bit TSEN in the control register must be reset before changing the timestamp trigger edge. | User manual |
| STANDBY wake-up | To wake from STANDBY mode with an RTC event, enable the WKUPRTCEN bit of the PWR control/status register. | User manual |