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:
There are no prerequisites for this module.
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 | 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 |
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.
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.
n | Periodic event |
---|---|
7 | 1Hz |
6 | 2Hz |
5 | 4Hz |
4 | 8Hz |
3 | 16Hz |
2 | 32Hz |
1 | 64Hz |
0 | 128Hz |
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.
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.
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.
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 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 |
For extra information, see Extra Information for RTC COUNT Driver. This includes:
For a list of examples related to this driver, see Examples for RTC (COUNT) Driver.
Modules | |
Related Project(s) | |
In this section you can find all the projects related to the SAM RTC Count (RTC COUNT) Driver. | |
Data Structures | |
struct | rtc_count_config |
RTC Count configuration structure. More... | |
struct | rtc_count_events |
RTC Count event enable/disable structure. More... | |
Functions | |
uint32_t | rtc_tamper_get_stamp (struct rtc_module *const module) |
Get the tamper stamp value. More... | |
Driver Feature Definition | |
Define port features set according to different device family. | |
#define | FEATURE_RTC_PERIODIC_INT |
RTC periodic interval interrupt. More... | |
#define | FEATURE_RTC_PRESCALER_OFF |
RTC prescaler is off. More... | |
#define | FEATURE_RTC_CLOCK_SELECTION |
RTC clock selection. More... | |
#define | FEATURE_RTC_GENERAL_PURPOSE_REG |
General purpose registers. More... | |
#define | FEATURE_RTC_TAMPER_DETECTION |
RTC tamper detection. 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) |
void | rtc_count_enable (struct rtc_module *const module) |
void | rtc_count_disable (struct rtc_module *const module) |
enum status_code | rtc_count_init (struct rtc_module *const module, Rtc *const hw, const struct rtc_count_config *const config) |
enum status_code | rtc_count_frequency_correction (struct rtc_module *const module, const int8_t value) |
Count and Compare Value Management | |
enum status_code | rtc_count_set_count (struct rtc_module *const module, const uint32_t count_value) |
uint32_t | rtc_count_get_count (struct rtc_module *const module) |
enum status_code | rtc_count_set_compare (struct rtc_module *const module, const uint32_t comp_value, const enum rtc_count_compare comp_index) |
enum status_code | rtc_count_get_compare (struct rtc_module *const module, uint32_t *const comp_value, const enum rtc_count_compare comp_index) |
enum status_code | rtc_count_set_period (struct rtc_module *const module, uint16_t period_value) |
enum status_code | rtc_count_get_period (struct rtc_module *const module, uint16_t *const period_value) |
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... | |
static bool | rtc_count_is_periodic_interval (struct rtc_module *const module, enum rtc_count_periodic_interval n) |
Check if an RTC periodic interval interrupt has occurred. More... | |
static void | rtc_count_clear_periodic_interval (struct rtc_module *const module, enum rtc_count_periodic_interval n) |
Clears the RTC periodic interval flag. More... | |
bool | rtc_count_is_compare_match (struct rtc_module *const module, const enum rtc_count_compare comp_index) |
enum status_code | rtc_count_clear_compare_match (struct rtc_module *const module, const enum rtc_count_compare comp_index) |
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... | |
RTC General Purpose Registers | |
static void | rtc_write_general_purpose_reg (struct rtc_module *const module, const uint8_t index, uint32_t value) |
Write a value into general purpose register. More... | |
static uint32_t | rtc_read_general_purpose_reg (struct rtc_module *const module, const uint8_t index) |
Read the value from general purpose register. More... | |
Callbacks | |
enum status_code | rtc_count_register_callback (struct rtc_module *const module, rtc_count_callback_t callback, enum rtc_count_callback callback_type) |
enum status_code | rtc_count_unregister_callback (struct rtc_module *const module, enum rtc_count_callback callback_type) |
void | rtc_count_enable_callback (struct rtc_module *const module, enum rtc_count_callback callback_type) |
void | rtc_count_disable_callback (struct rtc_module *const module, enum rtc_count_callback callback_type) |
#define FEATURE_RTC_CLOCK_SELECTION |
RTC clock selection.
#define FEATURE_RTC_GENERAL_PURPOSE_REG |
General purpose registers.
#define FEATURE_RTC_PERIODIC_INT |
RTC periodic interval interrupt.
#define FEATURE_RTC_PRESCALER_OFF |
RTC prescaler is off.
#define FEATURE_RTC_TAMPER_DETECTION |
RTC tamper detection.
enum rtc_clock_sel |
Available clock source for RTC.
RTC clock source.
enum rtc_count_callback |
Callback types.
The available callback types for the RTC count module.
enum rtc_count_compare |
Available compare channels.
enum rtc_count_mode |
Available periodic interval source.
enum rtc_count_prescaler |
RTC input clock prescaler settings.
The available input clock prescaler values for the RTC count module.
enum status_code rtc_count_clear_compare_match | ( | struct rtc_module *const | module, |
const enum rtc_count_compare | comp_index | ||
) |
Referenced by main(), run_rtc_count_interrupt_test(), and run_rtc_count_test().
|
inlinestatic |
Clears the RTC overflow flag.
Clears the RTC module counter overflow flag, so that new overflow conditions can be detected.
[in,out] | module | RTC hardware module |
References Assert.
Referenced by run_rtc_count_test().
|
inlinestatic |
Clears the RTC periodic interval flag.
Clears the RTC module counter periodic interval flag, so that new periodic interval conditions can be detected.
[in,out] | module | RTC hardware module |
[in] | n | RTC periodic interval interrupt |
References Assert.
void rtc_count_disable | ( | struct rtc_module *const | module | ) |
void rtc_count_disable_callback | ( | struct rtc_module *const | module, |
enum rtc_count_callback | callback_type | ||
) |
Referenced by run_rtc_count_interrupt_test().
|
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.
[in,out] | module | RTC hardware module |
[in] | events | Struct containing flags of events to disable |
References Assert, rtc_count_events::generate_event_on_compare, rtc_count_events::generate_event_on_overflow, rtc_count_events::generate_event_on_periodic, rtc_count_events::generate_event_on_tamper, i, and rtc_count_events::on_event_to_tamper.
void rtc_count_enable | ( | struct rtc_module *const | module | ) |
void rtc_count_enable_callback | ( | struct rtc_module *const | module, |
enum rtc_count_callback | callback_type | ||
) |
Referenced by configure_rtc_callbacks(), and run_rtc_count_interrupt_test().
|
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.
[in,out] | module | RTC hardware module |
[in] | events | Struct containing flags of events to enable |
References Assert, rtc_count_events::generate_event_on_compare, rtc_count_events::generate_event_on_overflow, rtc_count_events::generate_event_on_periodic, rtc_count_events::generate_event_on_tamper, i, and rtc_count_events::on_event_to_tamper.
Referenced by rtc_setup(), and test_event_gen_user_init().
enum status_code rtc_count_frequency_correction | ( | struct rtc_module *const | module, |
const int8_t | value | ||
) |
enum status_code rtc_count_get_compare | ( | struct rtc_module *const | module, |
uint32_t *const | comp_value, | ||
const enum rtc_count_compare | comp_index | ||
) |
Referenced by run_rtc_count_test().
|
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:
[out] | config | Configuration structure to be initialized to default values |
References Assert, rtc_count_config::clear_on_match, rtc_count_config::compare_values, i, rtc_count_config::mode, rtc_count_config::prescaler, RTC_COUNT_MODE_32BIT, and RTC_COUNT_PRESCALER_DIV_1024.
Referenced by configure_rtc_count(), rtc_setup(), run_rtc_count_test(), and test_event_gen_user_init().
uint32_t rtc_count_get_count | ( | struct rtc_module *const | module | ) |
enum status_code rtc_count_get_period | ( | struct rtc_module *const | module, |
uint16_t *const | period_value | ||
) |
Referenced by run_rtc_count_test().
enum status_code rtc_count_init | ( | struct rtc_module *const | module, |
Rtc *const | hw, | ||
const struct rtc_count_config *const | config | ||
) |
Referenced by configure_rtc_count(), rtc_setup(), run_rtc_count_test(), and test_event_gen_user_init().
bool rtc_count_is_compare_match | ( | struct rtc_module *const | module, |
const enum rtc_count_compare | comp_index | ||
) |
Referenced by main(), and run_rtc_count_test().
|
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.
[in,out] | module | RTC hardware module |
true | If the RTC count value has overflowed |
false | If the RTC count value has not overflowed |
References Assert.
Referenced by run_rtc_count_test().
|
inlinestatic |
Check if an RTC periodic interval interrupt has occurred.
Checks the periodic interval flag in the RTC.
[in,out] | module | RTC hardware module |
[in] | n | RTC periodic interval interrupt |
true | RTC periodic interval interrupt occurs |
false | RTC periodic interval interrupt doesn't occur |
References Assert.
enum status_code rtc_count_register_callback | ( | struct rtc_module *const | module, |
rtc_count_callback_t | callback, | ||
enum rtc_count_callback | callback_type | ||
) |
Referenced by configure_rtc_callbacks(), and run_rtc_count_interrupt_test().
void rtc_count_reset | ( | struct rtc_module *const | module | ) |
enum status_code rtc_count_set_compare | ( | struct rtc_module *const | module, |
const uint32_t | comp_value, | ||
const enum rtc_count_compare | comp_index | ||
) |
Referenced by run_rtc_count_interrupt_test(), and run_rtc_count_test().
enum status_code rtc_count_set_count | ( | struct rtc_module *const | module, |
const uint32_t | count_value | ||
) |
enum status_code rtc_count_set_period | ( | struct rtc_module *const | module, |
uint16_t | period_value | ||
) |
enum status_code rtc_count_unregister_callback | ( | struct rtc_module *const | module, |
enum rtc_count_callback | callback_type | ||
) |
Referenced by run_rtc_count_interrupt_test().
|
inlinestatic |
Read the value from general purpose register.
[in] | module | Pointer to the software instance struct |
[in] | index | General purpose register index (0..3) |
References Assert.
uint32_t rtc_tamper_get_stamp | ( | struct rtc_module *const | module | ) |
Get the tamper stamp value.
[in,out] | module | Pointer to the software instance struct |
|
inlinestatic |
Write a value into general purpose register.
[in] | module | Pointer to the software instance struct |
[in] | n | General purpose type |
[in] | index | General purpose register index (0..3) |
References Assert.