examples: shell: get building in a hosted env

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-03 11:01:50 +08:00
parent 40d4483e4f
commit 0fae191651
6 changed files with 112 additions and 90 deletions

View file

@ -9,5 +9,5 @@ rustflags = [
"-C", "linker-plugin-lto", "-C", "linker-plugin-lto",
] ]
[build] #[build]
target = "riscv32imac-unknown-none-elf" #target = "riscv32imac-unknown-none-elf"

17
examples/shell/build.rs Normal file
View file

@ -0,0 +1,17 @@
// NOTE: Adapted from cortex-m/build.rs
use std::env;
fn main() {
let target = env::var("TARGET").unwrap();
let target_os = target.split('-').nth(2).unwrap_or("none");
// If we're not running on a desktop-class operating system, emit the "baremetal"
// config setting. This will enable software to do tasks such as
// managing memory.
if target_os == "none" {
println!("cargo:rustc-cfg=baremetal");
}
println!("cargo:rerun-if-changed=build.rs");
}

View file

@ -0,0 +1,68 @@
use core::panic::PanicInfo;
#[panic_handler]
fn handle_panic(arg: &PanicInfo) -> ! {
println!("PANIC!");
println!("Details: {:?}", arg);
xous::syscall::wait_event();
loop {}
}
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!();
}
/// 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();
}
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(0xf000_1000),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map uart");
unsafe { crate::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() };
println!("Mapped UART @ {:08x}", uart.addr.get());
xous::syscall::map_memory(
xous::MemoryAddress::new(0xf000_2000),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.map(|_| println!("!!!WARNING: managed to steal kernel's memory"))
.ok();
println!("Process: map success!");
crate::debug::DEFAULT.enable_rx();
println!("Allocating IRQ...");
xous::rsyscall(xous::SysCall::ClaimInterrupt(
2,
handle_irq as *mut usize,
core::ptr::null_mut::<usize>(),
))
.expect("couldn't claim interrupt");
main();
panic!("exited main");
}
#[export_name = "abort"]
pub extern "C" fn abort() -> ! {
panic!("aborted");
}

View file

@ -1,33 +1,18 @@
#![no_std] #![cfg_attr(baremetal, no_main)]
#![no_main] #![cfg_attr(baremetal, no_std)]
#[cfg(baremetal)]
#[macro_use] #[macro_use]
mod debug; mod debug;
mod start;
#[cfg(baremetal)]
mod baremetal;
mod timer; mod timer;
mod logstr; mod logstr;
#[cfg(baremetal)]
use core::fmt::Write; use core::fmt::Write;
use core::panic::PanicInfo;
#[panic_handler]
fn handle_panic(arg: &PanicInfo) -> ! {
println!("PANIC!");
println!("Details: {:?}", arg);
xous::syscall::wait_event();
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!();
}
// fn print_and_yield(index: *mut usize) -> ! { // fn print_and_yield(index: *mut usize) -> ! {
// let num = index as usize; // let num = index as usize;
@ -37,37 +22,8 @@ fn handle_irq(irq_no: usize, arg: *mut usize) {
// } // }
// } // }
#[no_mangle] #[cfg_attr(baremetal, no_mangle)]
fn main() { fn main() {
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(0xf000_1000),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map uart");
unsafe { debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() };
println!("Mapped UART @ {:08x}", uart.addr.get() );
xous::syscall::map_memory(
xous::MemoryAddress::new(0xf000_2000),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.map(|_| println!("!!!WARNING: managed to steal kernel's memory"))
.ok();
println!("Process: map success!");
debug::DEFAULT.enable_rx();
println!("Allocating IRQ...");
xous::rsyscall(xous::SysCall::ClaimInterrupt(
2,
handle_irq as *mut usize,
0 as *mut usize,
))
.expect("couldn't claim interrupt");
// println!("Allocating a ton of space on the stack..."); // println!("Allocating a ton of space on the stack...");
// { // {
// let _big_array = [42u8; 131072]; // let _big_array = [42u8; 131072];
@ -96,6 +52,7 @@ fn main() {
// println!("Unexpected return value: {:?}", heap); // println!("Unexpected return value: {:?}", heap);
// } // }
println!("Starting to initialize the timer");
timer::init(); timer::init();
// for i in 1usize..5 { // for i in 1usize..5 {
@ -123,22 +80,22 @@ fn main() {
let connection = connection.unwrap(); let connection = connection.unwrap();
println!("Connected: {:?}", connection); println!("Connected: {:?}", connection);
let mut counter = 0; let mut counter: usize = 0;
let mut ls = logstr::LogStr::new(); // let ls = logstr::LogStr::new();
loop { loop {
// println!("Sending a scalar message with id {}...", counter + 4096); println!("Sending a scalar message with id {}...", counter + 4096);
// xous::syscall::send_message( xous::syscall::send_message(
// connection, connection,
// xous::Message::Scalar(xous::ScalarMessage { xous::Message::Scalar(xous::ScalarMessage {
// id: counter + 4096, id: counter + 4096,
// arg1: counter, arg1: counter,
// arg2: counter * 2, arg2: counter * 2,
// arg3: !counter, arg3: !counter,
// arg4: counter + 1, arg4: counter + 1,
// }), }),
// ) )
// .expect("couldn't send scalar message"); .expect("couldn't send scalar message");
if counter & 0xfff == 0 { if counter.trailing_zeros() >= 12 {
println!("Loop {}", counter); println!("Loop {}", counter);
} }
counter += 1; counter += 1;

View file

@ -1,20 +0,0 @@
/// 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");
}

View file

@ -25,7 +25,7 @@ pub fn init() {
xous::rsyscall(xous::SysCall::ClaimInterrupt( xous::rsyscall(xous::SysCall::ClaimInterrupt(
1, 1,
timer_tick as *mut usize, timer_tick as *mut usize,
0 as *mut usize, core::ptr::null_mut::<usize>(),
)) ))
.expect("timer: couldn't claim interrupt"); .expect("timer: couldn't claim interrupt");