Skip to content

Commit

Permalink
std: xous: add support for time
Browse files Browse the repository at this point in the history
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 <sean@xobs.io>
  • Loading branch information
xobs committed Nov 8, 2022
1 parent faf1f32 commit d7c82e8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
1 change: 0 additions & 1 deletion library/std/src/sys/xous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub mod process;
pub mod stdio;
pub mod thread;
pub mod thread_local_key;
#[path = "../unsupported/time.rs"]
pub mod time;

#[path = "../unsupported/common.rs"]
Expand Down
59 changes: 59 additions & 0 deletions library/std/src/sys/xous/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::time::Duration;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant(Duration);

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct SystemTime(Duration);

pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));

impl Instant {
pub fn now() -> Instant {
let result = crate::os::xous::ffi::blocking_scalar(
crate::os::xous::services::ticktimer_server(),
[0 /* ElapsedMs */, 0, 0, 0, 0],
)
.expect("failed to request elapsed_ms");
let lower = result[0];
let upper = result[1];
Instant { 0: Duration::from_millis(lower as u64 | (upper as u64) << 32) }
}

pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
self.0.checked_sub(other.0)
}

pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant(self.0.checked_add(*other)?))
}

pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant(self.0.checked_sub(*other)?))
}
}

impl SystemTime {
pub fn now() -> SystemTime {
let result = crate::os::xous::ffi::blocking_scalar(
crate::os::xous::services::ticktimer_server(),
[3 /* GetUtcTimeMs */, 0, 0, 0, 0],
)
.expect("failed to request utc time in ms");
let lower = result[0];
let upper = result[1];
SystemTime { 0: Duration::from_millis((upper as u64) << 32 | lower as u64) }
}

pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
}

pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
Some(SystemTime(self.0.checked_add(*other)?))
}

pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
Some(SystemTime(self.0.checked_sub(*other)?))
}
}

0 comments on commit d7c82e8

Please sign in to comment.