Skip to content

R4 delay() does not properly handle millis() wrap-around #506

@WestfW

Description

@WestfW

Reading the source code for the renesas core, I find in time.cpp:

__attribute__((weak)) void delay(uint32_t ms)
{
  auto const start = millis();
  auto const stop = start + ms;
  while(millis() < stop) yield();
}

That's exactly the way we're taught NOT to implement delay, and it doesn't handle the wrap of millis() corrected (ie when start+ms is larger than 32bits, and thus stop will be truncated and millis() will immediately be greater than stop.)

(Also, this code has the "delay might be up to just less than 1ms shorter than expected, because of the quantization of millis(). This was fixed for AVR quite some time ago by having the loop check micros...)

Additional context

Related discussion:
https://forum.arduino.cc/t/r4-delay-does-not-properly-handle-millis-wrap-around/1416399

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions