From ee6561f5d92bf6f7563bbb5c8931b930564354d7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 18 Mar 2023 00:06:35 +0800 Subject: [PATCH] fix bugs in i2c timeout handler seems to work now, recovers from timeout conditions more gracefully. --- services/llio/src/i2c/hardware.rs | 7 ++++++- services/llio/src/main.rs | 16 ++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/services/llio/src/i2c/hardware.rs b/services/llio/src/i2c/hardware.rs index a92590785..e3b0b701f 100644 --- a/services/llio/src/i2c/hardware.rs +++ b/services/llio/src/i2c/hardware.rs @@ -282,7 +282,7 @@ impl I2cStateMachine { self.index = 0; self.error = I2cIntError::NoErr; } else { - panic!("Invalid state: response requested but no request pending {:?}", status); + log::error!("Invalid state: response requested but no request pending {:?}. I2C bus state may be incoherent.", status); } if self.workqueue.len() > 0 { log::debug!("workqueue has pending items: {}", self.workqueue.len()); @@ -326,6 +326,11 @@ impl I2cStateMachine { true } } + /// This returns if a transaction is currently in progress. This differs from is_busy in that is_busy + /// will return false if the work queue is also empty, even though the interface is performing a transaction. + pub fn in_progress(&self) -> bool { + self.state != I2cState::Idle + } pub(crate) fn trace(&self) { log::debug!("I2C trace '{:?}/{:?}'=> PENDING: {:x}, ENABLE: {:x}, CMD: {:x}, STATUS: {:x}, CONTROL: {:x}, PRESCALE: {:x}", self.state, diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index ff324303f..1d3df27c9 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -23,7 +23,9 @@ use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::thread; -const POLL_INTERVAL_MS: usize = 50; +// This is slower than most timeout specifiers, but it's easier to debug +// when the check interval isn't creating a ton of log spew. +const POLL_INTERVAL_MS: usize = 250; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] enum PumpOp { Pump, @@ -59,11 +61,11 @@ fn i2c_thread(i2c_sid: xous::SID) { match FromPrimitive::from_usize(msg.body.id()) { Some(PumpOp::Pump) => msg_scalar_unpack!(msg, _, _, _, _, { tt.sleep_ms(POLL_INTERVAL_MS).unwrap(); - let target_time = target_lsb.load(Ordering::SeqCst) as u64 | (target_msb.load(Ordering::SeqCst) as u64) << 32; - if tt.elapsed_ms() >= target_time { - try_send_message(main_cid, Message::new_scalar(I2cOpcode::I2cTimeout.to_usize().unwrap(), 0, 0, 0, 0)).ok(); - } if run.load(Ordering::SeqCst) { + let target_time = target_lsb.load(Ordering::SeqCst) as u64 | (target_msb.load(Ordering::SeqCst) as u64) << 32; + if tt.elapsed_ms() >= target_time { + try_send_message(main_cid, Message::new_scalar(I2cOpcode::I2cTimeout.to_usize().unwrap(), 0, 0, 0, 0)).ok(); + } try_send_message(cid, Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)).ok(); } }), @@ -168,10 +170,12 @@ fn i2c_thread(i2c_sid: xous::SID) { xous::return_scalar(msg.sender, busy as _).expect("couldn't return I2cIsBusy"); }), Some(I2cOpcode::I2cTimeout) => { - if i2c.is_busy() { + if i2c.in_progress() { // timeout happened + log::warn!("Timeout detected, re-initiating transaction"); i2c.re_initiate(); if let Some(expiry) = i2c.get_expiry() { + log::debug!("Setting new expiry to {}", expiry); target_msb.store((expiry >> 32) as u32, Ordering::SeqCst); target_lsb.store(expiry as u32, Ordering::SeqCst); // no need to pump since the timeout loop is already running