From 5a66f72938c1350cd4aacca220cc7b7749d16312 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 11 Nov 2020 16:07:55 +0800 Subject: [PATCH] ticktimer: add opcodes for msleep Signed-off-by: Sean Cross --- services/ticktimer-server/src/api.rs | 25 ++++++++++++++++++++++++- services/ticktimer-server/src/lib.rs | 23 ++++++++++------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/services/ticktimer-server/src/api.rs b/services/ticktimer-server/src/api.rs index 3919811f5..69ea3b2bd 100644 --- a/services/ticktimer-server/src/api.rs +++ b/services/ticktimer-server/src/api.rs @@ -4,8 +4,15 @@ use xous::{Message, ScalarMessage}; pub enum Opcode { /// Reset the timer Reset, + /// Get the elapsed time in milliseconds ElapsedMs, + + /// Sleep for the specified numer of milliseconds + SleepMs(usize), + + /// Recalculate the sleep time + RecalculateSleep, } impl<'a> core::convert::TryFrom<&'a Message> for Opcode { @@ -14,12 +21,14 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode { match message { Message::Scalar(m) => match m.id { 1 => Ok(Opcode::Reset), + 131072 => Ok(Opcode::RecalculateSleep), _ => Err("unrecognized opcode"), }, Message::BlockingScalar(m) => match m.id { 4919 => Ok(Opcode::ElapsedMs), + 3 => Ok(Opcode::SleepMs(m.arg1)), _ => Err("unrecognized opcode"), - } + }, _ => Err("unhandled message type"), } } @@ -35,6 +44,13 @@ impl Into for Opcode { arg3: 0, arg4: 0, }), + Opcode::RecalculateSleep => Message::Scalar(ScalarMessage { + id: 131072, + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + }), Opcode::ElapsedMs => Message::BlockingScalar(ScalarMessage { id: 4919, arg1: 0, @@ -42,6 +58,13 @@ impl Into for Opcode { arg3: 0, arg4: 0, }), + Opcode::SleepMs(ms) => Message::BlockingScalar(ScalarMessage { + id: 3, + arg1: ms, + arg2: 0, + arg3: 0, + arg4: 0, + }), } } } diff --git a/services/ticktimer-server/src/lib.rs b/services/ticktimer-server/src/lib.rs index 9d585271d..61504f79e 100644 --- a/services/ticktimer-server/src/lib.rs +++ b/services/ticktimer-server/src/lib.rs @@ -2,24 +2,21 @@ pub mod api; -use xous::{try_send_message, CID, Error}; +use xous::{send_message, Error, CID}; pub fn elapsed_ms(cid: CID) -> Result { - let response = try_send_message(cid, api::Opcode::ElapsedMs.into())?; + let response = send_message(cid, api::Opcode::ElapsedMs.into())?; if let xous::Result::Scalar2(upper, lower) = response { - Ok(upper as u64 | ((lower as u64) << 32)) + Ok(upper as u64 | ((lower as u64) << 32)) } else { - panic!("unexpected return value: {:#?}", response); - } - /* // scalar1 experimental reply to see if this makes it any better. It doesn't. - if let xous::Result::Scalar1(lower) = response { - Ok(lower as u64) - } else { panic!("unexpected return value: {:#?}", response); - } - */ - } + } +} pub fn reset(cid: CID) -> Result<(), xous::Error> { - try_send_message(cid, api::Opcode::Reset.into()).map(|_| ()) + send_message(cid, api::Opcode::Reset.into()).map(|_| ()) +} + +pub fn sleep_ms(cid: CID, ms: usize) -> Result<(), xous::Error> { + send_message(cid, api::Opcode::SleepMs(ms).into()).map(|_| ()) }