mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
refactor I2C to have work queue, cleaner (but untested) APIs
This commit is contained in:
parent
bb5bc1d212
commit
b596c40dd6
9 changed files with 410 additions and 67 deletions
157
Cargo.lock
generated
157
Cargo.lock
generated
|
|
@ -44,6 +44,15 @@ dependencies = [
|
|||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-polyfill"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30302dda7a66f8c55932ebf208f7def840743ff64d495e9ceffcd97c18f11d39"
|
||||
dependencies = [
|
||||
"cortex-m",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
|
|
@ -61,6 +70,15 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "bare-metal"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
|
||||
dependencies = [
|
||||
"rustc_version",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "benchmark"
|
||||
version = "0.1.0"
|
||||
|
|
@ -118,6 +136,12 @@ dependencies = [
|
|||
"which",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitfield"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
|
|
@ -169,7 +193,7 @@ name = "cb-test-c1"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cb-test-srv",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -187,7 +211,7 @@ name = "cb-test-c2"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cb-test-srv",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -296,7 +320,7 @@ dependencies = [
|
|||
name = "codec"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"llio",
|
||||
"log",
|
||||
"log-server",
|
||||
|
|
@ -316,7 +340,7 @@ name = "com"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"com_rs-ref",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -369,6 +393,18 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
|
||||
|
||||
[[package]]
|
||||
name = "cortex-m"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "643a210c1bdc23d0db511e2a576082f4ff4dcae9d0c37f50b431b8f8439d6d6b"
|
||||
dependencies = [
|
||||
"bare-metal",
|
||||
"bitfield",
|
||||
"embedded-hal",
|
||||
"volatile-register",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc"
|
||||
version = "1.8.1"
|
||||
|
|
@ -415,6 +451,16 @@ version = "1.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db184d3fa27bc7a2344250394c0264144dfe0bc81a4401801dcb964b8dd172ad"
|
||||
dependencies = [
|
||||
"nb 0.1.3",
|
||||
"void",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.28"
|
||||
|
|
@ -483,7 +529,7 @@ dependencies = [
|
|||
"com",
|
||||
"content-plugin-api",
|
||||
"graphics-server",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"ime-plugin-api",
|
||||
"keyboard",
|
||||
"log",
|
||||
|
|
@ -550,7 +596,7 @@ name = "graphics-server"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"blitstr-ref",
|
||||
"hash32",
|
||||
"hash32 0.1.1",
|
||||
"hash32-derive",
|
||||
"keyboard",
|
||||
"log",
|
||||
|
|
@ -575,6 +621,15 @@ dependencies = [
|
|||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hash32"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hash32-derive"
|
||||
version = "0.1.0"
|
||||
|
|
@ -594,7 +649,18 @@ checksum = "74911a68a1658cfcfb61bc0ccfbd536e3b6e906f8c2f7883ee50157e3e2184f1"
|
|||
dependencies = [
|
||||
"as-slice",
|
||||
"generic-array 0.13.3",
|
||||
"hash32",
|
||||
"hash32 0.1.1",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heapless"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e94c13b78b595d2adbd708bce276664f1047f98fc32ddbf463b4c191158334a6"
|
||||
dependencies = [
|
||||
"atomic-polyfill",
|
||||
"hash32 0.2.1",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
|
|
@ -667,7 +733,7 @@ dependencies = [
|
|||
name = "ime-plugin-shell"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"ime-plugin-api",
|
||||
"log",
|
||||
"log-server",
|
||||
|
|
@ -702,7 +768,7 @@ dependencies = [
|
|||
name = "keyboard"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"llio",
|
||||
"log",
|
||||
"log-server",
|
||||
|
|
@ -749,6 +815,7 @@ dependencies = [
|
|||
name = "llio"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"heapless 0.7.0",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -832,6 +899,21 @@ dependencies = [
|
|||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nb"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f"
|
||||
dependencies = [
|
||||
"nb 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nb"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "546c37ac5d9e56f55e73b677106873d9d9f5190605e41a856503623648488cae"
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.20.0"
|
||||
|
|
@ -1221,7 +1303,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"bitflags",
|
||||
"chrono",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"llio",
|
||||
"log",
|
||||
"log-server",
|
||||
|
|
@ -1247,6 +1329,15 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1956f5517128a2b6f23ab2dadf1a976f4f5b27962e7724c2bf3d45e539ec098c"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
|
|
@ -1281,6 +1372,21 @@ dependencies = [
|
|||
"version-compare",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
dependencies = [
|
||||
"semver-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver-parser"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.123"
|
||||
|
|
@ -1296,7 +1402,7 @@ dependencies = [
|
|||
"content-plugin-api",
|
||||
"gam",
|
||||
"graphics-server",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"ime-plugin-api",
|
||||
"keyboard",
|
||||
"llio",
|
||||
|
|
@ -1441,7 +1547,7 @@ dependencies = [
|
|||
name = "ticktimer-server"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -1480,7 +1586,7 @@ dependencies = [
|
|||
name = "trng"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
@ -1534,6 +1640,12 @@ dependencies = [
|
|||
"svd2utra",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcell"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
|
|
@ -1562,6 +1674,21 @@ version = "0.9.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
|
||||
|
||||
[[package]]
|
||||
name = "void"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
||||
|
||||
[[package]]
|
||||
name = "volatile-register"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286"
|
||||
dependencies = [
|
||||
"vcell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
|
|
@ -1781,9 +1908,9 @@ dependencies = [
|
|||
name = "xous-names"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hash32",
|
||||
"hash32 0.1.1",
|
||||
"hash32-derive",
|
||||
"heapless",
|
||||
"heapless 0.5.6",
|
||||
"log",
|
||||
"log-server",
|
||||
"num-derive",
|
||||
|
|
|
|||
|
|
@ -212,14 +212,14 @@ impl Codec {
|
|||
|
||||
fn w(&mut self, adr: u8, data: &[u8]) -> bool {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf: [u8; 258] = [0; 258];
|
||||
let mut txbuf: [u8; llio::I2C_MAX_LEN] = [0; llio::I2C_MAX_LEN];
|
||||
txbuf[0] = adr;
|
||||
for i in 0..data.len() {
|
||||
txbuf[i+1] = data[i];
|
||||
}
|
||||
transaction.bus_addr = TLV320AIC3100_I2C_ADR;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = data.len() as u32;
|
||||
transaction.txlen = (data.len() + 1) as u32;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
transaction.timeout_ms = I2C_TIMEOUT;
|
||||
|
||||
|
|
@ -254,7 +254,7 @@ impl Codec {
|
|||
}
|
||||
fn r(&mut self, adr: u8, data: &mut[u8]) -> bool {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf: [u8; 258] = [0; 258];
|
||||
let mut txbuf: [u8; llio::I2C_MAX_LEN] = [0; llio::I2C_MAX_LEN];
|
||||
txbuf[0] = adr;
|
||||
transaction.bus_addr = TLV320AIC3100_I2C_ADR;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
|
|
@ -262,7 +262,7 @@ impl Codec {
|
|||
transaction.status = I2cStatus::RequestIncoming;
|
||||
transaction.timeout_ms = I2C_TIMEOUT;
|
||||
|
||||
let rxbuf: [u8; 258] = [0; 258];
|
||||
let rxbuf: [u8; llio::I2C_MAX_LEN] = [0; llio::I2C_MAX_LEN];
|
||||
transaction.rxbuf = Some(rxbuf);
|
||||
transaction.rxlen = data.len() as u32;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ num-derive = {version = "0.3", default-features = false}
|
|||
num-traits = {version = "0.2", default-features = false}
|
||||
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
|
||||
|
||||
heapless = "0.7"
|
||||
|
||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||
utralib = {path = "../../utralib"}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ impl Into<u32> for UartType {
|
|||
}
|
||||
|
||||
/////////////////////// I2C
|
||||
// TODO: change this to pub(crate) once we've gotten rid of legacy API
|
||||
pub const I2C_MAX_LEN: usize = 33;
|
||||
// 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 {
|
||||
|
|
@ -71,9 +73,9 @@ pub enum I2cCallback {
|
|||
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 txbuf: Option<[u8; I2C_MAX_LEN]>, // long enough for a 256-byte operation + 2 bytes of "register address"
|
||||
pub txlen: u32,
|
||||
pub rxbuf: Option<[u8; 258]>,
|
||||
pub rxbuf: Option<[u8; I2C_MAX_LEN]>,
|
||||
pub rxlen: u32,
|
||||
pub listener: Option<(u32, u32, u32, u32)>, // where Rx split transactions should be routed to
|
||||
pub timeout_ms: u32,
|
||||
|
|
@ -85,6 +87,19 @@ impl I2cTransaction {
|
|||
I2cTransaction{ bus_addr: 0, txbuf: None, txlen: 0, rxbuf: None, rxlen: 0, timeout_ms: 500, status: I2cStatus::Uninitialized, listener: None, callback_id: 0 }
|
||||
}
|
||||
}
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub enum I2cOpcode {
|
||||
/// 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,
|
||||
}
|
||||
|
||||
////////////////////////////////// VIBE
|
||||
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||
|
|
@ -205,18 +220,6 @@ pub(crate) enum Opcode {
|
|||
/// Exit the server
|
||||
Quit,
|
||||
}
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub enum I2cOpcode {
|
||||
I2cTxRx, //(I2cTransaction), // type (tx or rx) encoded in struct
|
||||
/// 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,
|
||||
}
|
||||
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub(crate) enum EventCallback {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use utralib::*;
|
|||
|
||||
use num_traits::ToPrimitive;
|
||||
use susres::{RegManager, RegOrField, SuspendResume};
|
||||
use heapless::spsc::Queue;
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
enum I2cState {
|
||||
|
|
@ -53,13 +54,8 @@ pub(crate) struct I2cStateMachine {
|
|||
ticktimer: ticktimer_server::Ticktimer, // a connection to the ticktimer so we can measure timeouts
|
||||
listener: Option<xous::SID>,
|
||||
error: bool, // set if the interrupt handler encountered some kind of error
|
||||
}
|
||||
|
||||
fn send_i2c_response(listener: xous::SID, trans: I2cTransaction) -> Result<(), xous::Error> {
|
||||
let cid = xous::connect(listener).unwrap();
|
||||
let buf = xous_ipc::Buffer::into_buf(trans).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend(cid, I2cCallback::Result.to_u32().unwrap()).map(|_|())?;
|
||||
unsafe{xous::disconnect(cid)}
|
||||
workqueue: Queue<I2cTransaction, 8>,
|
||||
}
|
||||
|
||||
impl I2cStateMachine {
|
||||
|
|
@ -85,6 +81,8 @@ impl I2cStateMachine {
|
|||
index: 0,
|
||||
listener: None,
|
||||
error: false,
|
||||
|
||||
workqueue: Queue::new(),
|
||||
};
|
||||
|
||||
// disable interrupt, just in case it's enabled from e.g. a warm boot
|
||||
|
|
@ -126,6 +124,17 @@ impl I2cStateMachine {
|
|||
}
|
||||
|
||||
pub fn initiate(&mut self, transaction: I2cTransaction ) -> I2cStatus {
|
||||
if !self.workqueue.is_empty() {
|
||||
match self.workqueue.enqueue(transaction) {
|
||||
Ok(_) => return I2cStatus::ResponseInProgress,
|
||||
_ => return I2cStatus::ResponseBusy,
|
||||
}
|
||||
} else {
|
||||
self.checked_initiate(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
fn checked_initiate(&mut self, transaction: I2cTransaction) -> I2cStatus {
|
||||
log::trace!("I2C initated with {:x?}", transaction);
|
||||
// sanity-check the bounds limits
|
||||
if transaction.txlen > 258 || transaction.rxlen > 258 {
|
||||
|
|
@ -194,43 +203,50 @@ impl I2cStateMachine {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn i2c_followup(&mut self, trans: I2cTransaction) -> Result<(), xous::Error> {
|
||||
if let Some(listener) = self.listener.take() {
|
||||
let cid = xous::connect(listener).unwrap();
|
||||
let buf = xous_ipc::Buffer::into_buf(trans).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend(cid, I2cCallback::Result.to_u32().unwrap()).map(|_|())?;
|
||||
unsafe{xous::disconnect(cid)};
|
||||
};
|
||||
if let Some(work) = self.workqueue.dequeue() {
|
||||
if self.checked_initiate(work) != I2cStatus::ResponseInProgress {
|
||||
log::error!("Unable to initiate I2C transaction even though machine should be idle: {:?}.", work);
|
||||
log::error!("Probably, the I2C engine is going off the rails from here...");
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // keep this around in case we figure out the NACK issue
|
||||
fn report_nack(&mut self) {
|
||||
log::trace!("NACK");
|
||||
// report the NACK situation to the listener
|
||||
let mut nack = I2cTransaction::new();
|
||||
nack.status = I2cStatus::ResponseNack;
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, nack).expect("LLIO|I2C: couldn't send NACK to listeners");
|
||||
};
|
||||
self.i2c_followup(nack).expect("couldn't send NACK to listeners");
|
||||
}
|
||||
fn report_timeout(&mut self) {
|
||||
log::error!("I2C timeout on transaction {:?}", self.transaction);
|
||||
let mut timeout = I2cTransaction::new();
|
||||
timeout.status = I2cStatus::ResponseTimeout;
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, timeout).expect("LLIO|I2c: couldn't send timeout error to liseners");
|
||||
};
|
||||
self.i2c_followup(timeout).expect("couldn't send timeout error to liseners");
|
||||
}
|
||||
pub fn report_write_done(&mut self) {
|
||||
log::trace!("write_done");
|
||||
// report the end of a write-only transaction to all the listeners
|
||||
let mut ack = I2cTransaction::new();
|
||||
ack.status = I2cStatus::ResponseWriteOk;
|
||||
if let Some(listener) = self.listener {
|
||||
send_i2c_response(listener, ack).expect("LLIO|I2C: couldn't send write ACK to listeners");
|
||||
};
|
||||
self.i2c_followup(ack).expect("couldn't send write ACK to listeners");
|
||||
}
|
||||
pub fn report_read_done(&mut self) {
|
||||
log::trace!("read_done");
|
||||
// report the result of a read transaction to all the listeners
|
||||
self.transaction.status = I2cStatus::ResponseReadOk;
|
||||
if let Some(listener) = self.listener.take() {
|
||||
log::trace!("Sending read done to {:?} of {:?}", listener, self.transaction);
|
||||
send_i2c_response(listener, self.transaction).expect("LLIO|I2C: couldn't send read response to listeners");
|
||||
} else {
|
||||
log::error!("No listener for my read done resopnse!");
|
||||
}
|
||||
log::trace!("Sending read done {:?}", self.transaction);
|
||||
self.i2c_followup(self.transaction).expect("couldn't send read response to listeners");
|
||||
}
|
||||
pub fn is_busy(&self) -> bool {
|
||||
if self.state == I2cState::Idle {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,21 @@ use num_traits::{ToPrimitive, FromPrimitive};
|
|||
// this hooks the response of the I2C bus
|
||||
static mut I2C_CB: Option<fn(I2cTransaction)> = None;
|
||||
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
static I2C_IN_PROGRESS_MUTEX: AtomicBool = AtomicBool::new(false);
|
||||
static mut I2C_RX_HANDOFF: [u8; I2C_MAX_LEN] = [0; I2C_MAX_LEN]; // this is protected by the above mutex
|
||||
|
||||
fn sync_i2c_cb(transaction: I2cTransaction) {
|
||||
if let Some(rxbuf) = transaction.rxbuf {
|
||||
unsafe {
|
||||
for i in 0..transaction.rxlen as usize {
|
||||
I2C_RX_HANDOFF[i] = rxbuf[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
I2C_IN_PROGRESS_MUTEX.store(false, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Llio {
|
||||
conn: CID,
|
||||
|
|
@ -21,6 +36,7 @@ pub struct Llio {
|
|||
rtc_sid: Option<xous::SID>,
|
||||
usb_sid: Option<xous::SID>,
|
||||
gpio_sid: Option<xous::SID>,
|
||||
i2c_timeout_ms: u32,
|
||||
}
|
||||
impl Llio {
|
||||
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
||||
|
|
@ -34,6 +50,7 @@ impl Llio {
|
|||
usb_sid: None,
|
||||
gpio_sid: None,
|
||||
i2c_conn,
|
||||
i2c_timeout_ms: 10,
|
||||
})
|
||||
}
|
||||
pub fn vibe(&self, pattern: VibePattern) -> Result<(), xous::Error> {
|
||||
|
|
@ -95,14 +112,194 @@ impl Llio {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn i2c_sync_request(&self, transaction: &mut I2cTransaction) -> Result<I2cStatus, xous::Error> {
|
||||
|
||||
fn check_cb_init(&mut self) {
|
||||
if self.i2c_sid.is_none() {
|
||||
let sid = xous::create_server().unwrap();
|
||||
self.i2c_sid = Some(sid);
|
||||
let sid_tuple = sid.to_u32();
|
||||
xous::create_thread_4(i2c_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
||||
// note: we don't register a callback, because we hand our SID directly to the i2c request for a 1:1 message return
|
||||
}
|
||||
}
|
||||
pub fn i2c_set_timeout(&mut self, timeout: u32) {
|
||||
self.i2c_timeout_ms = timeout;
|
||||
}
|
||||
pub fn i2c_write_async(&mut self, dev: u8, adr: u8, data: &[u8], maybe_cb: Option<fn(I2cTransaction)>) -> Result<I2cStatus, xous::Error> {
|
||||
if data.len() > I2C_MAX_LEN - 1 {
|
||||
return Err(xous::Error::OutOfMemory)
|
||||
}
|
||||
let mut transaction = I2cTransaction::new();
|
||||
if let Some(cb) = maybe_cb {
|
||||
self.check_cb_init();
|
||||
unsafe {
|
||||
if let Some(old_cb) = I2C_CB {
|
||||
if old_cb != cb {
|
||||
log::warn!("Multiple outstanding write transactions, with different callbacks. You are probably making an error!");
|
||||
}
|
||||
}
|
||||
I2C_CB = Some(cb);
|
||||
}
|
||||
match self.i2c_sid {
|
||||
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
||||
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
||||
}
|
||||
}
|
||||
let mut txbuf = [0; I2C_MAX_LEN];
|
||||
txbuf[0] = adr;
|
||||
for i in 0..data.len() {
|
||||
txbuf[i+1] = data[i];
|
||||
}
|
||||
transaction.bus_addr = dev;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = (data.len() + 1) as u32;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
transaction.timeout_ms = self.i2c_timeout_ms;
|
||||
|
||||
pub fn i2c_async_request(&self, transaction: I2cTransaction, cb: fn(I2cTransaction)) -> Result<I2cStatus, xous::Error> {
|
||||
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||
let result = buf.to_original::<I2cStatus, _>().unwrap();
|
||||
Ok(result)
|
||||
}
|
||||
pub fn i2c_read_async(&mut self, dev: u8, adr: u8, data: &mut[u8], read_cb: fn(I2cTransaction)) -> Result<I2cStatus, xous::Error> {
|
||||
if data.len() > I2C_MAX_LEN - 1 {
|
||||
return Err(xous::Error::OutOfMemory)
|
||||
}
|
||||
let mut transaction = I2cTransaction::new();
|
||||
self.check_cb_init();
|
||||
unsafe {
|
||||
if let Some(old_cb) = I2C_CB {
|
||||
if old_cb != read_cb {
|
||||
log::warn!("Multiple outstanding read transactions, with different callbacks. Hope you know what you are doing!");
|
||||
}
|
||||
}
|
||||
I2C_CB = Some(read_cb);
|
||||
}
|
||||
match self.i2c_sid {
|
||||
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
||||
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
||||
}
|
||||
match self.i2c_sid {
|
||||
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
||||
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
||||
}
|
||||
let mut txbuf = [0; I2C_MAX_LEN];
|
||||
txbuf[0] = adr;
|
||||
let rxbuf = [0; I2C_MAX_LEN];
|
||||
transaction.bus_addr = dev;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = 1;
|
||||
transaction.rxbuf = Some(rxbuf);
|
||||
transaction.rxlen = data.len() as u32;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
transaction.timeout_ms = self.i2c_timeout_ms;
|
||||
|
||||
}*/
|
||||
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||
let result = buf.to_original::<I2cStatus, _>().unwrap();
|
||||
Ok(result)
|
||||
}
|
||||
// used by async callback handlers to indicate their completion, allowing e.g. later synchronous operations
|
||||
pub fn i2c_async_done(&self) {
|
||||
unsafe{I2C_CB = None};
|
||||
}
|
||||
pub fn i2c_write_sync(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<I2cStatus, xous::Error> {
|
||||
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
||||
log::error!("entering a synchronous routine, but somehow something was already in progress!");
|
||||
return Err(xous::Error::InternalError);
|
||||
}
|
||||
|
||||
if data.len() > I2C_MAX_LEN - 1 {
|
||||
return Err(xous::Error::OutOfMemory)
|
||||
}
|
||||
let mut transaction = I2cTransaction::new();
|
||||
self.check_cb_init();
|
||||
unsafe {
|
||||
if let Some(old_cb) = I2C_CB {
|
||||
if old_cb != sync_i2c_cb {
|
||||
log::error!("Mix-and-match of sync and async callbacks is not allowed. Finish your async callbacks first, and call i2c_async_done!");
|
||||
return Err(xous::Error::ShareViolation);
|
||||
}
|
||||
}
|
||||
I2C_CB = Some(sync_i2c_cb);
|
||||
}
|
||||
match self.i2c_sid {
|
||||
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
||||
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
||||
}
|
||||
let mut txbuf = [0; I2C_MAX_LEN];
|
||||
txbuf[0] = adr;
|
||||
for i in 0..data.len() {
|
||||
txbuf[i+1] = data[i];
|
||||
}
|
||||
transaction.bus_addr = dev;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = (data.len() + 1) as u32;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
transaction.timeout_ms = self.i2c_timeout_ms;
|
||||
|
||||
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
|
||||
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||
let result = buf.to_original::<I2cStatus, _>().unwrap();
|
||||
if result != I2cStatus::ResponseInProgress {
|
||||
return Err(xous::Error::OutOfMemory);
|
||||
}
|
||||
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
||||
xous::yield_slice();
|
||||
}
|
||||
Ok(I2cStatus::ResponseWriteOk)
|
||||
}
|
||||
pub fn i2c_read_sync(&mut self, dev: u8, adr: u8, data: &mut[u8]) -> Result<I2cStatus, xous::Error> {
|
||||
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
||||
log::error!("entering a synchronous routine, but somehow something was already in progress!");
|
||||
return Err(xous::Error::InternalError);
|
||||
}
|
||||
if data.len() > I2C_MAX_LEN - 1 {
|
||||
return Err(xous::Error::OutOfMemory)
|
||||
}
|
||||
let mut transaction = I2cTransaction::new();
|
||||
self.check_cb_init();
|
||||
unsafe {
|
||||
if let Some(old_cb) = I2C_CB {
|
||||
if old_cb != sync_i2c_cb {
|
||||
log::error!("Mix-and-match of sync and async callbacks is not allowed. Finish your async callbacks first, and call i2c_async_done!");
|
||||
return Err(xous::Error::ShareViolation);
|
||||
}
|
||||
}
|
||||
I2C_CB = Some(sync_i2c_cb);
|
||||
}
|
||||
match self.i2c_sid {
|
||||
Some(sid) => transaction.listener = Some(sid.to_u32()),
|
||||
None => log::error!("We requested a local listener, but somehow it's not there!"),
|
||||
}
|
||||
let mut txbuf = [0; I2C_MAX_LEN];
|
||||
txbuf[0] = adr;
|
||||
let rxbuf = [0; I2C_MAX_LEN];
|
||||
transaction.bus_addr = dev;
|
||||
transaction.txbuf = Some(txbuf);
|
||||
transaction.txlen = 1;
|
||||
transaction.rxbuf = Some(rxbuf);
|
||||
transaction.rxlen = data.len() as u32;
|
||||
transaction.status = I2cStatus::RequestIncoming;
|
||||
transaction.timeout_ms = self.i2c_timeout_ms;
|
||||
|
||||
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
|
||||
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
|
||||
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
|
||||
let result = buf.to_original::<I2cStatus, _>().unwrap();
|
||||
if result != I2cStatus::ResponseInProgress {
|
||||
return Err(xous::Error::OutOfMemory);
|
||||
}
|
||||
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
|
||||
xous::yield_slice();
|
||||
}
|
||||
unsafe {
|
||||
for (&src, dst) in I2C_RX_HANDOFF.iter().zip(data.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
}
|
||||
Ok(I2cStatus::ResponseReadOk)
|
||||
}
|
||||
///////////////////////// I2C ///////////////
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -503,7 +503,7 @@ fn i2c_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|||
let i2c_txrx = buffer.to_original::<llio::api::I2cTransaction, _>().unwrap();
|
||||
let status = i2c.initiate(i2c_txrx);
|
||||
buffer.replace(status).unwrap();
|
||||
}
|
||||
},
|
||||
Some(I2cOpcode::I2cIsBusy) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
||||
let busy = if i2c.is_busy() {1} else {0};
|
||||
xous::return_scalar(msg.sender, busy as _).expect("couldn't return I2cIsBusy");
|
||||
|
|
|
|||
|
|
@ -267,7 +267,7 @@ mod implementation {
|
|||
pub fn rtc_set(&mut self, secs: u8, mins: u8, hours: u8, days: u8, months: u8, years: u8, day: Weekday)
|
||||
-> Result<bool, xous::Error> {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf: [u8; 258] = [0; 258];
|
||||
let mut txbuf: [u8; llio::I2C_MAX_LEN] = [0; llio::I2C_MAX_LEN];
|
||||
|
||||
if secs > 59 { return Ok(false); }
|
||||
if mins > 59 { return Ok(false); }
|
||||
|
|
@ -316,8 +316,8 @@ mod implementation {
|
|||
|
||||
pub fn rtc_get(&mut self) -> Result<(), xous::Error> {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf = [0; 258];
|
||||
let rxbuf = [0; 258];
|
||||
let mut txbuf = [0; llio::I2C_MAX_LEN];
|
||||
let rxbuf = [0; llio::I2C_MAX_LEN];
|
||||
txbuf[0] = ABRTCMC_SECONDS;
|
||||
transaction.bus_addr = ABRTCMC_I2C_ADR;
|
||||
transaction.txlen = 1;
|
||||
|
|
@ -339,7 +339,7 @@ mod implementation {
|
|||
|
||||
fn blocking_i2c_write2(&self, tx: [u8; 2]) -> bool {
|
||||
let mut transaction = I2cTransaction::new();
|
||||
let mut txbuf: [u8; 258] = [0; 258];
|
||||
let mut txbuf: [u8; llio::I2C_MAX_LEN] = [0; llio::I2C_MAX_LEN];
|
||||
txbuf[0] = tx[0];
|
||||
txbuf[1] = tx[1];
|
||||
transaction.bus_addr = ABRTCMC_I2C_ADR;
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ impl Audio {
|
|||
unsafe{ raw_header[i] = (*samples)[i] };
|
||||
log::trace!("0x{:x}", raw_header[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
log::trace!("setting up audio stream");
|
||||
codec.setup_8k_stream().expect("couldn't set the CODEC to expected defaults");
|
||||
|
|
@ -40,8 +39,7 @@ impl Audio {
|
|||
log::trace!("hooking frame callback");
|
||||
codec.hook_frame_callback(0xDEAD_BEEF, callback_conn).unwrap(); // any non-handled IDs get routed to our callback port
|
||||
log::trace!("returning from setup");
|
||||
*/
|
||||
|
||||
*/
|
||||
Audio {
|
||||
codec,
|
||||
sample,
|
||||
|
|
|
|||
Loading…
Reference in a new issue