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

Erroneous, though benign, bit operation for nrf5x gpio #20736

Closed
steverpalmer opened this issue Jun 7, 2024 · 2 comments · Fixed by #20737
Closed

Erroneous, though benign, bit operation for nrf5x gpio #20736

steverpalmer opened this issue Jun 7, 2024 · 2 comments · Fixed by #20737

Comments

@steverpalmer
Copy link
Contributor

(FYI I am a RIOT newbie, but have 40 years of coding experience. Thanks for RIOT by-the-way.)

Description

Short version - replace
NRF_GPIOTE->INTENSET |= (GPIOTE_INTENSET_IN0_Msk << i);
with
NRF_GPIOTE->INTENSET = (GPIOTE_INTENSET_IN0_Msk << i);

In file cpu/nrf5x_common/periph/gpio.c function gpio_irq_enable sets register INTENSET with a C bitwise OR assignment (|=) on or around line 216. My understanding that this reads the register, ORs in the appropriate mask, and then writes it back. However, the behaviour of the ARM INTENSET register means that one only the assignment is required.

A similar case occurs in the following function gpio_irq_disable, which correctly assigns the mask value directly to the INTENCLR register, rather than trying to do a bitwise AND assignment with a negated mask.

I've done a "grep" search through the code base and have found no other instances of using bitwise OR assignment on an INTENSET register.

I'm 95% confident that this "bug" is benign (as in has no impact). The only case that I'm not sure of is whether the bitwise OR assignment is atomic. I could be wrong, but there may be a case if two pieces of thread/ISRs code are trying to gpio_irq_enable on different pins, when one interrupts the other. Maybe one of the assignments to INTENSET could be "lost"?

Steps to reproduce the issue

I wouldn't know how to start writing code to demonstrate this "bug". I found the fault just by code inspection as I was trying to debug my code and understand the RIOT code.

Versions

I noticed this in 2024-04 release, but it is also present in the master git branch.

@steverpalmer
Copy link
Contributor Author

Thinking further, my "not-atomic" worry is not a problem, since writing a zero bit to the INTENSET register is a NOP.

I can think of no cases where this fault is detectable. I'll adjust the title correspondingly.

@steverpalmer steverpalmer changed the title Erroneous, though mostly benign, bit operation for nf5x gpio Erroneous, though benign, bit operation for nrf5x gpio Jun 7, 2024
@dylad
Copy link
Member

dylad commented Jun 7, 2024

Hello @steverpalmer and welcome !
Indeed the read-modify-write operation is completely useless here, thanks for spotting this.
While this code pattern does not create bug in such case, it introduces unnecessary operations so better save a few instructions here.
Would you like to submit a PR to fix this ?
If not, I could submit one.

ant9000 pushed a commit to ant9000/RIOT that referenced this issue Aug 23, 2024

Unverified

This user has not yet uploaded their public signing key.
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 a pull request may close this issue.

2 participants