Clean up delay functions and rename them to be consistent with

what was used before the inline delay approach was attempted.
This commit is contained in:
Jim McGee 2011-05-10 22:19:45 -07:00 committed by Michael Moon
parent 757e1258e2
commit 1059f27592
2 changed files with 46 additions and 12 deletions

21
delay.c
View File

@ -4,20 +4,33 @@
\brief Delay routines \brief Delay routines
*/ */
#include <util/delay_basic.h> //#include <util/delay_basic.h>
#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 /// delay microseconds
/// \param delay time to wait in microseconds /// \param delay time to wait in microseconds
void delay_us(uint16_t delay) { void _delay(uint32_t delay) {
wd_reset(); wd_reset();
while (delay > (65536L / (F_CPU / 4000000L))) { 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_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)); delay -= (65536L / (F_CPU / 4000000L));
wd_reset(); wd_reset();
} }
_delay_loop_2(delay / (F_CPU / 4000000L)); _delay_loop_2(delay * (F_CPU / 4000000L));
wd_reset(); wd_reset();
} }

37
delay.h
View File

@ -3,28 +3,49 @@
#include <stdint.h> #include <stdint.h>
#include <util/delay_basic.h> #include <util/delay_basic.h>
#include "watchdog.h"
#define WAITING_DELAY 100 #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); 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); 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)); static void delay(uint32_t) __attribute__ ((always_inline));
inline void delay(uint32_t d) { inline void delay(uint32_t d) {
if (d >= (65536L / (F_CPU / 4000000L))) { if (d > (65536L / (F_CPU / 4000000L))) {
delay_us(d); _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)); static void delay_ms(uint32_t) __attribute__ ((always_inline));
inline void delay_ms(uint32_t d) { inline void delay_ms(uint32_t d) {
if (d > 65) if (d > 65)
delay_ms(d); _delay_ms(d);
else else
delay_us(d * 1000); delay(d * 1000);
} }
#endif /* _DELAY_H */ #endif /* _DELAY_H */