Repository navigation
✨ (minor) v6: Add hal::basic_lap_counter for 64-bit totals - #141
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
hal::basic_lap_counter<Laps>, so a lap counter can produce a correct 64-bit total.hal::lap_counterbecomes an alias forbasic_lap_counter<hal::u32>, so existing code and its size stay the same.The problem
lap_counterstores its lap count as ahal::u32. Withtotal<hal::u64>(), the total islaps * lap_length + position, so it wraps back to 0 after 2^32 laps instead of at theu64maximum. 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 foru32results.The lap length is a
hal::u32too, so two chained 16-bit timers can't uselap_countereither: their lap is the whole 32-bit count, 2^32.The change
Lapssets the type of both the lap count and the lap length:hal::u32orhal::u64. Astatic_assertrejects anything else.total()'s result type now defaults toLaps. The docs say a result wraps at its own maximum only whenCountis no wider thanLaps.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:
dma23,632 B,uart16,888 B andtimer23,752 B, all unchanged. They usehal::lap_counter.For comparison, making the lap count
u64outright added 40 to 48 B of code to every demo and 36 B of RAM. That's why this is a template, not a widerlap_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. Onmain, the same test withlap_counterfails 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).