diff --git a/delay.c b/delay.c index 0468c02..bf0be85 100644 --- a/delay.c +++ b/delay.c @@ -4,20 +4,33 @@ \brief Delay routines */ -#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)); +} /// delay microseconds /// \param delay time to wait in microseconds -void delay_us(uint16_t delay) { +void _delay(uint32_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 delay -= (65536L / (F_CPU / 4000000L)); wd_reset(); } - _delay_loop_2(delay / (F_CPU / 4000000L)); + _delay_loop_2(delay * (F_CPU / 4000000L)); wd_reset(); } diff --git a/delay.h b/delay.h index 4631928..e507199 100644 --- a/delay.h +++ b/delay.h @@ -3,28 +3,49 @@ #include #include +#include "watchdog.h" #define WAITING_DELAY 100 +#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_us(d); + if (d > (65536L / (F_CPU / 4000000L))) { + _delay(d); + } + else { + wd_reset(); + if( d ) { + _delay_loop_2(d * (F_CPU / 4000000L)); + wd_reset(); + } } - else - _delay_loop_2(d * (F_CPU / 4000000L)); } +// millisecond 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_ms(uint32_t) __attribute__ ((always_inline)); inline void delay_ms(uint32_t d) { if (d > 65) - delay_ms(d); + _delay_ms(d); else - delay_us(d * 1000); -} - + delay(d * 1000); + } #endif /* _DELAY_H */