aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-11-29 17:12:30 +0100
committerGitHub <[email protected]>2023-11-29 17:12:30 +0100
commit384bad7bfaa1f2415baf2cd3b69ebf36dc0a02d7 (patch)
tree1992bf003d6afcdeae926db2308f369bccfc42ea /embassy-time/src
parentb4bc9ac028568dfb3896dfb00cbd1e181863fd64 (diff)
parent4634316749c41dd5d8cc2f316033c9098369ed2f (diff)
Merge pull request #2231 from embassy-rs/stable3
Update embedded-(hal,io,nal).
Diffstat (limited to 'embassy-time/src')
-rw-r--r--embassy-time/src/delay.rs20
-rw-r--r--embassy-time/src/duration.rs9
-rw-r--r--embassy-time/src/lib.rs1
-rw-r--r--embassy-time/src/timer.rs9
4 files changed, 33 insertions, 6 deletions
diff --git a/embassy-time/src/delay.rs b/embassy-time/src/delay.rs
index be962747c..aab56b1f1 100644
--- a/embassy-time/src/delay.rs
+++ b/embassy-time/src/delay.rs
@@ -18,7 +18,11 @@ pub struct Delay;
18mod eh1 { 18mod eh1 {
19 use super::*; 19 use super::*;
20 20
21 impl embedded_hal_1::delay::DelayUs for Delay { 21 impl embedded_hal_1::delay::DelayNs for Delay {
22 fn delay_ns(&mut self, ns: u32) {
23 block_for(Duration::from_nanos(ns as u64))
24 }
25
22 fn delay_us(&mut self, us: u32) { 26 fn delay_us(&mut self, us: u32) {
23 block_for(Duration::from_micros(us as u64)) 27 block_for(Duration::from_micros(us as u64))
24 } 28 }
@@ -34,13 +38,17 @@ mod eha {
34 use super::*; 38 use super::*;
35 use crate::Timer; 39 use crate::Timer;
36 40
37 impl embedded_hal_async::delay::DelayUs for Delay { 41 impl embedded_hal_async::delay::DelayNs for Delay {
38 async fn delay_us(&mut self, micros: u32) { 42 async fn delay_ns(&mut self, ns: u32) {
39 Timer::after_micros(micros as _).await 43 Timer::after_nanos(ns as _).await
44 }
45
46 async fn delay_us(&mut self, us: u32) {
47 Timer::after_micros(us as _).await
40 } 48 }
41 49
42 async fn delay_ms(&mut self, millis: u32) { 50 async fn delay_ms(&mut self, ms: u32) {
43 Timer::after_millis(millis as _).await 51 Timer::after_millis(ms as _).await
44 } 52 }
45 } 53 }
46} 54}
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs
index 8366455be..647d208e3 100644
--- a/embassy-time/src/duration.rs
+++ b/embassy-time/src/duration.rs
@@ -2,6 +2,7 @@ use core::fmt;
2use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; 2use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
3 3
4use super::{GCD_1K, GCD_1M, TICK_HZ}; 4use super::{GCD_1K, GCD_1M, TICK_HZ};
5use crate::GCD_1G;
5 6
6#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 7#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))] 8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -61,6 +62,14 @@ impl Duration {
61 } 62 }
62 } 63 }
63 64
65 /// Creates a duration from the specified number of nanoseconds, rounding up.
66 /// NOTE: Delays this small may be inaccurate.
67 pub const fn from_nanos(micros: u64) -> Duration {
68 Duration {
69 ticks: div_ceil(micros * (TICK_HZ / GCD_1G), 1_000_000_000 / GCD_1G),
70 }
71 }
72
64 /// Creates a duration from the specified number of seconds, rounding down. 73 /// Creates a duration from the specified number of seconds, rounding down.
65 pub const fn from_secs_floor(secs: u64) -> Duration { 74 pub const fn from_secs_floor(secs: u64) -> Duration {
66 Duration { ticks: secs * TICK_HZ } 75 Duration { ticks: secs * TICK_HZ }
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs
index a90368d59..a0f6e3824 100644
--- a/embassy-time/src/lib.rs
+++ b/embassy-time/src/lib.rs
@@ -52,6 +52,7 @@ const fn gcd(a: u64, b: u64) -> u64 {
52 52
53pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000); 53pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000);
54pub(crate) const GCD_1M: u64 = gcd(TICK_HZ, 1_000_000); 54pub(crate) const GCD_1M: u64 = gcd(TICK_HZ, 1_000_000);
55pub(crate) const GCD_1G: u64 = gcd(TICK_HZ, 1_000_000_000);
55 56
56#[cfg(feature = "defmt-timestamp-uptime")] 57#[cfg(feature = "defmt-timestamp-uptime")]
57defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() } 58defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() }
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs
index ee2423daf..574d715da 100644
--- a/embassy-time/src/timer.rs
+++ b/embassy-time/src/timer.rs
@@ -74,6 +74,15 @@ impl Timer {
74 Self::after(Duration::from_ticks(ticks)) 74 Self::after(Duration::from_ticks(ticks))
75 } 75 }
76 76
77 /// Expire after the specified number of nanoseconds.
78 ///
79 /// This method is a convenience wrapper for calling `Timer::after(Duration::from_nanos())`.
80 /// For more details, refer to [`Timer::after()`] and [`Duration::from_nanos()`].
81 #[inline]
82 pub fn after_nanos(nanos: u64) -> Self {
83 Self::after(Duration::from_nanos(nanos))
84 }
85
77 /// Expire after the specified number of microseconds. 86 /// Expire after the specified number of microseconds.
78 /// 87 ///
79 /// This method is a convenience wrapper for calling `Timer::after(Duration::from_micros())`. 88 /// This method is a convenience wrapper for calling `Timer::after(Duration::from_micros())`.