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

Conversation

TheZoq2
Copy link
Member

@TheZoq2 TheZoq2 commented Dec 23, 2020

As per the discussion in #282

This seems better, but it also leaves the possibility of some flags not being cleared if multiple flags are raised at the same time. Is that something that can even happen, and how should we deal with it then?

CC: @Disasm

@Disasm
Copy link
Member

Disasm commented Dec 23, 2020

I think that all errors should be additionally cleared before a transaction begins.

@burrbull
Copy link
Member

I think this code is not optimal. You read sr1 twice. Before comparison and when do modify.

src/i2c.rs Outdated
@@ -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
Member

@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
Member

Choose a reason for hiding this comment

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

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

@TheZoq2 TheZoq2 force-pushed the fix_i2c_sr_clear branch 2 times, most recently from c0408b6 to 10b1b8d Compare January 23, 2021 09:34
@TheZoq2
Copy link
Member Author

TheZoq2 commented Jan 23, 2021

Updated the PR with the suggested changes, status flags should now be cleared before starting a new transaction, and only the error we actually noted is cleared.

I hope the unsafe I had to add is correct, writing to read only registers should have no effect.

We also don't have a way to read more than one error flag, perhaps that's something we should add at some point

burrbull added a commit that referenced this pull request Jul 30, 2021
burrbull added a commit that referenced this pull request Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants