diff options
| author | Sebastian Goll <[email protected]> | 2024-03-20 02:20:21 +0100 |
|---|---|---|
| committer | Sebastian Goll <[email protected]> | 2024-03-20 02:59:30 +0100 |
| commit | 8f19a2b537c90c2bf04f6e5ea9a7107f6e000067 (patch) | |
| tree | 14f34f99f83a5c05b1d65ab1b3519e25dd109423 | |
| parent | c96062fbcdb32b5ecc9b9d3507097f09bbe4b7ca (diff) | |
Avoid missing stop condition when write/read with empty read buffer
| -rw-r--r-- | embassy-stm32/src/i2c/v1.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 678568706..c96935d8f 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs | |||
| @@ -367,6 +367,12 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 367 | 367 | ||
| 368 | /// Blocking write, restart, read. | 368 | /// Blocking write, restart, read. |
| 369 | pub fn blocking_write_read(&mut self, addr: u8, write: &[u8], read: &mut [u8]) -> Result<(), Error> { | 369 | pub fn blocking_write_read(&mut self, addr: u8, write: &[u8], read: &mut [u8]) -> Result<(), Error> { |
| 370 | // Check empty read buffer before starting transaction. Otherwise, we would not generate the | ||
| 371 | // stop condition below. | ||
| 372 | if read.is_empty() { | ||
| 373 | return Err(Error::Overrun); | ||
| 374 | } | ||
| 375 | |||
| 370 | let timeout = self.timeout(); | 376 | let timeout = self.timeout(); |
| 371 | 377 | ||
| 372 | self.write_bytes(addr, write, timeout, FrameOptions::FirstFrame)?; | 378 | self.write_bytes(addr, write, timeout, FrameOptions::FirstFrame)?; |
| @@ -381,6 +387,15 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { | |||
| 381 | /// | 387 | /// |
| 382 | /// [transaction contract]: embedded_hal_1::i2c::I2c::transaction | 388 | /// [transaction contract]: embedded_hal_1::i2c::I2c::transaction |
| 383 | pub fn blocking_transaction(&mut self, addr: u8, operations: &mut [Operation<'_>]) -> Result<(), Error> { | 389 | pub fn blocking_transaction(&mut self, addr: u8, operations: &mut [Operation<'_>]) -> Result<(), Error> { |
| 390 | // Check empty read buffer before starting transaction. Otherwise, we would not generate the | ||
| 391 | // stop condition below. | ||
| 392 | if operations.iter().any(|op| match op { | ||
| 393 | Operation::Read(read) => read.is_empty(), | ||
| 394 | Operation::Write(_) => false, | ||
| 395 | }) { | ||
| 396 | return Err(Error::Overrun); | ||
| 397 | } | ||
| 398 | |||
| 384 | let timeout = self.timeout(); | 399 | let timeout = self.timeout(); |
| 385 | 400 | ||
| 386 | let mut operations = operations.iter_mut(); | 401 | let mut operations = operations.iter_mut(); |
