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 Calendar operating mode, for the configuration and retrieval of the current time and date as maintained by the RTC module.
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 Calendar mode. This allows for an easy integration of a real time clock and calendar into a user application to track the passing of time and/or perform scheduled tasks.
Whilst operating in Calendar 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 has up to four independent hardware alarms that can be configured by the user application. These alarms will be triggered on match with the current clock value, and can be set up to trigger an interrupt, event, or both. The RTC can also be configured to clear the clock value on alarm match, resetting the clock to the original start time.
If the RTC is operated in clock-only mode (i.e. with calendar disabled), the RTC counter value will instead be cleared on overflow once the maximum count value has been reached:
\[ COUNT_{MAX} = 2^{32}-1 \]
When the RTC is operated with the calendar enabled and run using a nominal 1Hz input clock frequency, a register overflow will occur after 64 years.
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 set up in the RTC module configuration. For the RTC to operate correctly in calendar mode, this frequency must be 1KHz, while the RTC's internal prescaler should be set to divide by 1024. The n parameter is the event source generator index of the RTC module. If the asynchronous clock is operated at the recommended 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 Generic Clock (GCLK) cycles. The adjustment is applied the specified number of time (maximum 127) over 976 of these periods. The corresponding correction in parts per million (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.
See RTC Tamper Detect.
The RTC module has a year range of 63 years from the starting year configured when the module is initialized. Dates outside the start to end year range described below will need software adjustment:
\[ [YEAR_{START}, YEAR_{START}+64] \]
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 Resistor/Capacitor (RC)-oscillator with a prescaler of 32, giving a resulting clock frequency of 1024Hz to the RTC. When the internal RTC prescaler is set to 1024, this yields an end-frequency of 1Hz for correct time keeping operations.
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. 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.
asfdoc_sam0_rtc_calendar_rtc_clk lists the available RTC clock.
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 3KkHz 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 (CAL) Driver. This includes:
For a list of examples related to this driver, see Examples for RTC CAL Driver.
Data Structures | |
struct | rtc_calendar_alarm_time |
Alarm structure. More... | |
struct | rtc_calendar_config |
RTC configuration structure. More... | |
struct | rtc_calendar_events |
RTC Calendar event enable/disable structure. More... | |
struct | rtc_calendar_time |
Time structure. More... | |
Enumerations | |
enum | rtc_calendar_alarm { RTC_CALENDAR_ALARM_0 = 0, RTC_CALENDAR_ALARM_1 = 1, RTC_CALENDAR_ALARM_2 = 2, RTC_CALENDAR_ALARM_3 = 3 } |
Available alarm channels. More... | |
enum | rtc_calendar_alarm_mask { RTC_CALENDAR_ALARM_MASK_DISABLED = RTC_MODE2_MASK_SEL_OFF, RTC_CALENDAR_ALARM_MASK_SEC = RTC_MODE2_MASK_SEL_SS, RTC_CALENDAR_ALARM_MASK_MIN = RTC_MODE2_MASK_SEL_MMSS, RTC_CALENDAR_ALARM_MASK_HOUR = RTC_MODE2_MASK_SEL_HHMMSS, RTC_CALENDAR_ALARM_MASK_DAY = RTC_MODE2_MASK_SEL_DDHHMMSS, RTC_CALENDAR_ALARM_MASK_MONTH = RTC_MODE2_MASK_SEL_MMDDHHMMSS, RTC_CALENDAR_ALARM_MASK_YEAR = RTC_MODE2_MASK_SEL_YYMMDDHHMMSS } |
Available mask options for alarms. More... | |
enum | rtc_calendar_prescaler { RTC_CALENDAR_PRESCALER_DIV_1 = RTC_MODE2_CTRL_PRESCALER_DIV1, RTC_CALENDAR_PRESCALER_DIV_2 = RTC_MODE2_CTRL_PRESCALER_DIV2, RTC_CALENDAR_PRESCALER_DIV_4 = RTC_MODE2_CTRL_PRESCALER_DIV4, RTC_CALENDAR_PRESCALER_DIV_8 = RTC_MODE2_CTRL_PRESCALER_DIV8, RTC_CALENDAR_PRESCALER_DIV_16 = RTC_MODE2_CTRL_PRESCALER_DIV16, RTC_CALENDAR_PRESCALER_DIV_32 = RTC_MODE2_CTRL_PRESCALER_DIV32, RTC_CALENDAR_PRESCALER_DIV_64 = RTC_MODE2_CTRL_PRESCALER_DIV64, RTC_CALENDAR_PRESCALER_DIV_128 = RTC_MODE2_CTRL_PRESCALER_DIV128, RTC_CALENDAR_PRESCALER_DIV_256 = RTC_MODE2_CTRL_PRESCALER_DIV256, RTC_CALENDAR_PRESCALER_DIV_512 = RTC_MODE2_CTRL_PRESCALER_DIV512, RTC_CALENDAR_PRESCALER_DIV_1024 = RTC_MODE2_CTRL_PRESCALER_DIV1024 } |
RTC input clock prescaler settings. More... | |
#define | FEATURE_RTC_CONTINUOUSLY_UPDATED |
Define port features set according to different device family. More... | |
Configuration and Initialization | |
static void | rtc_calendar_get_time_defaults (struct rtc_calendar_time *const time) |
Initialize a time structure. More... | |
static void | rtc_calendar_get_config_defaults (struct rtc_calendar_config *const config) |
Gets the RTC default settings. More... | |
void | rtc_calendar_reset (struct rtc_module *const module) |
Resets the RTC module. More... | |
void | rtc_calendar_enable (struct rtc_module *const module) |
Enables the RTC module. More... | |
void | rtc_calendar_disable (struct rtc_module *const module) |
Disables the RTC module. More... | |
void | rtc_calendar_init (struct rtc_module *const module, Rtc *const hw, const struct rtc_calendar_config *const config) |
Initializes the RTC module with given configurations. More... | |
void | rtc_calendar_swap_time_mode (struct rtc_module *const module) |
Swaps between 12h and 24h clock mode. More... | |
enum status_code | rtc_calendar_frequency_correction (struct rtc_module *const module, const int8_t value) |
Calibrate for too-slow or too-fast oscillator. More... | |
Time and Alarm Management | |
uint32_t | rtc_calendar_time_to_register_value (struct rtc_module *const module, const struct rtc_calendar_time *const time) |
Convert time structure to register_value. More... | |
void | rtc_calendar_register_value_to_time (struct rtc_module *const module, const uint32_t register_value, struct rtc_calendar_time *const time) |
Convert register_value to time structure. More... | |
void | rtc_calendar_set_time (struct rtc_module *const module, const struct rtc_calendar_time *const time) |
Set the current calendar time to desired time. More... | |
void | rtc_calendar_get_time (struct rtc_module *const module, struct rtc_calendar_time *const time) |
Get the current calendar value. More... | |
enum status_code | rtc_calendar_set_alarm (struct rtc_module *const module, const struct rtc_calendar_alarm_time *const alarm, const enum rtc_calendar_alarm alarm_index) |
Set the alarm time for the specified alarm. More... | |
enum status_code | rtc_calendar_get_alarm (struct rtc_module *const module, struct rtc_calendar_alarm_time *const alarm, const enum rtc_calendar_alarm alarm_index) |
Get the current alarm time of specified alarm. More... | |
Status Flag Management | |
static bool | rtc_calendar_is_overflow (struct rtc_module *const module) |
Check if an RTC overflow has occurred. More... | |
static void | rtc_calendar_clear_overflow (struct rtc_module *const module) |
Clears the RTC overflow flag. More... | |
static bool | rtc_calendar_is_alarm_match (struct rtc_module *const module, const enum rtc_calendar_alarm alarm_index) |
Check the RTC alarm flag. More... | |
static enum status_code | rtc_calendar_clear_alarm_match (struct rtc_module *const module, const enum rtc_calendar_alarm alarm_index) |
Clears the RTC alarm match flag. More... | |
Event Management | |
static void | rtc_calendar_enable_events (struct rtc_module *const module, struct rtc_calendar_events *const events) |
Enables an RTC event output. More... | |
static void | rtc_calendar_disable_events (struct rtc_module *const module, struct rtc_calendar_events *const events) |
Disables an RTC event output. More... | |
#define FEATURE_RTC_CONTINUOUSLY_UPDATED |
Define port features set according to different device family.
RTC continuously updated.
enum rtc_calendar_alarm |
Available mask options for alarms.
Available mask options for alarms.
RTC input clock prescaler settings.
The available input clock prescaler values for the RTC calendar module.
|
inlinestatic |
Clears the RTC alarm match flag.
Clear the requested alarm match flag, so that future alarm matches can be determined.
[in,out] | module | Pointer to the software instance struct |
[in] | alarm_index | The index of the alarm match to clear |
STATUS_OK | If flag was cleared correctly |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
References Assert, STATUS_ERR_INVALID_ARG, and STATUS_OK.
|
inlinestatic |
Clears the RTC overflow flag.
[in,out] | module | Pointer to the software instance struct |
Clears the RTC module counter overflow flag, so that new overflow conditions can be detected.
References Assert.
void rtc_calendar_disable | ( | struct rtc_module *const | module | ) |
Disables the RTC module.
Disables the RTC module.
[in,out] | module | Pointer to the software instance struct |
References Assert, rtc_calendar_is_syncing(), system_interrupt_disable(), and SYSTEM_INTERRUPT_MODULE_RTC.
Referenced by rtc_calendar_reset(), and rtc_calendar_swap_time_mode().
|
inlinestatic |
Disables an RTC event output.
Disabled one or more output events from the RTC module. See rtc_calendar_events for a list of events this module supports.
[in,out] | module | Pointer to the software instance struct |
[in] | events | Struct containing flags of events to disable |
References Assert, rtc_calendar_events::generate_event_on_alarm, rtc_calendar_events::generate_event_on_overflow, and rtc_calendar_events::generate_event_on_periodic.
void rtc_calendar_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 | Pointer to the software instance struct |
References Assert, rtc_calendar_is_syncing(), system_interrupt_enable(), and SYSTEM_INTERRUPT_MODULE_RTC.
Referenced by rtc_calendar_swap_time_mode().
|
inlinestatic |
Enables an RTC event output.
Enables one or more output events from the RTC module. See rtc_calendar_events for a list of events this module supports.
[in,out] | module | Pointer to the software instance struct |
[in] | events | Struct containing flags of events to enable |
References Assert, rtc_calendar_events::generate_event_on_alarm, rtc_calendar_events::generate_event_on_overflow, and rtc_calendar_events::generate_event_on_periodic.
enum status_code rtc_calendar_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 in either direction.
If no correction is needed, set value to zero.
[in,out] | module | Pointer to the software instance struct |
[in] | value | Between -127 and 127 used for the correction |
STATUS_OK | If calibration was done correctly |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
References Assert, rtc_calendar_is_syncing(), STATUS_ERR_INVALID_ARG, and STATUS_OK.
enum status_code rtc_calendar_get_alarm | ( | struct rtc_module *const | module, |
struct rtc_calendar_alarm_time *const | alarm, | ||
const enum rtc_calendar_alarm | alarm_index | ||
) |
Get the current alarm time of specified alarm.
Retrieves the current alarm time for the specified alarm.
[in,out] | module | Pointer to the software instance struct |
[out] | alarm | Pointer to the struct that will be filled with alarm time and mask of the specified alarm. |
[in] | alarm_index | Index of alarm to get alarm time from |
STATUS_OK | If alarm was read correctly |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
References Assert, rtc_calendar_alarm_time::mask, rtc_calendar_register_value_to_time(), STATUS_ERR_INVALID_ARG, STATUS_OK, and rtc_calendar_alarm_time::time.
Referenced by rtc_calendar_swap_time_mode().
|
inlinestatic |
Gets the RTC default settings.
Initializes the configuration structure to the known default values. This function should be called at the start of any RTC initiation.
The default configuration is as follows:
[out] | config | Configuration structure to be initialized to default values |
References rtc_calendar_config::alarm, Assert, rtc_calendar_config::clear_on_match, rtc_calendar_config::clock_24h, rtc_calendar_config::continuously_update, rtc_calendar_config::enable_read_sync, rtc_calendar_alarm_time::mask, rtc_calendar_config::prescaler, RTC_CALENDAR_ALARM_MASK_YEAR, rtc_calendar_get_time_defaults(), RTC_CALENDAR_PRESCALER_DIV_1024, rtc_calendar_alarm_time::time, and rtc_calendar_config::year_init_value.
void rtc_calendar_get_time | ( | struct rtc_module *const | module, |
struct rtc_calendar_time *const | time | ||
) |
Get the current calendar value.
Retrieves the current time of the calendar.
[in,out] | module | Pointer to the software instance struct |
[out] | time | Pointer to value that will be filled with current time |
References Assert, rtc_calendar_is_syncing(), and rtc_calendar_register_value_to_time().
Referenced by get_fattime(), and rtc_calendar_swap_time_mode().
|
inlinestatic |
Initialize a time
structure.
This will initialize a given time structure to the time 00:00:00 (hh:mm:ss) and date 2000-01-01 (YYYY-MM-DD).
[out] | time | Time structure to initialize |
References rtc_calendar_time::day, rtc_calendar_time::hour, rtc_calendar_time::minute, rtc_calendar_time::month, rtc_calendar_time::pm, rtc_calendar_time::second, and rtc_calendar_time::year.
Referenced by rtc_calendar_get_config_defaults().
void rtc_calendar_init | ( | struct rtc_module *const | module, |
Rtc *const | hw, | ||
const struct rtc_calendar_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 |
References _rtc_calendar_set_config(), Assert, rtc_calendar_config::clock_24h, rtc_calendar_reset(), RTC_CLOCK_SOURCE, system_apb_clock_set_mask(), SYSTEM_CLOCK_APB_APBA, and rtc_calendar_config::year_init_value.
|
inlinestatic |
Check the RTC alarm flag.
Check if the specified alarm flag is set. The flag is set when there is a compare match between the alarm value and the clock.
[in,out] | module | Pointer to the software instance struct |
[in] | alarm_index | Index of the alarm to check |
true | If the specified alarm has matched the current time |
false | If the specified alarm has not matched the current time |
References Assert.
|
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 | Pointer to the software instance struct |
true | If the RTC count value has overflowed |
false | If the RTC count value has not overflowed |
References Assert.
void rtc_calendar_register_value_to_time | ( | struct rtc_module *const | module, |
const uint32_t | register_value, | ||
struct rtc_calendar_time *const | time | ||
) |
Convert register_value to time structure.
Retrieves the time structure convert by register_value.
[in,out] | module | Pointer to the software instance struct |
[in] | register_value | The value stored in register |
[out] | time | Pointer to the time structure |
References rtc_calendar_time::day, rtc_calendar_time::hour, rtc_calendar_time::minute, rtc_calendar_time::month, rtc_calendar_time::pm, rtc_calendar_time::second, and rtc_calendar_time::year.
Referenced by rtc_calendar_get_alarm(), and rtc_calendar_get_time().
void rtc_calendar_reset | ( | struct rtc_module *const | module | ) |
Resets the RTC module.
Resets the RTC module to hardware defaults.
[in,out] | module | Pointer to the software instance struct |
References Assert, rtc_calendar_disable(), and rtc_calendar_is_syncing().
Referenced by rtc_calendar_init().
enum status_code rtc_calendar_set_alarm | ( | struct rtc_module *const | module, |
const struct rtc_calendar_alarm_time *const | alarm, | ||
const enum rtc_calendar_alarm | alarm_index | ||
) |
Set the alarm time for the specified alarm.
Sets the time and mask specified to the requested alarm.
[in,out] | module | Pointer to the software instance struct |
[in] | alarm | The alarm struct to set the alarm with |
[in] | alarm_index | The index of the alarm to set |
STATUS_OK | If alarm was set correctly |
STATUS_ERR_INVALID_ARG | If invalid argument(s) were provided |
References Assert, rtc_calendar_alarm_time::mask, rtc_calendar_is_syncing(), rtc_calendar_time_to_register_value(), STATUS_ERR_INVALID_ARG, STATUS_OK, and rtc_calendar_alarm_time::time.
Referenced by _rtc_calendar_set_config(), and rtc_calendar_swap_time_mode().
void rtc_calendar_set_time | ( | struct rtc_module *const | module, |
const struct rtc_calendar_time *const | time | ||
) |
Set the current calendar time to desired time.
Sets the time provided to the calendar.
[in,out] | module | Pointer to the software instance struct |
[in] | time | The time to set in the calendar |
References Assert, rtc_calendar_is_syncing(), and rtc_calendar_time_to_register_value().
Referenced by rtc_calendar_swap_time_mode().
void rtc_calendar_swap_time_mode | ( | struct rtc_module *const | module | ) |
Swaps between 12h and 24h clock mode.
Swaps the current RTC time mode.
[in,out] | module | Pointer to the software instance struct |
References Assert, rtc_calendar_time::hour, rtc_calendar_time::pm, rtc_calendar_disable(), rtc_calendar_enable(), rtc_calendar_get_alarm(), rtc_calendar_get_time(), rtc_calendar_set_alarm(), rtc_calendar_set_time(), and rtc_calendar_alarm_time::time.
uint32_t rtc_calendar_time_to_register_value | ( | struct rtc_module *const | module, |
const struct rtc_calendar_time *const | time | ||
) |
Convert time structure to register_value.
Retrieves register_value convert by the time structure.
[in,out] | module | Pointer to the software instance struct |
[in] | time | Pointer to the time structure |
References rtc_calendar_time::day, rtc_calendar_time::hour, rtc_calendar_time::minute, rtc_calendar_time::month, rtc_calendar_time::pm, rtc_calendar_time::second, and rtc_calendar_time::year.
Referenced by rtc_calendar_set_alarm(), and rtc_calendar_set_time().