The S32 SDK provides both HAL and Peripheral Driver for the Real Time Clock (RTC) module of S32 SDK devices.
.
Hardware background
The Real Time Clock Module is a independent timer that keeps track of the exact date and time with no software overhead, with low power usage.
Features of the RTC module include:
- 32-bit seconds counter with roll-over protection and 32-bit alarm
- 16-bit prescaler with compensation that can correct errors between 0.12 ppm and 3906 ppm
- Option to increment prescaler using the LPO (prescaler increments by 32 every clock edge)
- Register write protection
- Lock register requires POR or software reset to enable write access
- Configurable 1, 2, 4, 8, 16, 32, 64 or 128 Hz square wave output with optional interrupt
- Alarm interrupt configured by the driver automatically refreshes alarm time configured by the user
- User interrupt handlers can be configured for all interrupts
How to use the RTC driver in your application
In order to be able to use the RTC in your application, the first thing to do is initializing it with the desired configuration. This is done by calling the RTC_DRV_Init function. One of the arguments passed to this function is the configuration which will be used for the RTC instance, specified by the rtc_init_config_t structure.
The rtc_init_config_t structure allows you to configure the following:
- RTC clock source (32 KHz clock or 1 KHz LPO clock)
- Clock Out pin configuration (Clock OUT pin source)
- Compensation (Interval and value)
- Update enable - this allows updates to Time Counter Enable bit if the Status Register under limited conditions
- Enable non supervisor writes to the registers
The rtc_seconds_int_config_t structure configures the time seconds interrupt. To setup an interrupt every seconds you have to configure the structure mentioned with the following parameters:
- Frequency of the interrupt
- Interrupt Handler
- If needed - interrupt handler parameters
An alarm is configured with rtc_alarm_config_t structure, which is described by the following parameters:
- Alarm time in date-time format
- Interval of alarm repeat in seconds
- Number of alarm repeats (use 0 if the alarm is not recursive)
- Repeat forever field (if set, the number of repeats field will be ignored)
- Alarm interrupt enable
- Alarm interrupt handler
- Alarm interrupt handler parameters
Note If the alarm interrupt is not enabled, the user must make the updates of the alarm time manually.
After the RTC_DRV_Init call and, if needed, alarm and other configurations the RTC counter is started by calling RTC_DRV_Enable, with start time as parameter in rtc_timedate_t format.
To get the current time and date you can call RTC_DRV_GetCurrentTimeDate function, this method will get the seconds from the Time Seconds Register and will convert into human readable format as rtc_timedate_t.
Example
void rtcAlarmCallback(void)
{
}
int main()
{
{
.compensation = 0,
.updateEnable = false,
.nonSupervisorAccessEnable = false
};
{
.month = 01U,
.day = 01U,
.hour = 00U,
.minutes = 00U,
.seconds = 00U
};
{
{
.month = 01U,
.day = 01U,
.hour = 00U,
.minutes = 00U,
.seconds = 03U,
},
.repetitionInterval = 3UL,
.numberOfRepeats = 0UL,
.repeatForever = true,
.alarmIntEnable = true,
.alarmCallback = (void *)rtcAlarmCallback,
.callbackParams = (void *)NULL
};
while(1);
}
Important Notes
- Before using the RTC driver the module clock must be configured
- The driver enables the interrupts for the corresponding RTC module, but any interrupt priority must be done by the application
- The board specific configurations must be done prior to driver calls; the driver has no influence on the functionality of the clockout pin - they must be configured by application