xous-core/services/llio/src/api.rs
bunnie cad9c15849 intermediate save of suspend resume
building an awful rube-goldberg contraption of a "clean-suspend"
record.
2021-04-28 19:28:04 +08:00

235 lines
7 KiB
Rust

/////////////////////// UART TYPE
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum UartType {
Kernel,
Log,
Application,
Invalid,
}
// from/to for Xous messages
impl From<usize> for UartType {
fn from(code: usize) -> Self {
match code {
0 => UartType::Kernel,
1 => UartType::Log,
2 => UartType::Application,
_ => UartType::Invalid,
}
}
}
impl Into<usize> for UartType {
fn into(self) -> usize {
match self {
UartType::Kernel => 0,
UartType::Log => 1,
UartType::Application => 2,
UartType::Invalid => 3,
}
}
}
// for the actual bitmask going to hardware
impl Into<u32> for UartType {
fn into(self) -> u32 {
match self {
UartType::Kernel => 0,
UartType::Log => 1,
UartType::Application => 2,
UartType::Invalid => 3,
}
}
}
/////////////////////// I2C
// a small book-keeping struct used to report back to I2C requestors as to the status of a transaction
#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Eq, PartialEq)]
pub enum I2cStatus {
/// used only as the default, should always be set to one of the below before sending
Uninitialized,
/// used by a managing process to indicate a request
RequestIncoming,
/// everything was OK, request in progress
ResponseInProgress,
/// we tried to process your request, but there was a timeout
ResponseTimeout,
/// I2C had a NACK on the request
ResponseNack,
/// the I2C bus is currently busy and your request was ignored
ResponseBusy,
/// the request was malformed
ResponseFormatError,
/// everything is OK, data here should be valid
ResponseReadOk,
/// everything is OK, write finished. data fields have no meaning
ResponseWriteOk,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum I2cCallback {
Result,
Drop,
}
#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct I2cTransaction {
pub bus_addr: u8,
// write address and read address are encoded in the packet field below
pub txbuf: Option<[u8; 258]>, // long enough for a 256-byte operation + 2 bytes of "register address"
pub txlen: u32,
pub rxbuf: Option<[u8; 258]>,
pub rxlen: u32,
pub listener: Option<(u32, u32, u32, u32)>, // where Rx split transactions should be routed to
pub timeout_ms: u32,
pub status: I2cStatus,
pub callback_id: u32, // used by callback routines to help identify a return value
}
impl I2cTransaction {
pub fn new() -> Self {
I2cTransaction{ bus_addr: 0, txbuf: None, txlen: 0, rxbuf: None, rxlen: 0, timeout_ms: 500, status: I2cStatus::Uninitialized, listener: None, callback_id: 0 }
}
}
////////////////////////////////// VIBE
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum VibePattern {
Short,
Long,
Double,
}
impl From<usize> for VibePattern {
fn from(pattern: usize) -> Self {
match pattern {
0 => VibePattern::Long,
1 => VibePattern::Double,
_ => VibePattern::Short,
}
}
}
impl Into<usize> for VibePattern {
fn into(self) -> usize {
match self {
VibePattern::Long => 0,
VibePattern::Double => 1,
VibePattern::Short => 0xffff_ffff,
}
}
}
//////////////////////////////// CLOCK GATING (placeholder)
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum ClockMode {
Low,
AllOn,
}
impl From<usize> for ClockMode {
fn from(mode: usize) -> Self {
match mode {
0 => ClockMode::Low,
_ => ClockMode::AllOn,
}
}
}
impl Into<usize> for ClockMode {
fn into(self) -> usize {
match self {
ClockMode::Low => 0,
ClockMode::AllOn => 0xffff_ffff,
}
}
}
pub(crate) const SERVER_NAME_LLIO: &str = "_Low Level I/O manager_";
//////////////////////////////////// OPCODES
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum Opcode {
/// not tested - reboot
RebootRequest,
RebootSocConfirm, // all peripherals + CPU
RebootCpuConfirm, // just the CPU, peripherals (in particular the USB debug bridge) keep state
/// not tested - reboot address
RebootVector, //(u32),
/// not tested - set CRG parameters
CrgMode, //(ClockMode),
/// not tested -- set GPIO
GpioDataOut, //(u32),
GpioDataIn,
GpioDataDrive, //(u32),
GpioIntMask, //(u32),
GpioIntAsFalling, //(u32),
GpioIntPending,
GpioIntEna, //(u32),
GpioIntSubscribe, //(String<64>), // TODO
GpioIntHappened,
/// not tested - set UART mux
UartMux, //(UartType),
/// not tested - information about the SoC build and revision
//TODO InfoLitexId, //(String<64>), // TODO: returns the ASCII string baked into the FPGA that describes the FPGA build, inside Registration
InfoDna,
InfoGit,
InfoPlatform,
InfoTarget,
/// not tested -- power
PowerAudio, //(bool),
PowerSelf, //(bool), // setting this to false allows the EC to turn off our power
PowerBoostMode, //(bool),
EcSnoopAllow, //(bool),
EcReset,
EcPowerOn,
SelfDestruct, //(u32), // requires a series of writes to enable
/// not tested -- vibe
Vibe, //(VibePattern),
/// not tested -- xadc
AdcVbus,
AdcVccInt,
AdcVccAux,
AdcVccBram,
AdcUsbN,
AdcUsbP,
AdcTemperature,
AdcGpio5,
AdcGpio2,
/// not tested - I2C functions
I2cTxRx, //(I2cTransaction), // type (tx or rx) encoded in struct
/// from i2c interrupt handler (internal API only)
IrqI2cTxrxDone,
/// checks if the I2C engine is currently busy, for polling implementations
I2cIsBusy,
/// not tested -- events
EventComSubscribe, //(String<64>),
EventRtcSubscribe, //(String<64>),
EventUsbAttachSubscribe, //(String<64>),
EventComEnable, //(bool),
EventRtcEnable, //(bool),
EventUsbAttachEnable, //(bool),
/// internal from handler to main loop
EventComHappened,
EventRtcHappened,
EventUsbHappened,
}
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum EventCallback {
Event,
Drop,
}
/*
Implementation note: we use a ScalarHook with a two-stage message passing so we don't leak
the local SID. The local SID should be considered a secret, and not shared. A two-stage
message passing system creates a dedicated, one-time use server and shares this SID with
the LLIO server, thus protecting the local SID from disclosure.
*/
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub(crate) struct ScalarHook {
pub sid: (u32, u32, u32, u32),
pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API)
pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking.
}