diff --git a/Cargo.lock b/Cargo.lock index 0dfdf3741..e000c9e97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,7 +149,7 @@ checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" dependencies = [ "atty", "humantime", - "log", + "log 0.4.8", "regex", "termcolor", ] @@ -218,6 +218,13 @@ dependencies = [ "rand", ] +[[package]] +name = "log" +version = "0.1.0" +dependencies = [ + "xous", +] + [[package]] name = "log" version = "0.4.8" @@ -457,7 +464,7 @@ dependencies = [ "crc", "csv", "env_logger", - "log", + "log 0.4.8", "xmas-elf", ] diff --git a/Cargo.toml b/Cargo.toml index 4de8bbffd..7f508cf98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,9 @@ members = [ "tools", "xous-rs", "examples/shell", + "examples/log", ] -default-members = ["xous-rs", "examples/shell"] +default-members = ["xous-rs", "examples/shell", "examples/log"] [profile.release] codegen-units = 1 # 1 better optimizations diff --git a/examples/log/.cargo/config b/examples/log/.cargo/config new file mode 100644 index 000000000..478c166b7 --- /dev/null +++ b/examples/log/.cargo/config @@ -0,0 +1,13 @@ +[target.riscv32i-unknown-none-elf] +#runner = "riscv64-unknown-elf-gdb -x gdb_init" +rustflags = [ + "-C", "linker-plugin-lto", +] + +[target.riscv32imac-unknown-none-elf] +rustflags = [ + "-C", "linker-plugin-lto", +] + +[build] +target = "riscv32imac-unknown-none-elf" diff --git a/examples/log/.vscode/settings.json b/examples/log/.vscode/settings.json new file mode 100644 index 000000000..bc21aa68f --- /dev/null +++ b/examples/log/.vscode/settings.json @@ -0,0 +1,41 @@ +{ + "cSpell.words": [ + "TXFULL", + "Uart", + "asid", + "concat", + "ebreak", + "ebss", + "edata", + "eheap", + "estack", + "etext", + "getc", + "irqs", + "kernel", + "mcause", + "mext", + "msoft", + "mstatus", + "mtimer", + "pagetable", + "pending", + "processtable", + "putc", + "riscv", + "rt", + "satp", + "sbss", + "sdata", + "set", + "sheap", + "sidata", + "sstack", + "stext", + "syscalls", + "virt", + "vmim", + "vmip", + "xous" + ] +} \ No newline at end of file diff --git a/examples/log/Cargo.toml b/examples/log/Cargo.toml new file mode 100644 index 000000000..f7bfaa802 --- /dev/null +++ b/examples/log/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "log" +version = "0.1.0" +authors = ["Sean Cross "] +edition = "2018" +description = "Log output program" + +[dependencies] +xous = { path = "../../xous-rs" } + diff --git a/examples/log/README.md b/examples/log/README.md new file mode 100644 index 000000000..ec43509cb --- /dev/null +++ b/examples/log/README.md @@ -0,0 +1,4 @@ +# Log output + +This contains the core kernel for Xous. It is implemented as +a library that you include with your `pid 0` program. diff --git a/examples/log/src/debug.rs b/examples/log/src/debug.rs new file mode 100644 index 000000000..0cedc9006 --- /dev/null +++ b/examples/log/src/debug.rs @@ -0,0 +1,74 @@ +use core::fmt::{Error, Write}; + +#[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)+) + }); +} + +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) { + unsafe { + let base = DEFAULT_UART_ADDR; + // Wait until TXFULL is `0` + 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 + } + } + } + } +} + +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/log/src/main.rs b/examples/log/src/main.rs new file mode 100644 index 000000000..3db7b741b --- /dev/null +++ b/examples/log/src/main.rs @@ -0,0 +1,80 @@ +#![no_std] +#![no_main] + +#[macro_use] +mod debug; +mod start; + +use core::panic::PanicInfo; + +#[panic_handler] +fn handle_panic(arg: &PanicInfo) -> ! { + println!("PANIC!"); + println!("Details: {:?}", arg); + loop {} +} + +fn handle_irq(irq_no: usize, arg: *mut usize) { + print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize); + + while let Some(c) = debug::DEFAULT.getc() { + print!("0x{:02x}", c); + } + println!(""); +} + +#[no_mangle] +fn main() { + let uart = xous::syscall::map_memory( + xous::MemoryAddress::new(0xf0004000), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map serial port"); + unsafe { debug::DEFAULT_UART_ADDR = uart.base as *mut usize }; + println!("Mapped UART @ {:08x}", uart.base as usize); + + println!("Process: map success!"); + debug::DEFAULT.enable_rx(); + println!("Allocating IRQ..."); + xous::rsyscall(xous::SysCall::ClaimInterrupt( + 4, + handle_irq as *mut usize, + 0 as *mut usize, + )) + .expect("couldn't claim interrupt"); + + println!("Allocating a ton of space on the stack..."); + { + let _big_array = [42u8; 131072]; + } + + println!("Increasing heap to 131072..."); + let heap = xous::rsyscall(xous::SysCall::IncreaseHeap( + 131072, + xous::MemoryFlags::R | xous::MemoryFlags::W, + )) + .expect("couldn't increase heap"); + if let xous::Result::MemoryRange(range) = heap { + println!( + "Heap goes from {:08x} - {:08x}", + range.base as usize, + range.base as usize + range.size + ); + use core::slice; + let mem_range = unsafe { slice::from_raw_parts_mut(range.base, range.size) }; + println!("Filling with bytes..."); + for word in mem_range.iter_mut() { + *word = 42; + } + println!("Done!"); + } else { + println!("Unexpected return value: {:?}", heap); + } + + loop { + println!("Waiting for an event..."); + xous::rsyscall(xous::SysCall::WaitEvent).expect("Couldn't call waitevent"); + } +} diff --git a/examples/log/src/start.rs b/examples/log/src/start.rs new file mode 100644 index 000000000..62a51d364 --- /dev/null +++ b/examples/log/src/start.rs @@ -0,0 +1,20 @@ + +/// Rust entry point (_start_rust) +/// +/// Zeros bss section, initializes data section and calls main. This function +/// never returns. +#[link_section = ".init.rust"] +#[export_name = "_start"] +pub unsafe extern "C" fn start_rust() -> ! { + extern "Rust" { + // This symbol will be provided by the kernel + fn main(); + } + main(); + panic!("exited main"); +} + +#[export_name = "abort"] +pub extern "C" fn abort() -> ! { + panic!("aborted"); +} diff --git a/examples/log/src/timer.rs b/examples/log/src/timer.rs new file mode 100644 index 000000000..295e58ca9 --- /dev/null +++ b/examples/log/src/timer.rs @@ -0,0 +1,62 @@ +const SYSTEM_CLOCK_FREQUENCY: u32 = 12_000_000; +const TIMER_BASE: usize = 0xF000_3000; + +fn timer_tick(_irq_no: usize, _arg: *mut usize) { + println!("Timer tick"); + let ptr = TIMER_BASE as *mut usize; + + // acknowledge the timer + unsafe { ptr.add(6).write_volatile(1) }; +} + +pub fn init() { + println!("Allocating timer..."); + xous::rsyscall(xous::SysCall::MapMemory( + TIMER_BASE as *mut usize, + TIMER_BASE as *mut usize, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + )) + .expect("timer: couldn't map timer"); + + xous::rsyscall(xous::SysCall::ClaimInterrupt( + 1, + timer_tick as *mut usize, + 0 as *mut usize, + )) + .expect("timer: couldn't claim interrupt"); + + let ms = 1000; // tick every 1000 ms + en(false); + load(SYSTEM_CLOCK_FREQUENCY / 1_000 * ms); + reload(SYSTEM_CLOCK_FREQUENCY / 1_000 * ms); + en(true); + + // Set EV_ENABLE + let ptr = TIMER_BASE as *mut usize; + unsafe { ptr.add(7).write_volatile(1) }; +} + +pub fn load(value: u32) { + let ptr = TIMER_BASE as *mut usize; + unsafe { + ptr.add(0).write_volatile(value as usize); + } +} + +pub fn reload(value: u32) { + let ptr = TIMER_BASE as *mut usize; + unsafe { + ptr.add(1).write_volatile(value as usize); + } +} + +pub fn en(en: bool) { + let ptr = TIMER_BASE as *mut usize; + if en { + unsafe { ptr.add(2).write_volatile(1) }; + } + else { + unsafe { ptr.add(2).write_volatile(0) }; + } +}