diff options
| author | Caleb Jamison <[email protected]> | 2023-02-09 20:57:27 -0500 |
|---|---|---|
| committer | Caleb Jamison <[email protected]> | 2023-02-09 20:57:27 -0500 |
| commit | bd7b3bd455fc5946a3944bd931acfdf255929cb6 (patch) | |
| tree | 134e12f460be9660e7be3fb4dcf92a9a938d4de2 /embassy-time | |
| parent | a4371e9544f284927aae3b0de0ff5318f81acc80 (diff) | |
Clamp ticks to 1 and round to nearest.
Diffstat (limited to 'embassy-time')
| -rw-r--r-- | embassy-time/src/duration.rs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index 846a9c3d5..9d0bab2dd 100644 --- a/embassy-time/src/duration.rs +++ b/embassy-time/src/duration.rs | |||
| @@ -82,8 +82,17 @@ impl Duration { | |||
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | /// Creates a duration corresponding to the specified Hz. | 84 | /// Creates a duration corresponding to the specified Hz. |
| 85 | /// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1 | ||
| 86 | /// tick. Doing so will not deadlock, but will certainly not produce the desired output. | ||
| 85 | pub const fn from_hz(hz: u64) -> Duration { | 87 | pub const fn from_hz(hz: u64) -> Duration { |
| 86 | Duration { ticks: TICK_HZ / hz } | 88 | let ticks = { |
| 89 | if hz >= TICK_HZ { | ||
| 90 | 1 | ||
| 91 | } else { | ||
| 92 | (TICK_HZ + hz / 2) / hz | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | Duration { ticks } | ||
| 87 | } | 96 | } |
| 88 | 97 | ||
| 89 | /// Adds one Duration to another, returning a new Duration or None in the event of an overflow. | 98 | /// Adds one Duration to another, returning a new Duration or None in the event of an overflow. |
