Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use modify instead of write for clearing sr1 #298

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix > 2 byte i2c reads
- Send stop after acknowledge errors on i2c
- Fix i2c interactions after errors
- Only clear individual i2c status registers on error

### Changed
- Use `cortex-m-rtic` instead of `cortex-m-rtfm` in the examples
Expand Down
8 changes: 4 additions & 4 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,16 @@ macro_rules! wait_for_flag {
let sr1 = $i2c.sr1.read();

if sr1.berr().bit_is_set() {
$i2c.sr1.write(|w| w.berr().clear_bit());
$i2c.sr1.modify(|_r, w| w.berr().clear_bit());
Copy link
Contributor

@burrbull burrbull Dec 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$i2c.sr1.modify(|_r, w| w.berr().clear_bit());
$i2c.sr1.write(|w| w
.bits(0xffff)
.berr().clear_bit()
);

Something like this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, one should use an all-one mask instead of the previous register value, because the register value can be changed during the read-modify-write sequence and this code will end up clearing bits it wasn't supposed to clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree
изображение

Err(Other(Error::Bus))
} else if sr1.arlo().bit_is_set() {
$i2c.sr1.write(|w| w.arlo().clear_bit());
$i2c.sr1.modify(|_r, w| w.arlo().clear_bit());
Err(Other(Error::Arbitration))
} else if sr1.af().bit_is_set() {
$i2c.sr1.write(|w| w.af().clear_bit());
$i2c.sr1.modify(|_r, w| w.af().clear_bit());
Err(Other(Error::Acknowledge))
} else if sr1.ovr().bit_is_set() {
$i2c.sr1.write(|w| w.ovr().clear_bit());
$i2c.sr1.modify(|_r, w| w.ovr().clear_bit());
Err(Other(Error::Overrun))
} else if sr1.$flag().bit_is_set() {
Ok(())
Expand Down