diff --git a/Cargo.lock b/Cargo.lock index ce456def9..56aba29f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -281,6 +281,7 @@ dependencies = [ name = "log-server" version = "0.1.0" dependencies = [ + "utralib", "xous", ] diff --git a/examples/log-server/Cargo.toml b/examples/log-server/Cargo.toml index 49c6bbf76..9adbd2223 100644 --- a/examples/log-server/Cargo.toml +++ b/examples/log-server/Cargo.toml @@ -10,3 +10,7 @@ xous = { path = "../../xous-rs" } #[target.'cfg(any(windows, unix))'.dependencies] #pancurses = "*" + + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} diff --git a/examples/shell/Cargo.toml b/examples/shell/Cargo.toml index bdf929faa..43e4242d3 100644 --- a/examples/shell/Cargo.toml +++ b/examples/shell/Cargo.toml @@ -13,3 +13,7 @@ ticktimer-server = { path = "../ticktimer-server" } [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} + +[features] +debugprint = [] +default = [] # "debugprint" diff --git a/examples/shell/src/debug.rs b/examples/shell/src/debug.rs index b26e88969..99bc6f263 100644 --- a/examples/shell/src/debug.rs +++ b/examples/shell/src/debug.rs @@ -42,33 +42,35 @@ pub const DEFAULT: Uart = Uart {}; impl Uart { pub fn putc(&self, c: u8) { - unsafe { - if DEFAULT_UART_ADDR as usize == 0 { - let uart = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::console::HW_CONSOLE_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map uart"); - DEFAULT_UART_ADDR = uart.as_mut_ptr() as _; - println!("Mapped UART @ {:08x}", uart.addr.get()); - // core::mem::forget(uart); + if cfg!(feature = "debugprint") { + unsafe { + if DEFAULT_UART_ADDR as usize == 0 { + let uart = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::console::HW_CONSOLE_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map uart"); + DEFAULT_UART_ADDR = uart.as_mut_ptr() as _; + println!("Mapped UART @ {:08x}", uart.addr.get()); + // core::mem::forget(uart); - println!("Allocating IRQ..."); - xous::claim_interrupt(utra::console::CONSOLE_IRQ, handle_irq, core::ptr::null_mut::()).expect("unable to allocate IRQ"); - self.enable_rx(); - } - let base = DEFAULT_UART_ADDR; - let mut uart_csr = CSR::new(DEFAULT_UART_ADDR as *mut u32); + println!("Allocating IRQ..."); + xous::claim_interrupt(utra::console::CONSOLE_IRQ, handle_irq, core::ptr::null_mut::()).expect("unable to allocate IRQ"); + self.enable_rx(); + } + let base = DEFAULT_UART_ADDR; + let mut uart_csr = CSR::new(DEFAULT_UART_ADDR as *mut u32); - // Wait until TXFULL is `0` - while uart_csr.r(utra::console::TXFULL) != 0 {} - uart_csr.wo(utra::console::RXTX, c as u32); - /* - while base.add(1).read_volatile() != 0 {} - base.add(0).write_volatile(c as usize)*/ - }; + // Wait until TXFULL is `0` + while uart_csr.r(utra::console::TXFULL) != 0 {} + uart_csr.wo(utra::console::RXTX, c as u32); + /* + while base.add(1).read_volatile() != 0 {} + base.add(0).write_volatile(c as usize)*/ + }; + } } pub fn enable_rx(&self) { diff --git a/examples/shell/src/main.rs b/examples/shell/src/main.rs index 978f21af2..c83ea0013 100644 --- a/examples/shell/src/main.rs +++ b/examples/shell/src/main.rs @@ -80,8 +80,10 @@ fn shell_main() -> ! { gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 0); loop { - //let elapsed_time = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); - //println!("elapsed time: {}ms", elapsed_time); + //println!("loop"); + println!("ticktimer request"); + let elapsed_time = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); + println!("elapsed time: {}ms", elapsed_time); // println!("Sending a scalar message with id {}...", counter + 4096); // match xous::syscall::send_message( diff --git a/examples/shell/src/timer.rs b/examples/shell/src/timer.rs index 7d18b2c78..e246bcba0 100644 --- a/examples/shell/src/timer.rs +++ b/examples/shell/src/timer.rs @@ -7,14 +7,14 @@ const TIMER_BASE: usize = utra::timer0::HW_TIMER0_BASE; // claim the same addres #[cfg(baremetal)] fn timer_tick(_irq_no: usize, _arg: *mut usize) { - println!(">>> Timer tick"); + //println!(">>> Timer tick"); let mut timer = CSR::new(TIMER_BASE as *mut u32); xous::rsyscall(xous::SysCall::ReturnToParentI(xous::PID::new(1).unwrap(), 0)).expect("couldn't return to parent"); // acknowledge the timer timer.wfo(utra::timer0::EV_PENDING_PENDING, 0b1); - println!("<<< Returning from timer_tick()"); + //println!("<<< Returning from timer_tick()"); } #[cfg(baremetal)] diff --git a/examples/ticktimer-server/Cargo.toml b/examples/ticktimer-server/Cargo.toml index 8c23fbada..444dbdf8d 100644 --- a/examples/ticktimer-server/Cargo.toml +++ b/examples/ticktimer-server/Cargo.toml @@ -11,3 +11,7 @@ embedded-graphics = "0.6" [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} + +[features] +debugprint = [] +default = ["debugprint"] diff --git a/examples/ticktimer-server/src/api.rs b/examples/ticktimer-server/src/api.rs index eba512c4c..83e8a62ec 100644 --- a/examples/ticktimer-server/src/api.rs +++ b/examples/ticktimer-server/src/api.rs @@ -32,7 +32,7 @@ impl Into for Opcode { arg3: 0, arg4: 0, }), - Opcode::ElapsedMs => Message::Scalar(ScalarMessage { + Opcode::ElapsedMs => Message::BlockingScalar(ScalarMessage { id: 2, arg1: 0, arg2: 0, diff --git a/examples/ticktimer-server/src/debug.rs b/examples/ticktimer-server/src/debug.rs new file mode 100644 index 000000000..99bc6f263 --- /dev/null +++ b/examples/ticktimer-server/src/debug.rs @@ -0,0 +1,108 @@ +use core::fmt::{Error, Write}; + +use utralib::generated::*; + +#[macro_export] +macro_rules! print +{ + ($($args:tt)+) => ({ + use core::fmt::Write; + let _ = write!(crate::debug::DEFAULT, $($args)+); + }); +} +#[macro_export] +macro_rules! println +{ + () => ({ + print!("\r\n") + }); + ($fmt:expr) => ({ + print!(concat!($fmt, "\r\n")) + }); + ($fmt:expr, $($args:tt)+) => ({ + print!(concat!($fmt, "\r\n"), $($args)+) + }); +} + + +fn handle_irq(irq_no: usize, arg: *mut usize) { + print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize); + + while let Some(c) = crate::debug::DEFAULT.getc() { + print!("0x{:02x}", c); + } + println!(); +} + +pub struct Uart {} + +pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize; + +pub const DEFAULT: Uart = Uart {}; + +impl Uart { + pub fn putc(&self, c: u8) { + if cfg!(feature = "debugprint") { + unsafe { + if DEFAULT_UART_ADDR as usize == 0 { + let uart = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::console::HW_CONSOLE_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map uart"); + DEFAULT_UART_ADDR = uart.as_mut_ptr() as _; + println!("Mapped UART @ {:08x}", uart.addr.get()); + // core::mem::forget(uart); + + println!("Allocating IRQ..."); + xous::claim_interrupt(utra::console::CONSOLE_IRQ, handle_irq, core::ptr::null_mut::()).expect("unable to allocate IRQ"); + self.enable_rx(); + } + let base = DEFAULT_UART_ADDR; + let mut uart_csr = CSR::new(DEFAULT_UART_ADDR as *mut u32); + + // Wait until TXFULL is `0` + while uart_csr.r(utra::console::TXFULL) != 0 {} + uart_csr.wo(utra::console::RXTX, c as u32); + /* + while base.add(1).read_volatile() != 0 {} + base.add(0).write_volatile(c as usize)*/ + }; + } + } + + pub fn enable_rx(&self) { + unsafe { + // let base = DEFAULT_UART_ADDR; + // base.add(5).write_volatile(base.add(5).read_volatile() | 2) + }; + } + + pub fn getc(&self) -> Option { + // unsafe { + // let base = DEFAULT_UART_ADDR; + // // If EV_PENDING_RX is 1, return the pending character. + // // Otherwise, return None. + // match base.add(4).read_volatile() & 2 { + // 0 => None, + // ack => { + // let c = Some(base.add(0).read_volatile() as u8); + // base.add(4).write_volatile(ack); + // c + // } + // } + // } + None + } +} + +impl Write for Uart { + fn write_str(&mut self, s: &str) -> Result<(), Error> { + for c in s.bytes() { + self.putc(c); + } + Ok(()) + } +} diff --git a/examples/ticktimer-server/src/main.rs b/examples/ticktimer-server/src/main.rs index e5e96229b..12ea4de54 100644 --- a/examples/ticktimer-server/src/main.rs +++ b/examples/ticktimer-server/src/main.rs @@ -1,6 +1,10 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] + +#[macro_use] +mod debug; + mod api; use api::Opcode; @@ -53,7 +57,9 @@ fn xmain() -> ! { let ticktimer_server = xous::create_server(b"ticktimer-server").expect("Couldn't create Ticktimer server"); loop { + println!("TickTimer: waiting for message"); let envelope = xous::receive_message(ticktimer_server).unwrap(); + println!("TickTimer: got message"); // println!("TickTimer: Message: {:?}", envelope); if let Ok(opcode) = Opcode::try_from(&envelope.body) { // println!("TickTimer: Opcode: {:?}", opcode); @@ -63,6 +69,7 @@ fn xmain() -> ! { }, Opcode::ElapsedMs => { let time = ticktimer.elapsed_ms(); + println!("TickTimer: returning time of {:?}", time); xous::return_scalar2(envelope.sender, (time & 0xFFFF_FFFFu64) as usize, ((time >> 32) & 0xFFF_FFFFu64) as usize,