Skip to content

Commit

Permalink
time: fix duration variable names, add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
techmccat committed Aug 13, 2024
1 parent 9ae512e commit 6212c61
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,18 @@ impl U32Ext for u32 {
}
}

/// Converts clock cycles at a given frequency into a Duration with arbitrary fraction
pub fn duration<const NOM: u32, const DENOM: u32>(
hz: Hertz,
cycles: u32,
) -> Duration<u32, NOM, DENOM> {
let cycles = cycles as u64;
let clk = hz.raw() as u64;
let us = cycles.saturating_mul(DENOM as u64) / clk / NOM as u64;
Duration::<u32, NOM, DENOM>::from_ticks(us as u32)
let duration_ticks = cycles.saturating_mul(DENOM as u64) / clk / NOM as u64;
Duration::<u32, NOM, DENOM>::from_ticks(duration_ticks as u32)
}

/// Converts a Duration with arbitrary fraction into a number of cycles at the specified frequency
pub fn cycles<const NOM: u32, const DENOM: u32>(ms: Duration<u32, NOM, DENOM>, clk: Hertz) -> u32 {
assert!(ms.ticks() > 0);
let clk = clk.raw() as u64;
Expand Down

0 comments on commit 6212c61

Please sign in to comment.