From 9757bb7dda5dfc3094f6f5e89d70653402ed629d Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 10 Dec 2010 16:52:06 +0100 Subject: [PATCH] * many fixes. vtimer_hammer OK now. --- sys/vtimer.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/sys/vtimer.c b/sys/vtimer.c index 74e05adf8f..2ea9420eb8 100644 --- a/sys/vtimer.c +++ b/sys/vtimer.c @@ -13,6 +13,7 @@ #include #define VTIMER_THRESHOLD 20U +#define VTIMER_BACKOFF 10U #define SECONDS_PER_TICK (4096U) #define NANOSECONDS_PER_TICK (4096U * 1000000) @@ -41,7 +42,7 @@ static int set_longterm(vtimer_t *timer) { } static int update_shortterm() { - if (hwtimer_id) { + if (hwtimer_id != -1) { if (hwtimer_next_absolute != shortterm_queue_root.next->priority) { hwtimer_remove(hwtimer_id); } else { @@ -49,23 +50,24 @@ static int update_shortterm() { } } - uint32_t now = hwtimer_now(); + hwtimer_next_absolute = shortterm_queue_root.next->priority; - - while ((hwtimer_next_absolute + longterm_tick_start)-(hwtimer_now()+VTIMER_THRESHOLD) > NANOSECONDS_PER_TICK) { - hwtimer_next_absolute += VTIMER_THRESHOLD; + + unsigned int next = hwtimer_next_absolute + longterm_tick_start; + unsigned int now = hwtimer_now(); + + if((next - VTIMER_THRESHOLD - now) > NANOSECONDS_PER_TICK ) { + next = now + VTIMER_BACKOFF; } - hwtimer_id = hwtimer_set_absolute(hwtimer_next_absolute + longterm_tick_start, vtimer_callback, NULL); + hwtimer_id = hwtimer_set_absolute(next, vtimer_callback, NULL); DEBUG("update_shortterm: Set hwtimer to %lu (now=%lu)\n", hwtimer_next_absolute + longterm_tick_start, hwtimer_now()); - //printf("%lu (now=%lu)\n", hwtimer_next_absolute + longterm_tick_start, hwtimer_now()); - return 0; } void vtimer_tick(void *ptr) { - puts("vtimer_tick()."); + DEBUG("vtimer_tick()."); seconds += SECONDS_PER_TICK; longterm_tick_start = longterm_tick_timer.absolute.nanoseconds; @@ -95,9 +97,12 @@ static int set_shortterm(vtimer_t *timer) { void vtimer_callback(void *ptr) { vtimer_t *timer; in_callback = true; + hwtimer_id = -1; + timer = (vtimer_t *)queue_remove_head(&shortterm_queue_root); DEBUG("vtimer_callback(): Shooting %lu.\n", timer->absolute.nanoseconds); + /* shoot timer */ timer->action(timer->arg); @@ -126,10 +131,6 @@ void normalize_to_tick(timex_t *time) { int vtimer_set(vtimer_t *timer, timex_t *offset) { DEBUG("vtimer_set(): New timer. Offset: %lu %lu\n", offset->seconds, offset->nanoseconds); - -// timex_t vnow = vtimer_now(); -// DEBUG("Now = %lu %lu\n", vnow.seconds, vnow.nanoseconds); - timer->absolute = timex_add(vtimer_now(), *offset); normalize_to_tick(&(timer->absolute)); @@ -137,6 +138,12 @@ int vtimer_set(vtimer_t *timer, timex_t *offset) { int result = 0; + if (timer->absolute.seconds == 0) { + if (timer->absolute.nanoseconds > 10) { + timer->absolute.nanoseconds -= 10; + } + } + int state = disableIRQ(); if (timer->absolute.seconds != seconds ) { /* we're long-term */ @@ -167,6 +174,7 @@ timex_t vtimer_now() { int vtimer_init() { DEBUG("vtimer_init().\n"); + int state = disableIRQ(); seconds = 0; longterm_tick_timer.action = vtimer_tick; @@ -180,13 +188,14 @@ int vtimer_init() { set_shortterm(&longterm_tick_timer); update_shortterm(); + restoreIRQ(state); return 0; } int vtimer_usleep(uint32_t usecs) { vtimer_t t; timex_t offset = timex_set(0, usecs); - t.action = thread_wakeup; + t.action = (void*) thread_wakeup; t.arg = (void*) thread_getpid(); vtimer_set(&t, &offset); thread_sleep();