arduino / arduino/ArduinoCore-renesas

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

Open
#506 0 comments 0 reactions 0 assignees View on GitHub
topic: code type: imperfection
Dominant language
C
Stars
193
Forks
112
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in time.cpp at the delay(uint32_t ms) implementation shown in the issue, then review the related Arduino forum discussion for the reported rollover behavior and millis() quantization concern. Done means delay remains correct when millis() wraps and does not return early for a duration that crosses the 32-bit boundary.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.