Skip to content

Commit 1059f27

Browse files
madscifitriffid
authored andcommitted
Clean up delay functions and rename them to be consistent with
what was used before the inline delay approach was attempted.
1 parent 757e125 commit 1059f27

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

delay.c

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@
44
\brief Delay routines
55
*/
66

7-
#include <util/delay_basic.h>
7+
//#include <util/delay_basic.h>
88

9-
#include "watchdog.h"
9+
//#include "watchdog.h"
10+
11+
12+
/// interruptable microsecond delay
13+
/// does NOT call wd_reset
14+
/// \param delay time in microseconds
15+
void delay_us( uint16_t delay )
16+
{
17+
while (delay > (65536UL / (F_CPU / 4000000UL))) {
18+
_delay_loop_2(65534); //
19+
delay -= (65536L / (F_CPU / 4000000L));
20+
}
21+
_delay_loop_2( delay * (F_CPU / 4000000UL));
22+
}
1023

1124
/// delay microseconds
1225
/// \param delay time to wait in microseconds
13-
void delay_us(uint16_t delay) {
26+
void _delay(uint32_t delay) {
1427
wd_reset();
1528
while (delay > (65536L / (F_CPU / 4000000L))) {
1629
_delay_loop_2(65534); // we use 65534 here to compensate for the time that the surrounding loop takes. TODO: exact figure needs tuning
1730
delay -= (65536L / (F_CPU / 4000000L));
1831
wd_reset();
1932
}
20-
_delay_loop_2(delay / (F_CPU / 4000000L));
33+
_delay_loop_2(delay * (F_CPU / 4000000L));
2134
wd_reset();
2235
}
2336

delay.h

+29-8
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,49 @@
33

44
#include <stdint.h>
55
#include <util/delay_basic.h>
6+
#include "watchdog.h"
67

78
#define WAITING_DELAY 100
89

10+
#if F_CPU < 4000000UL
11+
#error Delay functions only work with F_CPU >= 4000000UL
12+
#endif
13+
14+
// microsecond delay, does NOT reset WDT if feature enabled
915
void delay_us(uint16_t delay);
1016

17+
// microsecond delay, does reset WDT if feature enabled
18+
void _delay(uint32_t delay);
19+
20+
// millisecond delay, does reset WDT if feature enabled
1121
void _delay_ms(uint32_t delay);
1222

23+
24+
// microsecond timer, does reset WDT if feature enabled
25+
// 0 results in no real delay, but the watchdog
26+
// reset is called if the feature is enabled
1327
static void delay(uint32_t) __attribute__ ((always_inline));
1428
inline void delay(uint32_t d) {
15-
if (d >= (65536L / (F_CPU / 4000000L))) {
16-
delay_us(d);
29+
if (d > (65536L / (F_CPU / 4000000L))) {
30+
_delay(d);
31+
}
32+
else {
33+
wd_reset();
34+
if( d ) {
35+
_delay_loop_2(d * (F_CPU / 4000000L));
36+
wd_reset();
37+
}
1738
}
18-
else
19-
_delay_loop_2(d * (F_CPU / 4000000L));
2039
}
2140

41+
// millisecond timer, does reset WDT if feature enabled
42+
// 0 results in no real delay, but the watchdog
43+
// reset is called if the feature is enabled
2244
static void delay_ms(uint32_t) __attribute__ ((always_inline));
2345
inline void delay_ms(uint32_t d) {
2446
if (d > 65)
25-
delay_ms(d);
47+
_delay_ms(d);
2648
else
27-
delay_us(d * 1000);
28-
}
29-
49+
delay(d * 1000);
50+
}
3051
#endif /* _DELAY_H */

0 commit comments

Comments
 (0)