2020-11-24 14:45:44 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
2021-10-08 18:12:12 +00:00
|
|
|
use api::*;
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2021-04-01 21:43:47 +00:00
|
|
|
use num_traits::{ToPrimitive, FromPrimitive};
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2021-04-01 21:43:47 +00:00
|
|
|
use log::{error, info, trace};
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2021-01-20 08:22:38 +00:00
|
|
|
use com_rs_ref as com_rs;
|
2020-11-24 14:45:44 +00:00
|
|
|
use com_rs::*;
|
2022-01-13 18:23:57 +00:00
|
|
|
use com_rs::serdes::{STR_32_WORDS, STR_64_WORDS, StringSer, Ipv4Conf};
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2021-04-16 11:58:05 +00:00
|
|
|
use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack};
|
2021-04-01 21:43:47 +00:00
|
|
|
use xous_ipc::{Buffer, String};
|
2020-12-31 11:31:12 +00:00
|
|
|
|
2022-01-13 18:23:57 +00:00
|
|
|
use core::convert::TryInto;
|
|
|
|
|
|
2022-01-13 13:12:14 +00:00
|
|
|
const LEGACY_REV: u32 = 0x8b5b_8e50; // this is the git rev shipped before we went to version tagging
|
|
|
|
|
const LEGACY_TAG: u32 = 0x00_09_05_00; // this is corresponding tag
|
2020-12-31 11:31:12 +00:00
|
|
|
const STD_TIMEOUT: u32 = 100;
|
2022-01-30 21:01:58 +00:00
|
|
|
const EC_BOOT_WAIT_MS: usize = 3500;
|
2020-11-25 11:58:24 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
pub struct WorkRequest {
|
|
|
|
|
work: ComSpec,
|
2020-11-25 15:06:57 +00:00
|
|
|
sender: CID,
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 10:31:51 +00:00
|
|
|
fn return_battstats(cid: CID, stats: api::BattStats) -> Result<(), xous::Error> {
|
2021-04-01 21:43:47 +00:00
|
|
|
let rawstats: [usize; 2] = stats.into();
|
|
|
|
|
xous::send_message(cid,
|
2021-04-02 08:40:17 +00:00
|
|
|
xous::Message::new_scalar(api::Callback::BattStats.to_usize().unwrap(),
|
2021-04-01 21:43:47 +00:00
|
|
|
rawstats[0], rawstats[1], 0, 0)
|
|
|
|
|
).map(|_| ())
|
2020-11-25 11:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(any(target_os = "none", target_os = "xous"))]
|
2020-11-24 14:45:44 +00:00
|
|
|
mod implementation {
|
2020-11-24 21:11:32 +00:00
|
|
|
use crate::api::BattStats;
|
2021-01-03 10:31:51 +00:00
|
|
|
use crate::return_battstats;
|
2020-11-25 11:58:24 +00:00
|
|
|
use crate::WorkRequest;
|
2021-01-20 08:22:38 +00:00
|
|
|
use com_rs_ref as com_rs;
|
2020-11-24 21:11:32 +00:00
|
|
|
use com_rs::*;
|
2021-01-04 20:40:34 +00:00
|
|
|
use log::error;
|
2020-11-24 14:45:44 +00:00
|
|
|
use utralib::generated::*;
|
2021-04-29 15:53:35 +00:00
|
|
|
use susres::{RegManager, RegOrField, SuspendResume};
|
2020-11-24 21:11:32 +00:00
|
|
|
|
|
|
|
|
const STD_TIMEOUT: u32 = 100;
|
2020-11-24 14:45:44 +00:00
|
|
|
|
|
|
|
|
pub struct XousCom {
|
|
|
|
|
csr: utralib::CSR<u32>,
|
2021-04-29 15:53:35 +00:00
|
|
|
susres: RegManager::<{utra::com::COM_NUMREGS}>,
|
2021-04-01 21:43:47 +00:00
|
|
|
ticktimer: ticktimer_server::Ticktimer,
|
2021-08-03 16:07:47 +00:00
|
|
|
pub workqueue: Vec<WorkRequest>,
|
2020-11-25 11:58:24 +00:00
|
|
|
busy: bool,
|
2021-04-16 11:58:05 +00:00
|
|
|
stby_current: Option<i16>,
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_irq(_irq_no: usize, arg: *mut usize) {
|
2020-11-24 21:11:32 +00:00
|
|
|
let xc = unsafe { &mut *(arg as *mut XousCom) };
|
|
|
|
|
// just clear the pending request, as this is used as a "wait" until request function
|
2021-01-03 10:31:51 +00:00
|
|
|
xc.csr
|
|
|
|
|
.wo(utra::com::EV_PENDING, xc.csr.r(utra::com::EV_PENDING));
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl XousCom {
|
|
|
|
|
pub fn new() -> XousCom {
|
|
|
|
|
let csr = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::com::HW_COM_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map COM CSR range");
|
|
|
|
|
|
2021-03-28 10:14:41 +00:00
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
|
2020-11-24 21:11:32 +00:00
|
|
|
|
2020-11-24 14:45:44 +00:00
|
|
|
let mut xc = XousCom {
|
|
|
|
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
2021-04-29 15:53:35 +00:00
|
|
|
susres: RegManager::new(csr.as_mut_ptr() as *mut u32),
|
2021-04-01 21:43:47 +00:00
|
|
|
ticktimer,
|
2020-11-25 11:58:24 +00:00
|
|
|
workqueue: Vec::new(),
|
|
|
|
|
busy: false,
|
2021-04-16 11:58:05 +00:00
|
|
|
stby_current: None,
|
2020-11-24 14:45:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xous::claim_interrupt(
|
|
|
|
|
utra::com::COM_IRQ,
|
|
|
|
|
handle_irq,
|
|
|
|
|
(&mut xc) as *mut XousCom as *mut usize,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't claim irq");
|
2021-04-29 15:53:35 +00:00
|
|
|
|
|
|
|
|
xc.susres.push(RegOrField::Reg(utra::com::CONTROL), None);
|
|
|
|
|
xc.susres.push_fixed_value(RegOrField::Reg(utra::com::EV_PENDING), 0xFFFF_FFFF);
|
|
|
|
|
xc.susres.push(RegOrField::Reg(utra::com::EV_ENABLE), None);
|
|
|
|
|
|
2020-11-24 14:45:44 +00:00
|
|
|
xc
|
|
|
|
|
}
|
2021-04-29 15:53:35 +00:00
|
|
|
pub fn suspend(&mut self) {
|
|
|
|
|
self.susres.suspend();
|
|
|
|
|
self.csr.wo(utra::com::EV_ENABLE, 0);
|
2022-01-27 14:05:38 +00:00
|
|
|
self.csr.wo(utra::com::EV_PENDING, 0xFFFF_FFFF);
|
2021-04-29 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
pub fn resume(&mut self) {
|
|
|
|
|
self.susres.resume();
|
|
|
|
|
// issue a "link sync" command because the COM had continued running, and we may have sent garbage during suspend
|
|
|
|
|
self.txrx(ComState::LINK_SYNC.verb);
|
2021-05-15 20:29:12 +00:00
|
|
|
// wait a moment for the link to stabilize, before allowing any other commands to issue
|
|
|
|
|
self.ticktimer.sleep_ms(5).unwrap();
|
2021-04-29 15:53:35 +00:00
|
|
|
}
|
2020-11-24 14:45:44 +00:00
|
|
|
|
|
|
|
|
pub fn txrx(&mut self, tx: u16) -> u16 {
|
2021-01-03 19:19:23 +00:00
|
|
|
self.csr.wfo(utra::com::TX_TX, tx as u32);
|
|
|
|
|
/* transaction is automatically initiated on write
|
|
|
|
|
wait while transaction is in progress. A transaction takes about 80-100 CPU cycles;
|
|
|
|
|
not quite enough to be worth the overhead of an interrupt, so we just yield our time slice */
|
2020-11-24 14:45:44 +00:00
|
|
|
while self.csr.rf(utra::com::STATUS_TIP) == 1 {
|
2021-01-01 19:04:28 +00:00
|
|
|
// xous::yield_slice();
|
2021-01-03 19:19:23 +00:00
|
|
|
/* ... and it turns out yielding the slice is a bad idea, because you may not get re-scheduled
|
|
|
|
|
for a very long time, which causes the COM responder to timeout. Just waste the cycles. */
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// grab the RX value and return it
|
|
|
|
|
self.csr.rf(utra::com::RX_RX) as u16
|
|
|
|
|
}
|
2020-11-24 21:11:32 +00:00
|
|
|
|
|
|
|
|
pub fn wait_txrx(&mut self, tx: u16, timeout: Option<u32>) -> u16 {
|
|
|
|
|
if timeout.is_some() {
|
2021-03-28 20:35:11 +00:00
|
|
|
let curtime = self.ticktimer.elapsed_ms();
|
2020-11-24 21:11:32 +00:00
|
|
|
let mut timed_out = false;
|
|
|
|
|
let to = timeout.unwrap() as u64;
|
|
|
|
|
while self.csr.rf(utra::com::STATUS_HOLD) == 1 && !timed_out {
|
2021-03-28 20:35:11 +00:00
|
|
|
if (self.ticktimer.elapsed_ms() - curtime) > to
|
2021-01-03 10:31:51 +00:00
|
|
|
{
|
2021-10-17 13:43:39 +00:00
|
|
|
log::warn!("COM timeout");
|
2020-11-24 21:11:32 +00:00
|
|
|
timed_out = true;
|
|
|
|
|
}
|
2020-11-25 11:58:24 +00:00
|
|
|
xous::yield_slice();
|
2020-11-24 21:11:32 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
while self.csr.rf(utra::com::STATUS_HOLD) == 1 {
|
2020-11-25 11:58:24 +00:00
|
|
|
self.csr.wfo(utra::com::EV_ENABLE_SPI_HOLD, 1);
|
2020-11-24 21:11:32 +00:00
|
|
|
xous::wait_event();
|
2020-11-25 11:58:24 +00:00
|
|
|
self.csr.wfo(utra::com::EV_ENABLE_SPI_HOLD, 0);
|
2020-11-24 21:11:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.txrx(tx)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 18:12:12 +00:00
|
|
|
pub fn try_wait_txrx(&mut self, tx: u16, timeout: u32) -> Option<u16> {
|
|
|
|
|
let curtime = self.ticktimer.elapsed_ms();
|
|
|
|
|
let mut timed_out = false;
|
|
|
|
|
let to = timeout as u64;
|
|
|
|
|
while self.csr.rf(utra::com::STATUS_HOLD) == 1 && !timed_out {
|
|
|
|
|
if (self.ticktimer.elapsed_ms() - curtime) > to
|
|
|
|
|
{
|
2021-10-17 13:43:39 +00:00
|
|
|
log::warn!("COM timeout");
|
2021-10-08 18:12:12 +00:00
|
|
|
timed_out = true;
|
|
|
|
|
}
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
if timed_out {
|
|
|
|
|
self.txrx(tx); // still push the packet in, so that the interface is pumped
|
|
|
|
|
None // but note the failure
|
|
|
|
|
} else {
|
|
|
|
|
Some(self.txrx(tx))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 11:01:11 +00:00
|
|
|
pub fn process_queue(&mut self) -> Option<xous::CID> {
|
2020-11-25 11:58:24 +00:00
|
|
|
if !self.workqueue.is_empty() && !self.busy {
|
|
|
|
|
self.busy = true;
|
2021-08-03 17:25:32 +00:00
|
|
|
let work_descriptor = self.workqueue.remove(0);
|
2021-04-02 11:01:11 +00:00
|
|
|
let ret = if work_descriptor.work.verb == ComState::STAT.verb {
|
2020-11-25 11:58:24 +00:00
|
|
|
let stats = self.get_battstats();
|
2021-04-02 11:01:11 +00:00
|
|
|
match return_battstats(work_descriptor.sender, stats) {
|
|
|
|
|
Err(xous::Error::ServerNotFound) => {
|
|
|
|
|
// the callback target has quit, so de-allocate it from our list
|
|
|
|
|
Some(work_descriptor.sender)
|
|
|
|
|
},
|
|
|
|
|
Ok(()) => None,
|
|
|
|
|
_ => panic!("unhandled error in callback process_queue"),
|
|
|
|
|
}
|
2020-11-25 11:58:24 +00:00
|
|
|
} else {
|
2021-01-03 10:31:51 +00:00
|
|
|
error!(
|
|
|
|
|
"unimplemented work queue responder 0x{:x}",
|
|
|
|
|
work_descriptor.work.verb
|
|
|
|
|
);
|
2021-04-02 11:01:11 +00:00
|
|
|
None
|
|
|
|
|
};
|
2020-11-25 11:58:24 +00:00
|
|
|
self.busy = false;
|
2021-04-02 11:01:11 +00:00
|
|
|
ret
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2020-11-25 11:58:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 11:58:05 +00:00
|
|
|
pub fn stby_current(&self) -> Option<i16> { self.stby_current }
|
|
|
|
|
|
2020-11-24 21:11:32 +00:00
|
|
|
pub fn get_battstats(&mut self) -> BattStats {
|
|
|
|
|
let mut stats = BattStats::default();
|
|
|
|
|
|
|
|
|
|
self.txrx(ComState::GAS_GAUGE.verb);
|
|
|
|
|
stats.current = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as i16;
|
2021-04-16 11:58:05 +00:00
|
|
|
self.stby_current = Some(self.wait_txrx(ComState::LINK_READ.verb, Some(100)) as i16);
|
2020-11-24 21:11:32 +00:00
|
|
|
stats.voltage = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
self.wait_txrx(ComState::LINK_READ.verb, Some(100)); // power register value, not used
|
|
|
|
|
|
|
|
|
|
self.txrx(ComState::GG_SOC.verb);
|
|
|
|
|
stats.soc = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u8;
|
|
|
|
|
self.txrx(ComState::GG_REMAINING.verb);
|
|
|
|
|
stats.remaining_capacity = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
|
|
|
|
|
stats
|
|
|
|
|
}
|
2021-09-05 15:26:07 +00:00
|
|
|
|
|
|
|
|
pub fn get_more_stats(&mut self) -> [u16; 15] {
|
|
|
|
|
self.txrx(ComState::STAT.verb);
|
|
|
|
|
let ack = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
if ack != 0x8888 {
|
|
|
|
|
log::error!("didn't receive the expected ack header to the stats readout");
|
|
|
|
|
}
|
|
|
|
|
let mut ret: [u16; 15] = [0; 15];
|
|
|
|
|
for r in ret.iter_mut() {
|
|
|
|
|
*r = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
}
|
|
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn poll_usb_cc(&mut self) -> [u32; 2] {
|
|
|
|
|
self.txrx(ComState::POLL_USB_CC.verb);
|
|
|
|
|
let event = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let mut ret: [u16; 3] = [0; 3];
|
|
|
|
|
for r in ret.iter_mut() {
|
|
|
|
|
*r = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
}
|
2021-09-06 06:39:44 +00:00
|
|
|
let rev = self.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
2021-09-05 15:26:07 +00:00
|
|
|
// pack into a format that can be returned as a scalar2
|
|
|
|
|
[
|
2021-09-06 06:39:44 +00:00
|
|
|
((rev & 0xff) as u32) << 24 | (((event & 0xff) as u32) << 16) | ret[0] as u32,
|
2021-09-05 15:26:07 +00:00
|
|
|
ret[1] as u32 | ((ret[2] as u32) << 16)
|
|
|
|
|
]
|
|
|
|
|
}
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a stub to try to avoid breaking hosted mode for as long as possible.
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
2020-11-24 14:45:44 +00:00
|
|
|
mod implementation {
|
2020-11-24 21:11:32 +00:00
|
|
|
use crate::api::BattStats;
|
2021-01-03 10:31:51 +00:00
|
|
|
use crate::return_battstats;
|
2020-11-25 11:58:24 +00:00
|
|
|
use crate::WorkRequest;
|
2021-01-20 08:22:38 +00:00
|
|
|
use com_rs_ref as com_rs;
|
2020-11-25 15:06:57 +00:00
|
|
|
use com_rs::*;
|
2021-03-03 14:50:46 +00:00
|
|
|
use log::error;
|
2020-11-25 11:58:24 +00:00
|
|
|
|
2020-11-24 14:45:44 +00:00
|
|
|
pub struct XousCom {
|
2021-08-03 17:25:32 +00:00
|
|
|
pub workqueue: Vec<WorkRequest>,
|
2020-11-25 11:58:24 +00:00
|
|
|
busy: bool,
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl XousCom {
|
|
|
|
|
pub fn new() -> XousCom {
|
|
|
|
|
XousCom {
|
2020-11-25 11:58:24 +00:00
|
|
|
workqueue: Vec::new(),
|
|
|
|
|
busy: false,
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 15:53:35 +00:00
|
|
|
pub fn suspend(&self) {}
|
|
|
|
|
pub fn resume(&self) {}
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2020-11-24 21:11:32 +00:00
|
|
|
pub fn txrx(&mut self, _tx: u16) -> u16 {
|
2020-11-24 14:45:44 +00:00
|
|
|
0xDEAD as u16
|
|
|
|
|
}
|
2020-11-24 21:11:32 +00:00
|
|
|
|
2021-03-03 14:50:46 +00:00
|
|
|
pub fn wait_txrx(&mut self, _tx: u16, _timeout: Option<u32>) -> u16 {
|
2021-01-05 07:48:51 +00:00
|
|
|
0xDEAD as u16
|
|
|
|
|
}
|
2021-10-12 18:07:55 +00:00
|
|
|
pub fn try_wait_txrx(&mut self, _tx: u16, _timeout: u32) -> Option<u16> {
|
|
|
|
|
None
|
|
|
|
|
}
|
2021-01-05 07:48:51 +00:00
|
|
|
|
2020-11-24 21:11:32 +00:00
|
|
|
pub fn get_battstats(&mut self) -> BattStats {
|
|
|
|
|
BattStats {
|
|
|
|
|
voltage: 3700,
|
|
|
|
|
current: -150,
|
|
|
|
|
soc: 50,
|
|
|
|
|
remaining_capacity: 750,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-16 11:58:05 +00:00
|
|
|
pub fn stby_current(&self) -> Option<i16> { None }
|
2020-11-25 11:58:24 +00:00
|
|
|
|
2021-04-02 11:01:11 +00:00
|
|
|
pub fn process_queue(&mut self) -> Option<xous::CID> {
|
2020-11-25 11:58:24 +00:00
|
|
|
if !self.workqueue.is_empty() && !self.busy {
|
|
|
|
|
self.busy = true;
|
2021-08-03 17:25:32 +00:00
|
|
|
let work_descriptor = self.workqueue.remove(0);
|
2021-04-02 11:01:11 +00:00
|
|
|
let ret = if work_descriptor.work.verb == ComState::STAT.verb {
|
2020-11-25 11:58:24 +00:00
|
|
|
let stats = self.get_battstats();
|
2021-04-02 11:01:11 +00:00
|
|
|
match return_battstats(work_descriptor.sender, stats) {
|
|
|
|
|
Err(xous::Error::ServerNotFound) => {
|
|
|
|
|
// the callback target has quit, so de-allocate it from our list
|
|
|
|
|
Some(work_descriptor.sender)
|
|
|
|
|
},
|
|
|
|
|
Ok(()) => None,
|
|
|
|
|
_ => panic!("unhandled error in callback process_queue"),
|
|
|
|
|
}
|
2020-11-25 11:58:24 +00:00
|
|
|
} else {
|
2021-01-03 10:31:51 +00:00
|
|
|
error!(
|
|
|
|
|
"unimplemented work queue responder 0x{:x}",
|
|
|
|
|
work_descriptor.work.verb
|
|
|
|
|
);
|
2021-04-02 11:01:11 +00:00
|
|
|
None
|
|
|
|
|
};
|
2020-11-25 11:58:24 +00:00
|
|
|
self.busy = false;
|
2021-04-02 11:01:11 +00:00
|
|
|
ret
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2020-11-25 11:58:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-05 15:26:07 +00:00
|
|
|
pub fn get_more_stats(&mut self) -> [u16; 15] {
|
|
|
|
|
[0; 15]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn poll_usb_cc(&mut self) -> [u32; 2] {
|
|
|
|
|
[0; 2]
|
|
|
|
|
}
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
use crate::implementation::XousCom;
|
|
|
|
|
|
|
|
|
|
log_server::init_wait().unwrap();
|
2021-04-05 15:50:00 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2021-04-01 21:43:47 +00:00
|
|
|
info!("my PID is {}", xous::process::id());
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2021-03-29 18:13:23 +00:00
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
2021-06-15 09:41:14 +00:00
|
|
|
// unlimited connections allowed -- any server is currently allowed to talk to COM. This might need to be revisited.
|
|
|
|
|
let com_sid = xns.register_name(api::SERVER_NAME_COM, None).expect("can't register server");
|
2021-04-02 09:57:33 +00:00
|
|
|
trace!("registered with NS -- {:?}", com_sid);
|
2020-12-31 11:31:12 +00:00
|
|
|
|
2020-11-24 14:45:44 +00:00
|
|
|
// Create a new com object
|
|
|
|
|
let mut com = XousCom::new();
|
2022-01-17 14:46:18 +00:00
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
|
|
|
|
|
|
|
|
|
|
// reset the EC so we're in sync at boot on state
|
2022-01-30 21:01:58 +00:00
|
|
|
let llio = llio::Llio::new(&xns);
|
|
|
|
|
llio.ec_reset().unwrap();
|
|
|
|
|
ticktimer.sleep_ms(EC_BOOT_WAIT_MS).unwrap();
|
2020-11-24 14:45:44 +00:00
|
|
|
|
2021-04-29 15:53:35 +00:00
|
|
|
// register a suspend/resume listener
|
|
|
|
|
let sr_cid = xous::connect(com_sid).expect("couldn't create suspend callback connection");
|
2022-01-10 05:48:49 +00:00
|
|
|
let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Late), &xns, Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
|
2021-04-29 15:53:35 +00:00
|
|
|
|
2022-01-17 14:46:18 +00:00
|
|
|
// create an array to track return connections for battery stats TODO: refactor this to use a Vec instead of static allocations
|
2021-04-01 21:43:47 +00:00
|
|
|
let mut battstats_conns: [Option<xous::CID>; 32] = [None; 32];
|
2021-01-12 10:44:02 +00:00
|
|
|
// other future notification vectors shall go here
|
|
|
|
|
|
2021-05-15 20:29:12 +00:00
|
|
|
let mut bl_main = 0;
|
|
|
|
|
let mut bl_sec = 0;
|
|
|
|
|
|
2021-05-23 20:34:50 +00:00
|
|
|
let mut flash_id: Option<[u32;4]> = None; // only one process can acquire this, and its ID is stored here.
|
|
|
|
|
const FLASH_LEN: u32 = 0x10_0000;
|
|
|
|
|
const FLASH_TIMEOUT: u32 = 250;
|
|
|
|
|
|
2021-09-13 15:28:54 +00:00
|
|
|
// initial seed of the COM trng
|
|
|
|
|
let trng = trng::Trng::new(&xns).expect("couldn't connect to TRNG");
|
|
|
|
|
com.txrx(ComState::TRNG_SEED.verb);
|
|
|
|
|
for _ in 0..2 {
|
|
|
|
|
let mut rng = trng.get_u64().expect("couldn't fetch rngs");
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
|
com.txrx(rng as u16);
|
|
|
|
|
rng >>= 16;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 13:12:14 +00:00
|
|
|
// determine the version of the COM that we're talking to
|
|
|
|
|
let mut ec_git_rev = {
|
|
|
|
|
com.txrx(ComState::EC_GIT_REV.verb);
|
|
|
|
|
let rev_msb = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
let rev_lsb = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
let _dirty = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u8;
|
|
|
|
|
((rev_msb as u32) << 16) | (rev_lsb as u32)
|
|
|
|
|
};
|
|
|
|
|
let mut ec_tag = {
|
|
|
|
|
if ec_git_rev == LEGACY_REV {
|
|
|
|
|
LEGACY_TAG
|
|
|
|
|
} else {
|
|
|
|
|
parse_version(&mut com)
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-01-27 07:01:01 +00:00
|
|
|
let mut desired_int_mask = 0;
|
2022-01-13 13:12:14 +00:00
|
|
|
|
2021-04-02 09:57:33 +00:00
|
|
|
trace!("starting main loop");
|
2020-11-24 14:45:44 +00:00
|
|
|
loop {
|
2021-04-16 15:07:31 +00:00
|
|
|
let mut msg = xous::receive_message(com_sid).unwrap();
|
2021-04-01 21:43:47 +00:00
|
|
|
trace!("Message: {:?}", msg);
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
2021-04-29 15:53:35 +00:00
|
|
|
Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
2022-01-27 07:01:01 +00:00
|
|
|
com.txrx(ComState::LINK_SET_INTMASK.verb);
|
|
|
|
|
com.txrx(0); // suppress interrupts on suspend
|
|
|
|
|
|
2021-05-15 20:29:12 +00:00
|
|
|
if bl_main != 0 || bl_sec != 0 {
|
|
|
|
|
com.txrx(ComState::BL_START.verb); // this will turn off the backlights
|
|
|
|
|
}
|
2021-04-29 15:53:35 +00:00
|
|
|
com.suspend();
|
|
|
|
|
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
|
|
|
|
|
com.resume();
|
2022-01-27 07:01:01 +00:00
|
|
|
|
|
|
|
|
com.txrx(ComState::LINK_SET_INTMASK.verb);
|
|
|
|
|
com.txrx(desired_int_mask); // restore interrupts on resume
|
|
|
|
|
|
2021-05-15 20:29:12 +00:00
|
|
|
if bl_main != 0 || bl_sec != 0 { // restore the backlight settings, if they are not 0
|
|
|
|
|
com.txrx(ComState::BL_START.verb | (bl_main as u16) & 0x1f | (((bl_sec as u16) & 0x1f) << 5));
|
|
|
|
|
}
|
2021-04-29 15:53:35 +00:00
|
|
|
}),
|
2021-09-14 12:15:03 +00:00
|
|
|
Some(Opcode::LinkReset) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::LINK_SYNC.verb);
|
|
|
|
|
ticktimer.sleep_ms(200).unwrap(); // give some time for the link to reset - the EC does not have a guaranteed latency to respond to a link reset
|
2022-01-31 15:37:43 +00:00
|
|
|
let mut attempts = 0;
|
|
|
|
|
let ping_value = 0xaeedu16; // for various reasons, this is a string that is "unlikely" to happen randomly
|
|
|
|
|
loop {
|
|
|
|
|
com.txrx(ComState::LINK_PING.verb);
|
|
|
|
|
com.txrx(ping_value);
|
|
|
|
|
let pong = com.wait_txrx(ComState::LINK_READ.verb, Some(500));
|
|
|
|
|
let phase = com.wait_txrx(ComState::LINK_READ.verb, Some(500));
|
|
|
|
|
if pong == !ping_value &&
|
|
|
|
|
phase == 0x600d { // 0x600d is a hard-coded constant. It's included to confirm that we aren't "wedged" just sending one value back at us
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("Link reset: establishing link sync, attempt {} [{:04x}/{:04x}]", attempts, pong, phase);
|
|
|
|
|
com.txrx(ComState::LINK_SYNC.verb);
|
|
|
|
|
ticktimer.sleep_ms(200).unwrap();
|
|
|
|
|
attempts += 1;
|
|
|
|
|
}
|
|
|
|
|
if attempts > 50 {
|
|
|
|
|
// abort and return failure
|
|
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-14 12:15:03 +00:00
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
|
|
|
|
}),
|
2021-09-13 15:28:54 +00:00
|
|
|
Some(Opcode::ReseedTrng) => xous::msg_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::TRNG_SEED.verb);
|
|
|
|
|
for _ in 0..2 {
|
|
|
|
|
let mut rng = trng.get_u64().expect("couldn't fetch rngs");
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
|
com.txrx(rng as u16);
|
|
|
|
|
rng >>= 16;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::GetUptime) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
let mut uptime: u64 = 0;
|
|
|
|
|
com.txrx(ComState::UPTIME.verb);
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
|
uptime >>= 16;
|
|
|
|
|
uptime |= (com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u64) << 48;
|
|
|
|
|
}
|
|
|
|
|
xous::return_scalar2(msg.sender, uptime as usize, (uptime >> 32) as usize).expect("couldn't return uptime");
|
|
|
|
|
}),
|
2021-05-23 20:34:50 +00:00
|
|
|
Some(Opcode::FlashAcquire) => msg_blocking_scalar_unpack!(msg, id0, id1, id2, id3, {
|
|
|
|
|
let acquired = if flash_id.is_none() {
|
|
|
|
|
flash_id = Some([id0 as u32, id1 as u32, id2 as u32, id3 as u32]);
|
|
|
|
|
1
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
xous::return_scalar(msg.sender, acquired as usize).expect("couldn't acknowledge acquire message");
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::FlashOp) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let flash_op = buffer.to_original::<api::FlashRecord, _>().unwrap();
|
|
|
|
|
let mut pass = false;
|
|
|
|
|
if let Some(id) = flash_id {
|
|
|
|
|
if id == flash_op.id {
|
|
|
|
|
match flash_op.op {
|
|
|
|
|
api::FlashOp::Erase(addr, len) => {
|
|
|
|
|
if addr < FLASH_LEN && len + addr < FLASH_LEN {
|
2021-05-24 21:13:20 +00:00
|
|
|
log::debug!("Erasing EC region starting at 0x{:x}, lenth 0x{:x}", addr, len);
|
2021-05-23 20:34:50 +00:00
|
|
|
com.txrx(ComState::FLASH_ERASE.verb);
|
|
|
|
|
com.txrx((addr >> 16) as u16);
|
|
|
|
|
com.txrx(addr as u16);
|
|
|
|
|
com.txrx((len >> 16) as u16);
|
|
|
|
|
com.txrx(len as u16);
|
|
|
|
|
while ComState::FLASH_ACK.verb != com.wait_txrx(ComState::FLASH_WAITACK.verb, Some(FLASH_TIMEOUT)) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
pass = true;
|
|
|
|
|
} else {
|
|
|
|
|
pass = false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
api::FlashOp::Program(addr, some_pages) => {
|
|
|
|
|
com.txrx(ComState::FLASH_LOCK.verb);
|
|
|
|
|
let mut prog_ptr = addr;
|
|
|
|
|
// this will fill the 1280-deep FIFO with up to 4 pages of data for programming
|
|
|
|
|
pass = true;
|
|
|
|
|
for &maybe_page in some_pages.iter() {
|
|
|
|
|
if prog_ptr < FLASH_LEN - 256 {
|
2021-05-24 21:13:20 +00:00
|
|
|
log::trace!("Prog EC page at 0x{:x}", prog_ptr);
|
2021-05-23 20:34:50 +00:00
|
|
|
if let Some(page) = maybe_page {
|
|
|
|
|
com.txrx(ComState::FLASH_PP.verb);
|
|
|
|
|
com.txrx((prog_ptr >> 16) as u16);
|
|
|
|
|
com.txrx(prog_ptr as u16);
|
|
|
|
|
for i in 0..128 {
|
|
|
|
|
com.txrx(page[i*2] as u16 | ((page[i*2+1] as u16) << 8));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
prog_ptr += 256;
|
|
|
|
|
} else {
|
|
|
|
|
pass = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// wait for completion only after all 4 pages are sent
|
|
|
|
|
while ComState::FLASH_ACK.verb != com.wait_txrx(ComState::FLASH_WAITACK.verb, Some(FLASH_TIMEOUT)) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
|
|
|
|
com.txrx(ComState::FLASH_UNLOCK.verb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
pass = false;
|
|
|
|
|
}
|
|
|
|
|
let response = if pass {
|
|
|
|
|
api::FlashResult::Pass
|
|
|
|
|
} else {
|
|
|
|
|
api::FlashResult::Fail
|
|
|
|
|
};
|
|
|
|
|
buffer.replace(response).expect("couldn't return result on FlashOp");
|
|
|
|
|
}
|
2021-04-02 07:38:38 +00:00
|
|
|
Some(Opcode::RegisterBattStatsListener) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, {
|
2021-04-01 21:43:47 +00:00
|
|
|
let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _);
|
|
|
|
|
let cid = Some(xous::connect(sid).unwrap());
|
|
|
|
|
let mut found = false;
|
|
|
|
|
for entry in battstats_conns.iter_mut() {
|
|
|
|
|
if *entry == None {
|
|
|
|
|
*entry = cid;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
2021-02-17 15:29:20 +00:00
|
|
|
}
|
2021-04-01 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
error!("RegisterBattStatsListener ran out of space registering callback");
|
2021-02-17 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-02 07:38:38 +00:00
|
|
|
),
|
2021-05-16 08:30:58 +00:00
|
|
|
Some(Opcode::IsCharging) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::POWER_CHARGER_STATE.verb);
|
|
|
|
|
let result = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
xous::return_scalar(msg.sender, result as usize).expect("couldn't return charging state");
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::RequestCharging) => msg_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::CHG_START.verb);
|
|
|
|
|
}),
|
2021-04-16 11:58:05 +00:00
|
|
|
Some(Opcode::StandbyCurrent) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
if let Some(i) = com.stby_current() {
|
|
|
|
|
xous::return_scalar2(msg.sender, 1, i as usize).expect("couldn't return StandbyCurrent");
|
|
|
|
|
} else {
|
|
|
|
|
xous::return_scalar2(msg.sender, 0, 0).expect("couldn't return StandbyCurrent");
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-04-01 21:43:47 +00:00
|
|
|
Some(Opcode::Wf200PdsLine) => {
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
|
|
|
let l = buffer.to_original::<String::<512>, _>().unwrap();
|
|
|
|
|
info!("Wf200PdsLine got line {}", l);
|
|
|
|
|
let line = l.as_bytes();
|
|
|
|
|
let length = (l.len() + 0) as u16;
|
|
|
|
|
//info!("0x{:04x}", ComState::WFX_PDS_LINE_SET.verb);
|
|
|
|
|
com.txrx(ComState::WFX_PDS_LINE_SET.verb);
|
|
|
|
|
//info!("0x{:04x}", length);
|
|
|
|
|
com.txrx(length);
|
|
|
|
|
//for i in 0..(ComState::WFX_PDS_LINE_SET.w_words as usize - 1) {
|
|
|
|
|
for i in 0..128 {
|
|
|
|
|
let word: u16;
|
|
|
|
|
if (i * 2 + 1) == length as usize { // odd last element
|
|
|
|
|
word = line[i * 2] as u16;
|
|
|
|
|
} else if i * 2 < length as usize {
|
|
|
|
|
word = (line[i*2] as u16) | ((line[i*2+1] as u16) << 8);
|
|
|
|
|
} else {
|
|
|
|
|
word = 0;
|
|
|
|
|
}
|
|
|
|
|
com.txrx(word);
|
|
|
|
|
//info!("0x{:04x}", word);
|
2020-11-24 21:11:32 +00:00
|
|
|
}
|
2021-04-01 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
Some(Opcode::PowerOffSoc) => {
|
|
|
|
|
info!("power off called");
|
|
|
|
|
com.txrx(ComState::POWER_OFF.verb);
|
|
|
|
|
com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)); // consume the obligatory return value, even if not used
|
|
|
|
|
}
|
debugging BOOST mode glitch issue
Here's an arcane bug for you:
When powering down, the Spartan 7 3.3V I/O drivers will
output a roughly 800us wide glitch as 3.3V falls below around
1.7V, if the 0.95V core voltage comes down first (which it does
because we have an active discharge on 0.95V, but not on 3.3V).
In this case, it's just wide and high enough to cause the
BOOST mode line to engage, which takes the BOOST output
and ties it to VBUS, which is just enough for the system to
think you're powering it back on again. Which is not what
you want when powering down.
The ultimate fix was to put a heftier pull-down on the BOOST
line so that it can't trip the BOOST output into the VBUS.
Another option could be more active discharge FETs, but that
would require a full board spin and we are already at certification,
so we'll stick with the stronger pull-down on BOOST. It seems to
be a robust fix in the worst corner cases we can find.
The downside is that when powering an external USB device,
you are burning an extra 30mW of power overcoming the pull-down,
but typically, any external USB devices plugged into Precursor
will burn far in excess of that, so the delta should be negligible.
2021-04-23 07:33:14 +00:00
|
|
|
Some(Opcode::BoostOff) => {
|
|
|
|
|
com.txrx(ComState::CHG_BOOST_OFF.verb);
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::BoostOn) => {
|
|
|
|
|
com.txrx(ComState::CHG_BOOST_ON.verb);
|
|
|
|
|
}
|
2021-05-15 20:29:12 +00:00
|
|
|
Some(Opcode::SetBackLight) => msg_scalar_unpack!(msg, main, secondary, _, _, {
|
|
|
|
|
bl_main = main;
|
|
|
|
|
bl_sec = secondary;
|
|
|
|
|
com.txrx(ComState::BL_START.verb | (main as u16) & 0x1f | (((secondary as u16) & 0x1f) << 5));
|
|
|
|
|
}),
|
2021-05-16 08:52:27 +00:00
|
|
|
Some(Opcode::ImuAccelReadBlocking) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::GYRO_UPDATE.verb);
|
|
|
|
|
com.txrx(ComState::GYRO_READ.verb);
|
|
|
|
|
let x = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let y = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let z = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let id = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
xous::return_scalar2(msg.sender,
|
|
|
|
|
((x as usize) << 16) | y as usize,
|
|
|
|
|
((z as usize) << 16) | id as usize
|
2021-05-18 16:39:03 +00:00
|
|
|
).expect("coludn't return accelerometer read data");
|
2021-05-16 08:52:27 +00:00
|
|
|
}),
|
2021-04-01 21:43:47 +00:00
|
|
|
Some(Opcode::BattStats) => {
|
|
|
|
|
let stats = com.get_battstats();
|
|
|
|
|
let raw_stats: [usize; 2] = stats.into();
|
2021-09-05 15:26:07 +00:00
|
|
|
xous::return_scalar2(msg.sender, raw_stats[0], raw_stats[1])
|
2021-04-01 21:43:47 +00:00
|
|
|
.expect("couldn't return batt stats request");
|
|
|
|
|
}
|
2021-09-05 15:26:07 +00:00
|
|
|
Some(Opcode::MoreStats) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let stats = com.get_more_stats();
|
|
|
|
|
buffer.replace(stats).unwrap();
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::PollUsbCc) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
let usb_cc: [u32; 2] = com.poll_usb_cc();
|
|
|
|
|
xous::return_scalar2(msg.sender, usb_cc[0] as usize, usb_cc[1] as usize)
|
|
|
|
|
.expect("couldn't return Usb CC result");
|
|
|
|
|
}),
|
2021-04-01 21:43:47 +00:00
|
|
|
Some(Opcode::BattStatsNb) => {
|
|
|
|
|
for &maybe_conn in battstats_conns.iter() {
|
|
|
|
|
if let Some(conn) = maybe_conn {
|
2021-01-12 10:44:02 +00:00
|
|
|
com.workqueue
|
|
|
|
|
.push(WorkRequest {
|
|
|
|
|
work: ComState::STAT,
|
|
|
|
|
sender: conn,
|
2021-08-03 16:07:47 +00:00
|
|
|
});
|
|
|
|
|
//.unwrap();
|
2021-01-12 10:44:02 +00:00
|
|
|
}
|
2020-11-25 11:58:24 +00:00
|
|
|
}
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
2021-04-22 10:07:27 +00:00
|
|
|
Some(Opcode::ShipMode) => {
|
|
|
|
|
com.txrx(ComState::POWER_SHIPMODE.verb);
|
2021-04-22 17:49:03 +00:00
|
|
|
xous::return_scalar(msg.sender, 1)
|
|
|
|
|
.expect("couldn't ack ship mode");
|
2021-04-22 10:07:27 +00:00
|
|
|
}
|
2021-04-01 21:43:47 +00:00
|
|
|
Some(Opcode::Wf200Rev) => {
|
|
|
|
|
com.txrx(ComState::WFX_FW_REV_GET.verb);
|
|
|
|
|
let major = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u8;
|
|
|
|
|
let minor = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u8;
|
|
|
|
|
let build = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u8;
|
|
|
|
|
xous::return_scalar(
|
|
|
|
|
msg.sender,
|
|
|
|
|
((major as usize) << 16) | ((minor as usize) << 8) | (build as usize)
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't return WF200 firmware rev");
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::EcGitRev) => {
|
|
|
|
|
com.txrx(ComState::EC_GIT_REV.verb);
|
|
|
|
|
let rev_msb = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
let rev_lsb = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
let dirty = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u8;
|
2022-01-13 13:12:14 +00:00
|
|
|
ec_git_rev = ((rev_msb as u32) << 16) | (rev_lsb as u32);
|
2021-04-01 21:43:47 +00:00
|
|
|
xous::return_scalar2(
|
|
|
|
|
msg.sender,
|
2022-01-13 13:12:14 +00:00
|
|
|
ec_git_rev as usize,
|
2021-04-01 21:43:47 +00:00
|
|
|
dirty as usize
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't return WF200 firmware rev");
|
|
|
|
|
}
|
2022-01-13 13:12:14 +00:00
|
|
|
Some(Opcode::EcSwTag) => {
|
|
|
|
|
if ec_git_rev == LEGACY_REV { // this corresponds to a 0.9.5 tag -- the last tag shipped that lacked detailed versioning
|
|
|
|
|
xous::return_scalar(
|
|
|
|
|
msg.sender,
|
|
|
|
|
0x00_09_05_00
|
|
|
|
|
).expect("couldn't return WF200 revision tag");
|
|
|
|
|
} else {
|
|
|
|
|
ec_tag = parse_version(&mut com);
|
|
|
|
|
xous::return_scalar(
|
|
|
|
|
msg.sender,
|
|
|
|
|
ec_tag as usize,
|
|
|
|
|
).expect("couldn't return WF200 revision tag");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-14 14:43:23 +00:00
|
|
|
Some(Opcode::Wf200Reset) => {
|
2022-01-30 11:07:29 +00:00
|
|
|
let start = ticktimer.elapsed_ms();
|
2021-04-14 14:43:23 +00:00
|
|
|
com.txrx(ComState::WF200_RESET.verb);
|
|
|
|
|
com.txrx(0);
|
2022-01-30 21:01:58 +00:00
|
|
|
let mut attempts = 0;
|
2022-01-31 15:32:59 +00:00
|
|
|
let ping_value = 0xaeedu16; // for various reasons, this is a string that is "unlikely" to happen randomly
|
2022-01-30 11:07:29 +00:00
|
|
|
loop {
|
2022-01-31 15:32:59 +00:00
|
|
|
com.txrx(ComState::LINK_PING.verb);
|
|
|
|
|
com.txrx(ping_value);
|
|
|
|
|
let pong = com.wait_txrx(ComState::LINK_READ.verb, Some(5000)); // should finish with 5 seconds
|
|
|
|
|
let phase = com.wait_txrx(ComState::LINK_READ.verb, Some(500));
|
|
|
|
|
if pong == !ping_value &&
|
|
|
|
|
phase == 0x600d { // 0x600d is a hard-coded constant. It's included to confirm that we aren't "wedged" just sending one value back at us
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("Wf200 reset: establishing link sync, attempt {} [{:04x}/{:04x}]", attempts, pong, phase);
|
2022-01-30 11:07:29 +00:00
|
|
|
com.txrx(ComState::LINK_SYNC.verb);
|
|
|
|
|
ticktimer.sleep_ms(200).unwrap();
|
2022-01-30 21:01:58 +00:00
|
|
|
attempts += 1;
|
2022-01-30 11:07:29 +00:00
|
|
|
}
|
2022-01-30 21:01:58 +00:00
|
|
|
if attempts > 50 {
|
|
|
|
|
// something has gone horribly wrong. Reset the EC entirely.
|
|
|
|
|
llio.ec_reset().unwrap();
|
|
|
|
|
ticktimer.sleep_ms(EC_BOOT_WAIT_MS).unwrap();
|
|
|
|
|
com.txrx(ComState::TRNG_SEED.verb);
|
|
|
|
|
for _ in 0..2 {
|
|
|
|
|
let mut rng = trng.get_u64().expect("couldn't fetch rngs");
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
|
com.txrx(rng as u16);
|
|
|
|
|
rng >>= 16;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-01-30 11:07:29 +00:00
|
|
|
}
|
|
|
|
|
xous::return_scalar(msg.sender, (ticktimer.elapsed_ms() - start) as usize).unwrap();
|
2021-04-14 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
Some(Opcode::Wf200Disable) => {
|
|
|
|
|
com.txrx(ComState::WF200_RESET.verb);
|
|
|
|
|
com.txrx(1);
|
|
|
|
|
}
|
2021-04-14 20:10:47 +00:00
|
|
|
Some(Opcode::ScanOn) => {
|
|
|
|
|
com.txrx(ComState::SSID_SCAN_ON.verb);
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::ScanOff) => {
|
|
|
|
|
com.txrx(ComState::SSID_SCAN_OFF.verb);
|
|
|
|
|
}
|
2021-04-16 15:07:31 +00:00
|
|
|
Some(Opcode::SsidCheckUpdate) => {
|
|
|
|
|
com.txrx(ComState::SSID_CHECK.verb);
|
|
|
|
|
let available = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
xous::return_scalar(
|
|
|
|
|
msg.sender, available as usize
|
|
|
|
|
).expect("couldn't return SsidCheckUpdate");
|
|
|
|
|
},
|
|
|
|
|
Some(Opcode::SsidFetchAsString) => {
|
2022-01-13 13:12:14 +00:00
|
|
|
if ec_tag == LEGACY_TAG {
|
|
|
|
|
// this is kept around because the original firmware shipped with units don't support software tagging
|
|
|
|
|
// and the SSID API was modified. This allows the SOC to interop with older versions of the EC for SSID scanning.
|
|
|
|
|
// If it's 2023 and you're looking at this comment and thinking about removing this code, it might actually be
|
|
|
|
|
// ok to do that. Just check that the factory test infrastructure is fully updated to match a modern EC rev.
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
|
|
|
|
|
com.txrx(ComState::SSID_FETCH.verb);
|
|
|
|
|
let mut ssid_list: [[u8; 32]; 6] = [[0; 32]; 6]; // index as ssid_list[6][32]
|
|
|
|
|
for i in 0..16 * 6 {
|
|
|
|
|
let data = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
let lsb : u8 = (data & 0xff) as u8;
|
|
|
|
|
let msb : u8 = ((data >> 8) & 0xff) as u8;
|
|
|
|
|
//if lsb == 0 { lsb = 0x20; }
|
|
|
|
|
//if msb == 0 { msb = 0x20; }
|
|
|
|
|
ssid_list[i / 16][(i % 16) * 2] = lsb;
|
|
|
|
|
ssid_list[i / 16][(i % 16) * 2 + 1] = msb;
|
|
|
|
|
}
|
|
|
|
|
// this is a questionably useful return format -- maybe it's actually more useful to return the raw characters??
|
|
|
|
|
// for now, this is good enough for human eyes, but a scrollable list of SSIDs might be more useful with the raw u8 representation
|
|
|
|
|
let mut ssid_str = xous_ipc::String::<256>::from_str("Top 6 SSIDs:\n");
|
|
|
|
|
let mut itemized = xous_ipc::String::<256>::new();
|
|
|
|
|
for i in 0..6 {
|
|
|
|
|
let mut stop = 0;
|
|
|
|
|
// truncate the nulls
|
|
|
|
|
for l in 0..ssid_list[i].len() {
|
|
|
|
|
stop = l;
|
|
|
|
|
if ssid_list[i][l] == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let ssid = core::str::from_utf8(&ssid_list[i][0..stop]);
|
|
|
|
|
match ssid {
|
|
|
|
|
Ok(textid) => {
|
|
|
|
|
itemized.clear();
|
|
|
|
|
write!(itemized, "{}. {}\n", i+1, textid).unwrap();
|
|
|
|
|
ssid_str.append(itemized.as_str().unwrap()).unwrap();
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
ssid_str.append("-Parse Error-\n").unwrap();
|
|
|
|
|
},
|
2021-04-16 15:58:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-13 13:12:14 +00:00
|
|
|
buffer.replace(ssid_str).unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("This API is not implemented for this EC firmware revision");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::SsidFetchAsStringV2) => {
|
|
|
|
|
if ec_tag == LEGACY_TAG {
|
|
|
|
|
log::error!("This API is not implemented for legacy EC revs");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
com.txrx(ComState::SSID_FETCH_STR.verb);
|
|
|
|
|
// these sizes are hard-coded constants from the EC firmware. We don't have a good cross-code base method for
|
|
|
|
|
// sharing these yet, so they are just magic numbers.
|
|
|
|
|
let mut ssid_list: [[u8; 34]; 8] = [[0; 34]; 8];
|
|
|
|
|
for record in ssid_list.iter_mut() {
|
|
|
|
|
for word in record.chunks_mut(2) {
|
|
|
|
|
let data = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
word[0] = (data & 0xff) as u8;
|
|
|
|
|
word[1] = ((data >> 8) & 0xff) as u8;
|
2021-04-16 15:07:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-13 13:12:14 +00:00
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let mut ssid_ret = buffer.to_original::<SsidReturn, _>().expect("couldn't convert incoming storage");
|
|
|
|
|
for (raw, ssid_rec) in ssid_list.iter().zip(ssid_ret.list.iter_mut()) {
|
|
|
|
|
ssid_rec.rssi = raw[0];
|
2022-01-13 18:23:57 +00:00
|
|
|
let len = if raw[1] < 32 {raw[1] as usize} else {32};
|
|
|
|
|
let ssid_str = core::str::from_utf8(&raw[2..2 + len as usize]).unwrap_or("UTF-8 parse error");
|
2022-01-13 13:12:14 +00:00
|
|
|
ssid_rec.name.clear(); // should be pre-cleared, but let's just be safe about it
|
|
|
|
|
ssid_rec.name.append(ssid_str).ok(); // don't panic if we truncate
|
|
|
|
|
}
|
|
|
|
|
buffer.replace(ssid_ret).unwrap();
|
2021-04-16 15:07:31 +00:00
|
|
|
}
|
2021-07-06 08:29:58 +00:00
|
|
|
Some(Opcode::WlanOn) => {
|
|
|
|
|
info!("TODO: implement WlanOn");
|
2021-07-07 08:43:20 +00:00
|
|
|
com.txrx(ComState::WLAN_ON.verb);
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
Some(Opcode::WlanOff) => {
|
|
|
|
|
info!("TODO: implement WlanOff");
|
2021-07-07 08:43:20 +00:00
|
|
|
com.txrx(ComState::WLAN_OFF.verb);
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
2022-01-26 19:56:39 +00:00
|
|
|
Some(Opcode::WlanRssi) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::WLAN_GET_RSSI.verb);
|
|
|
|
|
let maybe_rssi = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
// raw code sent, error interpretation done by lib function
|
|
|
|
|
xous::return_scalar(msg.sender, maybe_rssi as usize).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::WlanSyncState) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::WLAN_SYNC_STATE.verb);
|
|
|
|
|
let link = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let dhcp = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
xous::return_scalar2(msg.sender, link as usize, dhcp as usize).unwrap();
|
|
|
|
|
}),
|
2021-07-06 08:29:58 +00:00
|
|
|
Some(Opcode::WlanSetSSID) => {
|
2021-07-07 08:43:20 +00:00
|
|
|
const WF200_SSID_LEN: usize = 32;
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
|
|
|
let ssid = buffer.to_original::<String::<WF200_SSID_LEN>, _>().unwrap();
|
|
|
|
|
info!("WlanSetSSID: {}", ssid);
|
2021-08-03 14:04:20 +00:00
|
|
|
let mut str_ser = StringSer::<STR_32_WORDS>::new();
|
|
|
|
|
match str_ser.encode(&ssid.to_str()) {
|
|
|
|
|
Ok(tx_words) => {
|
|
|
|
|
com.txrx(ComState::WLAN_SET_SSID.verb);
|
|
|
|
|
for w in tx_words.iter() {
|
|
|
|
|
com.txrx(*w);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => info!("WlanSetSSID FAIL"),
|
2021-07-07 08:43:20 +00:00
|
|
|
}
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
Some(Opcode::WlanSetPass) => {
|
2021-07-07 08:43:20 +00:00
|
|
|
const WF200_PASS_LEN: usize = 64;
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
|
|
|
|
let pass = buffer.to_original::<String::<WF200_PASS_LEN>, _>().unwrap();
|
2022-01-26 20:07:22 +00:00
|
|
|
info!("WlanSetPass *ssssh!*");
|
2021-08-03 14:04:20 +00:00
|
|
|
let mut str_ser = StringSer::<STR_64_WORDS>::new();
|
|
|
|
|
match str_ser.encode(&pass.to_str()) {
|
|
|
|
|
Ok(tx_words) => {
|
|
|
|
|
com.txrx(ComState::WLAN_SET_PASS.verb);
|
|
|
|
|
for w in tx_words.iter() {
|
|
|
|
|
com.txrx(*w);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => info!("WlanSetPASS FAIL"),
|
2021-07-07 08:43:20 +00:00
|
|
|
}
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
Some(Opcode::WlanJoin) => {
|
2021-10-15 07:44:16 +00:00
|
|
|
info!("Sent WlanJoin");
|
2021-07-07 08:43:20 +00:00
|
|
|
com.txrx(ComState::WLAN_JOIN.verb);
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
Some(Opcode::WlanLeave) => {
|
2021-10-15 07:44:16 +00:00
|
|
|
info!("Sent WlanLeave");
|
2021-07-07 08:43:20 +00:00
|
|
|
com.txrx(ComState::WLAN_LEAVE.verb);
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
2021-07-07 08:43:20 +00:00
|
|
|
Some(Opcode::WlanStatus) => {
|
2022-01-13 18:23:57 +00:00
|
|
|
if ec_tag == LEGACY_TAG {
|
|
|
|
|
log::warn!("Legacy EC detected. Ignoring status request update");
|
|
|
|
|
} else {
|
|
|
|
|
let mut buffer = unsafe {
|
|
|
|
|
Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap())
|
|
|
|
|
};
|
|
|
|
|
com.txrx(ComState::WLAN_BIN_STATUS.verb);
|
|
|
|
|
let maybe_rssi = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let rssi = if (maybe_rssi >> 8) & 0xff != 0 {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(maybe_rssi & 0xff)
|
|
|
|
|
};
|
|
|
|
|
let link_state = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let mut ipv4_raw = Ipv4Conf::default().encode_u16();
|
|
|
|
|
for dest in ipv4_raw.iter_mut() {
|
|
|
|
|
*dest = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
2021-08-06 08:30:16 +00:00
|
|
|
}
|
2022-01-13 18:23:57 +00:00
|
|
|
let mut ssid_buf = [0u8; 34];
|
|
|
|
|
for w in ssid_buf.chunks_mut(2) {
|
|
|
|
|
let word = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)).to_le_bytes();
|
|
|
|
|
w[0] = word[0];
|
|
|
|
|
w[1] = word[1];
|
|
|
|
|
}
|
|
|
|
|
let ssid_len = u16::from_le_bytes(ssid_buf[0..2].try_into().unwrap()) as usize;
|
|
|
|
|
let ssid_checked_len = if ssid_len < 32 {ssid_len} else {32};
|
2022-01-28 15:32:45 +00:00
|
|
|
let ssid_str = core::str::from_utf8(&ssid_buf[2..2+ssid_checked_len]).unwrap_or("Disconnected");
|
2022-01-13 18:23:57 +00:00
|
|
|
let status = WlanStatusIpc {
|
|
|
|
|
ssid: if let Some(rssi) = rssi {
|
|
|
|
|
Some(SsidRecord {
|
|
|
|
|
rssi: rssi as u8,
|
|
|
|
|
name: xous_ipc::String::<32>::from_str(ssid_str)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
link_state,
|
|
|
|
|
ipv4: ipv4_raw,
|
|
|
|
|
};
|
|
|
|
|
buffer.replace(status).unwrap();
|
|
|
|
|
}
|
2021-07-06 08:29:58 +00:00
|
|
|
}
|
2021-10-08 18:12:12 +00:00
|
|
|
Some(Opcode::WlanGetConfig) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
|
|
|
|
|
com.txrx(ComState::WLAN_GET_IPV4_CONF.verb);
|
|
|
|
|
let mut prealloc = Ipv4Conf::default().encode_u16();
|
|
|
|
|
for dest in prealloc.iter_mut() {
|
|
|
|
|
*dest = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
}
|
|
|
|
|
buffer.replace(prealloc).expect("couldn't return result on FlashOp");
|
|
|
|
|
}
|
|
|
|
|
Some(Opcode::WlanFetchPacket) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let mut retbuf = buffer.to_original::<[u8; NET_MTU], _>().unwrap();
|
|
|
|
|
let be_bytes: [u8; 2] = [retbuf[0], retbuf[1]];
|
|
|
|
|
let len_bytes = u16::from_be_bytes(be_bytes);
|
|
|
|
|
let len_words = if len_bytes % 2 == 0 {
|
|
|
|
|
len_bytes / 2
|
|
|
|
|
} else {
|
|
|
|
|
len_bytes / 2 + 1
|
|
|
|
|
};
|
2021-10-09 17:57:31 +00:00
|
|
|
if len_bytes > NET_MTU as u16 {
|
|
|
|
|
log::error!("invalid packet fetch length: {}, aborting without fetch", len_bytes);
|
2021-10-08 18:12:12 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-09 17:57:31 +00:00
|
|
|
com.txrx(ComState::NET_FRAME_FETCH_0.verb | len_bytes);
|
2021-10-08 18:12:12 +00:00
|
|
|
for word_index in 0..len_words as usize {
|
|
|
|
|
let w = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let be_bytes = w.to_be_bytes();
|
|
|
|
|
retbuf[word_index * 2] = be_bytes[0];
|
|
|
|
|
retbuf[word_index * 2 + 1] = be_bytes[1];
|
|
|
|
|
}
|
2021-10-12 17:57:00 +00:00
|
|
|
log::trace!("rx: {:?}", &retbuf[..len_bytes as usize]);
|
2021-10-08 18:12:12 +00:00
|
|
|
buffer.replace(retbuf).expect("couldn't return packet");
|
|
|
|
|
}
|
2021-10-09 17:57:31 +00:00
|
|
|
Some(Opcode::WlanSendPacket) => {
|
|
|
|
|
let buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let txbuf = buffer.as_flat::<[u8; NET_MTU + 2], _>().unwrap();
|
|
|
|
|
let be_bytes: [u8; 2] = [txbuf[0], txbuf[1]];
|
|
|
|
|
let len_bytes = u16::from_be_bytes(be_bytes);
|
|
|
|
|
let len_words = if len_bytes % 2 == 0 {
|
|
|
|
|
len_bytes / 2
|
|
|
|
|
} else {
|
|
|
|
|
len_bytes / 2 + 1
|
|
|
|
|
};
|
|
|
|
|
if len_bytes > NET_MTU as u16 {
|
|
|
|
|
log::error!("invalid packet send length: {}, aborting without send", len_bytes);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-10-12 17:57:00 +00:00
|
|
|
log::trace!("tx: {:?}", &txbuf[2..len_bytes as usize + 2]);
|
2021-10-09 17:57:31 +00:00
|
|
|
com.txrx(ComState::NET_FRAME_SEND_0.verb | len_bytes);
|
|
|
|
|
for word_index in 0..len_words as usize {
|
|
|
|
|
let be_bytes: [u8; 2] = [txbuf[2 + word_index * 2], txbuf[2 + word_index * 2 + 1]];
|
|
|
|
|
com.txrx(u16::from_be_bytes(be_bytes));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-08 12:01:13 +00:00
|
|
|
Some(Opcode::IntSetMask) => msg_blocking_scalar_unpack!(msg, mask_val, _, _, _, {
|
2022-01-27 07:01:01 +00:00
|
|
|
desired_int_mask = mask_val as u16;
|
2021-10-08 12:01:13 +00:00
|
|
|
com.txrx(ComState::LINK_SET_INTMASK.verb);
|
|
|
|
|
com.txrx(mask_val as u16);
|
|
|
|
|
xous::return_scalar(msg.sender, 1)
|
|
|
|
|
.expect("couldn't ack IntSetMask");
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::IntAck) => msg_blocking_scalar_unpack!(msg, ack_val, _, _, _, {
|
|
|
|
|
com.txrx(ComState::LINK_ACK_INTERRUPT.verb);
|
|
|
|
|
com.txrx(ack_val as u16);
|
|
|
|
|
xous::return_scalar(msg.sender, 1)
|
|
|
|
|
.expect("couldn't ack IntAck");
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::IntGetMask) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::LINK_GET_INTMASK.verb);
|
|
|
|
|
let mask = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
xous::return_scalar(msg.sender, mask as usize)
|
|
|
|
|
.expect("couldn't return mask value");
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::IntFetchVector) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
com.txrx(ComState::LINK_GET_INTERRUPT.verb);
|
2021-10-08 18:12:12 +00:00
|
|
|
let maybe_vector = com.try_wait_txrx(ComState::LINK_READ.verb, 500); // longer timeout because interrupts tend to be busy times
|
2021-10-08 12:01:13 +00:00
|
|
|
let rxlen = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
2021-10-08 18:12:12 +00:00
|
|
|
if let Some(vector) = maybe_vector {
|
2021-10-10 21:21:01 +00:00
|
|
|
log::debug!("vector: 0x{:x}, len: {}", vector, rxlen);
|
2021-10-08 18:12:12 +00:00
|
|
|
xous::return_scalar2(msg.sender, vector as _, rxlen as _)
|
|
|
|
|
.expect("couldn't return IntFetchVector");
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("Timeout during interrupt vector fetch. EC may not be responsive. Returning error vector....");
|
|
|
|
|
xous::return_scalar2(msg.sender, com_rs::INT_INVALID as usize, 0)
|
|
|
|
|
.expect("couldn't return IntFetchVector");
|
|
|
|
|
}
|
2021-10-08 12:01:13 +00:00
|
|
|
}),
|
2022-01-14 07:27:24 +00:00
|
|
|
Some(Opcode::WlanDebug) => {
|
|
|
|
|
com.txrx(ComState::WLAN_GET_ERRCOUNTS.verb);
|
|
|
|
|
let mut tx_errs_16 = [0u16; 2];
|
|
|
|
|
let mut drops_16 = [0u16; 2];
|
|
|
|
|
tx_errs_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
tx_errs_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
drops_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
drops_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
com.txrx(ComState::WF200_DEBUG.verb);
|
|
|
|
|
let mut config_16 = [0u16; 2];
|
|
|
|
|
config_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
config_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let control = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let mut alloc_fail_16 = [0u16; 2];
|
|
|
|
|
alloc_fail_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
alloc_fail_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let mut alloc_oversize_16 = [0u16; 2];
|
|
|
|
|
alloc_oversize_16[0] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
alloc_oversize_16[1] = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let alloc_free_count = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT));
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let debug = WlanDebug {
|
|
|
|
|
tx_errs: from_le_words(tx_errs_16),
|
|
|
|
|
drops: from_le_words(drops_16),
|
|
|
|
|
config: from_le_words(config_16),
|
|
|
|
|
control,
|
|
|
|
|
alloc_fail: from_le_words(alloc_fail_16),
|
|
|
|
|
alloc_oversize: from_le_words(alloc_oversize_16),
|
|
|
|
|
alloc_free_count,
|
|
|
|
|
};
|
|
|
|
|
buffer.replace(debug).unwrap();
|
|
|
|
|
},
|
2021-04-05 15:50:00 +00:00
|
|
|
None => {error!("unknown opcode"); break},
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
2020-11-25 11:58:24 +00:00
|
|
|
|
2021-04-02 11:01:11 +00:00
|
|
|
if let Some(dropped_cid) = com.process_queue() {
|
|
|
|
|
for entry in battstats_conns.iter_mut() {
|
|
|
|
|
if let Some(cid) = *entry {
|
|
|
|
|
if cid == dropped_cid {
|
|
|
|
|
*entry = None;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
xns.unregister_server(com_sid).unwrap();
|
|
|
|
|
xous::destroy_server(com_sid).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
2021-05-20 03:01:00 +00:00
|
|
|
xous::terminate_process(0)
|
2020-11-24 14:45:44 +00:00
|
|
|
}
|
2022-01-13 13:12:14 +00:00
|
|
|
|
|
|
|
|
fn parse_version(com: &mut crate::implementation::XousCom) -> u32 {
|
|
|
|
|
com.txrx(ComState::EC_SW_TAG.verb);
|
|
|
|
|
let mut rev_ret = [0u16; ComState::EC_SW_TAG.r_words as usize];
|
|
|
|
|
for w in rev_ret.iter_mut() {
|
|
|
|
|
*w = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)) as u16;
|
|
|
|
|
}
|
|
|
|
|
// unpack u16 words into u8 array
|
|
|
|
|
let mut rev_bytes = [0u8; (ComState::EC_SW_TAG.r_words as usize - 1) * 2];
|
|
|
|
|
for (src, dst) in rev_ret[1..].iter().zip(rev_bytes.chunks_mut(2)) {
|
|
|
|
|
dst[0] = src.to_le_bytes()[0];
|
|
|
|
|
dst[1] = src.to_le_bytes()[1];
|
|
|
|
|
}
|
|
|
|
|
// translate u8 array into &str
|
2022-01-14 07:27:24 +00:00
|
|
|
let len_checked = if rev_ret[0] as usize <= rev_bytes.len() { rev_ret[0] as usize} else {rev_bytes.len()};
|
2022-01-17 14:46:18 +00:00
|
|
|
if len_checked < 2 { // something is very wrong if our length is too short
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-01-14 07:27:24 +00:00
|
|
|
let revstr = core::str::from_utf8(&rev_bytes[..len_checked]).unwrap_or("v0.0.0-0-xxxxxxx"); // fake version number for hosted mode
|
2022-01-13 13:12:14 +00:00
|
|
|
// parse &str -- we do it here not in the EC, because the EC is memory-constrained
|
|
|
|
|
// v0.9.5-2-gb3e2868 exemplar template
|
|
|
|
|
let hyphenstr: Vec<&str> = revstr[1..].split('-').collect(); // drop the leading 'v' by doing [1..]
|
|
|
|
|
let revcodes: Vec<&str> = hyphenstr[0].split('.').collect();
|
|
|
|
|
let major = revcodes[0].parse::<u8>().unwrap_or(0);
|
2022-01-17 14:46:18 +00:00
|
|
|
let minor = if revcodes.len() > 1 {revcodes[1].parse::<u8>().unwrap_or(0)} else {0};
|
|
|
|
|
let rev = if revcodes.len() > 2 {revcodes[2].parse::<u8>().unwrap_or(0)} else {0};
|
2022-01-13 13:12:14 +00:00
|
|
|
let commit = if hyphenstr.len() > 2 { // extra commits beyond the tag
|
|
|
|
|
// version-extra-commit
|
|
|
|
|
hyphenstr[1].parse::<u8>().unwrap_or(0)
|
|
|
|
|
} else {
|
|
|
|
|
// I think the "extra" might disappear in the case that there is no extra beyond the tag. In this case, we would have just
|
|
|
|
|
// v0.9.6-gxxxxxx only, not v0.9.6-0-gxxxxxx. I'm actually not 100% sure, but also, I can't seem to find a definitive
|
|
|
|
|
// answer right now and I don't want to fuck up the tag state of my repo just to find out. Either way, this
|
|
|
|
|
// logic will cause the right thing to happen (a 0 for the extra commits)
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
(commit & 0xff) as u32 | ((rev & 0xff) as u32) << 8 | (((minor & 0xff) as u32) << 16) | (((major & 0xff) as u32) << 24)
|
2022-01-14 07:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_le_words(words: [u16; 2]) -> u32 {
|
|
|
|
|
words[0] as u32 | (words[1] as u32) << 16
|
2022-01-13 13:12:14 +00:00
|
|
|
}
|