Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator 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.

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 have triggered. More...
 
static void rtc_disable_correction (void)
 Disable correction of RTC clock. More...
 
static void rtc_enable_correction (void)
 Enable correction of RTC clock. More...
 
uint32_t rtc_get_time (void)
 Get current time. More...
 
void rtc_init (void)
 Initialize the RTC. More...
 
void rtc_load_calibration (uint8_t error_value, uint8_t sign)
 Load Error value to RTC Calibration register with sign bit. 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

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 have triggered.

Return values
trueAlarm have triggered
falseAlarm is pending

References RTC_COMPARE_INT_LEVEL.

static void rtc_disable_correction ( void  )
inlinestatic

Disable correction of RTC clock.

static void rtc_enable_correction ( void  )
inlinestatic

Enable correction of RTC clock.

Note
The RTC-CALIB register should be loaded with proper error and sign value before calling this function.
uint32_t rtc_get_time ( void  )

Get current time.

Returns
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, and rtc_is_busy().

Referenced by get_fattime(), and rtc_set_alarm_relative().

void rtc_init ( void  )

Initialize the RTC.

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, RTC_OVERFLOW_INT_LEVEL, sleepmgr_lock_mode(), SLEEPMGR_PSAVE, sysclk_enable_module(), SYSCLK_PORT_GEN, and SYSCLK_RTC.

Referenced by main().

void rtc_load_calibration ( uint8_t  error_value,
uint8_t  sign 
)

Load Error value to RTC Calibration register with sign bit.

Parameters
error_valueError value for correction operation of RTC
signDirection of correction or sign of correction. If this bit is LOW then the RTC counter will be slowed down by adding clocks. If this bit is HIGH, then the RTC counter will be speeded up by removing clocks
Note
To set sign bit as HIGH,it is required to set the prescaler to minimum setting of DIV2 (RTC clock/2)

References Assert.

void rtc_set_alarm ( uint32_t  time)

Set alarm time.

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, RTC_COMPARE_INT_LEVEL, rtc_data, rtc_is_busy(), and RTC_OVERFLOW_INT_LEVEL.

Referenced by rtc_set_alarm_relative().

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.

void rtc_set_time ( uint32_t  time)

Set current time.

Parameters
timeTime value to set

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