Skip to content

Commit

Permalink
I2C: tolerate invalid event in idle state.
Browse files Browse the repository at this point in the history
Do not panic if invalid event occurs in idle
state; instead ignore it.
  • Loading branch information
surban committed Jun 11, 2024
1 parent 8c04399 commit 315995c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion openemc-firmware/src/i2c_reg_slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
self.pos = 0;
return Ok(Event::Read { reg: self.reg });
}
_ => defmt::unreachable!(),
other => defmt::error!("Invalid I2C event in idle state: {:?}", other),
},
State::ReceiveReg => match self.i2c_event()? {
I2cEvent::Read(value) => {
Expand Down
13 changes: 13 additions & 0 deletions openemc-firmware/src/i2c_slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ pub enum Event<'a, I2C, PINS> {
End,
}

impl<'a, I2C, PINS> defmt::Format for Event<'a, I2C, PINS> {
fn format(&self, fmt: defmt::Formatter) {
match self {
Self::StartRead => defmt::write!(fmt, "StartRead"),
Self::StartWrite => defmt::write!(fmt, "StartWrite"),
Self::Read(_) => defmt::write!(fmt, "Read"),
Self::Write(_) => defmt::write!(fmt, "Write"),
Self::Restart => defmt::write!(fmt, "Restart"),
Self::End => defmt::write!(fmt, "End"),
}
}
}

/// Transmits a byte to the I2C master.
pub struct Writer<'a, I2C, PINS>(&'a mut I2cSlave<I2C, PINS>);

Expand Down

0 comments on commit 315995c

Please sign in to comment.