Skip to content

The known bugs, each shown by a test first (OIL 2) - #5

Merged
theAndreas merged 4 commits into
mainfrom
bugs
Nov 10, 2026
Merged

theAndreas merged 4 commits into
mainfrom
bugs

Conversation

@theAndreas

@theAndreas theAndreas commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

The second of the steps in OIL.md: the bugs found while bringing TimerTwo onto the template, each shown by a test that failed on main before it was fixed. The API stays as it is; version 2.0.0 (OIL 3) is where it changes.

What changes for a sketch

  • Resuming a stopped timer lets it run again; before, it stayed marked stopped, so reading the time was refused and starting it began again from zero.
  • Stopping a timer that was never set up does nothing; before, it could then be started without a period.
  • Detaching the interrupt lasts: starting the timer again no longer brings the callback back.
  • The full duty keeps the output high and the empty one keeps it low; the duty is a share of 255 now, rounded, where it was a share of 256 that never reached the top. A new period keeps the share.
  • Reading the time keeps every step of the counter; at 16 MHz it lost up to a microsecond and more at the larger prescalers.
  • The period is right at any clock: below 2 MHz every period became a standing counter, and at 20 MHz the longest period was cut short.
  • A period too short for one step is refused, by setting up the timer as well as by changing the period; before, the counter stood still and starting it waited forever.
  • Two more, found on the way, that only the board can show: attaching a callback while the timer runs could let the interrupt call through a half-written address, and the duty overflowed the board's 16-bit integer arithmetic.

What was measured

  • 52 tests, 18 of them failing on the code of main and two of those hanging; all pass now, by GCC, by clang, under the sanitizers and with coverage. The tests of the clocks run in builds of their own at 1, 16 and 20 MHz, with expectations computed apart from the library's arithmetic.
  • A test that hangs fails after 10 seconds rather than stopping the run.
  • make check passes; coverage of the library 98 % of the lines.
  • Flash and RAM on the Uno grow a little: the basic example by 82 bytes of flash and one of RAM, the one that reads the time by 104 and one, for the kept duty and the exact arithmetic.
  • Not on hardware.

Not in this pull request

The self check in simavr, planned for this step, is OIL 5 now: the simavr of the dev container, 1.6, does not count this timer in its phase-correct modes at all, measured with the registers alone. Nor does the latest release, 1.8, built from source for the measurement: it lists the modes, but the counter stands, the PWM pin is never driven, and the overflow interrupt seems to fire without end. OIL 5 waits for a simavr that models them.

🤖 Generated with Claude Code

theAndreas and others added 4 commits November 10, 2026 21:41
Each of these had a test that failed on the code of main, 18 in all, two of them by hanging:

- resume() lets the timer run again, rather than leaving it marked stopped.
- stop() stops only a timer that runs: before init() it took the timer for initialized.
- detachInterrupt() forgets the callback, so that start() does not enable the interrupt again.
- A duty of 255 keeps the output high, as 0 keeps it low: the duty is a share of 255 of the
  top, rounded, no longer of 256. setPeriod() applies it to the new top.
- read() multiplies before it divides, and keeps the steps below 16 at 16 MHz.
- The period and its longest value are computed so that the clocks below 2 MHz and at 20 MHz
  are right; a test of them runs at 1, 16 and 20 MHz, each a build of its own.
- A period too short for one step of the counter is refused: its top of 0 stopped the counter,
  and start() waited for it forever. init() with a period it refuses leaves the timer as it was.

Two more, found while fixing these, which no test on the host can show: attachInterrupt() keeps
the interrupt off while it writes the pointer, which the AVR writes a byte at a time; and the
duty is computed in 16 bits unsigned, as 255 * 255 overflows the AVR's int.

A test that hangs fails after 10 s rather than stopping the run.

Flash and RAM on the Uno grow: Basics from 1290 / 25 to 1372 / 26 bytes, Read from 2790 / 210
to 2894 / 211, for the duty kept across a period and the exact arithmetic.

The self check in simavr is an item of its own now, OIL 5: simavr 1.6 of the dev container
leaves TCNT2 at 0 in the phase correct modes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ither

Measured with the registers alone in simavr 1.6 and in 1.8 built from source: TCNT2 stays at 0
in modes 1 and 5, OC2B never drives pin 3, and the overflow interrupt seems to fire without end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…orted in simavr #442

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@theAndreas
theAndreas merged commit 1b45697 into main Nov 10, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant