attempt a refactor to add non-blocking battery status checking

stuck at trying to receive messages in the main shell in a
non-blocking manner
This commit is contained in:
bunnie 2020-11-25 19:58:24 +08:00
parent a4a6065348
commit 62f8ce32f0
7 changed files with 117 additions and 27 deletions

1
Cargo.lock generated
View file

@ -793,6 +793,7 @@ version = "0.1.0"
dependencies = [
"bitflags",
"com",
"com_rs",
"graphics-server",
"log",
"log-server",

@ -1 +1 @@
Subproject commit 09935173976137e4010503f3c7e9f6db13cc28f9
Subproject commit 9a91a81f482ea8eba2f2de5d6c20b9bd399f39a9

View file

@ -39,6 +39,12 @@ pub enum Opcode {
/// Battery stats
BattStats,
/// Battery stats, non-blocking
BattStatsNb,
/// Battery stats return
BattStatsReturn(BattStats),
/// Query Full charge capacity of the battery
BattFullCapacity,
@ -92,6 +98,11 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode {
Ok(Opcode::ShipMode)
} else if m.id as u16 == ComState::POWER_OFF.verb {
Ok(Opcode::PowerOffSoc)
} else if m.id as u16 == ComState::STAT.verb {
Ok(Opcode::BattStatsNb)
} else if m.id as u16 == ComState::STAT_RETURN.verb {
let raw_stats: [usize; 2] = [m.arg1, m.arg2];
Ok(Opcode::BattStatsReturn(raw_stats.into()))
} else {
Err("unrecognized command")
}
@ -123,8 +134,20 @@ impl Into<Message> for Opcode {
id: ComState::POWER_OFF.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
Opcode::BattStats => Message::BlockingScalar(ScalarMessage {
id: ComState::STAT.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
Opcode::BattStatsNb => Message::Scalar(ScalarMessage {
id: ComState::STAT.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
Opcode::ImuAccelRead => Message::BlockingScalar(ScalarMessage {
id: ComState::GYRO_READ.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
Opcode::BattStatsReturn(stats) => {
let raw_stats: [usize; 2] = stats.into();
Message::Scalar(ScalarMessage {
id: ComState::STAT_RETURN.verb as usize,
arg1: raw_stats[1],
arg2: raw_stats[0],
arg3: 0,
arg4: 0,
})
}
_ => todo!("message type not yet implemented")
}
}

View file

@ -22,21 +22,6 @@ pub fn get_batt_stats(cid: CID) -> Result<BattStats, xous::Error> {
}
}
/*
pub fn elapsed_ms(cid: CID) -> Result<u64, Error> {
let response = send_message(cid, api::Opcode::ElapsedMs.into())?;
if let xous::Result::Scalar2(upper, lower) = response {
Ok(upper as u64 | ((lower as u64) << 32))
} else {
panic!("unexpected return value: {:#?}", response);
}
pub fn get_batt_stats_nb(cid: CID) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::BattStatsNb.into()).map(|_|())
}
pub fn reset(cid: CID) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Reset.into()).map(|_| ())
}
pub fn sleep_ms(cid: CID, ms: usize) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SleepMs(ms).into()).map(|_| ())
}
*/

View file

@ -14,28 +14,38 @@ use log::{error, info};
use com_rs::*;
#[derive(Debug, Copy, Clone)]
pub struct WorkRequest {
work: ComSpec,
sender: xous::MessageSender,
}
#[cfg(target_os = "none")]
mod implementation {
use crate::api::BattStats;
use crate::WorkRequest;
use ticktimer_server::*;
use com_rs::*;
use utralib::generated::*;
use xous::CID;
use log::{error, info};
/*
#[macro_use]
use heapless::Vec;
use heapless::consts::*;
/*
use typenum::{UInt, UTerm};
use typenum::bit::{B0, B1};
type U1280 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
*/
*/
const STD_TIMEOUT: u32 = 100;
pub struct XousCom {
csr: utralib::CSR<u32>,
ticktimer: CID,
pub workqueue: Vec<WorkRequest, U64>,
busy: bool,
}
fn handle_irq(_irq_no: usize, arg: *mut usize) {
@ -45,6 +55,10 @@ mod implementation {
xc.csr.wo(utra::com::EV_PENDING, xc.csr.r(utra::com::EV_PENDING));
}
fn return_battstats(cid: CID, stats: BattStats) -> Result<(), xous::Error> {
xous::send_message(cid, crate::api::Opcode::BattStatsReturn(stats).into()).map(|_| ())
}
impl XousCom {
pub fn new() -> XousCom {
let csr = xous::syscall::map_memory(
@ -55,12 +69,16 @@ mod implementation {
)
.expect("couldn't map COM CSR range");
info!("test");
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap();
let mut xc = XousCom {
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
ticktimer: ticktimer_conn,
workqueue: Vec::new(),
busy: false,
//tx_queue: Vec::new(),
//rx_queue: Vec::new(),
//in_progress: false,
@ -88,28 +106,43 @@ mod implementation {
}
pub fn wait_txrx(&mut self, tx: u16, timeout: Option<u32>) -> u16 {
self.csr.wfo(utra::com::EV_ENABLE_SPI_HOLD, 1);
if timeout.is_some() {
let curtime = ticktimer_server::elapsed_ms(self.ticktimer).expect("couldn't connect to ticktimer");
let mut timed_out = false;
let to = timeout.unwrap() as u64;
// timeout after 0.25 second to avoid hangs in case of a bug in the protocol
while self.csr.rf(utra::com::STATUS_HOLD) == 1 && !timed_out {
if (ticktimer_server::elapsed_ms(self.ticktimer).expect("couldn't connect to ticktimer") - curtime) > to {
timed_out = true;
}
xous::wait_event();
xous::yield_slice();
}
} else {
while self.csr.rf(utra::com::STATUS_HOLD) == 1 {
self.csr.wfo(utra::com::EV_ENABLE_SPI_HOLD, 1);
xous::wait_event();
self.csr.wfo(utra::com::EV_ENABLE_SPI_HOLD, 0);
}
}
self.csr.wfo(utra::com::EV_ENABLE_SPI_HOLD, 0);
self.txrx(tx)
}
pub fn process_queue(&mut self) {
if !self.workqueue.is_empty() && !self.busy {
self.busy = true;
let work_descriptor = self.workqueue.swap_remove(0); // not quite FIFO, but Vec does not support FIFO (best we can do with "heapless")
if work_descriptor.work.verb == ComState::STAT.verb {
let stats = self.get_battstats();
return_battstats(work_descriptor.sender, stats).expect("Could not return BattStatsNb value");
info!("Returned BattStatsNb value");
} else {
error!("unimplemented work queue responder 0x{:x}", work_descriptor.work.verb);
}
self.busy = false;
}
}
pub fn get_battstats(&mut self) -> BattStats {
let mut stats = BattStats::default();
@ -133,12 +166,23 @@ mod implementation {
#[cfg(not(target_os = "none"))]
mod implementation {
use crate::api::BattStats;
use crate::WorkRequest;
use log::{error, info};
#[macro_use]
use heapless::Vec;
use heapless::consts::*;
pub struct XousCom {
workqueue: Vec<WorkRequest, U64>,
busy: bool,
}
impl XousCom {
pub fn new() -> XousCom {
XousCom {
workqueue: Vec::new(),
busy: false,
}
}
@ -154,6 +198,21 @@ mod implementation {
remaining_capacity: 750,
}
}
pub fn process_queue(&mut self) {
if !self.workqueue.is_empty() && !self.busy {
self.busy = true;
let work_descriptor = self.workqueue.swap_remove(0); // not quite FIFO, but Vec does not support FIFO (best we can do with "heapless")
if work_descriptor.work.verb == ComState::STAT.verb {
let stats = self.get_battstats();
shell::return_battstats(work_descriptor.sender, stats).expect("Could not return BattStatsNb value");
info!("Returned BattStatsNb value");
} else {
error!("unimplemented work queue responder 0x{:x}", work_descriptor.work.verb);
}
self.busy = false;
}
}
}
}
@ -192,10 +251,16 @@ fn xmain() -> ! {
).expect("COM: couldn't return batt stats request");
info!("COM: done returning batt stats request");
}
_ => error!("unknown opcode"),
Opcode::BattStatsNb => {
info!("COM: batt stats non-blocking request received");
com.workqueue.push(WorkRequest { work: ComState::STAT, sender: envelope.sender }).unwrap();
}
_ => error!("unknown opcode"),
}
} else {
error!("couldn't convert opcode");
}
com.process_queue();
}
}

View file

@ -13,6 +13,8 @@ ticktimer-server = { path = "../ticktimer-server" }
log-server = { path = "../log-server" }
log = "0.4"
com = { path = "../com"}
com_rs = { path = "../com/com_rs" }
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}

View file

@ -15,6 +15,8 @@ use graphics_server::GlyphSet;
use com::api::BattStats;
use com::*;
use core::convert::TryFrom;
// fn print_and_yield(index: *mut usize) -> ! {
// let num = index as usize;
// loop {
@ -126,6 +128,7 @@ fn shell_main() -> ! {
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
let log_server_id = xous::SID::from_bytes(b"xous-log-server ").unwrap();
let com_id = xous::SID::from_bytes(b"com ").unwrap();
let shell_id = xous::SID::from_bytes(b"shel ").unwrap();
println!("SHELL: Attempting to connect to servers...");
let log_conn = xous::connect(log_server_id).unwrap();
@ -170,7 +173,7 @@ fn shell_main() -> ! {
)
.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);
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 0);
}
graphics_server::set_style(
@ -221,5 +224,16 @@ fn shell_main() -> ! {
.expect("unable to draw to screen");
graphics_server::flush(graphics_conn).expect("unable to draw to screen");
/*
let envelope = xous::receive_message(shell_id).unwrap();
if let Ok(opcode) = com::api::Opcode::try_from(&envelope.body) {
match opcode {
com::api::Opcode::BattStatsReturn(stats) => {
batt_stats = stats;
},
_ => error!("SHELL: received unexpected opcode"),
}
}*/
}
}