ticktimer: add opcodes for msleep

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-11-11 16:07:55 +08:00
parent b8f69b4a7f
commit 5a66f72938
2 changed files with 34 additions and 14 deletions

View file

@ -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<Message> 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<Message> for Opcode {
arg3: 0,
arg4: 0,
}),
Opcode::SleepMs(ms) => Message::BlockingScalar(ScalarMessage {
id: 3,
arg1: ms,
arg2: 0,
arg3: 0,
arg4: 0,
}),
}
}
}

View file

@ -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<u64, Error> {
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(|_| ())
}