From b17ffabce5a4c3a174a7eb8168a594dfbc9efd68 Mon Sep 17 00:00:00 2001 From: Christian Mehlis Date: Tue, 10 Dec 2013 22:08:15 +0100 Subject: [PATCH 1/2] reduce minimal timer from 100ms to 10ms --- cpu/native/hwtimer_cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu/native/hwtimer_cpu.c b/cpu/native/hwtimer_cpu.c index 45e70f4b12..5866bab315 100644 --- a/cpu/native/hwtimer_cpu.c +++ b/cpu/native/hwtimer_cpu.c @@ -45,7 +45,7 @@ #include "debug.h" -#define HWTIMERMINOFFSET 100000 +#define HWTIMERMINOFFSET (10 * 1000UL) // 10 ms static unsigned long native_hwtimer_now; static unsigned long time_null; From 2bfb06230a1fdf6fd6c496cf15a0e7515af4da95 Mon Sep 17 00:00:00 2001 From: Christian Mehlis Date: Mon, 9 Dec 2013 22:24:28 +0100 Subject: [PATCH 2/2] fix: native hwtimer backend emulation searching for the next pending timer was broken this commit rewrites this functionality --- cpu/native/hwtimer_cpu.c | 67 +++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/cpu/native/hwtimer_cpu.c b/cpu/native/hwtimer_cpu.c index 5866bab315..3f8f64c878 100644 --- a/cpu/native/hwtimer_cpu.c +++ b/cpu/native/hwtimer_cpu.c @@ -89,45 +89,39 @@ unsigned long ts2ticks(struct timespec *tp) */ void schedule_timer(void) { - int l = next_timer; + next_timer = -1; - for ( - int i = ((next_timer + 1) % ARCH_MAXTIMERS); - i != next_timer; - i = ((i + 1) % ARCH_MAXTIMERS) - ) { - - if (native_hwtimer_isset[l] != 1) { - /* make sure we dont compare to garbage in the following - * if condition */ - l = i; + /* try to find *an active* timer */ + for (int i = 0; i < ARCH_MAXTIMERS; i++) { + if (native_hwtimer_isset[i] == 1) { + next_timer = i; + break; } + } + if (next_timer == -1) { + DEBUG("schedule_timer(): no valid timer found - nothing to schedule"); + // TODO: unset timer. + return; + } + + /* find the next pending timer (next_timer now points to *a* valid pending timer) */ + for (int i = 0; i < ARCH_MAXTIMERS; i++) { if ( (native_hwtimer_isset[i] == 1) && - (tv2ticks(&(native_hwtimer[i].it_value)) < tv2ticks(&(native_hwtimer[l].it_value))) + (tv2ticks(&(native_hwtimer[i].it_value)) < tv2ticks(&(native_hwtimer[next_timer].it_value))) ) { - /* set l to the lowest active time */ - l = i; + /* timer in slot i is active and the timeout is more recent than next_timer */ + next_timer = i; } - } - next_timer = l; - - /* l could still point to some unused (garbage) timer if no timers - * are set at all */ - if (native_hwtimer_isset[next_timer] == 1) { - if (setitimer(ITIMER_REAL, &native_hwtimer[next_timer], NULL) == -1) { - err(EXIT_FAILURE, "schedule_timer"); - } - else { - DEBUG("schedule_timer(): set next timer.\n"); - } + /* next pending timer is in slot next_timer */ + if (setitimer(ITIMER_REAL, &native_hwtimer[next_timer], NULL) == -1) { + err(EXIT_FAILURE, "schedule_timer"); } else { - DEBUG("schedule_timer(): no next timer!? This looks suspicous.\n"); - // TODO: unset timer. + DEBUG("schedule_timer(): set next timer.\n"); } } @@ -138,21 +132,22 @@ void schedule_timer(void) */ void hwtimer_isr_timer() { - int i; - DEBUG("hwtimer_isr_timer()\n"); - i = next_timer; - native_hwtimer_isset[next_timer] = 0; - schedule_timer(); + if (next_timer == -1) { + DEBUG("hwtimer_isr_timer(): next_timer in invalid\n"); + return; + } - if (native_hwtimer_irq[i] == 1) { - DEBUG("hwtimer_isr_timer(): calling hwtimer.int_handler(%i)\n", i); - int_handler(i); + if (native_hwtimer_irq[next_timer] == 1) { + DEBUG("hwtimer_isr_timer(): calling hwtimer.int_handler(%i)\n", next_timer); + int_handler(next_timer); + native_hwtimer_isset[next_timer] = 0; } else { DEBUG("hwtimer_isr_timer(): this should not have happened"); } + schedule_timer(); } void hwtimer_arch_enable_interrupt(void)