1 use core::{fmt, ops};
2 
3 use self::timekeep::ktime_get_real_ns;
4 
5 pub mod sleep;
6 pub mod timekeep;
7 pub mod timer;
8 
9 /* Time structures. (Partitially taken from smoltcp)
10 
11 The `time` module contains structures used to represent both
12 absolute and relative time.
13 
14  - [Instant] is used to represent absolute time.
15  - [Duration] is used to represent relative time.
16 
17 [Instant]: struct.Instant.html
18 [Duration]: struct.Duration.html
19 */
20 
21 /// 表示时间的结构体
22 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
23 pub struct TimeSpec {
24     pub tv_sec: i64,
25     pub tv_nsec: i64,
26 }
27 
28 impl TimeSpec {
29     #[allow(dead_code)]
new(sec: i64, nsec: i64) -> TimeSpec30     pub fn new(sec: i64, nsec: i64) -> TimeSpec {
31         return TimeSpec {
32             tv_sec: sec,
33             tv_nsec: nsec,
34         };
35     }
36 }
37 
38 /// A representation of an absolute time value.
39 ///
40 /// The `Instant` type is a wrapper around a `i64` value that
41 /// represents a number of microseconds, monotonically increasing
42 /// since an arbitrary moment in time, such as system startup.
43 ///
44 /// * A value of `0` is inherently arbitrary.
45 /// * A value less than `0` indicates a time before the starting
46 ///   point.
47 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
48 #[cfg_attr(feature = "defmt", derive(defmt::Format))]
49 pub struct Instant {
50     micros: i64,
51 }
52 
53 #[allow(dead_code)]
54 impl Instant {
55     pub const ZERO: Instant = Instant::from_micros_const(0);
56 
57     /// Create a new `Instant` from a number of microseconds.
from_micros<T: Into<i64>>(micros: T) -> Instant58     pub fn from_micros<T: Into<i64>>(micros: T) -> Instant {
59         Instant {
60             micros: micros.into(),
61         }
62     }
63 
from_micros_const(micros: i64) -> Instant64     pub const fn from_micros_const(micros: i64) -> Instant {
65         Instant { micros }
66     }
67 
68     /// Create a new `Instant` from a number of milliseconds.
from_millis<T: Into<i64>>(millis: T) -> Instant69     pub fn from_millis<T: Into<i64>>(millis: T) -> Instant {
70         Instant {
71             micros: millis.into() * 1000,
72         }
73     }
74 
75     /// Create a new `Instant` from a number of milliseconds.
from_millis_const(millis: i64) -> Instant76     pub const fn from_millis_const(millis: i64) -> Instant {
77         Instant {
78             micros: millis * 1000,
79         }
80     }
81 
82     /// Create a new `Instant` from a number of seconds.
from_secs<T: Into<i64>>(secs: T) -> Instant83     pub fn from_secs<T: Into<i64>>(secs: T) -> Instant {
84         Instant {
85             micros: secs.into() * 1000000,
86         }
87     }
88 
89     /// Create a new `Instant` from the current time
now() -> Instant90     pub fn now() -> Instant {
91         Self::from_micros(ktime_get_real_ns() / 1000)
92     }
93 
94     /// The fractional number of milliseconds that have passed
95     /// since the beginning of time.
millis(&self) -> i6496     pub const fn millis(&self) -> i64 {
97         self.micros % 1000000 / 1000
98     }
99 
100     /// The fractional number of microseconds that have passed
101     /// since the beginning of time.
micros(&self) -> i64102     pub const fn micros(&self) -> i64 {
103         self.micros % 1000000
104     }
105 
106     /// The number of whole seconds that have passed since the
107     /// beginning of time.
secs(&self) -> i64108     pub const fn secs(&self) -> i64 {
109         self.micros / 1000000
110     }
111 
112     /// The total number of milliseconds that have passed since
113     /// the beginning of time.
total_millis(&self) -> i64114     pub const fn total_millis(&self) -> i64 {
115         self.micros / 1000
116     }
117     /// The total number of milliseconds that have passed since
118     /// the beginning of time.
total_micros(&self) -> i64119     pub const fn total_micros(&self) -> i64 {
120         self.micros
121     }
122 }
123 
124 impl fmt::Display for Instant {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result125     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126         write!(f, "{}.{:0>3}s", self.secs(), self.millis())
127     }
128 }
129 
130 impl ops::Add<Duration> for Instant {
131     type Output = Instant;
132 
add(self, rhs: Duration) -> Instant133     fn add(self, rhs: Duration) -> Instant {
134         Instant::from_micros(self.micros + rhs.total_micros() as i64)
135     }
136 }
137 
138 impl ops::AddAssign<Duration> for Instant {
add_assign(&mut self, rhs: Duration)139     fn add_assign(&mut self, rhs: Duration) {
140         self.micros += rhs.total_micros() as i64;
141     }
142 }
143 
144 impl ops::Sub<Duration> for Instant {
145     type Output = Instant;
146 
sub(self, rhs: Duration) -> Instant147     fn sub(self, rhs: Duration) -> Instant {
148         Instant::from_micros(self.micros - rhs.total_micros() as i64)
149     }
150 }
151 
152 impl ops::SubAssign<Duration> for Instant {
sub_assign(&mut self, rhs: Duration)153     fn sub_assign(&mut self, rhs: Duration) {
154         self.micros -= rhs.total_micros() as i64;
155     }
156 }
157 
158 impl ops::Sub<Instant> for Instant {
159     type Output = Duration;
160 
sub(self, rhs: Instant) -> Duration161     fn sub(self, rhs: Instant) -> Duration {
162         Duration::from_micros((self.micros - rhs.micros).unsigned_abs())
163     }
164 }
165 
166 /// A relative amount of time.
167 #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
168 #[cfg_attr(feature = "defmt", derive(defmt::Format))]
169 pub struct Duration {
170     micros: u64,
171 }
172 
173 impl Duration {
174     pub const ZERO: Duration = Duration::from_micros(0);
175     /// Create a new `Duration` from a number of microseconds.
from_micros(micros: u64) -> Duration176     pub const fn from_micros(micros: u64) -> Duration {
177         Duration { micros }
178     }
179 
180     /// Create a new `Duration` from a number of milliseconds.
from_millis(millis: u64) -> Duration181     pub const fn from_millis(millis: u64) -> Duration {
182         Duration {
183             micros: millis * 1000,
184         }
185     }
186 
187     /// Create a new `Instant` from a number of seconds.
from_secs(secs: u64) -> Duration188     pub const fn from_secs(secs: u64) -> Duration {
189         Duration {
190             micros: secs * 1000000,
191         }
192     }
193 
194     /// The fractional number of milliseconds in this `Duration`.
millis(&self) -> u64195     pub const fn millis(&self) -> u64 {
196         self.micros / 1000 % 1000
197     }
198 
199     /// The fractional number of milliseconds in this `Duration`.
micros(&self) -> u64200     pub const fn micros(&self) -> u64 {
201         self.micros % 1000000
202     }
203 
204     /// The number of whole seconds in this `Duration`.
secs(&self) -> u64205     pub const fn secs(&self) -> u64 {
206         self.micros / 1000000
207     }
208 
209     /// The total number of milliseconds in this `Duration`.
total_millis(&self) -> u64210     pub const fn total_millis(&self) -> u64 {
211         self.micros / 1000
212     }
213 
214     /// The total number of microseconds in this `Duration`.
total_micros(&self) -> u64215     pub const fn total_micros(&self) -> u64 {
216         self.micros
217     }
218 }
219 
220 impl fmt::Display for Duration {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result221     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
222         write!(f, "{}.{:03}s", self.secs(), self.millis())
223     }
224 }
225 
226 impl ops::Add<Duration> for Duration {
227     type Output = Duration;
228 
add(self, rhs: Duration) -> Duration229     fn add(self, rhs: Duration) -> Duration {
230         Duration::from_micros(self.micros + rhs.total_micros())
231     }
232 }
233 
234 impl ops::AddAssign<Duration> for Duration {
add_assign(&mut self, rhs: Duration)235     fn add_assign(&mut self, rhs: Duration) {
236         self.micros += rhs.total_micros();
237     }
238 }
239 
240 impl ops::Sub<Duration> for Duration {
241     type Output = Duration;
242 
sub(self, rhs: Duration) -> Duration243     fn sub(self, rhs: Duration) -> Duration {
244         Duration::from_micros(
245             self.micros
246                 .checked_sub(rhs.total_micros())
247                 .expect("overflow when subtracting durations"),
248         )
249     }
250 }
251 
252 impl ops::SubAssign<Duration> for Duration {
sub_assign(&mut self, rhs: Duration)253     fn sub_assign(&mut self, rhs: Duration) {
254         self.micros = self
255             .micros
256             .checked_sub(rhs.total_micros())
257             .expect("overflow when subtracting durations");
258     }
259 }
260 
261 impl ops::Mul<u32> for Duration {
262     type Output = Duration;
263 
mul(self, rhs: u32) -> Duration264     fn mul(self, rhs: u32) -> Duration {
265         Duration::from_micros(self.micros * rhs as u64)
266     }
267 }
268 
269 impl ops::MulAssign<u32> for Duration {
mul_assign(&mut self, rhs: u32)270     fn mul_assign(&mut self, rhs: u32) {
271         self.micros *= rhs as u64;
272     }
273 }
274 
275 impl ops::Div<u32> for Duration {
276     type Output = Duration;
277 
div(self, rhs: u32) -> Duration278     fn div(self, rhs: u32) -> Duration {
279         Duration::from_micros(self.micros / rhs as u64)
280     }
281 }
282 
283 impl ops::DivAssign<u32> for Duration {
div_assign(&mut self, rhs: u32)284     fn div_assign(&mut self, rhs: u32) {
285         self.micros /= rhs as u64;
286     }
287 }
288 
289 impl ops::Shl<u32> for Duration {
290     type Output = Duration;
291 
shl(self, rhs: u32) -> Duration292     fn shl(self, rhs: u32) -> Duration {
293         Duration::from_micros(self.micros << rhs)
294     }
295 }
296 
297 impl ops::ShlAssign<u32> for Duration {
shl_assign(&mut self, rhs: u32)298     fn shl_assign(&mut self, rhs: u32) {
299         self.micros <<= rhs;
300     }
301 }
302 
303 impl ops::Shr<u32> for Duration {
304     type Output = Duration;
305 
shr(self, rhs: u32) -> Duration306     fn shr(self, rhs: u32) -> Duration {
307         Duration::from_micros(self.micros >> rhs)
308     }
309 }
310 
311 impl ops::ShrAssign<u32> for Duration {
shr_assign(&mut self, rhs: u32)312     fn shr_assign(&mut self, rhs: u32) {
313         self.micros >>= rhs;
314     }
315 }
316 
317 impl From<::core::time::Duration> for Duration {
from(other: ::core::time::Duration) -> Duration318     fn from(other: ::core::time::Duration) -> Duration {
319         Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64)
320     }
321 }
322 
323 impl From<Duration> for ::core::time::Duration {
from(val: Duration) -> Self324     fn from(val: Duration) -> Self {
325         ::core::time::Duration::from_micros(val.total_micros())
326     }
327 }
328 
329 /// 支持与smoltcp的时间转换
330 impl From<smoltcp::time::Instant> for Instant {
from(val: smoltcp::time::Instant) -> Self331     fn from(val: smoltcp::time::Instant) -> Self {
332         Instant::from_micros(val.micros())
333     }
334 }
335 
336 impl Into<smoltcp::time::Instant> for Instant {
into(self) -> smoltcp::time::Instant337     fn into(self) -> smoltcp::time::Instant {
338         smoltcp::time::Instant::from_millis(self.millis())
339     }
340 }
341 
342 /// 支持与smoltcp的时间转换
343 impl From<smoltcp::time::Duration> for Duration {
from(val: smoltcp::time::Duration) -> Self344     fn from(val: smoltcp::time::Duration) -> Self {
345         Duration::from_micros(val.micros())
346     }
347 }
348 
349 impl Into<smoltcp::time::Duration> for Duration {
into(self) -> smoltcp::time::Duration350     fn into(self) -> smoltcp::time::Duration {
351         smoltcp::time::Duration::from_millis(self.millis())
352     }
353 }
354