From 78e982495094b3dadac20bc4933e3735ae88b1f1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 23 Mar 2023 16:01:13 +0800 Subject: [PATCH] log if an unexpected interrupt happens on I2C --- services/llio/src/i2c/hardware.rs | 23 +++++++++++++++++------ services/llio/src/main.rs | 6 +++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/services/llio/src/i2c/hardware.rs b/services/llio/src/i2c/hardware.rs index a186a2c18..967579275 100644 --- a/services/llio/src/i2c/hardware.rs +++ b/services/llio/src/i2c/hardware.rs @@ -18,11 +18,13 @@ enum I2cIntError { MissingTx, MissingRx, UnexpectedState, + UnexpectedInterrupt, } // ASSUME: we are only ever handling txrx done interrupts. If implementing ARB interrupts, this needs to be refactored to read the source and dispatch accordingly. fn handle_i2c_irq(_irq_no: usize, arg: *mut usize) { let i2c = unsafe { &mut *(arg as *mut I2cStateMachine) }; + let event = i2c.i2c_csr.r(utra::i2c::EV_PENDING); if let Some(conn) = i2c.handler_conn { match i2c.handler_i() { @@ -41,11 +43,18 @@ fn handle_i2c_irq(_irq_no: usize, arg: *mut usize) { } }, } + if event != i2c.i2c_csr.ms(utra::i2c::EV_ENABLE_TXRX_DONE, 1) { + i2c.error = I2cIntError::UnexpectedInterrupt; + xous::try_send_message(conn, + xous::Message::new_scalar(I2cOpcode::IrqI2cTrace.to_usize().unwrap(), event as usize, 0, 0, 0)).map(|_| ()).unwrap(); + } } else { panic!("|handle_i2c_irq: TXRX done interrupt, but no connection for notification!"); } + + // clear all interrupts, regardless of the source i2c.i2c_csr - .wo(utra::i2c::EV_PENDING, i2c.i2c_csr.r(utra::i2c::EV_PENDING)); + .wo(utra::i2c::EV_PENDING, 0xFFFF_FFFF); } #[derive(Debug, Eq, PartialEq)] @@ -178,6 +187,7 @@ impl I2cStateMachine { } pub fn driver_reset(&mut self) { + log::warn!("I2C driver reset called"); self.i2c_csr.wfo(utra::i2c::EV_ENABLE_TXRX_DONE, 0); self.i2c_csr.wfo(utra::i2c::CORE_RESET_RESET, 1); self.ticktimer.sleep_ms(10).ok(); @@ -259,7 +269,7 @@ impl I2cStateMachine { self.i2c_csr.ms(utra::i2c::COMMAND_STA, 1) ); log::debug!("Initiate write"); - self.trace(); + self.trace(0); } else if transaction.rxbuf.is_some() { // initiate bus address with read bit set self.state = I2cState::Read; @@ -271,11 +281,11 @@ impl I2cStateMachine { self.i2c_csr.ms(utra::i2c::COMMAND_STA, 1) ); log::debug!("Initiate read"); - self.trace(); + self.trace(0); } else { // no buffers specified, erase everything and go to idle log::error!("Initiation error"); - self.trace(); + self.trace(0); self.report_response(I2cStatus::ResponseFormatError, None); return; } @@ -355,8 +365,8 @@ impl I2cStateMachine { 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}", + pub(crate) fn trace(&self, arg: usize) { + log::debug!("I2C trace '{:?}/{:?}'=> PENDING: {:x}, ENABLE: {:x}, CMD: {:x}, STATUS: {:x}, CONTROL: {:x}, PRESCALE: {:x}, arg: {:x}", self.state, self.error, self.i2c_csr.r(utra::i2c::EV_PENDING), @@ -365,6 +375,7 @@ impl I2cStateMachine { self.i2c_csr.r(utra::i2c::STATUS), self.i2c_csr.r(utra::i2c::CONTROL), self.i2c_csr.r(utra::i2c::PRESCALE), + arg ); } diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index f5560d700..f2daa1b6e 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -149,9 +149,9 @@ fn i2c_thread(i2c_sid: xous::SID) { xous::return_scalar(next, 1).ok(); // this unblocks the waiting thread, and immediately hands the quantum to that thread } } - Some(I2cOpcode::IrqI2cTrace) => { - i2c.trace(); - }, + Some(I2cOpcode::IrqI2cTrace) => msg_scalar_unpack!(msg, arg, _, _, _, { + i2c.trace(arg); + }), Some(I2cOpcode::I2cTxRx) => { if !i2c_mutex_acquired { log::warn!("TxRx operation was initiated without an acquired mutex. This will become a hard error in future revisions");