aboutsummaryrefslogtreecommitdiff
path: root/rtic-time/src/lib.rs
diff options
context:
space:
mode:
authorFinomnis <Finomnis@users.noreply.github.com>2023-12-01 08:59:22 +0100
committerGitHub <noreply@github.com>2023-12-01 07:59:22 +0000
commit612a47ef4d09da3553145d77bf1750314bbb7b16 (patch)
tree7f6f6beca7e0c81397994b4f835c6a7f0bbee331 /rtic-time/src/lib.rs
parent9f5820da1d36a8c84455b1bc0458d34eb7dd9a70 (diff)
Fix mono delay (#843)
* rtic-time: Compenstate for timer uncertainty * Update changelog and incorrect cargo.lock in an example * Fix Monotonic impls * Fix tests * Fix other monotonics, again * Update changelog * Fix example * Fix DelayUs and DelayMs impls * Minor coding style fix in u64 conversions * Fix all changelogs * Fix changelog * Fix blocking DelayUs * Minor monotonic rework * Add delay precision test * Add more tests * Add rust-version tags to Cargo.toml * Fix imxrt, rp2040 and systick timer * Fix more monotonics * Fix systick monotonic * Some reverts * Fix imxrt * Fix nrf * Fix rp2040 * Fix stm32 * Fix systick * Fix rtic-time tests * Bump to e-h.rc2 * Apply e-h.rc2 fixes to rtic-time * Apply fixes from arbiter * Fix clippy warning * Minor beautification * Revert previous changes * Fix variable name * Add blocking tests, but disable them by default
Diffstat (limited to 'rtic-time/src/lib.rs')
-rw-r--r--rtic-time/src/lib.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/rtic-time/src/lib.rs b/rtic-time/src/lib.rs
index 0c3e349..4c89d52 100644
--- a/rtic-time/src/lib.rs
+++ b/rtic-time/src/lib.rs
@@ -181,22 +181,36 @@ impl<Mono: Monotonic> TimerQueue<Mono> {
}
}
- /// Timeout after a specific duration.
+ /// Timeout after at least a specific duration.
#[inline]
pub async fn timeout_after<F: Future>(
&self,
duration: Mono::Duration,
future: F,
) -> Result<F::Output, TimeoutError> {
- self.timeout_at(Mono::now() + duration, future).await
+ let now = Mono::now();
+ let mut timeout = now + duration;
+ if now != timeout {
+ timeout = timeout + Mono::TICK_PERIOD;
+ }
+
+ // Wait for one period longer, because by definition timers have an uncertainty
+ // of one period, so waiting for 'at least' needs to compensate for that.
+ self.timeout_at(timeout, future).await
}
- /// Delay for some duration of time.
+ /// Delay for at least some duration of time.
#[inline]
pub async fn delay(&self, duration: Mono::Duration) {
let now = Mono::now();
+ let mut timeout = now + duration;
+ if now != timeout {
+ timeout = timeout + Mono::TICK_PERIOD;
+ }
- self.delay_until(now + duration).await;
+ // Wait for one period longer, because by definition timers have an uncertainty
+ // of one period, so waiting for 'at least' needs to compensate for that.
+ self.delay_until(timeout).await;
}
/// Delay to some specific time instant.