From a2ce509bed2242fceaad9a9fd98f35fcfb16e21e Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Tue, 19 Mar 2013 20:41:35 +0100 Subject: [PATCH] Eliminate _delay(), delay() and _delay_us(). Doesn't change much, even the binary size stays the same. Much cleaner code though, now we have only delay_us() and delay_ms(). --- dda_queue.c | 2 +- delay.c | 23 +++++++---------------- delay.h | 29 ----------------------------- sd.c | 10 +++++----- 4 files changed, 13 insertions(+), 51 deletions(-) diff --git a/dda_queue.c b/dda_queue.c index 7674c9f..2b9e646 100644 --- a/dda_queue.c +++ b/dda_queue.c @@ -94,7 +94,7 @@ void queue_step() { void enqueue_home(TARGET *t, uint8_t endstop_check, uint8_t endstop_stop_cond) { // don't call this function when the queue is full, but just in case, wait for a move to complete and free up the space for the passed target while (queue_full()) - delay(100); + delay_us(100); uint8_t h = mb_head + 1; h &= (MOVEBUFFER_SIZE - 1); diff --git a/delay.c b/delay.c index 8c24364..713d59f 100644 --- a/delay.c +++ b/delay.c @@ -4,26 +4,17 @@ \brief Delay routines */ -//#include +#include +#include +#include "watchdog.h" -//#include "watchdog.h" - - -/// interruptable microsecond delay -/// does NOT call wd_reset -/// \param delay time in microseconds -void delay_us( uint16_t delay ) -{ - while (delay > (65536UL / (F_CPU / 4000000UL))) { - _delay_loop_2(65534); // - delay -= (65536L / (F_CPU / 4000000L)); - } - _delay_loop_2( delay * (F_CPU / 4000000UL)); -} +#if F_CPU < 4000000UL +#error Delay functions only work with F_CPU >= 4000000UL +#endif /// delay microseconds /// \param delay time to wait in microseconds -void _delay(uint32_t delay) { +void delay_us(uint16_t delay) { wd_reset(); while (delay > (65536L / (F_CPU / 4000000L))) { _delay_loop_2(65534); // we use 65534 here to compensate for the time that the surrounding loop takes. TODO: exact figure needs tuning diff --git a/delay.h b/delay.h index 58c5cb1..b044839 100644 --- a/delay.h +++ b/delay.h @@ -1,39 +1,10 @@ #ifndef _DELAY_H #define _DELAY_H -#include -#include -#include "watchdog.h" - -#if F_CPU < 4000000UL -#error Delay functions only work with F_CPU >= 4000000UL -#endif - // microsecond delay, does NOT reset WDT if feature enabled void delay_us(uint16_t delay); -// microsecond delay, does reset WDT if feature enabled -void _delay(uint32_t delay); - // millisecond delay, does reset WDT if feature enabled void delay_ms(uint32_t delay); - -// microsecond timer, does reset WDT if feature enabled -// 0 results in no real delay, but the watchdog -// reset is called if the feature is enabled -static void delay(uint32_t) __attribute__ ((always_inline)); -inline void delay(uint32_t d) { - if (d > (65536L / (F_CPU / 4000000L))) { - _delay(d); - } - else { - wd_reset(); - if( d ) { - _delay_loop_2(d * (F_CPU / 4000000L)); - wd_reset(); - } - } -} - #endif /* _DELAY_H */ diff --git a/sd.c b/sd.c index b4fd92d..ebfefdc 100644 --- a/sd.c +++ b/sd.c @@ -120,7 +120,7 @@ byte sdc_initialize(void) { if (retries >= 0xFF) { return(NULL); // timed out! } - delay(5); + delay_ms(5); } // at this stage, the card is in idle mode and ready for start up retries = 0; @@ -159,7 +159,7 @@ void sdc_readRegister(byte sentCommand) { byte retries=0x00; byte res=sdc_cmd(sentCommand, 0); while(res != 0x00) { - delay(1); + delay_ms(1); retries++; if (retries >= 0xFF) return; // timed out! res=spi_cmd(0xFF); // retry @@ -180,7 +180,7 @@ void sdc_readRegister(byte sentCommand) { void sdc_writeBlock(long blockIndex) { byte retries=0; while(sdc_cmd(WRITE_BLOCK, blockIndex * blockSize) != 0x00) { - delay(1); + delay_ms(1); retries++; if (retries >= 0xFF) return; // timed out! } @@ -196,7 +196,7 @@ void sdc_writeBlock(long blockIndex) { spi_cmd(0xFF); // LSB spi_cmd(0xFF); // MSB // wait until write is finished - while (spi_cmd(0xFF) != 0xFF) delay(1); // kind of NOP + while (spi_cmd(0xFF) != 0xFF) delay_ms(1); // kind of NOP } // read block on SD card and copy data in block vector @@ -205,7 +205,7 @@ void sdc_readBlock(long blockIndex) { byte retries = 0x00; byte res = sdc_cmd(READ_SINGLE_BLOCK, (blockIndex * blockSize)); while(res != 0x00) { - delay(1); + delay_ms(1); retries++; if (retries >= 0xFF) return; // timed out! res=spi_cmd(0xFF); // retry