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:
# define CONF_CLOCK_OSC32K_ENABLE true
# define CONF_CLOCK_OSC32K_STARTUP_TIME SYSTEM_OSC32K_STARTUP_130
# define CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT true
# define CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT true
# define CONF_CLOCK_OSC32K_ON_DEMAND true
# define CONF_CLOCK_OSC32K_RUN_IN_STANDBY false
The following generic clock settings are needed for SAMD21:
# define CONF_CLOCK_GCLK_2_ENABLE true
# define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY false
# define CONF_CLOCK_GCLK_2_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_OSC32K
# define CONF_CLOCK_GCLK_2_PRESCALER 32
# define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE false
Setup
Code
Create an rtc_module struct and add to the main application source file, outside of any functions:
struct rtc_module rtc_instance;
The following must be added to the user application:
Function for setting up the module:
void configure_rtc_calendar(void)
{
config_rtc_calendar.clock_24h = true;
config_rtc_calendar.alarm[0].time =
alarm.
time;
}
Callback function:
void rtc_match_callback(void)
{
}
Function for setting up the callback functionality of the driver:
void configure_rtc_callbacks(void)
{
}
Add to user application main()
:
time.year = 2012;
time.month = 12;
time.day = 31;
time.hour = 23;
time.minute = 59;
time.second = 59;
configure_rtc_calendar();
configure_rtc_callbacks();
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.
configure_rtc_calendar();
- 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.
configure_rtc_callbacks();
- 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.
alarm.time.second += 5;
alarm.time.second = alarm.time.second % 60;