Skip to content

✨ (minor) v6: Add hal::basic_lap_counter for 64-bit totals - #141

Merged
kammce merged 1 commit into
mainfrom
v6-basic-lap-counter
Nov 29, 2026
Merged

kammce merged 1 commit into
mainfrom
v6-basic-lap-counter

Conversation

@kammce

@kammce kammce commented Nov 27, 2026

Copy link
Copy Markdown
Member

Adds hal::basic_lap_counter<Laps>, so a lap counter can produce a correct 64-bit total. hal::lap_counter becomes an alias for basic_lap_counter<hal::u32>, so existing code and its size stay the same.

The problem

lap_counter stores its lap count as a hal::u32. With total<hal::u64>(), the total is laps * lap_length + position, so it wraps back to 0 after 2^32 laps instead of at the u64 maximum. For a 16-bit timer used as a steady clock, that is 2^48 ticks: 45 days at 72 MHz with no prescaler. A steady clock must never go backwards. The documentation said a result wraps at its own maximum, which held only for u32 results.

The lap length is a hal::u32 too, so two chained 16-bit timers can't use lap_counter either: their lap is the whole 32-bit count, 2^32.

The change

hal::lap_counter dma_laps;                          // basic_lap_counter<hal::u32>, as before
hal::basic_lap_counter<hal::u64> timer_laps;        // 64-bit lap count and lap length
auto const ticks = timer_laps.total(1ULL << 32, lap_finished, position);  // hal::u64
  • Laps sets the type of both the lap count and the lap length: hal::u32 or hal::u64. A static_assert rejects anything else.
  • total()'s result type now defaults to Laps. The docs say a result wraps at its own maximum only when Count is no wider than Laps.
  • Only the class is a template, so a program instantiates at most two classes.

A 64-bit lap count can tear when read on a 32-bit core. total() already reads the lap count before and after the position, and reads again if they differ, which catches a tear. The class docs describe the one case it misses: the interrupt counts a lap during both reads, and the second lap carries into the upper half. That needs laps about as short as a read and happens at most once every 2^32 laps.

Size

LLVM 23, MinSizeRel, stm32f103c8, libhal-arm-mcu v2 demos built against this branch: dma 23,632 B, uart 16,888 B and timer 23,752 B, all unchanged. They use hal::lap_counter.

For comparison, making the lap count u64 outright added 40 to 48 B of code to every demo and 36 B of RAM. That's why this is a template, not a wider lap_counter.

Tested

Two new host tests. All 18 tests pass, and clang-tidy is clean.

  • basic_lap_counter<u64> counts 2^32 + 1 laps and returns the exact 64-bit total. On main, the same test with lap_counter fails with 65,552 instead of 281,474,976,776,208. The test counts the laps one at a time, which takes about 1.6 s.
  • basic_lap_counter<u64> takes a 2^32 count lap, with a lap waiting for the interrupt.

Needed by libhal-arm-mcu's timer steady clocks (#275 and #276 there).

lap_counter counts laps in a u32, so a u64 total from it wraps back to
0 after 2^32 laps instead of at the u64 maximum. A 16-bit timer steady
clock with no prescaler gets there in 45 days at 72 MHz. Its lap length
is also a u32, so two chained 16-bit timers (a 2^32 count lap) can't
use it.

basic_lap_counter<Laps> takes the lap count and lap length type, u32 or
u64. lap_counter is now an alias for basic_lap_counter<u32>, so its
users and their code size are unchanged.

@kammce kammce left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅

@kammce
kammce merged commit 2847083 into main Nov 29, 2026
9 of 10 checks passed
@kammce
kammce deleted the v6-basic-lap-counter branch November 29, 2026 15:12
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