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

stm32xx-sys: configure GPIO IRQ edge with an enum #1706

Merged
merged 3 commits into from
Apr 2, 2024

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Apr 2, 2024

The current Sys::gpio_irq_configure IPC interface is a bit confusing, as it takes two bools for the rising and falling edge sensitivities, as in:

sys.gpio_irq_configure(mask, true, false);

This isn't particularly clear for a reader, who then has to remember the order of the arguments to determine which edge sensitivity is being enabled here. This is a case of "boolean blindness".

This commit improves the interface by changing it to take an enum with variants for Rising, Falling, and Both. Now, the same call to the IPC looks like this:

sys.gpio_irq_configure(mask, Edge::Rising);

which should be much more obvious to a reader, who no longer needs to remember the argument ordering to decypher calls to this function.

Also, this has the advantage of preventing the case where both rising and falling edge sensitivities are disabled at compile-time. Currently, this is a runtime error (reply fault), but with the new interface, a caller can provide an invalid configuration if they hand-write the input buffer rather than using the Idol client stub, so it should be harder to mess up.

@hawkw hawkw requested review from cbiffle and bcantrill April 2, 2024 18:20
@hawkw hawkw marked this pull request as ready for review April 2, 2024 18:20
The current `Sys::gpio_irq_configure` IPC interface is a bit confusing,
as it takes two `bool`s for the rising and falling edge sensitivities,
as in:

```rust
sys.gpio_irq_configure(mask, true, false);
```

This isn't particularly clear for a reader, who then has to remember the
order of the arguments to determine which edge sensitivity is being
enabled here. This is a case of ["boolean blindness"][1].

This commit improves the interface by changing it to take an `enum` with
variants for `Rising`, `Falling`, and `Both`. Now, the same call to the
IPC looks like this:

```rust
sys.gpio_irq_configure(mask, Edge::Rising);
```

which should be much more obvious to a reader, who no longer needs to
remember the argument ordering to decypher calls to this function.

Also, this has the advantage of preventing the case where both rising
and falling edge sensitivities are disabled at compile-time. Currently,
this is a runtime error (reply fault), but with the new interface, a
caller can provide an invalid configuration if they hand-write the input
buffer rather than using the Idol client stub, so it should be harder to
mess up.

[1]: https://runtimeverification.com/blog/code-smell-boolean-blindness
Copy link
Collaborator

@cbiffle cbiffle left a comment

Choose a reason for hiding this comment

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

Mere nits and suggestions below.

drv/stm32xx-sys-api/src/lib.rs Show resolved Hide resolved
task/nucleo-user-button/src/main.rs Outdated Show resolved Hide resolved
task/nucleo-user-button/src/main.rs Outdated Show resolved Hide resolved
Signed-off-by: Eliza Weisman <eliza@elizas.website>
Copy link
Collaborator

@bcantrill bcantrill left a comment

Choose a reason for hiding this comment

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

This looks great and will be less error prone to write and easier to review -- thank you!

Signed-off-by: Eliza Weisman <eliza@elizas.website>
@hawkw hawkw enabled auto-merge (squash) April 2, 2024 19:25
@hawkw hawkw disabled auto-merge April 2, 2024 19:25
@hawkw hawkw enabled auto-merge (squash) April 2, 2024 19:25
@hawkw hawkw merged commit ea4c725 into master Apr 2, 2024
103 checks passed
@cbiffle cbiffle deleted the eliza/gpio-irq-configure branch April 2, 2024 22:25
hawkw added a commit that referenced this pull request Apr 5, 2024
The current `Sys::gpio_irq_configure` IPC interface is a bit confusing,
as it takes two `bool`s for the rising and falling edge sensitivities,
as in:

```rust
sys.gpio_irq_configure(mask, true, false);
```

This isn't particularly clear for a reader, who then has to remember the
order of the arguments to determine which edge sensitivity is being
enabled here. This is a case of ["boolean blindness"][1].

This commit improves the interface by changing it to take an `enum` with
variants for `Rising`, `Falling`, and `Both`. Now, the same call to the
IPC looks like this:

```rust
sys.gpio_irq_configure(mask, Edge::Rising);
```

which should be much more obvious to a reader, who no longer needs to
remember the argument ordering to decypher calls to this function.

Also, this has the advantage of preventing the case where both rising
and falling edge sensitivities are disabled at compile-time. Currently,
this is a runtime error (reply fault), but with the new interface, a
caller can provide an invalid configuration if they hand-write the input
buffer rather than using the Idol client stub, so it should be harder to
mess up.

[1]: https://runtimeverification.com/blog/code-smell-boolean-blindness
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.

None yet

3 participants