Xmega Application Note


Real Time Counter (RTC)

Typedefs

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

Functions

bool rtc_alarm_has_triggered (void)
 Check if pending alarm have triggered.
uint32_t rtc_get_time (void)
 Get current time.
void rtc_init (void)
 Initialize the RTC.
void rtc_set_alarm (uint32_t time)
 Set alarm time.
static void rtc_set_alarm_relative (uint32_t offset)
 Set alarm relative to current time.
void rtc_set_callback (rtc_callback_t callback)
 Set callback to call on alarm.
void rtc_set_time (uint32_t time)
 Set current time.

Detailed Description

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.


Typedef Documentation

typedef void(* rtc_callback_t)(uint32_t time)

Callback definition for alarm callback.

Parameters:
time The time of the alarm

Definition at line 121 of file rtc.h.


Function Documentation

bool rtc_alarm_has_triggered ( void   ) 

Check if pending alarm have triggered.

Return values:
true Alarm have triggered
false Alarm is pending

Definition at line 153 of file rtc.c.

References RTC_COMPARE_INT_LEVEL.

00154 {
00155         // Interrupt enable is used on pending alarm
00156         return !(RTC.INTCTRL & RTC_COMPARE_INT_LEVEL);
00157 }

uint32_t rtc_get_time ( void   ) 

Get current time.

Returns:
Current time value
Note:
Due to errate, this can return old values shortly after waking up from sleep.

Definition at line 107 of file rtc.c.

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

Referenced by rtc_set_alarm_relative().

00108 {
00109         irqflags_t flags;
00110         uint16_t   count_high;
00111         uint16_t   count_low;
00112 
00113         flags = cpu_irq_save();
00114         count_high = rtc_data.counter_high;
00115         count_low = RTC.CNT;
00116         // Test for possible pending increase of high count value
00117         if ((count_low == 0) && (RTC.INTFLAGS & RTC_OVFIF_bm))
00118                 count_high++;
00119         cpu_irq_restore(flags);
00120         return ((uint32_t)count_high << 16) | count_low;
00121 }

Here is the call graph for this function:

void rtc_init ( void   ) 

Initialize the RTC.

Start up the RTC and start counting from 0

Definition at line 174 of file rtc.c.

References RTC_OVERFLOW_INT_LEVEL, sleepmgr_lock_mode(), SLEEPMGR_PSAVE, sysclk_enable_module(), SYSCLK_PORT_GEN, and SYSCLK_RTC.

00175 {
00176         sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_RTC);
00177         CLK.RTCCTRL = CONFIG_RTC_CLOCK_SOURCE | CLK_RTCEN_bm;
00178         RTC.PER = 0xffff;
00179         RTC.CNT = 0;
00180         /* Since overflow interrupt is needed all the time we limit sleep to
00181          * power-save.
00182          */
00183         sleepmgr_lock_mode(SLEEPMGR_PSAVE);
00184         RTC.INTCTRL = RTC_OVERFLOW_INT_LEVEL;
00185         RTC.CTRL = CONFIG_RTC_PRESCALER;
00186 }

Here is the call graph for this function:

void rtc_set_alarm ( uint32_t  time  ) 

Set alarm time.

Will set absolute alarm time that will call the callback specifed 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:
time Absolute time value. See also Minimum allowed alarm time
Precondition:
Needs interrupts disabled if used from several contexts

Definition at line 135 of file rtc.c.

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().

00136 {
00137         RTC.INTCTRL = RTC_OVERFLOW_INT_LEVEL;
00138         while (rtc_is_busy());
00139         RTC.COMP = time;
00140         rtc_data.alarm_low = time;
00141         rtc_data.alarm_high = time >> 16;
00142         while (rtc_is_busy());
00143         RTC.INTCTRL = RTC_COMPARE_INT_LEVEL
00144                 | RTC_OVERFLOW_INT_LEVEL;
00145 }

Here is the call graph for this function:

static void rtc_set_alarm_relative ( uint32_t  offset  )  [inline, static]

Set alarm relative to current time.

Parameters:
offset Offset 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:
Due to errata, this can be unsafe to do shortly after waking up from sleep.

Definition at line 139 of file rtc.h.

References rtc_get_time(), and rtc_set_alarm().

00140 {
00141         rtc_set_alarm(rtc_get_time() + offset);
00142 }

Here is the call graph for this function:

void rtc_set_callback ( rtc_callback_t  callback  ) 

Set callback to call on alarm.

Parameters:
callback Callback function pointer

Definition at line 164 of file rtc.c.

References rtc_data_struct::callback, and rtc_data.

00165 {
00166         rtc_data.callback = callback;
00167 }

void rtc_set_time ( uint32_t  time  ) 

Set current time.

Parameters:
time Time value to set

Definition at line 90 of file rtc.c.

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

00091 {
00092         RTC.CTRL = RTC_PRESCALER_OFF_gc;
00093         while (rtc_is_busy());
00094         RTC.CNT = time;
00095         rtc_data.counter_high = time >> 16;
00096         RTC.CTRL = CONFIG_RTC_PRESCALER;
00097 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Fri Oct 22 12:15:26 2010 for AVR1300 Using the Xmega ADC by doxygen 1.6.3