log if an unexpected interrupt happens on I2C

This commit is contained in:
bunnie 2023-03-23 16:01:13 +08:00
parent 673a3f1364
commit 78e9824950
2 changed files with 20 additions and 9 deletions

View file

@ -18,11 +18,13 @@ enum I2cIntError {
MissingTx, MissingTx,
MissingRx, MissingRx,
UnexpectedState, 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. // 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) { fn handle_i2c_irq(_irq_no: usize, arg: *mut usize) {
let i2c = unsafe { &mut *(arg as *mut I2cStateMachine) }; 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 { if let Some(conn) = i2c.handler_conn {
match i2c.handler_i() { 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 { } else {
panic!("|handle_i2c_irq: TXRX done interrupt, but no connection for notification!"); panic!("|handle_i2c_irq: TXRX done interrupt, but no connection for notification!");
} }
// clear all interrupts, regardless of the source
i2c.i2c_csr 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)] #[derive(Debug, Eq, PartialEq)]
@ -178,6 +187,7 @@ impl I2cStateMachine {
} }
pub fn driver_reset(&mut self) { 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::EV_ENABLE_TXRX_DONE, 0);
self.i2c_csr.wfo(utra::i2c::CORE_RESET_RESET, 1); self.i2c_csr.wfo(utra::i2c::CORE_RESET_RESET, 1);
self.ticktimer.sleep_ms(10).ok(); self.ticktimer.sleep_ms(10).ok();
@ -259,7 +269,7 @@ impl I2cStateMachine {
self.i2c_csr.ms(utra::i2c::COMMAND_STA, 1) self.i2c_csr.ms(utra::i2c::COMMAND_STA, 1)
); );
log::debug!("Initiate write"); log::debug!("Initiate write");
self.trace(); self.trace(0);
} else if transaction.rxbuf.is_some() { } else if transaction.rxbuf.is_some() {
// initiate bus address with read bit set // initiate bus address with read bit set
self.state = I2cState::Read; self.state = I2cState::Read;
@ -271,11 +281,11 @@ impl I2cStateMachine {
self.i2c_csr.ms(utra::i2c::COMMAND_STA, 1) self.i2c_csr.ms(utra::i2c::COMMAND_STA, 1)
); );
log::debug!("Initiate read"); log::debug!("Initiate read");
self.trace(); self.trace(0);
} else { } else {
// no buffers specified, erase everything and go to idle // no buffers specified, erase everything and go to idle
log::error!("Initiation error"); log::error!("Initiation error");
self.trace(); self.trace(0);
self.report_response(I2cStatus::ResponseFormatError, None); self.report_response(I2cStatus::ResponseFormatError, None);
return; return;
} }
@ -355,8 +365,8 @@ impl I2cStateMachine {
pub fn in_progress(&self) -> bool { pub fn in_progress(&self) -> bool {
self.state != I2cState::Idle self.state != I2cState::Idle
} }
pub(crate) fn trace(&self) { pub(crate) fn trace(&self, arg: usize) {
log::debug!("I2C trace '{:?}/{:?}'=> PENDING: {:x}, ENABLE: {:x}, CMD: {:x}, STATUS: {:x}, CONTROL: {:x}, PRESCALE: {:x}", log::debug!("I2C trace '{:?}/{:?}'=> PENDING: {:x}, ENABLE: {:x}, CMD: {:x}, STATUS: {:x}, CONTROL: {:x}, PRESCALE: {:x}, arg: {:x}",
self.state, self.state,
self.error, self.error,
self.i2c_csr.r(utra::i2c::EV_PENDING), 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::STATUS),
self.i2c_csr.r(utra::i2c::CONTROL), self.i2c_csr.r(utra::i2c::CONTROL),
self.i2c_csr.r(utra::i2c::PRESCALE), self.i2c_csr.r(utra::i2c::PRESCALE),
arg
); );
} }

View file

@ -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 xous::return_scalar(next, 1).ok(); // this unblocks the waiting thread, and immediately hands the quantum to that thread
} }
} }
Some(I2cOpcode::IrqI2cTrace) => { Some(I2cOpcode::IrqI2cTrace) => msg_scalar_unpack!(msg, arg, _, _, _, {
i2c.trace(); i2c.trace(arg);
}, }),
Some(I2cOpcode::I2cTxRx) => { Some(I2cOpcode::I2cTxRx) => {
if !i2c_mutex_acquired { if !i2c_mutex_acquired {
log::warn!("TxRx operation was initiated without an acquired mutex. This will become a hard error in future revisions"); log::warn!("TxRx operation was initiated without an acquired mutex. This will become a hard error in future revisions");