X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=drv%2Ftimer.c;h=b2caf90d9706526aed8ba8f065c40a2a7e5c4ee6;hb=8e770a994c62f109c91854136e1bbae049b07a2f;hp=a01f6f3cb88e4cc52d1f4883c5e4bae99b41678a;hpb=0fee90eca2cd56674ba7708dc113e8794327cee0;p=bertos.git diff --git a/drv/timer.c b/drv/timer.c index a01f6f3c..b2caf90d 100755 --- a/drv/timer.c +++ b/drv/timer.c @@ -3,7 +3,7 @@ * * * \version $Id$ @@ -15,6 +15,39 @@ /* * $Log$ + * Revision 1.13 2004/08/10 06:59:09 bernie + * timer_gettick(): Rename to timer_ticks() and add backwards compatibility inline. + * + * Revision 1.12 2004/08/08 05:59:37 bernie + * Remove a few useless casts. + * + * Revision 1.11 2004/08/02 20:20:29 aleph + * Merge from project_ks + * + * Revision 1.10 2004/07/30 14:15:53 rasky + * Nuovo supporto unificato per detect della CPU + * + * Revision 1.9 2004/07/21 00:15:13 bernie + * Put timer driver on diet. + * + * Revision 1.8 2004/07/18 21:57:07 bernie + * Fix preprocessor warning with potentially undefined symbol. + * + * Revision 1.6 2004/06/07 18:10:06 aleph + * Remove free pool of timers; use user-provided Timer structure instead + * + * Revision 1.5 2004/06/07 15:56:55 aleph + * Some tabs cleanup and add timer strobe macros + * + * Revision 1.4 2004/06/06 18:25:44 bernie + * Rename event macros to look like regular functions. + * + * Revision 1.3 2004/06/06 17:18:42 bernie + * Fix \!CONFIG_KERN_SIGNALS code paths. + * + * Revision 1.2 2004/06/03 11:27:09 bernie + * Add dual-license information. + * * Revision 1.1 2004/05/23 18:23:30 bernie * Import drv/timer module. * @@ -23,77 +56,22 @@ #include "hw.h" #include "kdebug.h" #include "timer.h" +#include CPU_HEADER(timer) -#ifdef CONFIG_KERN_SIGNALS -#include -#endif - -#if (ARCH & ARCH_EMUL) - -# error To be recoded - -#elif defined(__AVR__) -# include "timer_avr.h" -#elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */ -# include "timer_i196.h" -#elif defined (__m56800__) -# include "timer_dsp56k.h" -#else -# error Unknown system +#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS + #include #endif - -/*! Number of available timers */ -#define MAX_TIMERS 4 - - //! Master system clock (1ms accuracy) volatile time_t _clock; -static Timer soft_timers[MAX_TIMERS]; /*!< Pool of Timer structures */ -static List timers_pool; /*!< Pool of free timers */ -REGISTER static List timers_queue; /*!< Active timers */ +#ifndef CONFIG_TIMER_DISABLE_EVENTS /*! - * Return a new timer picking and removing it from the available - * timers pool. Return NULL if no more timers are available. + * List of active asynchronous timers. */ -Timer *timer_new(void) -{ - Timer *timer; - cpuflags_t flags; - - DISABLE_IRQSAVE(flags); - - /* Should never happen */ - if (ISLISTEMPTY(&timers_pool)) - { - ENABLE_IRQRESTORE(flags); - DB(kprintf("Tmrspool empty\n");) - return NULL; - } - - /* Get a timer from the free pool */ - timer = (Timer *)timers_pool.head; - REMOVE((Node *)timer); - - ENABLE_IRQRESTORE(flags); - - return timer; -} - - -/*! - * Delete a timer, putting it in the available timers queue. - */ -void timer_delete(Timer *timer) -{ - cpuflags_t flags; - DISABLE_IRQSAVE(flags); - ADDHEAD(&timers_pool, &timer->link); - ENABLE_IRQRESTORE(flags); -} +REGISTER static List timers_queue; /*! @@ -119,7 +97,7 @@ void timer_add(Timer *timer) node = (Timer *)timers_queue.head; while (node->link.succ) { - /* Stop just after the insert point */ + /* Stop just after the insertion point */ if (node->tick > timer->tick) break; @@ -128,7 +106,7 @@ void timer_add(Timer *timer) } /* Enqueue timer request into the list */ - INSERTBEFORE((Node *)timer, (Node *)node); + INSERTBEFORE(&timer->link, &node->link); ENABLE_IRQRESTORE(flags); } @@ -141,19 +119,21 @@ Timer *timer_abort(Timer *timer) { cpuflags_t flags; DISABLE_IRQSAVE(flags); - REMOVE((Node *)timer); + REMOVE(&timer->link); ENABLE_IRQRESTORE(flags); return timer; } +#endif /* CONFIG_TIMER_DISABLE_EVENTS */ + /*! * Wait for the specified amount of time (expressed in ms) */ void timer_delay(time_t time) { -#ifdef CONFIG_KERN_SIGNALS +#if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS Timer t; ASSERT(!sig_check(SIG_SINGLE)); @@ -161,19 +141,29 @@ void timer_delay(time_t time) timer_set_delay(&t, time); timer_add(&t); sig_wait(SIG_SINGLE); -#else - time_t start = timer_gettick(); - while (timer_gettick() - start < time) { /* nop */ } -#endif +#else /* !CONFIG_KERN_SIGNALS */ + + time_t start = timer_ticks(); + + /* Busy wait */ + while (timer_ticks() - start < time) { /* nop */ } + +#endif /* !CONFIG_KERN_SIGNALS */ } + +#ifndef CONFIG_TIMER_DISABLE_UDELAY /*! * Wait for the specified amount of time (expressed in microseconds) + * + * \bug In AVR arch the maximum amount of time that can be used as + * delay could be very limited, depending on the hardware timer + * used. Check timer_avr.h, and what register is used as hptime_t. */ void timer_udelay(utime_t usec_delay) { - if (usec_delay > 1000) + if (UNLIKELY(usec_delay > 1000)) { timer_delay(usec_delay / 1000); usec_delay %= 1000; @@ -185,9 +175,10 @@ void timer_udelay(utime_t usec_delay) hptime_t start = timer_hw_hpread(); hptime_t delay = (uint32_t)usec_delay * TIMER_HW_HPTICKS_PER_SEC / 1000000ul; - while (timer_hw_hpread() - start < delay) + while (timer_hw_hpread() - start < delay) {} } +#endif /* CONFIG_TIMER_DISABLE_UDELAY */ /*! @@ -196,20 +187,27 @@ void timer_udelay(utime_t usec_delay) */ DEFINE_TIMER_ISR { - /* With the Metrowerks compiler, the only way to force the compiler generate - an interrupt service routine is to put a pragma directive within the function - body. */ + /* + * With the Metrowerks compiler, the only way to force the compiler generate + * an interrupt service routine is to put a pragma directive within the function + * body. + */ #ifdef __MWERKS__ #pragma interrupt saveall #endif +#ifndef CONFIG_TIMER_DISABLE_EVENTS Timer *timer; +#endif + + TIMER_STROBE_ON; timer_hw_irq(); /* Update the master ms counter */ ++_clock; +#ifndef CONFIG_TIMER_DISABLE_EVENTS /* * Check the first timer request in the list and process * it when it has expired. Repeat this check until the @@ -224,13 +222,15 @@ DEFINE_TIMER_ISR break; /* Retreat the expired timer */ - REMOVE((Node *)timer); + REMOVE(&timer->link); /* Execute the associated event */ - DOEVENT_INTR(&timer->expire); + event_do(&timer->expire); } -} +#endif /* CONFIG_TIMER_DISABLE_EVENTS */ + TIMER_STROBE_OFF; +} /*! @@ -238,17 +238,13 @@ DEFINE_TIMER_ISR */ void timer_init(void) { - int i; + TIMER_STROBE_INIT; +#ifndef CONFIG_TIMER_DISABLE_EVENTS INITLIST(&timers_queue); - INITLIST(&timers_pool); - - /* Init all software timers in the free pool */ - for (i = 0; i < MAX_TIMERS; i++) - ADDTAIL(&timers_pool, (Node *)&soft_timers[i]); +#endif _clock = 0; timer_hw_init(); } -