arduino / arduino/ArduinoCore-API

Add API to advance millis() and micros()

Open
#69 2 comments 3 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
306
Forks
150
PR merge metrics
No merged PRs in 30d

Description

When an AVR MCU sleeps in power-down mode, it's clock stops running. Typically time is being kept using the watchdog timer, or an asynchronous timer2, to allow waking up after a specified time. However, the `millis()` and `micros()` counters are not updated during sleep, meaning that the sketch needs to jump through hoops (manually offsetting these counters, or modifying Arduino's global variables) to make various timing work. The former only works with sketch code, not libraries that rely on both counters, and the latter is obviously very ugly and non-portable.

Ideally, there would be an API that allows modifying the millis() and micros() timers by adding (or perhaps also subtracting?) a specified time from both. Most obvious would be an int32_t specifying the number of microseconds. This allows offsetting the time by about 35 minutes in one call. Alternatively, specifying both millis and micros separately would increase the range, but also complicate the code a bit (needing actual division instead of just bitshifting, I think). In either case, I'd say that both counters are always updated at the same time and stay in sync, not being updated individually.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the declarations and implementations of millis() and micros(). Review how both counters are kept in sync, then resolve whether the API should add or subtract a shared int32_t microsecond offset or accept separate millisecond and microsecond values. Done means sleep-related code can advance both timers together without modifying Arduino globals.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.