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

Race condition in gpio #126

Open
usbalbin opened this issue Jun 13, 2024 · 3 comments
Open

Race condition in gpio #126

usbalbin opened this issue Jun 13, 2024 · 3 comments

Comments

@usbalbin
Copy link
Contributor

I hope I am wrong, but I think there might be a race condition in the gpio's into_{x} methods.

gpio.pupdr.modify(|r, w| {

Notice how we perform a read-modify-write sequence without anything preventing the same thing being done to another pin in the same port.

As far as I can see, I believe we would need either a critical section or exclusive access to some sort of token to guarantee exclusive access. A critical section would cause a slight bit of overhead but would otherwise be non-breaking API-wise, I think.

@dtjones190
Copy link
Contributor

I think that you are correct, there is a potential race condition here.
It looks like the stm32g4 supports bit-banding on peripherals, so that would give you atomic writes of single bits without needing critical sections or API changes.

@usbalbin
Copy link
Contributor Author

But writing the bits one at a time would on the other hand cause potentialy bad things to happen since the write would be split up into multiple writes.

For example looking at PUPD[15:0][1:0] in the RM, changing from 01: Pull-up to 10: Pull-down would be

  1. Store 1 to left bit at bit banding address <-- We now have 11 which is reserved
  2. Store 0 to right bit

@usbalbin
Copy link
Contributor Author

However i suppose it would work very well for things that are single bit like OT[15:0] in GPIOx_OTYPER

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

No branches or pull requests

2 participants