arduino / arduino/ArduinoCore-avr

delayMicroseconds() is broken in AVR core versions that do LTO...

Open
#118 17 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Recent versions of the Arduino IDE/Core for AVR do Link Time Optimization, which means that calls to delayMicroseconds() where the argument is constant are likely to be optimized to the point where they become significantly inaccurate for small numbers...

The simplest solution is probably to define delayMicroseconds() with a "never inline" attribute, but it would probably be a better idea to use libc and/or compiler built-ins instead.

test sketch:
```
void setup() {
}

void loop() {
byte x;
x = PORTB;
delayMicroseconds(1);
x = PORTB;
delayMicroseconds(2);
x = PORTB;
delayMicroseconds(3);
x = PORTB;
delayMicroseconds(100);
x = PORTB;
delayMicroseconds(2334);
x = PORTB;
delayMicroseconds(x);
x = PORTB;
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the supplied test sketch with an AVR core build that uses Link Time Optimization, focusing on delayMicroseconds() calls with constant and variable arguments. Compare the timing for the listed delays, then inspect the delayMicroseconds() implementation and compiler behavior. Done means small constant delays remain accurate while the existing sketch behavior is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.