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.
Data Structures | |
struct | rtc_count_config |
RTC Count configuration structure. More... | |
struct | rtc_count_events |
RTC Count event enable/disable structure. 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... | |
Callbacks | |
enum status_code | rtc_count_register_callback (struct rtc_module *const module, rtc_count_callback_t callback, enum rtc_count_callback callback_type) |
Registers callback for the specified callback type. More... | |
enum status_code | rtc_count_unregister_callback (struct rtc_module *const module, enum rtc_count_callback callback_type) |
Unregisters callback for the specified callback type. More... | |
void | rtc_count_enable_callback (struct rtc_module *const module, enum rtc_count_callback callback_type) |
Enables callback. More... | |
void | rtc_count_disable_callback (struct rtc_module *const module, enum rtc_count_callback callback_type) |
Disables callback. More... | |
#define FEATURE_RTC_CONTINUOUSLY_UPDATED |
RTC continuously updated.
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 |
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 | ||
) |
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.
[in,out] | module | Pointer to the software instance struct |
[in] | comp_index | Index of compare to check current flag |
STATUS_OK | If flag was successfully cleared |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
STATUS_ERR_BAD_FORMAT | If 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.
Referenced by 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().
void rtc_count_disable | ( | struct rtc_module *const | module | ) |
Disables the RTC module.
Disables the RTC module.
[in,out] | module | RTC hardware module |
References Assert, rtc_count_is_syncing(), system_interrupt_disable(), and SYSTEM_INTERRUPT_MODULE_RTC.
Referenced by rtc_count_reset(), run_rtc_count_interrupt_test(), and run_rtc_count_test().
void rtc_count_disable_callback | ( | struct rtc_module *const | module, |
enum rtc_count_callback | callback_type | ||
) |
Disables callback.
Disables the callback specified by the callback_type.
[in,out] | module | Pointer to the software instance struct |
[in] | callback_type | Callback type to disable |
References Assert, and RTC_COUNT_CALLBACK_OVERFLOW.
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, 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.
[in,out] | module | RTC hardware module |
References Assert, rtc_count_is_syncing(), system_interrupt_enable(), and SYSTEM_INTERRUPT_MODULE_RTC.
Referenced by run_rtc_count_interrupt_test(), and run_rtc_count_test().
void rtc_count_enable_callback | ( | struct rtc_module *const | module, |
enum rtc_count_callback | callback_type | ||
) |
Enables callback.
Enables the callback specified by the callback_type.
[in,out] | module | Pointer to the software instance struct |
[in] | callback_type | Callback type to enable |
References Assert, and RTC_COUNT_CALLBACK_OVERFLOW.
Referenced by 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, and rtc_count_events::generate_event_on_periodic.
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.
[in,out] | module | Pointer to the software instance struct |
[in] | value | Ranging from -127 to 127 used for the correction |
STATUS_OK | If calibration was executed correctly |
STATUS_ERR_INVALID_ARG | If 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.
[in,out] | module | Pointer to the software instance struct |
[out] | comp_value | Pointer to 32-bit integer that will be populated with the current compare value |
[in] | comp_index | Index of compare to check |
STATUS_OK | If the value was read correctly |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
STATUS_ERR_BAD_FORMAT | If 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.
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, 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 run_rtc_count_test().
uint32_t rtc_count_get_count | ( | struct rtc_module *const | module | ) |
Get the current count value.
[in,out] | module | Pointer to the software instance struct |
Returns the current count value.
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.
[in,out] | module | Pointer to the software instance struct |
[out] | period_value | Pointer to value for return argument |
STATUS_OK | If the period value was read correctly |
STATUS_ERR_UNSUPPORTED_DEV | If incorrect mode was set |
References Assert, RTC_COUNT_MODE_16BIT, STATUS_ERR_UNSUPPORTED_DEV, and STATUS_OK.
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 | ||
) |
Initializes the RTC module with given configurations.
Initializes the module, setting up all given configurations to provide the desired functionality of the RTC.
[out] | module | Pointer to the software instance struct |
[in] | hw | Pointer to hardware instance |
[in] | config | Pointer to the configuration structure |
STATUS_OK | If the initialization was run stressfully |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were given |
References _rtc_count_set_config(), _rtc_instance, Assert, rtc_count_config::continuously_update, GCLK_GENERATOR_2, rtc_count_config::mode, rtc_count_reset(), system_gclk_chan_config::source_generator, system_apb_clock_set_mask(), SYSTEM_CLOCK_APB_APBA, system_gclk_chan_enable(), system_gclk_chan_get_config_defaults(), and system_gclk_chan_set_config().
Referenced by run_rtc_count_test().
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.
[in,out] | module | Pointer to the software instance struct |
[in] | comp_index | Index of compare to check current flag |
References Assert, RTC_COUNT_MODE_16BIT, and RTC_COUNT_MODE_32BIT.
Referenced by 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().
enum status_code rtc_count_register_callback | ( | struct rtc_module *const | module, |
rtc_count_callback_t | callback, | ||
enum rtc_count_callback | callback_type | ||
) |
Registers callback for the specified callback type.
Associates the given callback function with the specified callback type. To enable the callback, the rtc_count_enable_callback function must be used.
[in,out] | module | Pointer to the software instance struct |
[in] | callback | Pointer to the function desired for the specified callback |
[in] | callback_type | Callback type to register |
STATUS_OK | Registering was done successfully |
STATUS_ERR_INVALID_ARG | If trying to register a callback not available |
References RTC_COUNT_CALLBACK_OVERFLOW, RTC_COUNT_MODE_16BIT, RTC_COUNT_MODE_32BIT, STATUS_ERR_INVALID_ARG, and STATUS_OK.
Referenced by run_rtc_count_interrupt_test().
void rtc_count_reset | ( | struct rtc_module *const | module | ) |
Resets the RTC module.
Resets the RTC to hardware defaults.
[in,out] | module | Pointer 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.
[in,out] | module | Pointer to the software instance struct |
[in] | comp_value | The value to be written to the compare |
[in] | comp_index | Index of the compare to set |
STATUS_OK | If compare was successfully set |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
STATUS_ERR_BAD_FORMAT | If 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(), 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 | ||
) |
Set the current count value to desired value.
Sets the value of the counter to the specified value.
[in,out] | module | Pointer to the software instance struct |
[in] | count_value | The value to be set in count register |
STATUS_OK | If everything was executed correctly |
STATUS_ERR_INVALID_ARG | If 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 run_rtc_count_interrupt_test(), and run_rtc_count_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.
[in,out] | module | Pointer to the software instance struct |
[in] | period_value | The value to set to the period |
STATUS_OK | If the period was set correctly |
STATUS_ERR_UNSUPPORTED_DEV | If 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_rtc_count_test().
enum status_code rtc_count_unregister_callback | ( | struct rtc_module *const | module, |
enum rtc_count_callback | callback_type | ||
) |
Unregisters callback for the specified callback type.
When called, the currently registered callback for the given callback type will be removed.
[in,out] | module | Pointer to the software instance struct |
[in] | callback_type | Specifies the callback type to unregister |
STATUS_OK | Unregistering was done successfully |
STATUS_ERR_INVALID_ARG | If trying to unregister a callback not available |
References RTC_COUNT_CALLBACK_OVERFLOW, RTC_COUNT_MODE_16BIT, RTC_COUNT_MODE_32BIT, STATUS_ERR_INVALID_ARG, and STATUS_OK.
Referenced by run_rtc_count_interrupt_test().