From 7cd25a55a52e8059d05fff8550a280fa97534e2e Mon Sep 17 00:00:00 2001 From: Zhiyao Ma Date: Sun, 2 Jul 2023 18:06:57 -0400 Subject: [PATCH] A better fix and Change log update. --- CHANGELOG.md | 2 ++ src/i2c/dma.rs | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0f22b52..3c43ceea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - move gpio, dma impls, adc pins in subdir, remove unused `From` impls [#658] [#664] - Bump `embedded-hal` to `1.0.0-alpha.10`. See [their changelog][embedded-hal-1.0.0-alpha.10] for further details. Note that this included breaking changes to the previous alpha APIs. [#663] + - Fix race condition in sending start condition in I2C. [#662] [#658]: https://github.com/stm32-rs/stm32f4xx-hal/pull/658 +[#662]: https://github.com/stm32-rs/stm32f4xx-hal/pull/662 [#663]: https://github.com/stm32-rs/stm32f4xx-hal/pull/663 [#664]: https://github.com/stm32-rs/stm32f4xx-hal/pull/664 [embedded-hal-1.0.0-alpha.10]: https://github.com/rust-embedded/embedded-hal/blob/v1.0.0-alpha.10/embedded-hal/CHANGELOG.md diff --git a/src/i2c/dma.rs b/src/i2c/dma.rs index 2af11c30..339292e8 100644 --- a/src/i2c/dma.rs +++ b/src/i2c/dma.rs @@ -421,10 +421,15 @@ where fn send_start(&mut self, read: bool) -> Result<(), super::Error> { let i2c = &self.hal_i2c.i2c; + + // Make sure the ack and start bit is set together in a single + // read-modify-write operation to avoid race condition. + // See PR: https://github.com/stm32-rs/stm32f4xx-hal/pull/662 if read { - i2c.cr1.modify(|_, w| w.ack().set_bit()); + i2c.cr1.modify(|_, w| w.ack().set_bit().start().set_bit()); + } else { + i2c.cr1.modify(|_, w| w.start().set_bit()); } - i2c.cr1.modify(|_, w| w.start().set_bit()); // Wait until START condition was generated while self