Skip to content

Commit dcb11fe

Browse files
authored
Merge pull request #662 from ZhiyaoMa98/master
Bug fix: race condition in I2C cr1 read-modify-write
2 parents d3edddd + 7cd25a5 commit dcb11fe

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
- move gpio, dma impls, adc pins in subdir, remove unused `From` impls [#658] [#664]
1313
- 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]
14+
- Fix race condition in sending start condition in I2C. [#662]
1415

1516
[#658]: https://github.com/stm32-rs/stm32f4xx-hal/pull/658
17+
[#662]: https://github.com/stm32-rs/stm32f4xx-hal/pull/662
1618
[#663]: https://github.com/stm32-rs/stm32f4xx-hal/pull/663
1719
[#664]: https://github.com/stm32-rs/stm32f4xx-hal/pull/664
1820
[embedded-hal-1.0.0-alpha.10]: https://github.com/rust-embedded/embedded-hal/blob/v1.0.0-alpha.10/embedded-hal/CHANGELOG.md

src/i2c/dma.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,14 @@ where
421421

422422
fn send_start(&mut self, read: bool) -> Result<(), super::Error> {
423423
let i2c = &self.hal_i2c.i2c;
424-
i2c.cr1.modify(|_, w| w.start().set_bit());
424+
425+
// Make sure the ack and start bit is set together in a single
426+
// read-modify-write operation to avoid race condition.
427+
// See PR: https://github.com/stm32-rs/stm32f4xx-hal/pull/662
425428
if read {
426-
i2c.cr1.modify(|_, w| w.ack().set_bit());
429+
i2c.cr1.modify(|_, w| w.ack().set_bit().start().set_bit());
430+
} else {
431+
i2c.cr1.modify(|_, w| w.start().set_bit());
427432
}
428433

429434
// Wait until START condition was generated

0 commit comments

Comments
 (0)