From 060d1f6e6f36b01e1f0ec4beb20499ac22d94b24 Mon Sep 17 00:00:00 2001 From: Jamie Bird Date: Fri, 21 Jun 2024 15:09:57 +0100 Subject: [PATCH 1/2] Fix: Ensure I2C bus is free before master-write operation The I2C master-write function was failing when executed immediately after an I2C read operation, requiring manual delays to function correctly. This fix introduces a check to ensure the I2C bus is free before initiating the write operation. According to the RM0399 manual for STM32H7 chips, the BUSY bit (Bit 15 in the I2C ISR register) indicates whether a communication is in progress on the bus. The BUSY bit is set by hardware when a START condition is detected and cleared when a STOP condition is detected or when PE = 0. This fix prevents the write operation from starting until the BUSY bit is cleared. --- embassy-stm32/src/i2c/v2.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 80163c287..27381cd3c 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -109,6 +109,11 @@ impl<'d, M: Mode> I2c<'d, M> { timeout.check()?; } + // Wait for the bus to be free + while info.regs.isr().read().busy(){ + timeout.check()?; + }; + let reload = if reload { i2c::vals::Reload::NOTCOMPLETED } else { From 18ba56534bbff5fb00382e54aba8dbc32365e896 Mon Sep 17 00:00:00 2001 From: Jamie Bird Date: Fri, 21 Jun 2024 15:29:02 +0100 Subject: [PATCH 2/2] Fix Formatting Issues --- embassy-stm32/src/i2c/v2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 27381cd3c..8c8df79dd 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -110,9 +110,9 @@ impl<'d, M: Mode> I2c<'d, M> { } // Wait for the bus to be free - while info.regs.isr().read().busy(){ + while info.regs.isr().read().busy() { timeout.check()?; - }; + } let reload = if reload { i2c::vals::Reload::NOTCOMPLETED