mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
helps to add the source code too
This commit is contained in:
parent
fa12e513cd
commit
058c4473bb
5 changed files with 386 additions and 0 deletions
22
services/com/Cargo.toml
Normal file
22
services/com/Cargo.toml
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
[package]
|
||||
name = "com"
|
||||
version = "0.1.0"
|
||||
authors = ["bunnie <bunnie@kosagi.com>"]
|
||||
edition = "2018"
|
||||
description = "COM interface to the EC"
|
||||
|
||||
[dependencies]
|
||||
xous = { path = "../../xous-rs" }
|
||||
heapless = "0.5"
|
||||
log-server = { path = "../log-server" }
|
||||
log = "0.4"
|
||||
com_rs = { path = "com_rs" }
|
||||
#[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||
utralib = { path = "../../utralib"}
|
||||
|
||||
[target.'cfg(any(windows,unix))'.dependencies]
|
||||
|
||||
[features]
|
||||
debugprint = []
|
||||
#default = [] # "debugprint"
|
||||
default = ["debugprint"]
|
||||
99
services/com/src/api.rs
Normal file
99
services/com/src/api.rs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
use xous::{Message, ScalarMessage};
|
||||
|
||||
// NOTE: the use of ComState "verbs" as commands is not meant as a 1:1 mapping of commands
|
||||
// It's just a convenient abuse of already-defined constants. However, it's intended that
|
||||
// the COM server on the SoC side abstracts much of the EC bus complexity away.
|
||||
use com_rs::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Opcode {
|
||||
/// Battery stats
|
||||
BattStats,
|
||||
|
||||
/// Turn Boost Mode On
|
||||
BoostOn,
|
||||
|
||||
/// Turn Boost Mode Off
|
||||
BoostOff,
|
||||
|
||||
/// Read the current accelerations off the IMU
|
||||
ImuAccelRead,
|
||||
|
||||
/// Power off the SoC
|
||||
PowerOffSoc,
|
||||
|
||||
/// Ship mode (battery disconnect)
|
||||
ShipMode,
|
||||
|
||||
/// Is the battery charging?
|
||||
IsCharging,
|
||||
|
||||
/// Set the backlight brightness
|
||||
SetBackLight,
|
||||
|
||||
/// Request charging
|
||||
RequestCharging,
|
||||
|
||||
/// Erase a region of EC FLASH
|
||||
FlashErase,
|
||||
|
||||
/// Program a page of FLASH
|
||||
FlashProgram,
|
||||
|
||||
/// Update the SSID list
|
||||
SsidScan,
|
||||
|
||||
/// Return the latest SSID list
|
||||
SsidFetch,
|
||||
}
|
||||
|
||||
impl<'a> core::convert::TryFrom<&'a Message> for Opcode {
|
||||
type Error = &'static str;
|
||||
fn try_from(message: &'a Message) -> Result<Self, Self::Error> {
|
||||
match message {
|
||||
Message::Scalar(m) => {
|
||||
if m.id as u16 == ComState::CHG_BOOST_ON.verb {
|
||||
Ok(Opcode::BoostOn)
|
||||
} else if m.id as u16 == ComState::CHG_BOOST_OFF.verb {
|
||||
Ok(Opcode::BoostOff)
|
||||
} else if m.id as u16 == ComState::POWER_SHIPMODE.verb {
|
||||
Ok(Opcode::ShipMode)
|
||||
} else if m.id as u16 == ComState::POWER_OFF.verb {
|
||||
Ok(Opcode::PowerOffSoc)
|
||||
} else {
|
||||
Err("unrecognized command")
|
||||
}
|
||||
},
|
||||
Message::BlockingScalar(m) => {
|
||||
if m.id as u16 == ComState::STAT.verb {
|
||||
Ok(Opcode::BattStats)
|
||||
} else if m.id as u16 == ComState::GYRO_READ.verb {
|
||||
Ok(Opcode::ImuAccelRead)
|
||||
} else {
|
||||
Err("unrecognized opcode")
|
||||
}
|
||||
},
|
||||
_ => Err("unhandled message type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Message> for Opcode {
|
||||
fn into(self) -> Message {
|
||||
match self {
|
||||
Opcode::BoostOn => Message::Scalar(ScalarMessage {
|
||||
id: ComState::CHG_BOOST_ON.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
|
||||
Opcode::BoostOff => Message::Scalar(ScalarMessage {
|
||||
id: ComState::CHG_BOOST_OFF.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
|
||||
Opcode::ShipMode => Message::Scalar(ScalarMessage {
|
||||
id: ComState::POWER_SHIPMODE.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
|
||||
Opcode::PowerOffSoc => Message::Scalar(ScalarMessage {
|
||||
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::ImuAccelRead => Message::BlockingScalar(ScalarMessage {
|
||||
id: ComState::GYRO_READ.verb as _, arg1: 0, arg2: 0, arg3: 0, arg4: 0 }),
|
||||
_ => todo!("message type not yet implemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
113
services/com/src/debug.rs
Normal file
113
services/com/src/debug.rs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
use core::fmt::{Error, Write};
|
||||
|
||||
use utralib::generated::*;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print
|
||||
{
|
||||
($($args:tt)+) => ({
|
||||
use core::fmt::Write;
|
||||
let _ = write!(crate::debug::DEFAULT, $($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)+)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
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!("{}", c as char);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
pub struct Uart {}
|
||||
|
||||
// this is a hack to bypass an explicit initialization/allocation step for the debug structure
|
||||
pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize;
|
||||
|
||||
pub const DEFAULT: Uart = Uart {};
|
||||
|
||||
impl Uart {
|
||||
fn map_uart(&self) {
|
||||
/*
|
||||
Note: the memory address and interrupt specified here needs to map to a unique hardware
|
||||
UART resource. Modify in this function as necessary.
|
||||
*/
|
||||
let uart = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utra::server0::HW_SERVER0_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map debug uart");
|
||||
unsafe{ DEFAULT_UART_ADDR = uart.as_mut_ptr() as _; }
|
||||
println!("Mapped UART @ {:08x}", uart.addr.get());
|
||||
// core::mem::forget(uart);
|
||||
|
||||
println!("Allocating IRQ...");
|
||||
xous::claim_interrupt(utra::server0::SERVER0_IRQ, handle_irq, core::ptr::null_mut::<usize>()).expect("unable to allocate IRQ");
|
||||
self.enable_rx();
|
||||
}
|
||||
|
||||
pub fn putc(&self, c: u8) {
|
||||
if cfg!(feature = "debugprint") {
|
||||
if unsafe{DEFAULT_UART_ADDR} as usize == 0 {
|
||||
self.map_uart();
|
||||
}
|
||||
let mut uart_csr = CSR::new(unsafe{ DEFAULT_UART_ADDR as *mut u32});
|
||||
|
||||
// Wait until TXFULL is `0`
|
||||
while uart_csr.r(utra::uart::TXFULL) != 0 {}
|
||||
uart_csr.wo(utra::uart::RXTX, c as u32);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enable_rx(&self) {
|
||||
if cfg!(feature = "debugprint") {
|
||||
let mut uart_csr = CSR::new(unsafe{DEFAULT_UART_ADDR as *mut u32});
|
||||
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getc(&self) -> Option<u8> {
|
||||
if cfg!(feature = "debugprint") {
|
||||
if unsafe{DEFAULT_UART_ADDR} as usize == 0 {
|
||||
self.map_uart();
|
||||
}
|
||||
let mut uart_csr = CSR::new(unsafe{DEFAULT_UART_ADDR as *mut u32});
|
||||
match uart_csr.rf(utra::uart::EV_PENDING_RX) {
|
||||
0 => None,
|
||||
ack => {
|
||||
let c = Some(uart_csr.rf(utra::uart::RXTX_RXTX) as u8);
|
||||
uart_csr.wfo(utra::uart::EV_PENDING_RX, ack);
|
||||
c
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Uart {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
for c in s.bytes() {
|
||||
self.putc(c);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
28
services/com/src/lib.rs
Normal file
28
services/com/src/lib.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
|
||||
pub mod api;
|
||||
|
||||
use xous::{send_message, Error, CID};
|
||||
|
||||
pub fn power_off_soc(cid: CID) -> Result<(), xous::Error> {
|
||||
send_message(cid, api::Opcode::PowerOffSoc.into()).map(|_| ())
|
||||
}
|
||||
|
||||
/*
|
||||
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 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(|_| ())
|
||||
}
|
||||
*/
|
||||
124
services/com/src/main.rs
Normal file
124
services/com/src/main.rs
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
#![cfg_attr(target_os = "none", no_std)]
|
||||
#![cfg_attr(target_os = "none", no_main)]
|
||||
|
||||
#[cfg(target_os = "none")]
|
||||
#[macro_use]
|
||||
mod debug;
|
||||
|
||||
mod api;
|
||||
use api::Opcode;
|
||||
|
||||
use core::convert::TryFrom;
|
||||
|
||||
/*
|
||||
use heapless::binary_heap::{BinaryHeap, Min};
|
||||
use heapless::Vec;
|
||||
use heapless::consts::*;
|
||||
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>;
|
||||
*/
|
||||
|
||||
use log::{error, info};
|
||||
|
||||
use com_rs::*;
|
||||
|
||||
//#[cfg(target_os = "none")]
|
||||
mod implementation {
|
||||
use utralib::generated::*;
|
||||
|
||||
pub struct XousCom {
|
||||
csr: utralib::CSR<u32>,
|
||||
}
|
||||
|
||||
fn handle_irq(_irq_no: usize, arg: *mut usize) {
|
||||
let _xc = unsafe { &mut *(arg as *mut XousCom) };
|
||||
println!("COM IRQ");
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
let mut xc = XousCom {
|
||||
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
||||
};
|
||||
|
||||
xous::claim_interrupt(
|
||||
utra::com::COM_IRQ,
|
||||
handle_irq,
|
||||
(&mut xc) as *mut XousCom as *mut usize,
|
||||
)
|
||||
.expect("couldn't claim irq");
|
||||
xc
|
||||
}
|
||||
|
||||
pub fn txrx(&mut self, tx: u16) -> u16 {
|
||||
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
|
||||
while self.csr.rf(utra::com::STATUS_TIP) == 1 {
|
||||
xous::yield_slice();
|
||||
}
|
||||
|
||||
// grab the RX value and return it
|
||||
self.csr.rf(utra::com::RX_RX) as u16
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a stub to try to avoid breaking hosted mode for as long as possible.
|
||||
#[cfg(not(target_os = "none"))]
|
||||
mod implementation {
|
||||
pub struct XousCom {
|
||||
}
|
||||
|
||||
impl XousCom {
|
||||
pub fn new() -> XousCom {
|
||||
XousCom {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn txrx(tx: u16) -> u16 {
|
||||
0xDEAD as u16
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[xous::xous_main]
|
||||
fn xmain() -> ! {
|
||||
use crate::implementation::XousCom;
|
||||
|
||||
println!("COM Init");
|
||||
log_server::init_wait().unwrap();
|
||||
|
||||
let com_server =
|
||||
xous::create_server(b"com ").expect("Couldn't create COM server");
|
||||
|
||||
// Create a new com object
|
||||
let mut com = XousCom::new();
|
||||
|
||||
loop {
|
||||
info!("COM: waiting for message");
|
||||
let envelope = xous::receive_message(com_server).unwrap();
|
||||
info!("COM: Message: {:?}", envelope);
|
||||
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
|
||||
info!("COM: Opcode: {:?}", opcode);
|
||||
match opcode {
|
||||
Opcode::PowerOffSoc => {
|
||||
info!("COM: power off called");
|
||||
com.txrx(ComState::POWER_OFF.verb);
|
||||
}
|
||||
_ => error!("unknown opcode"),
|
||||
}
|
||||
} else {
|
||||
error!("couldn't convert opcode");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// xous::wait_event()
|
||||
Loading…
Reference in a new issue