2021-10-23 09:08:48 +00:00
use rkyv ::{ Archive , Deserialize , Serialize } ;
2021-03-10 17:45:54 +00:00
/////////////////////// UART TYPE
2021-06-24 05:52:44 +00:00
#[ allow(dead_code) ] // we actually use this constant, but clippy insists that we don't
pub ( crate ) const BOOT_UART : u32 = UartType ::Log as u32 ;
2021-06-24 18:09:42 +00:00
#[ derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, PartialEq, Eq) ]
2021-01-27 05:33:03 +00:00
pub enum UartType {
2021-06-24 05:52:44 +00:00
Kernel = 0 ,
Log = 1 ,
Application = 2 ,
Invalid = 3 ,
2021-01-27 05:33:03 +00:00
}
// 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 ,
}
}
}
2021-03-10 17:45:54 +00:00
/////////////////////// I2C
2021-03-15 16:11:23 +00:00
// a small book-keeping struct used to report back to I2C requestors as to the status of a transaction
2021-03-17 08:06:06 +00:00
#[ derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Eq, PartialEq) ]
2021-03-15 16:11:23 +00:00
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 ,
}
2021-04-06 11:32:35 +00:00
#[ derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive) ]
2021-05-05 21:13:23 +00:00
pub ( crate ) enum I2cCallback {
2021-04-06 11:32:35 +00:00
Result ,
Drop ,
}
2021-05-05 21:13:23 +00:00
// maybe once things stabilize, it's probably a good idea to make this structure private to the crate,
// and create a "public" version for return values via callbacks. But for now, it's pretty
// convenient to reach into the state of the I2C machine to debug problems in the callbacks.
2021-10-23 09:50:33 +00:00
pub ( crate ) const I2C_MAX_LEN : usize = 33 ; // note: due to a regression in Rust 1.56, we can't use the I2C_MAX_LEN symbol in the structure below. So you must manually update that if you change it here.
2021-10-31 20:18:53 +00:00
// pending https://github.com/rust-lang/rust/issues/90195
2021-10-23 09:08:48 +00:00
#[ derive(Debug, Copy, Clone, Archive, Serialize, Deserialize) ]
2021-01-27 05:33:03 +00:00
pub struct I2cTransaction {
2021-03-16 08:37:38 +00:00
pub bus_addr : u8 ,
2021-01-27 05:33:03 +00:00
// write address and read address are encoded in the packet field below
2021-10-23 09:50:33 +00:00
// NOTE: the number 33 that appears twice below mirrors I2C_MAX_LEN; due to a regression in Rust 1.56 we can't use constants in array types
2021-10-31 20:18:53 +00:00
// pending https://github.com/rust-lang/rust/issues/90195
2021-10-23 09:25:41 +00:00
pub txbuf : Option < [ u8 ; 33 ] > , // long enough for a 256-byte operation + 2 bytes of "register address"
2021-03-16 08:37:38 +00:00
pub txlen : u32 ,
2021-10-23 09:25:41 +00:00
pub rxbuf : Option < [ u8 ; 33 ] > ,
2021-03-16 08:37:38 +00:00
pub rxlen : u32 ,
2021-04-07 18:30:57 +00:00
pub listener : Option < ( u32 , u32 , u32 , u32 ) > , // where Rx split transactions should be routed to
2021-03-16 08:37:38 +00:00
pub timeout_ms : u32 ,
pub status : I2cStatus ,
2021-04-07 18:30:57 +00:00
pub callback_id : u32 , // used by callback routines to help identify a return value
2021-01-27 05:33:03 +00:00
}
2021-03-10 17:45:54 +00:00
impl I2cTransaction {
pub fn new ( ) -> Self {
2021-04-08 15:07:02 +00:00
I2cTransaction { bus_addr : 0 , txbuf : None , txlen : 0 , rxbuf : None , rxlen : 0 , timeout_ms : 500 , status : I2cStatus ::Uninitialized , listener : None , callback_id : 0 }
2021-03-10 17:45:54 +00:00
}
}
2021-05-05 19:40:04 +00:00
#[ derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive) ]
2021-05-05 21:13:23 +00:00
pub ( crate ) enum I2cOpcode {
2021-05-05 19:40:04 +00:00
/// initiate an I2C transaction
I2cTxRx ,
/// from i2c interrupt handler (internal API only)
IrqI2cTxrxWriteDone ,
IrqI2cTxrxReadDone ,
/// checks if the I2C engine is currently busy, for polling implementations
I2cIsBusy ,
/// SuspendResume callback
SuspendResume ,
Quit ,
}
2021-01-27 05:33:03 +00:00
2021-03-10 17:45:54 +00:00
////////////////////////////////// VIBE
2021-03-15 18:02:53 +00:00
#[ derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) ]
2021-01-27 05:33:03 +00:00
pub enum VibePattern {
Short ,
Long ,
Double ,
}
impl From < usize > for VibePattern {
fn from ( pattern : usize ) -> Self {
match pattern {
0 = > VibePattern ::Long ,
1 = > VibePattern ::Double ,
2021-03-10 17:45:54 +00:00
_ = > VibePattern ::Short ,
2021-01-27 05:33:03 +00:00
}
}
}
impl Into < usize > for VibePattern {
fn into ( self ) -> usize {
match self {
VibePattern ::Long = > 0 ,
VibePattern ::Double = > 1 ,
VibePattern ::Short = > 0xffff_ffff ,
}
}
}
2021-03-10 17:45:54 +00:00
//////////////////////////////// CLOCK GATING (placeholder)
2021-03-15 18:02:53 +00:00
#[ derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) ]
2021-01-27 05:33:03 +00:00
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 ,
}
}
}
2021-04-06 11:32:35 +00:00
pub ( crate ) const SERVER_NAME_LLIO : & str = " _Low Level I/O manager_ " ;
2021-05-05 16:45:14 +00:00
pub ( crate ) const SERVER_NAME_I2C : & str = " _Threaded I2C manager_ " ;
2021-03-10 17:45:54 +00:00
//////////////////////////////////// OPCODES
2021-04-06 11:32:35 +00:00
#[ derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive) ]
pub ( crate ) enum Opcode {
2021-01-27 05:33:03 +00:00
/// not tested - set CRG parameters
2021-04-06 11:32:35 +00:00
CrgMode , //(ClockMode),
2021-01-27 05:33:03 +00:00
/// not tested -- set GPIO
2021-04-06 11:32:35 +00:00
GpioDataOut , //(u32),
2021-01-27 05:33:03 +00:00
GpioDataIn ,
2021-04-06 11:32:35 +00:00
GpioDataDrive , //(u32),
GpioIntMask , //(u32),
GpioIntAsFalling , //(u32),
2021-01-27 05:33:03 +00:00
GpioIntPending ,
2021-04-06 11:32:35 +00:00
GpioIntEna , //(u32),
2021-07-11 10:47:19 +00:00
GpioIntSubscribe , //(String<64>), //
2021-04-07 10:29:50 +00:00
GpioIntHappened ,
2021-01-27 05:33:03 +00:00
2021-05-05 16:45:14 +00:00
/// set UART mux
2021-04-06 11:32:35 +00:00
UartMux , //(UartType),
2021-01-27 05:33:03 +00:00
2021-07-11 10:47:19 +00:00
// InfoLitexId, //(String<64>), // TODO: returns the ASCII string baked into the FPGA that describes the FPGA build, inside Registration
2021-01-27 05:33:03 +00:00
InfoDna ,
InfoGit ,
InfoPlatform ,
InfoTarget ,
2021-05-05 16:45:14 +00:00
/// partially tested -- power
2021-04-06 11:32:35 +00:00
PowerAudio , //(bool),
PowerSelf , //(bool), // setting this to false allows the EC to turn off our power
PowerBoostMode , //(bool),
2021-05-29 16:12:18 +00:00
PowerCrypto ,
PowerCryptoStatus ,
2021-06-01 09:02:30 +00:00
WfiOverride ,
DebugPowerdown ,
2021-06-01 17:08:31 +00:00
GetActivity ,
2021-06-01 09:02:30 +00:00
DebugWakeup ,
2021-04-06 11:32:35 +00:00
EcSnoopAllow , //(bool),
2021-01-27 05:33:03 +00:00
EcReset ,
EcPowerOn ,
2021-04-06 11:32:35 +00:00
SelfDestruct , //(u32), // requires a series of writes to enable
2021-01-27 05:33:03 +00:00
2021-05-05 16:45:14 +00:00
/// vibe motor
2021-04-06 11:32:35 +00:00
Vibe , //(VibePattern),
2021-01-27 20:03:30 +00:00
2021-03-10 18:57:58 +00:00
/// not tested -- xadc
AdcVbus ,
AdcVccInt ,
AdcVccAux ,
AdcVccBram ,
AdcUsbN ,
AdcUsbP ,
AdcTemperature ,
AdcGpio5 ,
AdcGpio2 ,
2021-05-05 16:45:14 +00:00
/// partially tested -- events
2021-04-07 10:29:50 +00:00
EventComSubscribe , //(String<64>),
EventRtcSubscribe , //(String<64>),
2021-04-06 11:32:35 +00:00
EventUsbAttachSubscribe , //(String<64>),
2021-04-07 10:29:50 +00:00
EventComEnable , //(bool),
EventRtcEnable , //(bool),
EventUsbAttachEnable , //(bool),
2021-06-01 17:08:31 +00:00
EventActivityHappened ,
2021-04-07 10:29:50 +00:00
/// internal from handler to main loop
EventComHappened ,
EventRtcHappened ,
EventUsbHappened ,
2021-04-29 15:15:47 +00:00
2021-07-11 10:47:19 +00:00
/// debug USB status
DebugUsbOp ,
2021-04-29 15:15:47 +00:00
/// SuspendResume callback
SuspendResume ,
2021-05-05 16:45:14 +00:00
/// Exit the server
Quit ,
2021-01-27 05:33:03 +00:00
}
2021-05-05 16:45:14 +00:00
2021-04-06 11:32:35 +00:00
#[ derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive) ]
pub ( crate ) enum EventCallback {
Event ,
Drop ,
2021-01-27 05:33:03 +00:00
}
2021-04-07 10:29:50 +00:00
/*
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 .
* /
2021-04-06 11:32:35 +00:00
#[ derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone) ]
2021-04-07 10:29:50 +00:00
pub ( crate ) struct ScalarHook {
2021-04-06 11:32:35 +00:00
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.
2021-01-27 05:33:03 +00:00
}
2021-04-06 11:32:35 +00:00