Skip to content

Commit

Permalink
nrf52: i2c: fixup buffer usage
Browse files Browse the repository at this point in the history
An `events_rxstarted` is when the master has written data, which
corresponds to the `buf` buffer. In contrast, the `slave_read_buf` is
set for a read_send(), from which the master reads data out of.

Additionally make sure we restore the buffer after use.

Signed-off-by: Wilfred Mallawa <wilfred.mallawa@wdc.com>
  • Loading branch information
twilfredo committed Nov 22, 2023
1 parent e02f7f9 commit d2a2f5d
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions chips/nrf52/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,29 @@ impl<'a> TWI<'a> {
} else {
self.registers.events_stopped.write(EVENT::EVENT::CLEAR);

// If RX started and we don't have a buffer then report
// read_expected()
// If RX started (master started write) and we don't have a buffer then report
// write_expected()
if self.registers.events_rxstarted.is_set(EVENT::EVENT) {
self.registers.events_rxstarted.write(EVENT::EVENT::CLEAR);
self.slave_client
.map(|client| match self.slave_read_buf.take() {
None => {
client.read_expected();
}
Some(_buf) => {}
});
self.slave_client.map(|client| {
if self.buf.is_none() {
client.write_expected();
}
});
}

// If TX started (master started read) and we don't have a buffer then report
// read_expected()
if self.registers.events_txstarted.is_set(EVENT::EVENT) {
self.registers.events_txstarted.write(EVENT::EVENT::CLEAR);
self.slave_client.map(|client| {
if self.slave_read_buf.is_none() {
client.read_expected();
}
});
}

// Write command received
if self.registers.events_write.is_set(EVENT::EVENT) {
self.registers.events_write.write(EVENT::EVENT::CLEAR);
let length = self.registers.rxd_amount.read(AMOUNT::AMOUNT) as usize;
Expand Down Expand Up @@ -173,7 +183,6 @@ impl<'a> TWI<'a> {

// We can blindly clear the following events since we're not using them.
self.registers.events_suspended.write(EVENT::EVENT::CLEAR);
self.registers.events_rxstarted.write(EVENT::EVENT::CLEAR);
self.registers.events_lastrx.write(EVENT::EVENT::CLEAR);
self.registers.events_lasttx.write(EVENT::EVENT::CLEAR);
}
Expand Down

0 comments on commit d2a2f5d

Please sign in to comment.