Skip to content

Commit 7afc0bd

Browse files
committed
delay() with micros()
1 parent aaf8a32 commit 7afc0bd

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/ArduinoShrink.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ inline void delayMicroseconds(unsigned int us)
1212
_delay_us(us);
1313
}
1414

15-
extern "C" uint32_t micros_raw();
16-
inline uint32_t micros()
17-
{
18-
/*
19-
register uint32_t micros_ asm("r22");
20-
asm ("call micros_raw" : "=r" (micros_) );
21-
return micros_;
22-
*/
23-
// asm volatile ("call micros_raw");
24-
return micros_raw() * 4;
25-
}
26-
2715
extern "C" uint32_t millis_raw();
2816
inline uint32_t millis()
2917
{

src/millis.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ struct {
88
uint8_t ovfl_pad;
99
} t0_millis;
1010

11+
extern uint32_t micros_raw();
12+
uint32_t micros()
13+
{
14+
register uint32_t u asm("r22");
15+
asm ("%~call %x1" : "=r" (u) : "i"(micros_raw) : "r30", "r31" );
16+
return u * 4;
17+
}
18+
1119
// portability macros for mega8
1220
#ifndef TCCR0B
1321
#define TCCR0B TCCR0

src/wiring.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@
44
#include <Arduino.h>
55
#include <util/delay.h>
66

7+
/*
78
__attribute ((noinline))
89
void delay(unsigned long ms)
910
{
1011
do {
1112
_delay_us(999); // leave 1us for loop overhead
1213
} while (--ms);
1314
}
15+
*/
16+
17+
void delay(unsigned long ms)
18+
{
19+
// lower 16-bits of micros is enough considering wraparound
20+
uint16_t start = micros();
21+
22+
while (ms > 0) {
23+
yield();
24+
while (ms > 0 && ((uint16_t)micros() - start) >= 1000) {
25+
ms --;
26+
start += 1000;
27+
}
28+
}
29+
}
1430

1531
// avr-libc math.h does not declare gcc log2 builtin
1632
double log2(double);

0 commit comments

Comments
 (0)