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:
parent
6988507998
commit
eb4eeb6f73
|
@ -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();
|
uint32_t now = timer_read_time();
|
||||||
int32_t diff = next - now;
|
if (timer_is_before(next + timer_from_us(1000), now))
|
||||||
if (diff > (int32_t)TIMER_MIN_TRY_TICKS)
|
shutdown("Rescheduled timer in the past");
|
||||||
// Schedule next timer normally.
|
|
||||||
goto done;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
// Too many repeat timers from a single interrupt - force a pause
|
|
||||||
timer_repeat_until = now + TIMER_REPEAT_TICKS;
|
timer_repeat_until = now + TIMER_REPEAT_TICKS;
|
||||||
next = now + TIMER_DEFER_REPEAT_TICKS;
|
return now + TIMER_DEFER_REPEAT_TICKS;
|
||||||
|
|
||||||
done:
|
|
||||||
timer_set(next);
|
|
||||||
return 1;
|
|
||||||
fail:
|
|
||||||
shutdown("Rescheduled timer in the past");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke timers - called from board irq code.
|
// Invoke timers - called from board irq code.
|
||||||
void
|
uint32_t
|
||||||
timer_dispatch_many(void)
|
timer_dispatch_many(void)
|
||||||
{
|
{
|
||||||
|
uint32_t tru = timer_repeat_until;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
uint32_t next_waketime = sched_timer_dispatch();
|
// Run the next software timer
|
||||||
|
uint32_t next = sched_timer_dispatch();
|
||||||
|
|
||||||
// Schedule next timer event (or run next timer if it's ready)
|
uint32_t now = timer_read_time();
|
||||||
int res = timer_try_set_next(next_waketime);
|
int32_t diff = next - now;
|
||||||
if (res)
|
if (diff > (int32_t)TIMER_MIN_TRY_TICKS)
|
||||||
break;
|
// Schedule next timer normally.
|
||||||
|
return next;
|
||||||
|
|
||||||
|
if (unlikely(timer_is_before(tru, now)))
|
||||||
|
// Too many repeat timers from a single interrupt - force a pause
|
||||||
|
return force_defer(next);
|
||||||
|
|
||||||
|
// Next timer in the past or near future - wait for it to be ready
|
||||||
|
irq_enable();
|
||||||
|
while (unlikely(diff > 0))
|
||||||
|
diff = next - timer_read_time();
|
||||||
|
irq_disable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue