X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fdrv%2Ftimer.c;h=4ace035cdac9875ba5df10119a7892e34c70fac9;hb=32d1445272120a254d77ce8d1af1f527da7a2c17;hp=bd0ff9daed9ec26bfe5ae35ff5266917b72329b1;hpb=e62ca0b357f09804d7d894949df44224c9d74bb7;p=bertos.git diff --git a/bertos/drv/timer.c b/bertos/drv/timer.c index bd0ff9da..4ace035c 100644 --- a/bertos/drv/timer.c +++ b/bertos/drv/timer.c @@ -32,15 +32,17 @@ * * \brief Hardware independent timer driver (implementation) * - * \version $Id$ * \author Bernie Innocenti + * \author Francesco Sacchi */ #include "timer.h" +#include "hw/hw_timer.h" #include "cfg/cfg_timer.h" #include "cfg/cfg_wdt.h" -#include "cfg/cfg_kern.h" +#include "cfg/cfg_proc.h" +#include "cfg/cfg_signal.h" #include #include #include @@ -48,6 +50,9 @@ #include #include #include +#include // cpu_relax() + +#include // proc_decQuantun() /* * Include platform-specific binding code if we're hosted. @@ -57,7 +62,10 @@ //#include OS_CSOURCE(timer) #include #else - #include CPU_CSOURCE(timer) + #ifndef WIZ_AUTOGEN + #warning Deprecated: now you should include timer_ directly in the makefile. Remove this line and the following once done. + #include CPU_CSOURCE(timer) + #endif #endif /* @@ -109,25 +117,16 @@ volatile ticks_t _clock; */ REGISTER static List timers_queue; - /** - * Add the specified timer to the software timer service queue. - * When the delay indicated by the timer expires, the timer - * device will execute the event associated with it. - * - * \note Interrupt safe + * This function really does the job. It adds \a timer to \a queue. + * \see timer_add for details. */ -void timer_add(Timer *timer) +INLINE void timer_addToList(Timer *timer, List *queue) { - Timer *node; - cpu_flags_t flags; - - /* Inserting timers twice causes mayhem. */ ASSERT(timer->magic != TIMER_MAGIC_ACTIVE); DB(timer->magic = TIMER_MAGIC_ACTIVE;) - IRQ_SAVE_DISABLE(flags); /* Calculate expiration time for this timer */ timer->tick = _clock + timer->_delay; @@ -136,7 +135,7 @@ void timer_add(Timer *timer) * Search for the first node whose expiration time is * greater than the timer we want to add. */ - node = (Timer *)LIST_HEAD(&timers_queue); + Timer *node = (Timer *)LIST_HEAD(queue); while (node->link.succ) { /* @@ -152,10 +151,19 @@ void timer_add(Timer *timer) /* Enqueue timer request into the list */ INSERT_BEFORE(&timer->link, &node->link); - - IRQ_RESTORE(flags); } +/** + * Add the specified timer to the software timer service queue. + * When the delay indicated by the timer expires, the timer + * device will execute the event associated with it. + * + * \note Interrupt safe + */ +void timer_add(Timer *timer) +{ + ATOMIC(timer_addToList(timer, &timers_queue)); +} /** * Remove a timer from the timers queue before it has expired. @@ -171,39 +179,97 @@ Timer *timer_abort(Timer *timer) return timer; } + +INLINE void timer_poll(List *queue) +{ + Timer *timer; + + /* + * Check the first timer request in the list and process + * it when it has expired. Repeat this check until the + * first node has not yet expired. Since the list is sorted + * by expiry time, all the following requests are guaranteed + * to expire later. + */ + while ((timer = (Timer *)LIST_HEAD(queue))->link.succ) + { + /* This request in list has not yet expired? */ + if (timer_clock() - timer->tick < 0) + break; + + /* Retreat the expired timer */ + REMOVE(&timer->link); + DB(timer->magic = TIMER_MAGIC_INACTIVE;) + + /* Execute the associated event */ + event_do(&timer->expire); + } +} + +/** + * Add \a timer to \a queue. + * \see synctimer_poll() for details. + */ +void synctimer_add(Timer *timer, List *queue) +{ + timer_addToList(timer, queue); +} + +/** + * Simple synchronous timer based scheduler polling routine. + * + * Sometimes you would like to have a proper scheduler, + * but you can't afford it due to memory constraints. + * + * This is a simple replacement: you can create events and call + * them periodically at specific time intervals. + * All you have to do is to set up normal timers, and call synctimer_add() + * instead of timer_add() to add the events to your specific queue. + * Then, in the main loop or wherever you want, you can call + * synctimer_poll() to process expired events. The associated callbacks will be + * executed. + * As this is done synchronously you don't have to worry about race conditions. + * You can kill an event by simply calling synctimer_abort(). + * + */ +void synctimer_poll(List *queue) +{ + timer_poll(queue); +} + #endif /* CONFIG_TIMER_EVENTS */ /** * Wait for the specified amount of timer ticks. + * + * \note Sleeping while preemption is disabled fallbacks to a busy wait sleep. */ void timer_delayTicks(ticks_t delay) { /* We shouldn't sleep with interrupts disabled */ IRQ_ASSERT_ENABLED(); -#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS +#if CONFIG_KERN_SIGNALS Timer t; - ASSERT(!sig_check(SIG_SINGLE)); - timer_setSignal(&t, proc_current(), SIG_SINGLE); - timer_setDelay(&t, delay); - timer_add(&t); - sig_wait(SIG_SINGLE); - -#else /* !CONFIG_KERN_SIGNALS */ - - ticks_t start = timer_clock(); - - /* Busy wait */ - while (timer_clock() - start < delay) + if (proc_preemptAllowed()) { -#if CONFIG_WATCHDOG - wdt_reset(); -#endif + ASSERT(!sig_check(SIG_SINGLE)); + timer_setSignal(&t, proc_current(), SIG_SINGLE); + timer_setDelay(&t, delay); + timer_add(&t); + sig_wait(SIG_SINGLE); } - + else #endif /* !CONFIG_KERN_SIGNALS */ + { + ticks_t start = timer_clock(); + + /* Busy wait */ + while (timer_clock() - start < delay) + cpu_relax(); + } } @@ -255,7 +321,6 @@ void timer_delayHp(hptime_t delay) } #endif /* CONFIG_TIMER_UDELAY */ - /** * Timer interrupt handler. Find soft timers expired and * trigger corresponding events. @@ -271,10 +336,6 @@ DEFINE_TIMER_ISR #pragma interrupt saveall #endif -#if CONFIG_TIMER_EVENTS - Timer *timer; -#endif - /* * On systems sharing IRQ line and vector, this check is needed * to ensure that IRQ is generated by timer source. @@ -290,28 +351,12 @@ DEFINE_TIMER_ISR /* Update the master ms counter */ ++_clock; -#if CONFIG_TIMER_EVENTS - /* - * Check the first timer request in the list and process - * it when it has expired. Repeat this check until the - * first node has not yet expired. Since the list is sorted - * by expiry time, all the following requests are guaranteed - * to expire later. - */ - while ((timer = (Timer *)LIST_HEAD(&timers_queue))->link.succ) - { - /* This request in list has not yet expired? */ - if (_clock - timer->tick < 0) - break; - - /* Retreat the expired timer */ - REMOVE(&timer->link); - DB(timer->magic = TIMER_MAGIC_INACTIVE;) + /* Update the current task's quantum (if enabled). */ + proc_decQuantum(); - /* Execute the associated event */ - event_do(&timer->expire); - } -#endif /* CONFIG_TIMER_EVENTS */ + #if CONFIG_TIMER_EVENTS + timer_poll(&timers_queue); + #endif TIMER_STROBE_OFF; }