timer_irq: Integrate timer_try_set_next() into timer_dispatch_many()

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-03-28 11:25:11 -04:00
parent 6988507998
commit eb4eeb6f73
3 changed files with 40 additions and 57 deletions

View File

@ -11,11 +11,6 @@
#include "command.h" // shutdown #include "command.h" // shutdown
#include "sched.h" // sched_timer_kick #include "sched.h" // sched_timer_kick
// In order to use this code the board must still define
// timer_read_time(). In addition, it must also provide a timer_set()
// function.
void timer_set(uint32_t next);
DECL_CONSTANT(CLOCK_FREQ, CONFIG_CLOCK_FREQ); DECL_CONSTANT(CLOCK_FREQ, CONFIG_CLOCK_FREQ);
// Return the number of clock ticks for a given number of microseconds // Return the number of clock ticks for a given number of microseconds
@ -47,55 +42,41 @@ static uint32_t timer_repeat_until;
#define TIMER_MIN_TRY_TICKS timer_from_us(1) #define TIMER_MIN_TRY_TICKS timer_from_us(1)
#define TIMER_DEFER_REPEAT_TICKS timer_from_us(5) #define TIMER_DEFER_REPEAT_TICKS timer_from_us(5)
// Set the next timer wake time (in absolute clock ticks) or return 1 // Reschedule timers after a brief pause to prevent task starvation
// if the next timer is too close to schedule. Caller must disable static uint32_t noinline
// irqs. force_defer(uint32_t next)
static int
timer_try_set_next(unsigned int next)
{ {
uint32_t now = timer_read_time();
if (timer_is_before(next + timer_from_us(1000), now))
shutdown("Rescheduled timer in the past");
timer_repeat_until = now + TIMER_REPEAT_TICKS;
return now + TIMER_DEFER_REPEAT_TICKS;
}
// Invoke timers - called from board irq code.
uint32_t
timer_dispatch_many(void)
{
uint32_t tru = timer_repeat_until;
for (;;) {
// Run the next software timer
uint32_t next = sched_timer_dispatch();
uint32_t now = timer_read_time(); uint32_t now = timer_read_time();
int32_t diff = next - now; int32_t diff = next - now;
if (diff > (int32_t)TIMER_MIN_TRY_TICKS) if (diff > (int32_t)TIMER_MIN_TRY_TICKS)
// Schedule next timer normally. // Schedule next timer normally.
goto done; return next;
// Next timer is in the past or near future - can't reschedule to it
if (likely(timer_is_before(now, timer_repeat_until))) {
// Can run more timers from this irq; briefly allow irqs
irq_enable();
while (diff >= 0) {
// Next timer is in the near future - wait for time to occur
now = timer_read_time();
diff = next - now;
}
irq_disable();
return 0;
}
if (diff < (int32_t)(-timer_from_us(1000)))
goto fail;
if (unlikely(timer_is_before(tru, now)))
// Too many repeat timers from a single interrupt - force a pause // Too many repeat timers from a single interrupt - force a pause
timer_repeat_until = now + TIMER_REPEAT_TICKS; return force_defer(next);
next = now + TIMER_DEFER_REPEAT_TICKS;
done: // Next timer in the past or near future - wait for it to be ready
timer_set(next); irq_enable();
return 1; while (unlikely(diff > 0))
fail: diff = next - timer_read_time();
shutdown("Rescheduled timer in the past"); irq_disable();
}
// Invoke timers - called from board irq code.
void
timer_dispatch_many(void)
{
for (;;) {
uint32_t next_waketime = sched_timer_dispatch();
// Schedule next timer event (or run next timer if it's ready)
int res = timer_try_set_next(next_waketime);
if (res)
break;
} }
} }

View File

@ -1,6 +1,6 @@
#ifndef __GENERIC_TIMER_IRQ_H #ifndef __GENERIC_TIMER_IRQ_H
#define __GENERIC_TIMER_IRQ_H #define __GENERIC_TIMER_IRQ_H
void timer_dispatch_many(void); uint32_t timer_dispatch_many(void);
#endif // timer_irq.h #endif // timer_irq.h

View File

@ -11,19 +11,8 @@
#include "sam3x8e.h" // TC0 #include "sam3x8e.h" // TC0
#include "sched.h" // sched_timer_kick #include "sched.h" // sched_timer_kick
// IRQ handler
void __visible
TC0_Handler(void)
{
irq_disable();
uint32_t status = TC0->TC_CHANNEL[0].TC_SR; // read to clear irq pending
if (likely(status & TC_SR_CPAS))
timer_dispatch_many();
irq_enable();
}
// Set the next irq time // Set the next irq time
void static void
timer_set(uint32_t value) timer_set(uint32_t value)
{ {
TC0->TC_CHANNEL[0].TC_RA = value; TC0->TC_CHANNEL[0].TC_RA = value;
@ -63,3 +52,16 @@ timer_shutdown(void)
TC0->TC_CHANNEL[0].TC_SR; // read to clear irq pending TC0->TC_CHANNEL[0].TC_SR; // read to clear irq pending
} }
DECL_SHUTDOWN(timer_shutdown); DECL_SHUTDOWN(timer_shutdown);
// IRQ handler
void __visible
TC0_Handler(void)
{
irq_disable();
uint32_t status = TC0->TC_CHANNEL[0].TC_SR; // read to clear irq pending
if (likely(status & TC_SR_CPAS)) {
uint32_t next = timer_dispatch_many();
timer_set(next);
}
irq_enable();
}