timer.c File Reference

#include <stdint.h>
#include <rtc.h>
#include <intc.h>
#include "timer.h"

Go to the source code of this file.

Data Structures

struct  timeout_t
struct  timer_t

Defines

#define ARRAY_SIZE(a)   sizeof(a) / sizeof(a[0])

Functions

static void irq_handler (void)
void timer_cancel_timeout (U32 id)
void timer_delay (U32 ms)
U32 timer_get_ms (void)
void timer_init (void(*tick_isr)(void *ctx), void *ctx)
void timer_poll (void)
 Called from application main loop to invoke any scheduled timeout cbs.
static U32 timer_sched_timeout (U32 ms, U8 type)
U32 timer_sched_timeout_cb (U32 ms, U8 type, void(*cb)(void *ctx), void *ctx)

Variables

static struct timer_t TIMER


Define Documentation

#define ARRAY_SIZE (  )     sizeof(a) / sizeof(a[0])

Definition at line 52 of file timer.c.


Function Documentation

static void irq_handler ( void   )  [static]

Definition at line 172 of file timer.c.

References timer_t::ctx, timer_t::tick, timer_t::tick_isr, and TIMER.

Referenced by timer_init().

00173 {
00174         volatile avr32_rtc_t *rtc = &AVR32_RTC;
00175         struct timer_t* priv = &TIMER;
00176         priv->tick++;
00177 
00178         if(priv->tick_isr)
00179                 priv->tick_isr(priv->ctx);
00180         
00181         rtc->icr = AVR32_RTC_ICR_TOPI_MASK;
00182         rtc->isr;
00183 }

void timer_cancel_timeout ( U32  id  ) 

Definition at line 163 of file timer.c.

References timeout_t::expired, timer_t::timeout, TIMER, and TRUE.

00164 {
00165         struct timer_t* priv = &TIMER;
00166         struct timeout_t* tmo;
00167     
00168         tmo = &priv->timeout[id];
00169         tmo->expired = TRUE;
00170 }

void timer_delay ( U32  ms  ) 

Definition at line 88 of file timer.c.

References timer_t::MS_PER_TICK, timer_t::tick, and TIMER.

00089 {
00090         struct timer_t* priv = &TIMER;
00091         U32 expire_at_tick = priv->tick + ms / priv->MS_PER_TICK;
00092         while (priv->tick < expire_at_tick);
00093 }

U32 timer_get_ms ( void   ) 

Definition at line 82 of file timer.c.

References timer_t::MS_PER_TICK, timer_t::tick, and TIMER.

Referenced by cmd_ping(), init_ping_info(), ping_recv(), ping_send(), poll(), print_stats(), tcp_accept_cb(), tcp_connect_cb(), ttcp_print_stats(), udp_recv_cb(), and udp_send_data().

00083 {
00084         struct timer_t* priv = &TIMER;
00085         return priv->tick * priv->MS_PER_TICK;
00086 }

void timer_init ( void(*)(void *ctx tick_isr,
void *  ctx 
)

Definition at line 62 of file timer.c.

References ARRAY_SIZE, timer_t::ctx, timeout_t::expired, irq_handler(), timer_t::MS_PER_TICK, timer_t::tick_isr, timer_t::timeout, TIMER, and TRUE.

Referenced by main().

00063 {
00064         struct timer_t* priv = &TIMER;
00065         uint8_t id;
00066         
00067         INTC_register_interrupt(&irq_handler, AVR32_RTC_IRQ, AVR32_INTC_INT0);
00068         if (!rtc_init(&AVR32_RTC, RTC_OSC_RC, 0))
00069                 Assert(0);
00070 
00071     priv->tick_isr = tick_isr;
00072     priv->ctx = ctx;
00073         rtc_set_top_value(&AVR32_RTC, 115 * priv->MS_PER_TICK / 2);
00074         rtc_enable_interrupt(&AVR32_RTC);
00075         rtc_enable(&AVR32_RTC);
00076 
00077         for (id = 0; id < ARRAY_SIZE(priv->timeout); id++)
00078                 priv->timeout[id].expired = TRUE;
00079 }

void timer_poll ( void   ) 

Called from application main loop to invoke any scheduled timeout cbs.

This function might be called as often as possible rather than at each tick to support the timeout value '0', e.g a timeout within less than one tick.

Definition at line 101 of file timer.c.

References ARRAY_SIZE, timeout_t::cb, timeout_t::ctx, timeout_t::expire_at_tick, timeout_t::expired, timeout_t::tick, timer_t::tick, timer_t::timeout, TIMEOUT_PERIODIC, TIMER, TRUE, and timeout_t::type.

Referenced by poll().

00102 {
00103         struct timer_t* priv = &TIMER;
00104         U8 i;
00105 
00106         for (i = 0; i < ARRAY_SIZE(priv->timeout); i++) {
00107                 struct timeout_t* tmo = &priv->timeout[i];
00108                 if (tmo->expired)
00109                         continue;
00110         
00111                 if (tmo->expire_at_tick > priv->tick)
00112                         continue;
00113 
00114                 if (tmo->cb)
00115                         tmo->cb(tmo->ctx);
00116                 
00117                 if (tmo->type == TIMEOUT_PERIODIC)
00118                         tmo->expire_at_tick = priv->tick + tmo->tick;
00119                 else
00120                         tmo->expired = TRUE;
00121         }
00122 }

static U32 timer_sched_timeout ( U32  ms,
U8  type 
) [static]

Definition at line 124 of file timer.c.

References ARRAY_SIZE, timeout_t::expire_at_tick, timeout_t::expired, FALSE, timer_t::MS_PER_TICK, timer_t::tick, timeout_t::tick, timer_t::timeout, TIMEOUT_ONESHOT, TIMEOUT_PERIODIC, TIMER, and timeout_t::type.

Referenced by timer_sched_timeout_cb().

00125 {
00126         struct timer_t* priv = &TIMER;
00127         struct timeout_t* tmo;
00128         U8 id;
00129 
00130         Assert(type == TIMEOUT_ONESHOT || type == TIMEOUT_PERIODIC);
00131         
00132         for (id = 0; id < ARRAY_SIZE(priv->timeout); id++) {
00133                 tmo = &priv->timeout[id];
00134                 if (tmo->expired)
00135                         break;
00136         }
00137 
00138         Assert(id != ARRAY_SIZE(priv->timeout));
00139 
00140         tmo->tick = ms / priv->MS_PER_TICK;
00141         tmo->expire_at_tick = priv->tick + tmo->tick;
00142         tmo->type = type;
00143         tmo->expired = FALSE;
00144         return id;
00145 }

U32 timer_sched_timeout_cb ( U32  ms,
U8  type,
void(*)(void *ctx cb,
void *  ctx 
)

Definition at line 147 of file timer.c.

References timeout_t::cb, cb, timeout_t::ctx, timer_t::timeout, TIMER, and timer_sched_timeout().

Referenced by tcp_send_data(), tcp_timeout_cb(), udp_send_data(), and wl_init_complete_cb().

00148 {
00149         struct timer_t* priv = &TIMER;
00150         struct timeout_t* tmo;
00151         U8 id;
00152 
00153         Assert(cb);
00154         id = timer_sched_timeout(ms, type);
00155         tmo = &priv->timeout[id];
00156 
00157         tmo->cb = cb;
00158         tmo->ctx = ctx;
00159         return id;
00160 }


Variable Documentation

struct timer_t TIMER [static]

Initial value:

 {
        .tick = 0,
        .MS_PER_TICK = TIMER_HZ,
        .timeout = { { 0 } },
}

Definition at line 56 of file timer.c.

Referenced by irq_handler(), timer_cancel_timeout(), timer_delay(), timer_get_ms(), timer_init(), timer_poll(), timer_sched_timeout(), and timer_sched_timeout_cb().


Generated on Fri Feb 19 02:24:08 2010 for AVR32 - H&D by  doxygen 1.5.5