log: add log example

Note: does not currently log

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-03-13 20:32:44 +08:00
parent 1ba52da9a3
commit 5ce653882e
10 changed files with 315 additions and 3 deletions

11
Cargo.lock generated
View file

@ -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",
]

View file

@ -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

View file

@ -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"

41
examples/log/.vscode/settings.json vendored Normal file
View file

@ -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"
]
}

10
examples/log/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "log"
version = "0.1.0"
authors = ["Sean Cross <sean@xobs.io>"]
edition = "2018"
description = "Log output program"
[dependencies]
xous = { path = "../../xous-rs" }

4
examples/log/README.md Normal file
View file

@ -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.

74
examples/log/src/debug.rs Normal file
View file

@ -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<u8> {
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(())
}
}

80
examples/log/src/main.rs Normal file
View file

@ -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");
}
}

20
examples/log/src/start.rs Normal file
View file

@ -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");
}

62
examples/log/src/timer.rs Normal file
View file

@ -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) };
}
}