From 6d05dd07f59ef21dc81a299e865cdee1fc1d3f8e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 27 Mar 2017 16:38:01 -0400 Subject: [PATCH] sched: Move timer dispatch loop to board code Rename sched_timer_kick() to sched_timer_dispatch() and move its loop into its callers in the board code. This eliminates the need to export timer_try_set_next() from the board code. Signed-off-by: Kevin O'Connor --- docs/Code_Overview.md | 6 ++--- src/avr/timer.c | 20 ++++++++++----- src/generic/misc.h | 1 - src/generic/timer.c | 16 +++++++++++- src/sam3x8e/timer.c | 5 +++- src/sched.c | 57 ++++++++++++++++++++----------------------- src/sched.h | 2 +- src/simulator/main.c | 6 ----- 8 files changed, 63 insertions(+), 50 deletions(-) diff --git a/docs/Code_Overview.md b/docs/Code_Overview.md index ee640457..8b1ca06c 100644 --- a/docs/Code_Overview.md +++ b/docs/Code_Overview.md @@ -58,9 +58,9 @@ Timer functions are scheduled by calling sched_add_timer() (located in **src/sched.c**). The scheduler code will arrange for the given function to be called at the requested clock time. Timer interrupts are initially handled in an architecture specific interrupt handler -(eg, **src/avr/timer.c**), but this just calls sched_timer_kick() -located in **src/sched.c**. The timer interrupt leads to execution of -schedule timer functions. Timer functions always run with interrupts +(eg, **src/avr/timer.c**) which calls sched_timer_dispatch() located +in **src/sched.c**. The timer interrupt leads to execution of schedule +timer functions. Timer functions always run with interrupts disabled. The timer functions should always complete within a few micro-seconds. At completion of the timer event, the function may choose to reschedule itself. diff --git a/src/avr/timer.c b/src/avr/timer.c index e9ec8dd0..f8686998 100644 --- a/src/avr/timer.c +++ b/src/avr/timer.c @@ -70,11 +70,6 @@ timer_repeat_set(uint16_t next) TIFR1 = 1<TC_CHANNEL[0].TC_SR; // read to clear irq pending if (likely(status & TC_SR_CPAS)) - sched_timer_kick(); + timer_dispatch_many(); irq_enable(); } diff --git a/src/sched.c b/src/sched.c index 2b96465f..8617108a 100644 --- a/src/sched.c +++ b/src/sched.c @@ -135,39 +135,34 @@ sched_del_timer(struct timer *del) irq_restore(flag); } -// Invoke timers - called from board timer irq code. -void -sched_timer_kick(void) +// Invoke the next timer - called from board hardware irq code. +unsigned int +sched_timer_dispatch(void) { - for (;;) { - // Invoke timer callback - struct timer *t = timer_list; - uint_fast8_t res; - uint32_t updated_waketime; - if (CONFIG_INLINE_STEPPER_HACK && likely(!t->func)) { - res = stepper_event(t); - updated_waketime = t->waketime; - } else { - res = t->func(t); - updated_waketime = t->waketime; - } - - // Update timer_list (rescheduling current timer if necessary) - unsigned int next_waketime = updated_waketime; - if (unlikely(res == SF_DONE)) { - next_waketime = t->next->waketime; - timer_list = t->next; - } else if (!timer_is_before(updated_waketime, t->next->waketime)) { - next_waketime = t->next->waketime; - timer_list = t->next; - insert_timer(t, updated_waketime); - } - - // Schedule next timer event (or run next timer if it's ready) - res = timer_try_set_next(next_waketime); - if (res) - break; + // Invoke timer callback + struct timer *t = timer_list; + uint_fast8_t res; + uint32_t updated_waketime; + if (CONFIG_INLINE_STEPPER_HACK && likely(!t->func)) { + res = stepper_event(t); + updated_waketime = t->waketime; + } else { + res = t->func(t); + updated_waketime = t->waketime; } + + // Update timer_list (rescheduling current timer if necessary) + unsigned int next_waketime = updated_waketime; + if (unlikely(res == SF_DONE)) { + next_waketime = t->next->waketime; + timer_list = t->next; + } else if (!timer_is_before(updated_waketime, t->next->waketime)) { + next_waketime = t->next->waketime; + timer_list = t->next; + insert_timer(t, updated_waketime); + } + + return next_waketime; } // Shutdown all user timers on an emergency stop. diff --git a/src/sched.h b/src/sched.h index 7a98257a..d2d3471e 100644 --- a/src/sched.h +++ b/src/sched.h @@ -25,7 +25,7 @@ enum { SF_DONE=0, SF_RESCHEDULE=1 }; uint8_t sched_check_periodic(uint16_t time, uint16_t *pnext); void sched_add_timer(struct timer*); void sched_del_timer(struct timer *del); -void sched_timer_kick(void); +unsigned int sched_timer_dispatch(void); uint8_t sched_is_shutdown(void); uint16_t sched_shutdown_reason(void); void sched_clear_shutdown(void); diff --git a/src/simulator/main.c b/src/simulator/main.c index eb3025ca..42fb7a25 100644 --- a/src/simulator/main.c +++ b/src/simulator/main.c @@ -75,12 +75,6 @@ timer_read_time(void) return 0; // XXX } -uint8_t -timer_try_set_next(unsigned int next) -{ - return 1; -} - /**************************************************************** * Turn stdin/stdout into serial console