Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Real Time Counter (RTC)

See Quick start guide for RTC driver.

This is a driver implementation for the XMEGA RTC.

Minimum allowed alarm time

If current time is close to a time unit roll over, there is a risk to miss this when using a value of 0.

A safe use of this can be in an alarm callback.

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the Real Time Counter (RTC).
 

Macros

#define CONFIG_RTC_COMPARE_INT_LEVEL
 Configuration symbol for interrupt level to use on alarm. More...
 
#define CONFIG_RTC_OVERFLOW_INT_LEVEL
 Configuration symbol for interrupt level to use on overflow. More...
 
#define CONFIG_RTC_PRESCALER
 Configuration symbol for prescaler to use. More...
 

Typedefs

typedef void(* rtc_callback_t )(uint32_t time)
 Callback definition for alarm callback. More...
 

Functions

bool rtc_alarm_has_triggered (void)
 Check if pending alarm has triggered. More...
 
uint32_t rtc_get_time (void)
 Get current time. More...
 
void rtc_init (void)
 Initialize the 32kHz oscillator and RTC32. More...
 
void rtc_set_alarm (uint32_t time)
 Set alarm time. More...
 
static void rtc_set_alarm_relative (uint32_t offset)
 Set alarm relative to current time. More...
 
void rtc_set_callback (rtc_callback_t callback)
 Set callback to call on alarm. More...
 
void rtc_set_time (uint32_t time)
 Set current time. More...
 

#define CONFIG_RTC_COMPARE_INT_LEVEL

Configuration symbol for interrupt level to use on alarm.

Possible values:

  • RTC_COMPINTLVL_LO_gc
  • RTC_COMPINTLVL_MED_gc
  • RTC_COMPINTLVL_HI_gc
#define CONFIG_RTC_OVERFLOW_INT_LEVEL

Configuration symbol for interrupt level to use on overflow.

Possible values:

  • RTC_OVFINTLVL_LO_gc
  • RTC_OVFINTLVL_MED_gc
  • RTC_OVFINTLVL_HI_gc
#define CONFIG_RTC_PRESCALER

Configuration symbol for prescaler to use.

Possible values:

  • RTC_PRESCALER_DIV1_gc
  • RTC_PRESCALER_DIV2_gc
  • RTC_PRESCALER_DIV8_gc
  • RTC_PRESCALER_DIV16_gc
  • RTC_PRESCALER_DIV64_gc
  • RTC_PRESCALER_DIV256_gc
  • RTC_PRESCALER_DIV1024_gc

Referenced by rtc_init(), and rtc_set_time().

typedef void(* rtc_callback_t)(uint32_t time)

Callback definition for alarm callback.

Parameters
timeThe time of the alarm

bool rtc_alarm_has_triggered ( void  )

Check if pending alarm has triggered.

Return values
trueAlarm has triggered
falseAlarm is pending

Check if pending alarm has triggered.

Return values
trueAlarm have triggered
falseAlarm is pending

References RTC32, and RTC32_COMPARE_INT_LEVEL.

uint32_t rtc_get_time ( void  )

Get current time.

Returns
Current time value
Current time value
Note
For devices with the errata "RTC Counter value not correctly read after sleep", this can return old values shortly after waking up from sleep.
Without this errata this function can block for up to 1 RTC clock source cycle after waking up from sleep.

References rtc_data_struct::counter_high, cpu_irq_restore(), cpu_irq_save(), rtc_data, rtc_get_counter(), and rtc_is_busy().

Referenced by get_fattime(), ISR(), and rtc_set_alarm_relative().

void rtc_init ( void  )

Initialize the 32kHz oscillator and RTC32.

Starts up the 32kHz oscillator in the backup system and initializes the RTC32.

Note
When the backup system is used, the function rtc_vbat_system_check should be called to determine if a re-initialization must be done.

Initialize the 32kHz oscillator and RTC32.

Start up the RTC and start counting from 0

Note
The RTC clock source used by the RTC module should be set up before calling this function.

References CONFIG_RTC_PRESCALER, RTC32, rtc_is_busy(), RTC_OVERFLOW_INT_LEVEL, sleepmgr_lock_mode(), SLEEPMGR_PSAVE, sysclk_enable_module(), SYSCLK_PORT_GEN, SYSCLK_RTC, and vbat_init().

Referenced by main(), and timeout_init().

void rtc_set_alarm ( uint32_t  time)

Set alarm time.

Will set absolute time of the alarm that will call the callback function specified by rtc_set_callback on expiration. Alternatively, you may use rtc_alarm_has_triggered to check if the alarm has expired.

Any pending alarm will be overwritten with this function.

Parameters
timeAbsolute time value. See also Minimum allowed alarm time
Precondition
Needs interrupts disabled if used from several contexts

Will set absolute alarm time that will call the callback specified by rtc_set_callback on completion. Or possibly use rtc_alarm_has_triggered to check for it.

Any pending alarm will be overwritten with this function.

Parameters
timeAbsolute time value. See also Minimum allowed alarm time
Precondition
Needs interrupts disabled if used from several contexts

References rtc_data_struct::alarm_high, rtc_data_struct::alarm_low, RTC32, RTC32_COMPARE_INT_LEVEL, RTC_COMPARE_INT_LEVEL, rtc_data, rtc_is_busy(), and RTC_OVERFLOW_INT_LEVEL.

Referenced by alarm(), rtc_set_alarm_relative(), tick_handler(), and timeout_init().

static void rtc_set_alarm_relative ( uint32_t  offset)
inlinestatic

Set alarm relative to current time.

Parameters
offsetOffset to current time. This is minimum value, so the alarm might happen at up to one time unit later. See also Minimum allowed alarm time
Note
For devices with the errata "RTC Counter value not correctly read after sleep", this can be unsafe to do shortly after waking up from sleep.
Without this errata this function can block for up to 1 RTC clock source cycle after waking up from sleep.

References rtc_get_time(), and rtc_set_alarm().

void rtc_set_callback ( rtc_callback_t  callback)

Set callback to call on alarm.

Parameters
callbackCallback function pointer

References rtc_data_struct::callback, and rtc_data.

Referenced by main(), and timeout_init().

void rtc_set_time ( uint32_t  time)

Set current time.

Parameters
timeTime value to set

References CONFIG_RTC_PRESCALER, rtc_data_struct::counter_high, RTC32, rtc_data, and rtc_is_busy().

Referenced by tick_handler(), and timeout_init().