Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SAM RTC Count (RTC COUNT) Driver

This driver for Atmel® | SMART ARM®-based microcontrollers provides an interface for the configuration and management of the device's Real Time Clock functionality in Count operating mode, for the configuration and retrieval of the current RTC counter value.

The following driver API modes are covered by this manual:

The following peripheral is used by this module:

The following devices can use this module:

The outline of this documentation is as follows:

Prerequisites

There are no prerequisites for this module.

Module Overview

The RTC module in the SAM devices is a 32-bit counter, with a 10-bit programmable prescaler. Typically, the RTC clock is run continuously, including in the device's low-power sleep modes, to track the current time and date information. The RTC can be used as a source to wake up the system at a scheduled time or periodically using the alarm functions.

In this driver, the RTC is operated in Count mode. This allows for an easy integration of an asynchronous counter into a user application, which is capable of operating while the device is in sleep mode.

Whilst operating in Count mode, the RTC features:

Driver Feature Macro Definition

Driver Feature Macro Supported devices
FEATURE_RTC_PERIODIC_INT SAM L21/L22/C20/C21/R30
FEATURE_RTC_PRESCALER_OFF SAM L21/L22/C20/C21/R30
FEATURE_RTC_CLOCK_SELECTION SAM L21/L22/C20/C21/R30
FEATURE_RTC_GENERAL_PURPOSE_REG SAM L21/L22/R30
FEATURE_RTC_CONTINUOUSLY_UPDATED SAM D20, SAM D21, SAM R21, SAM D10, SAM D11, SAM DA1, SAM HA1
FEATURE_RTC_TAMPER_DETECTION SAM L22
Note
The specific features are only available in the driver when the selected device supports those features.

Compare and Overflow

The RTC can be used with up to 4/6 compare values (depending on selected operation mode). These compare values will trigger on match with the current RTC counter value, and can be set up to trigger an interrupt, event, or both. The RTC can also be configured to clear the counter value on compare match in 32-bit mode, resetting the count value back to zero.

If the RTC is operated without the Clear on Match option enabled, or in 16-bit mode, the RTC counter value will instead be cleared on overflow once the maximum count value has been reached:

\[ COUNT_{MAX} = 2^{32}-1 \]

for 32-bit counter mode, and

\[ COUNT_{MAX} = 2^{16}-1 \]

for 16-bit counter mode.

When running in 16-bit mode, the overflow value is selectable with a period value. The counter overflow will then occur when the counter value reaches the specified period value.

Periodic Events

The RTC can generate events at periodic intervals, allowing for direct peripheral actions without CPU intervention. The periodic events can be generated on the upper eight bits of the RTC prescaler, and will be generated on the rising edge transition of the specified bit. The resulting periodic frequency can be calculated by the following formula:

\[ f_{PERIODIC}=\frac{f_{ASY}}{2^{n+3}} \]

Where \(f_{ASY}\) refers to the asynchronous clock is set up in the RTC module configuration. The n parameter is the event source generator index of the RTC module. If the asynchronous clock is operated at the recommended frequency of 1KHz, the formula results in the values shown in the table below.

RTC Event Frequencies for Each Prescaler Bit Using a 1KHz Clock
n Periodic event
7 1Hz
6 2Hz
5 4Hz
4 8Hz
3 16Hz
2 32Hz
1 64Hz
0 128Hz
Note
The connection of events between modules requires the use of the SAM Event System (EVENTS) Driver to route output event of one module to the the input event of another. For more information on event routing, refer to the event driver documentation.

Digital Frequency Correction

The RTC module contains Digital Frequency Correction logic to compensate for inaccurate source clock frequencies which would otherwise result in skewed time measurements. The correction scheme requires that at least two bits in the RTC module prescaler are reserved by the correction logic. As a result of this implementation, frequency correction is only available when the RTC is running from a 1Hz reference clock.

The correction procedure is implemented by subtracting or adding a single cycle from the RTC prescaler every 1024 RTC GCLK cycles. The adjustment is applied the specified number of time (maximum 127) over 976 of these periods. The corresponding correction in PPM will be given by:

\[ Correction(PPM) = \frac{VALUE}{999424}10^6 \]

The RTC clock will tick faster if provided with a positive correction value, and slower when given a negative correction value.

RTC Tamper Detect

see RTC Tamper Detect

Special Considerations

Clock Setup

SAM D20/D21/R21/D10/D11/DA1/HA1 Clock Setup

The RTC is typically clocked by a specialized GCLK generator that has a smaller prescaler than the others. By default the RTC clock is on, selected to use the internal 32KHz RC-oscillator with a prescaler of 32, giving a resulting clock frequency of 1KHz to the RTC. When the internal RTC prescaler is set to 1024, this yields an end-frequency of 1Hz.

The implementer also has the option to set other end-frequencies. The table below lists the available RTC frequencies for each possible GCLK and RTC input prescaler options.

RTC Output Frequencies from Allowable Input Clocks
End-frequency GCLK prescaler RTC prescaler
32KHz 1 1
1KHz 32 1
1Hz 32 1024

The overall RTC module clocking scheme is shown in the figure below.

SAM L21/C20/C21/R30 Clock Setup

The RTC clock can be selected from OSC32K, XOSC32K, or OSCULP32K, and a 32KHz or 1KHz oscillator clock frequency is required. This clock must be configured and enabled in the 32KHz oscillator controller before using the RTC.

The table below lists the available RTC clock asfdoc_sam0_rtc_count_rtc_clk.

RTC Clocks Source
RTC clock frequency Clock source Description
1.024KHz ULP1K 1.024KHz from 32KHz internal ULP oscillator
32.768KHz ULP32K 32.768KHz from 32KHz internal ULP oscillator
1.024KHz OSC1K 1.024KHz from 32KHz internal oscillator
32.768KHz OSC32K 32.768KHz from 32KHz internal oscillator
1.024KHz XOSC1K 1.024KHz from 32KHz internal oscillator
32.768KHz XOSC32K 32.768KHz from 32KHz external crystal oscillator

Extra Information

For extra information, see Extra Information for RTC COUNT Driver. This includes:

Examples

For a list of examples related to this driver, see Examples for RTC (COUNT) Driver.

API Overview

Data Structures

struct  rtc_count_config
 RTC Count configuration structure. More...
 
struct  rtc_count_events
 RTC Count event enable/disable structure. More...
 

Enumerations

enum  rtc_count_compare {
  RTC_COUNT_COMPARE_0 = 0,
  RTC_COUNT_COMPARE_1 = 1,
  RTC_COUNT_COMPARE_2 = 2,
  RTC_COUNT_COMPARE_3 = 3,
  RTC_COUNT_COMPARE_4 = 4,
  RTC_COUNT_COMPARE_5 = 5
}
 Available compare channels. More...
 
enum  rtc_count_mode {
  RTC_COUNT_MODE_16BIT = 0,
  RTC_COUNT_MODE_32BIT = 1
}
 Available operation modes for the RTC. More...
 
enum  rtc_count_prescaler {
  RTC_COUNT_PRESCALER_DIV_1 = RTC_MODE0_CTRL_PRESCALER_DIV1,
  RTC_COUNT_PRESCALER_DIV_2 = RTC_MODE0_CTRL_PRESCALER_DIV2,
  RTC_COUNT_PRESCALER_DIV_4 = RTC_MODE0_CTRL_PRESCALER_DIV4,
  RTC_COUNT_PRESCALER_DIV_8 = RTC_MODE0_CTRL_PRESCALER_DIV8,
  RTC_COUNT_PRESCALER_DIV_16 = RTC_MODE0_CTRL_PRESCALER_DIV16,
  RTC_COUNT_PRESCALER_DIV_32 = RTC_MODE0_CTRL_PRESCALER_DIV32,
  RTC_COUNT_PRESCALER_DIV_64 = RTC_MODE0_CTRL_PRESCALER_DIV64,
  RTC_COUNT_PRESCALER_DIV_128 = RTC_MODE0_CTRL_PRESCALER_DIV128,
  RTC_COUNT_PRESCALER_DIV_256 = RTC_MODE0_CTRL_PRESCALER_DIV256,
  RTC_COUNT_PRESCALER_DIV_512 = RTC_MODE0_CTRL_PRESCALER_DIV512,
  RTC_COUNT_PRESCALER_DIV_1024 = RTC_MODE0_CTRL_PRESCALER_DIV1024
}
 RTC input clock prescaler settings. More...
 

Driver Feature Definition

Define port features set according to different device family.

#define FEATURE_RTC_CONTINUOUSLY_UPDATED
 RTC continuously updated. More...
 

Configuration and Initialization

static void rtc_count_get_config_defaults (struct rtc_count_config *const config)
 Gets the RTC default configurations. More...
 
void rtc_count_reset (struct rtc_module *const module)
 Resets the RTC module. More...
 
void rtc_count_enable (struct rtc_module *const module)
 Enables the RTC module. More...
 
void rtc_count_disable (struct rtc_module *const module)
 Disables the RTC module. More...
 
enum status_code rtc_count_init (struct rtc_module *const module, Rtc *const hw, const struct rtc_count_config *const config)
 Initializes the RTC module with given configurations. More...
 
enum status_code rtc_count_frequency_correction (struct rtc_module *const module, const int8_t value)
 Calibrate for too-slow or too-fast oscillator. More...
 

Count and Compare Value Management

enum status_code rtc_count_set_count (struct rtc_module *const module, const uint32_t count_value)
 Set the current count value to desired value. More...
 
uint32_t rtc_count_get_count (struct rtc_module *const module)
 Get the current count value. More...
 
enum status_code rtc_count_set_compare (struct rtc_module *const module, const uint32_t comp_value, const enum rtc_count_compare comp_index)
 Set the compare value for the specified compare. More...
 
enum status_code rtc_count_get_compare (struct rtc_module *const module, uint32_t *const comp_value, const enum rtc_count_compare comp_index)
 Get the current compare value of specified compare. More...
 
enum status_code rtc_count_set_period (struct rtc_module *const module, uint16_t period_value)
 Set the given value to the period. More...
 
enum status_code rtc_count_get_period (struct rtc_module *const module, uint16_t *const period_value)
 Retrieves the value of period. More...
 

Status Management

static bool rtc_count_is_overflow (struct rtc_module *const module)
 Check if an RTC overflow has occurred. More...
 
static void rtc_count_clear_overflow (struct rtc_module *const module)
 Clears the RTC overflow flag. More...
 
bool rtc_count_is_compare_match (struct rtc_module *const module, const enum rtc_count_compare comp_index)
 Check if RTC compare match has occurred. More...
 
enum status_code rtc_count_clear_compare_match (struct rtc_module *const module, const enum rtc_count_compare comp_index)
 Clears RTC compare match flag. More...
 

Event Management

static void rtc_count_enable_events (struct rtc_module *const module, struct rtc_count_events *const events)
 Enables an RTC event output. More...
 
static void rtc_count_disable_events (struct rtc_module *const module, struct rtc_count_events *const events)
 Disables an RTC event output. More...
 

#define FEATURE_RTC_CONTINUOUSLY_UPDATED

RTC continuously updated.

Available compare channels.

Note
Not all compare channels are available in all devices and modes.
Enumerator
RTC_COUNT_COMPARE_0 

Compare channel 0.

RTC_COUNT_COMPARE_1 

Compare channel 1.

RTC_COUNT_COMPARE_2 

Compare channel 2.

RTC_COUNT_COMPARE_3 

Compare channel 3.

RTC_COUNT_COMPARE_4 

Compare channel 4.

RTC_COUNT_COMPARE_5 

Compare channel 5.

Available operation modes for the RTC.

RTC Count operating modes, to select the counting width and associated module operation.

Enumerator
RTC_COUNT_MODE_16BIT 

RTC Count module operates in 16-bit mode.

RTC_COUNT_MODE_32BIT 

RTC Count module operates in 32-bit mode.

RTC input clock prescaler settings.

The available input clock prescaler values for the RTC count module.

Enumerator
RTC_COUNT_PRESCALER_DIV_1 

RTC input clock frequency is prescaled by a factor of 1.

RTC_COUNT_PRESCALER_DIV_2 

RTC input clock frequency is prescaled by a factor of 2.

RTC_COUNT_PRESCALER_DIV_4 

RTC input clock frequency is prescaled by a factor of 4.

RTC_COUNT_PRESCALER_DIV_8 

RTC input clock frequency is prescaled by a factor of 8.

RTC_COUNT_PRESCALER_DIV_16 

RTC input clock frequency is prescaled by a factor of 16.

RTC_COUNT_PRESCALER_DIV_32 

RTC input clock frequency is prescaled by a factor of 32.

RTC_COUNT_PRESCALER_DIV_64 

RTC input clock frequency is prescaled by a factor of 64.

RTC_COUNT_PRESCALER_DIV_128 

RTC input clock frequency is prescaled by a factor of 128.

RTC_COUNT_PRESCALER_DIV_256 

RTC input clock frequency is prescaled by a factor of 256.

RTC_COUNT_PRESCALER_DIV_512 

RTC input clock frequency is prescaled by a factor of 512.

RTC_COUNT_PRESCALER_DIV_1024 

RTC input clock frequency is prescaled by a factor of 1024.

enum status_code rtc_count_clear_compare_match ( struct rtc_module *const  module,
const enum rtc_count_compare  comp_index 
)

Clears RTC compare match flag.

Clears the compare flag. The compare flag is set when there is a compare match between the counter and the compare.

Note
Compare 4 and 5 are only available in 16-bit mode.
Parameters
[in,out]modulePointer to the software instance struct
[in]comp_indexIndex of compare to check current flag
Returns
Status indicating if flag was successfully cleared.
Return values
STATUS_OKIf flag was successfully cleared
STATUS_ERR_INVALID_ARGIf invalid argument(s) were provided
STATUS_ERR_BAD_FORMATIf the module was not initialized in a mode

References Assert, RTC_COUNT_MODE_16BIT, RTC_COUNT_MODE_32BIT, STATUS_ERR_BAD_FORMAT, STATUS_ERR_INVALID_ARG, and STATUS_OK.

static void rtc_count_clear_overflow ( struct rtc_module *const  module)
inlinestatic

Clears the RTC overflow flag.

Clears the RTC module counter overflow flag, so that new overflow conditions can be detected.

Parameters
[in,out]moduleRTC hardware module

References Assert.

void rtc_count_disable ( struct rtc_module *const  module)

Disables the RTC module.

Disables the RTC module.

Parameters
[in,out]moduleRTC hardware module

References Assert, rtc_count_is_syncing(), system_interrupt_disable(), and SYSTEM_INTERRUPT_MODULE_RTC.

Referenced by cleanup_asynchronous_event_test(), cleanup_resynchronous_event_test(), cleanup_synchronous_event_test(), and rtc_count_reset().

static void rtc_count_disable_events ( struct rtc_module *const  module,
struct rtc_count_events *const  events 
)
inlinestatic

Disables an RTC event output.

Disabled one or more output events from the RTC module. See rtc_count_events for a list of events this module supports.

Note
Events cannot be altered while the module is enabled.
Parameters
[in,out]moduleRTC hardware module
[in]eventsStruct containing flags of events to disable

References Assert, rtc_count_events::generate_event_on_compare, rtc_count_events::generate_event_on_overflow, and rtc_count_events::generate_event_on_periodic.

void rtc_count_enable ( struct rtc_module *const  module)

Enables the RTC module.

Enables the RTC module once it has been configured, ready for use. Most module configuration parameters cannot be altered while the module is enabled.

Parameters
[in,out]moduleRTC hardware module

References Assert, rtc_count_is_syncing(), system_interrupt_enable(), and SYSTEM_INTERRUPT_MODULE_RTC.

Referenced by run_asynchronous_event_test(), run_resynchronous_event_test(), and run_synchronous_event_test().

static void rtc_count_enable_events ( struct rtc_module *const  module,
struct rtc_count_events *const  events 
)
inlinestatic

Enables an RTC event output.

Enables one or more output events from the RTC module. See rtc_count_events for a list of events this module supports.

Note
Events cannot be altered while the module is enabled.
Parameters
[in,out]moduleRTC hardware module
[in]eventsStruct containing flags of events to enable

References Assert, rtc_count_events::generate_event_on_compare, rtc_count_events::generate_event_on_overflow, and rtc_count_events::generate_event_on_periodic.

Referenced by test_event_gen_user_init().

enum status_code rtc_count_frequency_correction ( struct rtc_module *const  module,
const int8_t  value 
)

Calibrate for too-slow or too-fast oscillator.

When used, the RTC will compensate for an inaccurate oscillator. The RTC module will add or subtract cycles from the RTC prescaler to adjust the frequency in approximately 1 PPM steps. The provided correction value should be between 0 and 127, allowing for a maximum 127 PPM correction.

If no correction is needed, set value to zero.

Note
Can only be used when the RTC is operated in 1Hz.
Parameters
[in,out]modulePointer to the software instance struct
[in]valueRanging from -127 to 127 used for the correction
Returns
Status of the calibration procedure.
Return values
STATUS_OKIf calibration was executed correctly
STATUS_ERR_INVALID_ARGIf invalid argument(s) were provided

References Assert, rtc_count_is_syncing(), STATUS_ERR_INVALID_ARG, and STATUS_OK.

enum status_code rtc_count_get_compare ( struct rtc_module *const  module,
uint32_t *const  comp_value,
const enum rtc_count_compare  comp_index 
)

Get the current compare value of specified compare.

Retrieves the current value of the specified compare.

Note
Compare 4 and 5 are only available in 16-bit mode.
Parameters
[in,out]modulePointer to the software instance struct
[out]comp_valuePointer to 32-bit integer that will be populated with the current compare value
[in]comp_indexIndex of compare to check
Returns
Status of the reading procedure.
Return values
STATUS_OKIf the value was read correctly
STATUS_ERR_INVALID_ARGIf invalid argument(s) were provided
STATUS_ERR_BAD_FORMATIf the module was not initialized in a mode

References Assert, rtc_count_is_syncing(), RTC_COUNT_MODE_16BIT, RTC_COUNT_MODE_32BIT, STATUS_ERR_BAD_FORMAT, STATUS_ERR_INVALID_ARG, and STATUS_OK.

static void rtc_count_get_config_defaults ( struct rtc_count_config *const  config)
inlinestatic

Gets the RTC default configurations.

Initializes the configuration structure to default values. This function should be called at the start of any RTC initialization.

The default configuration is:

  • Input clock divided by a factor of 1024
  • RTC in 32-bit mode
  • Clear on compare match off
  • Continuously sync count register off
  • No event source on
  • All compare values equal 0
  • Count read synchronization is enabled for SAM L22
Parameters
[out]configConfiguration structure to be initialized to default values

References Assert, rtc_count_config::clear_on_match, rtc_count_config::compare_values, rtc_count_config::continuously_update, rtc_count_config::enable_read_sync, rtc_count_config::mode, rtc_count_config::prescaler, RTC_COUNT_MODE_32BIT, and RTC_COUNT_PRESCALER_DIV_1024.

Referenced by test_event_gen_user_init().

uint32_t rtc_count_get_count ( struct rtc_module *const  module)

Get the current count value.

Parameters
[in,out]modulePointer to the software instance struct

Returns the current count value.

Returns
The current counter value as a 32-bit unsigned integer.

References Assert, rtc_count_is_syncing(), RTC_COUNT_MODE_16BIT, and RTC_COUNT_MODE_32BIT.

enum status_code rtc_count_get_period ( struct rtc_module *const  module,
uint16_t *const  period_value 
)

Retrieves the value of period.

Retrieves the value of the period for the 16-bit mode counter.

Note
Only available in 16-bit mode.
Parameters
[in,out]modulePointer to the software instance struct
[out]period_valuePointer to value for return argument
Returns
Status of getting the period value.
Return values
STATUS_OKIf the period value was read correctly
STATUS_ERR_UNSUPPORTED_DEVIf incorrect mode was set

References Assert, rtc_count_is_syncing(), RTC_COUNT_MODE_16BIT, STATUS_ERR_UNSUPPORTED_DEV, and STATUS_OK.

enum status_code rtc_count_init ( struct rtc_module *const  module,
Rtc *const  hw,
const struct rtc_count_config *const  config 
)

Initializes the RTC module with given configurations.

Initializes the module, setting up all given configurations to provide the desired functionality of the RTC.

Parameters
[out]modulePointer to the software instance struct
[in]hwPointer to hardware instance
[in]configPointer to the configuration structure
Returns
Status of the initialization procedure.
Return values
STATUS_OKIf the initialization was run stressfully
STATUS_ERR_INVALID_ARGIf invalid argument(s) were given

References _rtc_count_set_config(), Assert, rtc_count_config::mode, RTC_CLOCK_SOURCE, rtc_count_reset(), system_apb_clock_set_mask(), and SYSTEM_CLOCK_APB_APBA.

Referenced by test_event_gen_user_init().

bool rtc_count_is_compare_match ( struct rtc_module *const  module,
const enum rtc_count_compare  comp_index 
)

Check if RTC compare match has occurred.

Checks the compare flag to see if a match has occurred. The compare flag is set when there is a compare match between counter and the compare.

Note
Compare 4 and 5 are only available in 16-bit mode.
Parameters
[in,out]modulePointer to the software instance struct
[in]comp_indexIndex of compare to check current flag

References Assert, RTC_COUNT_MODE_16BIT, and RTC_COUNT_MODE_32BIT.

static bool rtc_count_is_overflow ( struct rtc_module *const  module)
inlinestatic

Check if an RTC overflow has occurred.

Checks the overflow flag in the RTC. The flag is set when there is an overflow in the clock.

Parameters
[in,out]moduleRTC hardware module
Returns
Overflow state of the RTC module.
Return values
trueIf the RTC count value has overflowed
falseIf the RTC count value has not overflowed

References Assert.

void rtc_count_reset ( struct rtc_module *const  module)

Resets the RTC module.

Resets the RTC to hardware defaults.

Parameters
[in,out]modulePointer to the software instance struct

References Assert, rtc_count_disable(), and rtc_count_is_syncing().

Referenced by rtc_count_init().

enum status_code rtc_count_set_compare ( struct rtc_module *const  module,
const uint32_t  comp_value,
const enum rtc_count_compare  comp_index 
)

Set the compare value for the specified compare.

Sets the value specified by the implementer to the requested compare.

Note
Compare 4 and 5 are only available in 16-bit mode.
Parameters
[in,out]modulePointer to the software instance struct
[in]comp_valueThe value to be written to the compare
[in]comp_indexIndex of the compare to set
Returns
Status indicating if compare was successfully set.
Return values
STATUS_OKIf compare was successfully set
STATUS_ERR_INVALID_ARGIf invalid argument(s) were provided
STATUS_ERR_BAD_FORMATIf the module was not initialized in a mode

References Assert, rtc_count_is_syncing(), RTC_COUNT_MODE_16BIT, RTC_COUNT_MODE_32BIT, STATUS_ERR_BAD_FORMAT, STATUS_ERR_INVALID_ARG, and STATUS_OK.

Referenced by _rtc_count_set_config().

enum status_code rtc_count_set_count ( struct rtc_module *const  module,
const uint32_t  count_value 
)

Set the current count value to desired value.

Sets the value of the counter to the specified value.

Parameters
[in,out]modulePointer to the software instance struct
[in]count_valueThe value to be set in count register
Returns
Status of setting the register.
Return values
STATUS_OKIf everything was executed correctly
STATUS_ERR_INVALID_ARGIf invalid argument(s) were provided

References Assert, rtc_count_is_syncing(), RTC_COUNT_MODE_16BIT, RTC_COUNT_MODE_32BIT, STATUS_ERR_INVALID_ARG, and STATUS_OK.

Referenced by cleanup_asynchronous_event_test(), cleanup_resynchronous_event_test(), and cleanup_synchronous_event_test().

enum status_code rtc_count_set_period ( struct rtc_module *const  module,
const uint16_t  period_value 
)

Set the given value to the period.

Sets the given value to the period.

Note
Only available in 16-bit mode.
Parameters
[in,out]modulePointer to the software instance struct
[in]period_valueThe value to set to the period
Returns
Status of setting the period value.
Return values
STATUS_OKIf the period was set correctly
STATUS_ERR_UNSUPPORTED_DEVIf module is not operated in 16-bit mode

References Assert, rtc_count_is_syncing(), RTC_COUNT_MODE_16BIT, STATUS_ERR_UNSUPPORTED_DEV, and STATUS_OK.

Referenced by run_asynchronous_event_test(), run_resynchronous_event_test(), and run_synchronous_event_test().