|
3 | 3 |
|
4 | 4 | #include <stdint.h>
|
5 | 5 | #include <util/delay_basic.h>
|
| 6 | +#include "watchdog.h" |
6 | 7 |
|
7 | 8 | #define WAITING_DELAY 100
|
8 | 9 |
|
| 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 |
9 | 15 | void delay_us(uint16_t delay);
|
10 | 16 |
|
| 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 |
11 | 21 | void _delay_ms(uint32_t delay);
|
12 | 22 |
|
| 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 |
13 | 27 | static void delay(uint32_t) __attribute__ ((always_inline));
|
14 | 28 | 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 | + } |
17 | 38 | }
|
18 |
| - else |
19 |
| - _delay_loop_2(d * (F_CPU / 4000000L)); |
20 | 39 | }
|
21 | 40 |
|
| 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 |
22 | 44 | static void delay_ms(uint32_t) __attribute__ ((always_inline));
|
23 | 45 | inline void delay_ms(uint32_t d) {
|
24 | 46 | if (d > 65)
|
25 |
| - delay_ms(d); |
| 47 | + _delay_ms(d); |
26 | 48 | else
|
27 |
| - delay_us(d * 1000); |
28 |
| -} |
29 |
| - |
| 49 | + delay(d * 1000); |
| 50 | + } |
30 | 51 | #endif /* _DELAY_H */
|
0 commit comments