2020-06-03 07:39:24 +00:00
|
|
|
#![cfg_attr(baremetal, no_std)]
|
|
|
|
|
#![cfg_attr(baremetal, no_main)]
|
2020-03-13 12:32:44 +00:00
|
|
|
|
2020-06-03 07:39:24 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-13 12:32:44 +00:00
|
|
|
#[macro_use]
|
|
|
|
|
mod debug;
|
2020-06-03 07:39:24 +00:00
|
|
|
|
2020-03-31 10:03:19 +00:00
|
|
|
mod log_string;
|
2020-03-13 12:32:44 +00:00
|
|
|
mod start;
|
|
|
|
|
|
2020-06-03 07:39:24 +00:00
|
|
|
use core::fmt::Write;
|
2020-07-23 11:06:41 +00:00
|
|
|
use log_string::LogString;
|
|
|
|
|
|
|
|
|
|
#[cfg(bracket_terminal)]
|
|
|
|
|
mod implementation {
|
|
|
|
|
use bracket_terminal::prelude::*;
|
|
|
|
|
use core::fmt::{Error, Write};
|
|
|
|
|
use std::sync::mpsc::{channel, Sender};
|
|
|
|
|
use std::thread::{spawn, JoinHandle};
|
|
|
|
|
|
|
|
|
|
struct State {}
|
|
|
|
|
|
|
|
|
|
impl GameState for State {
|
|
|
|
|
fn tick(&mut self, ctx: &mut BTerm) {
|
|
|
|
|
ctx.print(1, 1, "Hello Bracket World");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ControlMessage {
|
|
|
|
|
Text(String),
|
|
|
|
|
Exit,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Output {
|
|
|
|
|
stream: Sender<ControlMessage>,
|
|
|
|
|
context: Option<BTerm>,
|
|
|
|
|
gs: Option<State>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init() -> Output {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
|
|
|
|
|
let context = BTermBuilder::simple80x50()
|
|
|
|
|
.with_title("Hello Minimal Bracket World")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let gs: State = State {};
|
|
|
|
|
Output {
|
|
|
|
|
stream: tx,
|
|
|
|
|
context: Some(context),
|
|
|
|
|
gs: Some(gs),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Output {
|
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
|
main_loop(self.context.take().unwrap(), self.gs.take().unwrap());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Output {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.stream.send(ControlMessage::Exit).unwrap();
|
|
|
|
|
// self.finish.take().unwrap().join().unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Write for Output {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
|
|
|
|
// It would be nice if this worked with &str
|
|
|
|
|
self.stream
|
|
|
|
|
.send(ControlMessage::Text(s.to_owned()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 12:32:44 +00:00
|
|
|
|
2020-06-03 07:39:24 +00:00
|
|
|
#[cfg(not(baremetal))]
|
2020-07-23 11:06:41 +00:00
|
|
|
mod implementation {
|
|
|
|
|
use core::fmt::{Error, Write};
|
|
|
|
|
use pancurses::{endwin, initscr, Window};
|
2020-08-03 09:40:57 +00:00
|
|
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
2020-07-23 11:06:41 +00:00
|
|
|
|
|
|
|
|
enum ControlMessage {
|
|
|
|
|
Text(String),
|
|
|
|
|
Exit,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Output {
|
|
|
|
|
window: Option<Window>,
|
|
|
|
|
tx: Sender<ControlMessage>,
|
|
|
|
|
rx: Receiver<ControlMessage>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init() -> Output {
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let window = initscr();
|
|
|
|
|
window.nodelay(true);
|
|
|
|
|
|
|
|
|
|
Output {
|
|
|
|
|
tx,
|
|
|
|
|
rx,
|
|
|
|
|
window: Some(window),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Output {
|
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
|
loop {
|
|
|
|
|
match self.rx.recv_timeout(std::time::Duration::from_millis(50)) {
|
2020-08-03 09:40:57 +00:00
|
|
|
Ok(msg) => match msg {
|
|
|
|
|
ControlMessage::Exit => break,
|
|
|
|
|
ControlMessage::Text(s) => {
|
|
|
|
|
self.window.as_ref().unwrap().printw(s);
|
|
|
|
|
self.window.as_ref().unwrap().refresh();
|
2020-07-23 11:06:41 +00:00
|
|
|
}
|
2020-08-03 09:40:57 +00:00
|
|
|
},
|
2020-07-23 11:06:41 +00:00
|
|
|
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
|
|
|
|
// Calling `getch` refreshes the screen
|
|
|
|
|
self.window.as_ref().unwrap().getch();
|
|
|
|
|
}
|
|
|
|
|
Err(e) => panic!("Error: {}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_writer(&self) -> OutputWriter {
|
2020-08-03 09:40:57 +00:00
|
|
|
OutputWriter {
|
|
|
|
|
tx: self.tx.clone(),
|
|
|
|
|
}
|
2020-07-23 11:06:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Output {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.tx.send(ControlMessage::Exit).unwrap();
|
|
|
|
|
endwin();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Write for Output {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
|
|
|
|
// It would be nice if this worked with &str
|
2020-08-03 09:40:57 +00:00
|
|
|
self.tx.send(ControlMessage::Text(s.to_owned())).unwrap();
|
2020-07-23 11:06:41 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct OutputWriter {
|
|
|
|
|
tx: Sender<ControlMessage>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Write for OutputWriter {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
|
|
|
|
// It would be nice if this worked with &str
|
2020-08-03 09:40:57 +00:00
|
|
|
self.tx.send(ControlMessage::Text(s.to_owned())).unwrap();
|
2020-07-23 11:06:41 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 12:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-03 07:39:24 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-07-23 11:06:41 +00:00
|
|
|
mod implementation {
|
2020-08-03 09:40:57 +00:00
|
|
|
use core::fmt::{Error, Write};
|
|
|
|
|
|
2020-07-23 11:06:41 +00:00
|
|
|
pub struct Output {
|
2020-08-10 14:47:18 +00:00
|
|
|
// addr: usize,
|
2020-08-03 09:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init() -> Output {
|
|
|
|
|
let uart = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(0xf000_4000),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map serial port");
|
|
|
|
|
unsafe { crate::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ };
|
|
|
|
|
println!("Mapped UART @ {:08x}", uart.addr.get());
|
|
|
|
|
|
|
|
|
|
println!("Process: map success!");
|
|
|
|
|
crate::debug::DEFAULT.enable_rx();
|
|
|
|
|
|
|
|
|
|
println!("Allocating IRQ...");
|
|
|
|
|
xous::syscall::claim_interrupt(4, handle_irq, core::ptr::null_mut::<usize>())
|
|
|
|
|
.expect("couldn't claim interrupt");
|
2020-08-10 13:17:48 +00:00
|
|
|
println!("Claimed IRQ 4");
|
2020-08-03 09:40:57 +00:00
|
|
|
Output {
|
2020-08-10 14:47:18 +00:00
|
|
|
// addr: uart.as_mut_ptr() as usize,
|
2020-08-03 09:40:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Output {
|
|
|
|
|
pub fn get_writer(&self) -> OutputWriter {
|
|
|
|
|
OutputWriter { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
|
loop {
|
|
|
|
|
xous::wait_event();
|
|
|
|
|
// match self.rx.recv_timeout(std::time::Duration::from_millis(50)) {
|
|
|
|
|
// Ok(msg) => match msg {
|
|
|
|
|
// ControlMessage::Exit => break,
|
|
|
|
|
// ControlMessage::Text(s) => {
|
|
|
|
|
// self.window.as_ref().unwrap().printw(s);
|
|
|
|
|
// self.window.as_ref().unwrap().refresh();
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
|
|
|
|
// // Calling `getch` refreshes the screen
|
|
|
|
|
// self.window.as_ref().unwrap().getch();
|
|
|
|
|
// }
|
|
|
|
|
// Err(e) => panic!("Error: {}", e),
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 11:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-03 07:39:24 +00:00
|
|
|
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!();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 09:40:57 +00:00
|
|
|
pub struct OutputWriter {
|
|
|
|
|
}
|
2020-06-03 07:39:24 +00:00
|
|
|
|
2020-08-03 09:40:57 +00:00
|
|
|
impl OutputWriter {
|
|
|
|
|
pub fn putc(&self, c: u8) {
|
|
|
|
|
unsafe {
|
|
|
|
|
let base = crate::debug::DEFAULT_UART_ADDR;
|
|
|
|
|
// Wait until TXFULL is `0`
|
|
|
|
|
while base.add(1).read_volatile() != 0 {}
|
|
|
|
|
base.add(0).write_volatile(c as usize)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 12:32:44 +00:00
|
|
|
|
2020-08-03 09:40:57 +00:00
|
|
|
impl Write for OutputWriter {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
|
|
|
|
for c in s.bytes() {
|
|
|
|
|
self.putc(c);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2020-03-13 12:32:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 09:40:57 +00:00
|
|
|
fn reader_thread(mut output: implementation::OutputWriter) {
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(output, "Xous Logging Server starting up...").unwrap();
|
2020-08-03 09:40:57 +00:00
|
|
|
// std::thread::sleep(std::time::Duration::from_secs(1));
|
2020-03-13 12:32:44 +00:00
|
|
|
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(output, "Starting log server...").unwrap();
|
|
|
|
|
let server_addr = xous::create_server(b"xous-logs-output").unwrap();
|
|
|
|
|
writeln!(output, "Server listening on address {:?}", server_addr).unwrap();
|
2020-03-14 07:58:22 +00:00
|
|
|
|
2020-06-03 07:39:24 +00:00
|
|
|
let mut counter: usize = 0;
|
2020-03-13 12:32:44 +00:00
|
|
|
loop {
|
2020-06-03 07:39:24 +00:00
|
|
|
if counter.trailing_zeros() >= 12 {
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(output, "Counter tick: {}", counter).unwrap();
|
2020-03-31 10:03:19 +00:00
|
|
|
}
|
2020-04-06 07:52:07 +00:00
|
|
|
counter += 1;
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(output, "Waiting for an event...").unwrap();
|
2020-06-03 07:39:24 +00:00
|
|
|
let mut envelope =
|
|
|
|
|
xous::syscall::receive_message(server_addr).expect("couldn't get address");
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(output, "Got message envelope: {:?}", envelope).unwrap();
|
2020-06-03 07:39:24 +00:00
|
|
|
match &mut envelope.message {
|
|
|
|
|
xous::Message::Scalar(msg) => {
|
2020-08-03 09:40:57 +00:00
|
|
|
writeln!(output, "Scalar message from {}: {:?}", envelope.sender, msg).unwrap();
|
2020-06-03 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
xous::Message::Move(msg) => {
|
|
|
|
|
let log_entry = LogString::from_message(msg);
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(
|
|
|
|
|
output,
|
|
|
|
|
"Moved log message from {}: {}",
|
|
|
|
|
envelope.sender, log_entry
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2020-06-03 07:39:24 +00:00
|
|
|
}
|
2020-07-13 11:19:25 +00:00
|
|
|
xous::Message::Borrow(msg) => {
|
2020-06-03 07:39:24 +00:00
|
|
|
let log_entry = LogString::from_message(msg);
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(
|
|
|
|
|
output,
|
|
|
|
|
"Immutably borrowed log message from {}: {}",
|
|
|
|
|
envelope.sender, log_entry
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2020-06-03 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
xous::Message::MutableBorrow(msg) => {
|
|
|
|
|
let mut log_entry = LogString::from_message(msg);
|
2020-07-23 11:06:41 +00:00
|
|
|
writeln!(
|
|
|
|
|
output,
|
|
|
|
|
"Immutably borrowed log message from {}: {}",
|
|
|
|
|
envelope.sender, log_entry
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2020-06-03 07:39:24 +00:00
|
|
|
writeln!(log_entry, " << HELLO FROM THE SERVER").unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 12:32:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-23 11:06:41 +00:00
|
|
|
|
|
|
|
|
#[cfg_attr(baremetal, no_mangle)]
|
2020-08-03 09:40:57 +00:00
|
|
|
fn main() {
|
2020-07-23 11:06:41 +00:00
|
|
|
let mut output = implementation::init();
|
2020-08-03 09:40:57 +00:00
|
|
|
let writer = output.get_writer();
|
|
|
|
|
// xous::arch::ensure_connection().unwrap();
|
2020-08-10 13:17:48 +00:00
|
|
|
println!("Creating the reader thread");
|
2020-08-03 09:40:57 +00:00
|
|
|
xous::create_thread_simple(reader_thread, writer).unwrap();
|
2020-08-10 13:17:48 +00:00
|
|
|
println!("Running the output");
|
2020-07-23 11:06:41 +00:00
|
|
|
output.run();
|
|
|
|
|
}
|