2020-06-03 03:01:50 +00:00
|
|
|
#![cfg_attr(baremetal, no_main)]
|
|
|
|
|
#![cfg_attr(baremetal, no_std)]
|
2020-03-10 04:46:05 +00:00
|
|
|
|
2020-06-03 03:01:50 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:46:05 +00:00
|
|
|
#[macro_use]
|
|
|
|
|
mod debug;
|
|
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
mod logstr;
|
2020-10-28 03:14:09 +00:00
|
|
|
mod timer;
|
2020-10-14 15:01:57 +00:00
|
|
|
use core::fmt::Write;
|
2020-03-31 10:03:19 +00:00
|
|
|
|
|
|
|
|
// fn print_and_yield(index: *mut usize) -> ! {
|
|
|
|
|
// let num = index as usize;
|
|
|
|
|
// loop {
|
|
|
|
|
// println!("THREAD {}", num);
|
|
|
|
|
// xous::syscall::yield_slice();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2020-03-30 08:07:58 +00:00
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-10-12 21:22:19 +00:00
|
|
|
use utralib::generated::*;
|
2020-10-11 19:56:10 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
fn move_lfsr(mut lfsr: u32) -> u32 {
|
|
|
|
|
lfsr ^= lfsr >> 7;
|
|
|
|
|
lfsr ^= lfsr << 9;
|
|
|
|
|
lfsr ^= lfsr >> 13;
|
|
|
|
|
lfsr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_connection(server: xous::SID) -> xous::CID {
|
|
|
|
|
loop {
|
2020-08-30 09:41:02 +00:00
|
|
|
if let Ok(cid) = xous::syscall::try_connect(server) {
|
2020-08-29 14:09:20 +00:00
|
|
|
return cid;
|
|
|
|
|
}
|
|
|
|
|
xous::syscall::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 02:29:58 +00:00
|
|
|
struct StringBuffer<'a> {
|
|
|
|
|
raw_slice: &'a mut [u8],
|
|
|
|
|
pub s: &'a str,
|
|
|
|
|
pub len: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> StringBuffer<'a> {
|
|
|
|
|
pub fn new(max: usize) -> StringBuffer<'a> {
|
|
|
|
|
let mem = xous::map_memory(None, None, max, xous::MemoryFlags::R | xous::MemoryFlags::W).unwrap();
|
|
|
|
|
let p = mem.as_mut_ptr();
|
|
|
|
|
for i in 0..max {
|
|
|
|
|
unsafe { p.add(i).write_volatile(0) };
|
|
|
|
|
}
|
|
|
|
|
StringBuffer {
|
|
|
|
|
raw_slice: unsafe { core::slice::from_raw_parts_mut(mem.as_mut_ptr(), max)},
|
|
|
|
|
s: unsafe {
|
|
|
|
|
core::str::from_utf8_unchecked(core::slice::from_raw_parts(mem.as_ptr(), 0))
|
|
|
|
|
},
|
|
|
|
|
len: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Perform an immutable lend of this Carton to the specified server.
|
|
|
|
|
/// This function will block until the server returns.
|
|
|
|
|
pub fn lend(&self, connection: xous::CID, id: usize) -> Result<xous::Result, xous::Error> {
|
|
|
|
|
let memory_range = xous::MemoryRange::new(self.raw_slice.as_ptr() as _, self.raw_slice.len()).unwrap();
|
|
|
|
|
let msg = xous::MemoryMessage {
|
|
|
|
|
id,
|
|
|
|
|
buf: memory_range,
|
|
|
|
|
offset: None,
|
|
|
|
|
valid: xous::MemorySize::new(self.len).map(|x| Some(x)).unwrap_or(None),
|
|
|
|
|
};
|
|
|
|
|
xous::send_message(connection, xous::Message::Borrow(msg))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
|
self.len = 0;
|
|
|
|
|
self.s = unsafe {
|
|
|
|
|
core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.raw_slice.as_ptr(), self.len))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> core::fmt::Display for StringBuffer<'a> {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
|
write!(f, "{}", self.s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> core::fmt::Write for StringBuffer<'a> {
|
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), core::fmt::Error> {
|
|
|
|
|
for c in s.bytes() {
|
|
|
|
|
if self.len < self.raw_slice.len() {
|
|
|
|
|
self.raw_slice[self.len] = c;
|
|
|
|
|
self.len += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.s = unsafe {
|
|
|
|
|
core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.raw_slice.as_ptr(), self.len))
|
|
|
|
|
};
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-18 09:00:48 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn shell_main() -> ! {
|
2020-03-10 04:46:05 +00:00
|
|
|
timer::init();
|
|
|
|
|
|
2020-10-04 11:07:53 +00:00
|
|
|
// let log_server_id = xous::SID::from_bytes(b"xous-logs-output").unwrap();
|
2020-08-29 14:09:20 +00:00
|
|
|
let graphics_server_id = xous::SID::from_bytes(b"graphics-server ").unwrap();
|
2020-10-12 11:08:16 +00:00
|
|
|
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
|
2020-10-14 15:01:57 +00:00
|
|
|
let log_server_id = xous::SID::from_bytes(b"xous-log-server ").unwrap();
|
2020-07-29 11:31:49 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
println!("SHELL: Attempting to connect to servers...");
|
2020-10-14 15:01:57 +00:00
|
|
|
let log_conn = ensure_connection(log_server_id);
|
2020-08-29 14:09:20 +00:00
|
|
|
let graphics_conn = ensure_connection(graphics_server_id);
|
2020-10-12 11:08:16 +00:00
|
|
|
let ticktimer_conn = ensure_connection(ticktimer_server_id);
|
2020-07-29 11:31:49 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
println!(
|
2020-10-12 11:08:16 +00:00
|
|
|
"SHELL: Connected to Log server: {} Graphics server: {} Ticktimer server: {}",
|
|
|
|
|
log_conn, graphics_conn, ticktimer_conn,
|
2020-08-29 14:09:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_ne!(
|
|
|
|
|
log_conn, graphics_conn,
|
|
|
|
|
"SHELL: graphics and log connections are the same!"
|
|
|
|
|
);
|
2020-03-19 09:26:16 +00:00
|
|
|
|
2020-10-12 11:08:16 +00:00
|
|
|
assert_ne!(
|
|
|
|
|
ticktimer_conn, graphics_conn,
|
|
|
|
|
"SHELL: graphics and ticktimer connections are the same!"
|
|
|
|
|
);
|
|
|
|
|
|
2020-10-13 19:40:44 +00:00
|
|
|
// let mut counter: usize = 0;
|
2020-10-14 15:01:57 +00:00
|
|
|
let mut ls = logstr::LogStr::new();
|
2020-08-29 14:09:20 +00:00
|
|
|
let mut lfsr = 0xace1u32;
|
|
|
|
|
let dark = graphics_server::Color::from(0);
|
|
|
|
|
let light = graphics_server::Color::from(!0);
|
2020-10-12 21:22:19 +00:00
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
#[cfg(baremetal)]
|
|
|
|
|
{
|
|
|
|
|
let gpio_base = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::gpio::HW_GPIO_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map GPIO CSR range");
|
|
|
|
|
let mut gpio = CSR::new(gpio_base.as_mut_ptr() as *mut u32);
|
|
|
|
|
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 2);
|
|
|
|
|
}
|
2020-10-12 21:22:19 +00:00
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
let mut last_time: u64 = 0;
|
2020-03-10 04:46:05 +00:00
|
|
|
loop {
|
2020-10-14 15:01:57 +00:00
|
|
|
// a message passing demo -- checking time
|
|
|
|
|
if let Ok(elapsed_time) = ticktimer_server::elapsed_ms(ticktimer_conn) {
|
|
|
|
|
println!("SHELL: {}ms", elapsed_time);
|
2020-10-28 03:14:09 +00:00
|
|
|
if elapsed_time - last_time > 40 {
|
2020-10-14 15:01:57 +00:00
|
|
|
last_time = elapsed_time;
|
|
|
|
|
/*
|
|
|
|
|
xous::try_send_message(log_conn,
|
|
|
|
|
xous::Message::Scalar(xous::ScalarMessage{id:256, arg1: elapsed_time as usize, arg2: 257, arg3: 258, arg4: 259}));
|
|
|
|
|
*/
|
2020-10-14 16:01:02 +00:00
|
|
|
println!("Preparing a mutable borrow message");
|
|
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
ls.clear();
|
2020-10-28 03:14:09 +00:00
|
|
|
write!(
|
|
|
|
|
ls,
|
|
|
|
|
"Hello, Server! This memory is borrowed from another process. Elapsed: {}",
|
|
|
|
|
elapsed_time as usize
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't send hello message");
|
2020-10-14 15:01:57 +00:00
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
let mm = ls
|
|
|
|
|
.as_memory_message(0)
|
|
|
|
|
.expect("couldn't form memory message");
|
2020-10-14 16:01:02 +00:00
|
|
|
|
2020-10-14 15:01:57 +00:00
|
|
|
println!("Sending a mutable borrow message");
|
2020-10-14 16:01:02 +00:00
|
|
|
|
2020-10-28 03:14:09 +00:00
|
|
|
let response =
|
|
|
|
|
xous::syscall::try_send_message(log_conn, xous::Message::MutableBorrow(mm))
|
|
|
|
|
.expect("couldn't send memory message");
|
2020-10-14 15:01:57 +00:00
|
|
|
//unsafe { ls.set_len(response.0)};
|
|
|
|
|
//println!("Message came back with args ({}, {}) as: {}", response.0, response.1, ls);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
println!("error requesting ticktimer!")
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
// println!("Sending a scalar message with id {}...", counter + 4096);
|
|
|
|
|
// match xous::syscall::send_message(
|
|
|
|
|
// log_conn,
|
|
|
|
|
// xous::Message::Scalar(xous::ScalarMessage {
|
|
|
|
|
// id: counter + 4096,
|
|
|
|
|
// arg1: counter,
|
|
|
|
|
// arg2: counter * 2,
|
|
|
|
|
// arg3: !counter,
|
|
|
|
|
// arg4: counter + 1,
|
|
|
|
|
// }),
|
|
|
|
|
// ) {
|
|
|
|
|
// Err(xous::Error::ServerQueueFull) => {
|
|
|
|
|
// // println!("Server queue is full... retrying");
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
// Ok(_) => {
|
|
|
|
|
// println!("Loop {}", counter);
|
|
|
|
|
// counter += 1;
|
|
|
|
|
// }
|
|
|
|
|
// Err(e) => panic!("Unable to send message: {:?}", e),
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// lfsr = move_lfsr(lfsr);
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
match graphics_server::set_style(
|
|
|
|
|
graphics_conn,
|
|
|
|
|
5,
|
|
|
|
|
if lfsr & 1 == 0 { dark } else { light },
|
|
|
|
|
if lfsr & 1 == 0 { dark } else { light },
|
|
|
|
|
) {
|
|
|
|
|
Err(xous::Error::ServerQueueFull) => continue,
|
|
|
|
|
Ok(_) => break,
|
|
|
|
|
Err(e) => panic!("unable to draw to screen: {:?}", e),
|
|
|
|
|
}
|
2020-03-30 08:07:58 +00:00
|
|
|
}
|
2020-08-29 14:09:20 +00:00
|
|
|
let x1 = move_lfsr(lfsr);
|
|
|
|
|
let y1 = move_lfsr(x1);
|
|
|
|
|
let x2 = move_lfsr(y1);
|
|
|
|
|
let y2 = move_lfsr(x2);
|
|
|
|
|
lfsr = y2;
|
2020-08-10 15:04:36 +00:00
|
|
|
|
2020-08-29 14:09:20 +00:00
|
|
|
loop {
|
|
|
|
|
match graphics_server::draw_line(
|
|
|
|
|
graphics_conn,
|
|
|
|
|
graphics_server::Point::new((x1 % 336) as _, (y1 % 536) as _),
|
|
|
|
|
graphics_server::Point::new((x2 % 336) as _, (y2 % 536) as _),
|
|
|
|
|
) {
|
|
|
|
|
Err(xous::Error::ServerQueueFull) => continue,
|
|
|
|
|
Ok(_) => break,
|
|
|
|
|
Err(e) => panic!("unable to draw to screen: {:?}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
match graphics_server::flush(graphics_conn) {
|
|
|
|
|
Err(xous::Error::ServerQueueFull) => continue,
|
|
|
|
|
Ok(_) => break,
|
|
|
|
|
Err(e) => panic!("unable to draw to screen: {:?}", e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-04 11:07:53 +00:00
|
|
|
// let lfsr = move_lfsr(lfsr);
|
|
|
|
|
// if lfsr.trailing_zeros() >= 3 {
|
|
|
|
|
// loop {
|
|
|
|
|
// match xous::syscall::try_send_message(
|
|
|
|
|
// log_conn,
|
|
|
|
|
// xous::Message::Scalar(xous::ScalarMessage {
|
|
|
|
|
// id: counter + 4096,
|
|
|
|
|
// arg1: counter,
|
|
|
|
|
// arg2: counter * 2,
|
|
|
|
|
// arg3: !counter,
|
|
|
|
|
// arg4: lfsr as _,
|
|
|
|
|
// }),
|
|
|
|
|
// ) {
|
|
|
|
|
// Err(xous::Error::ServerQueueFull) => {
|
|
|
|
|
// println!("SHELL: Log Server queue is full... retrying");
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
// Ok(_) => {
|
|
|
|
|
// println!("SHELL: Loop {}", counter);
|
|
|
|
|
// counter += 1;
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
// Err(e) => panic!("Unable to send message: {:?}", e),
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// // #[cfg(not(target_os = "none"))]
|
2020-08-18 09:00:48 +00:00
|
|
|
// std::thread::sleep(std::time::Duration::from_millis(500));
|
2020-04-06 07:52:07 +00:00
|
|
|
// if counter & 2 == 0 {
|
|
|
|
|
// xous::syscall::yield_slice();
|
|
|
|
|
// }
|
2020-03-10 04:46:05 +00:00
|
|
|
}
|
|
|
|
|
}
|