mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
shell: add example shell
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
f50ec67553
commit
b9effd701b
8 changed files with 323 additions and 0 deletions
13
examples/shell/.cargo/config
Normal file
13
examples/shell/.cargo/config
Normal 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/shell/.vscode/settings.json
vendored
Normal file
41
examples/shell/.vscode/settings.json
vendored
Normal 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"
|
||||
]
|
||||
}
|
||||
11
examples/shell/Cargo.toml
Normal file
11
examples/shell/Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "shell"
|
||||
version = "0.1.0"
|
||||
authors = ["Sean Cross <sean@xobs.io>"]
|
||||
edition = "2018"
|
||||
description = "Shell test program"
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1.2.1"
|
||||
xous = { path = "../../xous-rs" }
|
||||
|
||||
4
examples/shell/README.md
Normal file
4
examples/shell/README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Xous Kernel
|
||||
|
||||
This contains the core kernel for Xous. It is implemented as
|
||||
a library that you include with your `pid 0` program.
|
||||
75
examples/shell/src/debug.rs
Normal file
75
examples/shell/src/debug.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
use core::fmt::{Error, Write};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print
|
||||
{
|
||||
($($args:tt)+) => ({
|
||||
use core::fmt::Write;
|
||||
let _ = write!(crate::debug::DEFAULT_UART, $($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 base: *mut usize,
|
||||
}
|
||||
|
||||
pub const DEFAULT_UART: Uart = Uart {
|
||||
base: 0xE000_1000 as *mut usize,
|
||||
};
|
||||
|
||||
impl Uart {
|
||||
pub fn putc(&self, c: u8) {
|
||||
unsafe {
|
||||
// Wait until TXFULL is `0`
|
||||
while self.base.add(1).read_volatile() != 0 {
|
||||
()
|
||||
}
|
||||
self.base.add(0).write_volatile(c as usize)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn enable_rx(self) {
|
||||
unsafe {
|
||||
self.base
|
||||
.add(5)
|
||||
.write_volatile(self.base.add(5).read_volatile() | 2)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getc(&self) -> Option<u8> {
|
||||
unsafe {
|
||||
// If EV_PENDING_RX is 1, return the pending character.
|
||||
// Otherwise, return None.
|
||||
match self.base.add(4).read_volatile() & 2 {
|
||||
0 => None,
|
||||
ack => {
|
||||
let c = Some(self.base.add(0).read_volatile() as u8);
|
||||
self.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(())
|
||||
}
|
||||
}
|
||||
89
examples/shell/src/main.rs
Normal file
89
examples/shell/src/main.rs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
mod debug;
|
||||
mod start;
|
||||
|
||||
mod timer;
|
||||
|
||||
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_UART.getc() {
|
||||
print!("0x{:02x}", c);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
fn main() {
|
||||
xous::rsyscall(xous::SysCall::MapPhysical(
|
||||
0xF0001000 as *mut usize,
|
||||
debug::DEFAULT_UART.base as *mut usize,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
))
|
||||
.expect("couldn't map address");
|
||||
xous::rsyscall(xous::SysCall::MapPhysical(
|
||||
0xF0002000 as *mut usize,
|
||||
0xF0002000 as *mut usize,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
))
|
||||
.map(|_| println!("!!!WARNING: managed to steal kernel's memory"))
|
||||
.ok();
|
||||
println!("Process: map success!");
|
||||
debug::DEFAULT_UART.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...");
|
||||
{
|
||||
let _big_array = [42u8; 16384];
|
||||
}
|
||||
|
||||
println!("Increasing heap to 32768...");
|
||||
let heap = xous::rsyscall(xous::SysCall::IncreaseHeap(
|
||||
32768,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
))
|
||||
.expect("couldn't increase heap");
|
||||
if let xous::Result::MemoryRange(start, len) = heap {
|
||||
println!(
|
||||
"Heap goes from {:08x} - {:08x}",
|
||||
start as usize,
|
||||
start as usize + len
|
||||
);
|
||||
use core::slice;
|
||||
let mem_range = unsafe { slice::from_raw_parts_mut(start, len) };
|
||||
println!("Filling with bytes...");
|
||||
for word in mem_range.iter_mut() {
|
||||
*word = 42;
|
||||
}
|
||||
println!("Done!");
|
||||
} else {
|
||||
println!("Unexpected return value: {:?}", heap);
|
||||
}
|
||||
|
||||
timer::init();
|
||||
|
||||
loop {
|
||||
println!("Waiting for an event...");
|
||||
xous::rsyscall(xous::SysCall::WaitEvent).expect("Couldn't call waitevent");
|
||||
}
|
||||
}
|
||||
20
examples/shell/src/start.rs
Normal file
20
examples/shell/src/start.rs
Normal 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");
|
||||
}
|
||||
70
examples/shell/src/timer.rs
Normal file
70
examples/shell/src/timer.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
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(15).write_volatile(1) };
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
println!("Allocating timer...");
|
||||
xous::rsyscall(xous::SysCall::MapPhysical(
|
||||
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(16).write_volatile(1) };
|
||||
}
|
||||
|
||||
pub fn load(value: u32) {
|
||||
let buf = value.to_le_bytes();
|
||||
let ptr = TIMER_BASE as *mut usize;
|
||||
unsafe {
|
||||
ptr.add(0).write_volatile(buf[0] as usize);
|
||||
ptr.add(1).write_volatile(buf[1] as usize);
|
||||
ptr.add(2).write_volatile(buf[2] as usize);
|
||||
ptr.add(3).write_volatile(buf[3] as usize);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reload(value: u32) {
|
||||
let buf = value.to_le_bytes();
|
||||
let ptr = TIMER_BASE as *mut usize;
|
||||
unsafe {
|
||||
ptr.add(4).write_volatile(buf[0] as usize);
|
||||
ptr.add(5).write_volatile(buf[1] as usize);
|
||||
ptr.add(6).write_volatile(buf[2] as usize);
|
||||
ptr.add(7).write_volatile(buf[3] as usize);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn en(en: bool) {
|
||||
let ptr = TIMER_BASE as *mut usize;
|
||||
if en {
|
||||
unsafe { ptr.add(8).write_volatile(1) };
|
||||
}
|
||||
else {
|
||||
unsafe { ptr.add(8).write_volatile(0) };
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue