Microchip® Advanced Software Framework

Quick Start Guide for RTC (COUNT) - Callback

In this use case, the RTC is set up in count mode.

The quick start configures the RTC in 16-bit mode and to continuously update COUNT register. The rest of the configuration is according to the default. A callback is implemented for when the RTC overflows.

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:

/* SYSTEM_CLOCK_SOURCE_OSC32K configuration - Internal 32KHz oscillator */
# 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:

/* Configure GCLK generator 2 (RTC) */
# 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_count(void)
{
struct rtc_count_config config_rtc_count;
rtc_count_get_config_defaults(&config_rtc_count);
config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
config_rtc_count.mode = RTC_COUNT_MODE_16BIT;
#ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
config_rtc_count.continuously_update = true;
#endif
rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
rtc_count_enable(&rtc_instance);
}

Callback function:

void rtc_overflow_callback(void)
{
/* Do something on RTC overflow here */
}

Function for setting up the callback functionality of the driver:

void configure_rtc_callbacks(void)
{
&rtc_instance, rtc_overflow_callback, RTC_COUNT_CALLBACK_OVERFLOW);
}

Add to user application main():

/* Initialize system. Must configure conf_clocks.h first. */
/* Configure and enable RTC */
configure_rtc_count();
/* Configure and enable callback */
configure_rtc_callbacks();
/* Set period */
rtc_count_set_period(&rtc_instance, 2000);

Workflow

  1. Initialize system.
  2. Configure and enable module.
    configure_rtc_count();
  3. Create an RTC configuration structure to hold the desired RTC driver settings and fill it with the default driver configuration values.
    struct rtc_count_config config_rtc_count;
    rtc_count_get_config_defaults(&config_rtc_count);
    Note
    This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  4. Alter the RTC driver configuration to run in 16-bit counting mode, with continuous counter register updates and a compare value of 1000ms.
    config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
    config_rtc_count.mode = RTC_COUNT_MODE_16BIT;
    #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
    config_rtc_count.continuously_update = true;
    #endif
  5. Initialize the RTC module.
    rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
  6. Enable the RTC module, so that it may begin counting.
    rtc_count_enable(&rtc_instance);
  7. Configure callback functionality.
    configure_rtc_callbacks();
    1. Register overflow callback.
      &rtc_instance, rtc_overflow_callback, RTC_COUNT_CALLBACK_OVERFLOW);
    2. Enable overflow callback.
  8. Set period.
    rtc_count_set_period(&rtc_instance, 2000);

Implementation

Code

Add to user application main:

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

Workflow

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

Callback

Each time the RTC counter overflows, the callback function will be called.

Workflow

  1. Perform the desired user action for each RTC overflow:
    /* Do something on RTC overflow here */