From 9c489911b6a3549b982d9ad24665a740259b504c Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Thu, 17 Nov 2011 13:33:31 +0100 Subject: [PATCH] timer.c: get rid of explicitely disabling the step timer. This is no longer neccessary, as the step timer is not designed to fire always exactly once. Saves 54 bytes binary size. --- dda_queue.c | 3 -- timer.c | 82 +++++++++++++++++++++++------------------------------ 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/dda_queue.c b/dda_queue.c index 854ee5e..784b54f 100644 --- a/dda_queue.c +++ b/dda_queue.c @@ -167,9 +167,6 @@ void next_move() { dda_start(current_movebuffer); } } - if (queue_empty()) - setTimer(0); - } /// DEBUG - print queue. diff --git a/timer.c b/timer.c index 7c9b991..9aa3a3a 100644 --- a/timer.c +++ b/timer.c @@ -138,56 +138,46 @@ void setTimer(uint32_t delay) // re-enable clock interrupt in case we're recovering from emergency stop TIMSK1 |= MASK(OCIE1B); - if (delay > 0) { + // if the delay is too small use a minimum delay so that there is time + // to set everything up before the timer expires. - // if the delay is too small use a minimum delay so that there is time - // to set everything up before the timer expires. + if (delay < 17 ) + delay = 17; - if (delay < 17 ) - delay = 17; - - // Assume all steps belong to one move. Within one move the delay is - // from one step to the next one, which should be more or less the same - // as from one step interrupt to the next one. The last step interrupt happend - // at OCR1A, so start delay from there. - step_start = OCR1A; - if (next_step_time == 0) { - // new move, take current time as start value - step_start = TCNT1; - } - - next_step_time = delay; - if (next_step_time < 65536) { - // set the comparator directly to the next real step - OCR1A = (next_step_time + step_start) & 0xFFFF; - } - else if (next_step_time < 75536) { - // Next comparator interrupt would have to trigger another - // interrupt within a short time (possibly within 1 cycle). - // Avoid the impossible by firing the interrupt earlier. - OCR1A = (step_start - 10000) & 0xFFFF; - next_step_time += 10000; - } - else { - OCR1A = step_start; - } - - // Enable this interrupt, but only do it after disabling - // global interrupts. This will cause push any possible - // timer1a interrupt to the far side of the return, protecting the - // stack from recursively clobbering memory. - cli(); - CLI_SEI_BUG_MEMORY_BARRIER(); - TIMSK1 |= MASK(OCIE1A); - - } else { - // TODO: as the interrupt is designed to fire only once, - // doing a setTimer(0) should be obsolete. - // flag: move has ended - next_step_time = 0; - TIMSK1 &= ~MASK(OCIE1A); + // Assume all steps belong to one move. Within one move the delay is + // from one step to the next one, which should be more or less the same + // as from one step interrupt to the next one. The last step interrupt happend + // at OCR1A, so start delay from there. + step_start = OCR1A; + if (next_step_time == 0) { + // new move, take current time as start value + step_start = TCNT1; } + next_step_time = delay; + if (next_step_time < 65536) { + // set the comparator directly to the next real step + OCR1A = (next_step_time + step_start) & 0xFFFF; + } + else if (next_step_time < 75536) { + // Next comparator interrupt would have to trigger another + // interrupt within a short time (possibly within 1 cycle). + // Avoid the impossible by firing the interrupt earlier. + OCR1A = (step_start - 10000) & 0xFFFF; + next_step_time += 10000; + } + else { + OCR1A = step_start; + } + + // Enable this interrupt, but only do it after disabling + // global interrupts. This will cause push any possible + // timer1a interrupt to the far side of the return, protecting the + // stack from recursively clobbering memory. + cli(); + CLI_SEI_BUG_MEMORY_BARRIER(); + TIMSK1 |= MASK(OCIE1A); + // restore interrupt flag MEMORY_BARRIER(); SREG = sreg;