From 98f648f30f489cd1bf7015fa6bcc95f453b3b6b7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 12 Oct 2020 19:08:16 +0800 Subject: [PATCH] attempt to add a ticktimer server --- Cargo.lock | 10 ++++ Cargo.toml | 4 +- examples/shell/Cargo.toml | 1 + examples/shell/src/main.rs | 14 ++++- examples/ticktimer-server/Cargo.toml | 13 +++++ examples/ticktimer-server/src/api.rs | 44 ++++++++++++++++ examples/ticktimer-server/src/lib.rs | 18 +++++++ examples/ticktimer-server/src/main.rs | 76 +++++++++++++++++++++++++++ xtask/src/main.rs | 6 +-- 9 files changed, 180 insertions(+), 6 deletions(-) create mode 100644 examples/ticktimer-server/Cargo.toml create mode 100644 examples/ticktimer-server/src/api.rs create mode 100644 examples/ticktimer-server/src/lib.rs create mode 100644 examples/ticktimer-server/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 904d46de2..ce456def9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -720,6 +720,7 @@ version = "0.1.0" dependencies = [ "bitflags", "graphics-server", + "ticktimer-server", "utralib", "xous", ] @@ -797,6 +798,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "ticktimer-server" +version = "0.1.0" +dependencies = [ + "embedded-graphics", + "utralib", + "xous", +] + [[package]] name = "tools" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 7f450ac39..8c066732c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "examples/shell", "examples/graphics-server", "examples/log-server", + "examples/ticktimer-server", "svd2utra", "xtask", ] @@ -13,6 +14,7 @@ default-members = [ "examples/shell", "examples/log-server", "examples/graphics-server", + "examples/ticktimer-server", ] # These packages have custom RUSTFLAGS, so if they @@ -24,7 +26,7 @@ exclude = [ "loader", # Exclude UTRA, since it is machine-generated. - "utra", + "utralib", ] [profile.release] diff --git a/examples/shell/Cargo.toml b/examples/shell/Cargo.toml index 4cfee6125..bdf929faa 100644 --- a/examples/shell/Cargo.toml +++ b/examples/shell/Cargo.toml @@ -9,6 +9,7 @@ description = "Shell test program" bitflags = "1.2.1" xous = { path = "../../xous-rs" } graphics-server = { path = "../graphics-server" } +ticktimer-server = { path = "../ticktimer-server" } [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/examples/shell/src/main.rs b/examples/shell/src/main.rs index 7d6bdc487..7c546348a 100644 --- a/examples/shell/src/main.rs +++ b/examples/shell/src/main.rs @@ -40,14 +40,16 @@ fn shell_main() -> ! { // let log_server_id = xous::SID::from_bytes(b"xous-logs-output").unwrap(); let graphics_server_id = xous::SID::from_bytes(b"graphics-server ").unwrap(); + let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap(); println!("SHELL: Attempting to connect to servers..."); let log_conn = 100;//ensure_connection(log_server_id); let graphics_conn = ensure_connection(graphics_server_id); + let ticktimer_conn = ensure_connection(ticktimer_server_id); println!( - "SHELL: Connected to Log server: {} Graphics server: {}", - log_conn, graphics_conn + "SHELL: Connected to Log server: {} Graphics server: {} Ticktimer server: {}", + log_conn, graphics_conn, ticktimer_conn, ); assert_ne!( @@ -55,6 +57,11 @@ fn shell_main() -> ! { "SHELL: graphics and log connections are the same!" ); + assert_ne!( + ticktimer_conn, graphics_conn, + "SHELL: graphics and ticktimer connections are the same!" + ); + let mut counter: usize = 0; // let ls = logstr::LogStr::new(); let mut lfsr = 0xace1u32; @@ -86,6 +93,9 @@ fn shell_main() -> ! { // lfsr = move_lfsr(lfsr); loop { + let elapsed_time = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); + println!("elapsed time: {}ms", elapsed_time); + match graphics_server::set_style( graphics_conn, 5, diff --git a/examples/ticktimer-server/Cargo.toml b/examples/ticktimer-server/Cargo.toml new file mode 100644 index 000000000..8c23fbada --- /dev/null +++ b/examples/ticktimer-server/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "ticktimer-server" +version = "0.1.0" +authors = ["bunnie "] +edition = "2018" +description = "Provide high-resolution, non-rollover system time" + +[dependencies] +xous = { path = "../../xous-rs" } +embedded-graphics = "0.6" + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} diff --git a/examples/ticktimer-server/src/api.rs b/examples/ticktimer-server/src/api.rs new file mode 100644 index 000000000..eba512c4c --- /dev/null +++ b/examples/ticktimer-server/src/api.rs @@ -0,0 +1,44 @@ +use xous::{Message, ScalarMessage}; + +#[derive(Debug)] +pub enum Opcode { + /// Reset the timer + Reset, + /// Get the elapsed time in milliseconds + ElapsedMs, +} + +impl<'a> core::convert::TryFrom<&'a Message> for Opcode { + type Error = &'static str; + fn try_from(message: &'a Message) -> Result { + match message { + Message::Scalar(m) => match m.id { + 1 => Ok(Opcode::Reset), + 2 => Ok(Opcode::ElapsedMs), + _ => Err("unrecognized opcode"), + }, + _ => Err("unhandled message type"), + } + } +} + +impl Into for Opcode { + fn into(self) -> Message { + match self { + Opcode::Reset => Message::Scalar(ScalarMessage { + id: 1, + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + }), + Opcode::ElapsedMs => Message::Scalar(ScalarMessage { + id: 2, + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + }), + } + } +} diff --git a/examples/ticktimer-server/src/lib.rs b/examples/ticktimer-server/src/lib.rs new file mode 100644 index 000000000..e301aa5bd --- /dev/null +++ b/examples/ticktimer-server/src/lib.rs @@ -0,0 +1,18 @@ +#![cfg_attr(target_os = "none", no_std)] + +pub mod api; + +use xous::{try_send_message, CID, Error}; + +pub fn elapsed_ms(cid: CID) -> Result { + let response = try_send_message(cid, api::Opcode::ElapsedMs.into())?; + if let xous::Result::Scalar2(upper, lower) = response { + Ok(upper as u64 | ((lower as u64) << 32)) + } else { + panic!("unexpected return value"); // or return Err(xous::Result::InternalError) + } +} + +pub fn reset(cid: CID) -> Result<(), xous::Error> { + try_send_message(cid, api::Opcode::Reset.into()).map(|_| ()) +} diff --git a/examples/ticktimer-server/src/main.rs b/examples/ticktimer-server/src/main.rs new file mode 100644 index 000000000..e5e96229b --- /dev/null +++ b/examples/ticktimer-server/src/main.rs @@ -0,0 +1,76 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod api; +use api::Opcode; + +use utralib::generated::*; + +use core::convert::TryFrom; + +pub struct XousTickTimer { + csr: xous::MemoryRange, +} + +const TICKS_PER_MS: u64 = 1; + +impl XousTickTimer { + pub fn new() -> XousTickTimer { + let ctrl = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::ticktimer::HW_TICKTIMER_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map Tick Timer CSR range"); + + XousTickTimer { csr: ctrl } + } + + pub fn reset(&self) { + let mut tt = CSR::new(self.csr.as_mut_ptr() as *mut u32); + tt.wfo(utra::ticktimer::CONTROL_RESET, 0b1); + tt.wo(utra::ticktimer::CONTROL, 0); // not paused, not reset -> free-run + } + + pub fn raw_ticktime(&self) -> u64 { + let mut tt = CSR::new(self.csr.as_mut_ptr() as *mut u32); + let mut time: u64 = tt.r(utra::ticktimer::TIME0) as u64; + time |= (tt.r(utra::ticktimer::TIME1) as u64) << 32; + + time + } + + pub fn elapsed_ms(&self) -> u64 { + self.raw_ticktime() / TICKS_PER_MS + } +} + +#[xous::xous_main] +fn xmain() -> ! { + // Create a new ticktimer object + let ticktimer = XousTickTimer::new(); + + let ticktimer_server = xous::create_server(b"ticktimer-server").expect("Couldn't create Ticktimer server"); + loop { + let envelope = xous::receive_message(ticktimer_server).unwrap(); + // println!("TickTimer: Message: {:?}", envelope); + if let Ok(opcode) = Opcode::try_from(&envelope.body) { + // println!("TickTimer: Opcode: {:?}", opcode); + match opcode { + Opcode::Reset => { + ticktimer.reset(); + }, + Opcode::ElapsedMs => { + let time = ticktimer.elapsed_ms(); + xous::return_scalar2(envelope.sender, + (time & 0xFFFF_FFFFu64) as usize, + ((time >> 32) & 0xFFF_FFFFu64) as usize, + ).expect("TickTimer: couldn't return time request"); + } + } + } else { + // println!("Couldn't convert opcode"); + } + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 109b8de5b..a680ac4d0 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -77,7 +77,7 @@ fn build_hw_image(debug: bool, svd: Option) -> Result<(), DynError> { let kernel = build_kernel(debug)?; let mut init = vec![]; - for pkg in &["shell", "graphics-server"] { + for pkg in &["shell", "graphics-server", "ticktimer-server"] { init.push(build(pkg, debug, Some(TARGET), None)?); } let loader = build("loader", debug, Some(TARGET), Some("loader".into()))?; @@ -144,7 +144,7 @@ fn build_hw_image(debug: bool, svd: Option) -> Result<(), DynError> { fn image(debug: bool) -> Result<(), DynError> { let kernel = build_kernel(debug)?; let mut init = vec![]; - for pkg in &["shell", "log-server", "graphics-server"] { + for pkg in &["shell", "log-server", "graphics-server", "ticktimer-server"] { init.push(build(pkg, debug, Some(TARGET), None)?); } build("loader", debug, Some(TARGET), Some("loader".into()))?; @@ -161,7 +161,7 @@ fn image(debug: bool) -> Result<(), DynError> { fn run(debug: bool) -> Result<(), DynError> { let stream = if debug { "debug" } else { "release" }; - let init = ["shell", "log-server", "graphics-server"]; + let init = ["shell", "log-server", "graphics-server", "ticktimer-server"]; // let mut init_paths = vec![]; for pkg in &init {