Skip to content

Commit d7c82e8

Browse files
committed
std: xous: add support for time
Add support for determining the current time. This connects to the ticktimer server in order to get the system uptime. Signed-off-by: Sean Cross <[email protected]>
1 parent faf1f32 commit d7c82e8

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

library/std/src/sys/xous/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub mod process;
2727
pub mod stdio;
2828
pub mod thread;
2929
pub mod thread_local_key;
30-
#[path = "../unsupported/time.rs"]
3130
pub mod time;
3231

3332
#[path = "../unsupported/common.rs"]

library/std/src/sys/xous/time.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::time::Duration;
2+
3+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
4+
pub struct Instant(Duration);
5+
6+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
7+
pub struct SystemTime(Duration);
8+
9+
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
10+
11+
impl Instant {
12+
pub fn now() -> Instant {
13+
let result = crate::os::xous::ffi::blocking_scalar(
14+
crate::os::xous::services::ticktimer_server(),
15+
[0 /* ElapsedMs */, 0, 0, 0, 0],
16+
)
17+
.expect("failed to request elapsed_ms");
18+
let lower = result[0];
19+
let upper = result[1];
20+
Instant { 0: Duration::from_millis(lower as u64 | (upper as u64) << 32) }
21+
}
22+
23+
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
24+
self.0.checked_sub(other.0)
25+
}
26+
27+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
28+
Some(Instant(self.0.checked_add(*other)?))
29+
}
30+
31+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
32+
Some(Instant(self.0.checked_sub(*other)?))
33+
}
34+
}
35+
36+
impl SystemTime {
37+
pub fn now() -> SystemTime {
38+
let result = crate::os::xous::ffi::blocking_scalar(
39+
crate::os::xous::services::ticktimer_server(),
40+
[3 /* GetUtcTimeMs */, 0, 0, 0, 0],
41+
)
42+
.expect("failed to request utc time in ms");
43+
let lower = result[0];
44+
let upper = result[1];
45+
SystemTime { 0: Duration::from_millis((upper as u64) << 32 | lower as u64) }
46+
}
47+
48+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
49+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
50+
}
51+
52+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
53+
Some(SystemTime(self.0.checked_add(*other)?))
54+
}
55+
56+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
57+
Some(SystemTime(self.0.checked_sub(*other)?))
58+
}
59+
}

0 commit comments

Comments
 (0)