From 1bbcccf4335f617c265acb2e53fe3752f0fe7d14 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 15 Apr 2024 15:17:29 +0100 Subject: [PATCH] Always stop after reading. For some reason suspending causes subsequent reads to overrun. --- nrf-hal-common/src/twim.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/nrf-hal-common/src/twim.rs b/nrf-hal-common/src/twim.rs index 3cf5fc13..caaafa9d 100644 --- a/nrf-hal-common/src/twim.rs +++ b/nrf-hal-common/src/twim.rs @@ -426,17 +426,11 @@ where compiler_fence(SeqCst); unsafe { self.set_rx_buffer(buffer)? }; - // Set appropriate lastrx shortcut. - if next_operation_write.is_none() { - self.0.shorts.write(|w| w.lastrx_stop().enabled()); - } else { - #[cfg(not(any( - feature = "5340-app", - feature = "5340-net", - feature = "52832" - )))] - self.0.shorts.write(|w| w.lastrx_suspend().enabled()); - } + // TODO: We should suspend rather than stopping if there are more operations to + // follow, but for some reason that results in an overrun error and reading bad + // data in the next read. + rtt_target::rprintln!("Stopping after read."); + self.0.shorts.write(|w| w.lastrx_stop().enabled()); // Start read. self.0.tasks_startrx.write(|w| unsafe { w.bits(1) }); @@ -514,8 +508,10 @@ impl I2c for Twim { // Copy the resulting data back to the various buffers. for j in (0..=i).rev() { if let Operation::Read(buffer) = &mut operations[j] { - buffer.copy_from_slice(&rx_copy[pending_rx_bytes-buffer.len()..pending_rx_bytes]); - pending_rx_bytes-= buffer.len(); + buffer.copy_from_slice( + &rx_copy[pending_rx_bytes - buffer.len()..pending_rx_bytes], + ); + pending_rx_bytes -= buffer.len(); } else { break; }