Microchip® Advanced Software Framework

Quick Start Guide for RTC (CAL) - Callback

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:

struct rtc_module rtc_instance;

The following must be added to the user application:

Function for setting up the module:

{
/* Initialize RTC in calendar mode. */
struct rtc_calendar_config config_rtc_calendar;
rtc_calendar_get_config_defaults(&config_rtc_calendar);
alarm.time.year = 2013;
alarm.time.day = 1;
config_rtc_calendar.clock_24h = true;
config_rtc_calendar.alarm[0].time = alarm.time;
config_rtc_calendar.alarm[0].mask = RTC_CALENDAR_ALARM_MASK_YEAR;
rtc_calendar_init(&rtc_instance, RTC, &config_rtc_calendar);
}

Callback function:

{
/* Do something on RTC alarm match here */
/* Set new alarm in 5 seconds */
}

Function for setting up the callback functionality of the driver:

Add to user application main():

struct rtc_calendar_time time;
time.year = 2012;
time.month = 12;
time.day = 31;
time.hour = 23;
time.minute = 59;
time.second = 59;
/* Configure and enable RTC */
/* Configure and enable callback */
/* Set current time. */

Workflow

  1. Initialize system.
  2. Create and initialize a time structure.
    struct rtc_calendar_time time;
    time.year = 2012;
    time.month = 12;
    time.day = 31;
    time.hour = 23;
    time.minute = 59;
    time.second = 59;
  3. Configure and enable module.
    1. Create an RTC configuration structure to hold the desired RTC driver settings and fill it with the default driver configuration values.
      struct rtc_calendar_config config_rtc_calendar;
      rtc_calendar_get_config_defaults(&config_rtc_calendar);
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    2. Create and initialize an alarm.
    3. Change settings in the configuration and set alarm.
      config_rtc_calendar.clock_24h = true;
      config_rtc_calendar.alarm[0].time = alarm.time;
      config_rtc_calendar.alarm[0].mask = RTC_CALENDAR_ALARM_MASK_YEAR;
    4. Initialize the module with the set configurations.
      rtc_calendar_init(&rtc_instance, RTC, &config_rtc_calendar);
    5. Enable the module.
  4. Configure callback functionality.
    1. Register overflow callback.
    2. Enable overflow callback.
  5. Set time of the RTC calendar.

Implementation

Code

Add to user application main:

while (true) {
/* Infinite loop */
}

Workflow

  1. Infinite while loop while waiting for callbacks.
    while (true) {

Callback

Each time the RTC time matches the configured alarm, the callback function will be called.

Workflow

  1. Create alarm struct and initialize the time with current time.
  2. Set alarm to trigger on seconds only.
  3. Add one second to the current time and set new alarm.