In this use case, the RTC is set up in calendar mode.
The time is set and an alarm is enabled, as well as a callback for when the alarm time is hit. Each time the callback fires, the alarm time is reset to five seconds in the future and the board LED toggled.
Prerequisites
The Generic Clock Generator for the RTC should be configured and enabled; if you are using the System Clock driver, this may be done via conf_clocks.h
.
Clocks and Oscillators
The conf_clock.h
file needs to be changed with the different values to configure the clocks and oscillators for the module according to the used device.
For example, the following oscillator settings are needed for SAMD21:
The following generic clock settings are needed for SAMD21:
Setup
Code
Create an rtc_module struct and add to the main application source file, outside of any functions:
The following must be added to the user application:
Function for setting up the module:
{
config_rtc_calendar.clock_24h = true;
config_rtc_calendar.alarm[0].time =
alarm.
time;
}
Callback function:
Function for setting up the callback functionality of the driver:
Add to user application main()
:
time.year = 2012;
time.month = 12;
time.day = 31;
time.hour = 23;
time.minute = 59;
time.second = 59;
Workflow
- Initialize system.
- Create and initialize a time structure.
time.year = 2012;
time.month = 12;
time.day = 31;
time.hour = 23;
time.minute = 59;
time.second = 59;
- Configure and enable module.
- Create an RTC configuration structure to hold the desired RTC driver settings and fill it with the default driver configuration values.
- Note
- This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
- Create and initialize an alarm.
- Change settings in the configuration and set alarm.
config_rtc_calendar.clock_24h = true;
config_rtc_calendar.alarm[0].time =
alarm.
time;
- Initialize the module with the set configurations.
- Enable the module.
- Configure callback functionality.
- Register overflow callback.
- Enable overflow callback.
- Set time of the RTC calendar.
Implementation
Code
Add to user application main:
Workflow
- Infinite while loop while waiting for callbacks.
Callback
Each time the RTC time matches the configured alarm, the callback function will be called.
Workflow
- Create alarm struct and initialize the time with current time.
- Set alarm to trigger on seconds only.
- Add one second to the current time and set new alarm.