1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Pure tick<->time arithmetic for the timer Counter.
//
// Like `baud.rs`, this file is intentionally dependency-free (pure `core`
// integer arithmetic, no PAC/HAL types) so the exact same source can be
// `include!`d by the host-side test crate in `unit_tests/`. The `Counter`
// methods are thin wrappers over these functions, so a regression in the
// conversion math fails the host tests. Do not add external `use`s here.
// (Regular `//` comments, not `//!`, so the file can be `include!`d mid-crate.)
/// Convert a tick delta to microseconds at `tick_hz` ticks/second.
///
/// Computed in `u64` so `ticks * 1_000_000` cannot overflow (the worst case,
/// `u32::MAX * 1_000_000 ≈ 4.3e15`, far exceeds `u32`); the divide brings it
/// back into range. The `u32` result therefore holds up to ~4295 s of µs.
/// Truncates toward zero (one tick of resolution at the configured rate).
pub
/// Convert a tick delta to nanoseconds at `tick_hz` ticks/second.
///
/// `ticks * 1_000_000_000` reaches ~4.3e18, still within `u64`. The `u32` result
/// only spans ~4.3 s of ns, so this is for short deltas; resolution is one tick.
pub
/// Convert microseconds to ticks at `tick_hz` ticks/second, or `None` if the
/// interval is longer than one 16-bit counter wrap (≥ 65536 ticks).
///
/// This is the inverse of [`ticks_to_us`] and the safe replacement for an
/// `as u16` narrowing: the multiply is widened to `u64` (worst case
/// `u32::MAX * 1_000_000`), and the result is rejected rather than truncated
/// when it will not fit a single `u16` CCR compare. `Some` therefore always
/// carries a value that a 16-bit compare can represent unambiguously (a full
/// 65536-tick wrap maps `now` onto itself, so it is excluded). Truncates toward
/// zero like the forward conversion.
pub
/// Assemble a 32-bit timestamp from the software overflow tally (`overflows`,
/// the high 16 bits) and the hardware counter (`cnt`, the low 16 bits).
///
/// The caller is responsible for having already folded in any pending overflow
/// (see `Counter::now32`); this is just the bit-packing half, isolated so it is
/// testable without hardware.
pub