Skip to content

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

@WestfW

Description

@WestfW

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions