Skip to content

Add async variants of the traits in embedded_hal::digital #647

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

Closed
i509VCB opened this issue Feb 6, 2025 · 0 comments · Fixed by #649
Closed

Add async variants of the traits in embedded_hal::digital #647

i509VCB opened this issue Feb 6, 2025 · 0 comments · Fixed by #649

Comments

@i509VCB
Copy link
Contributor

i509VCB commented Feb 6, 2025

Currently all the traits in embedded_hal::digital are blocking GPIO APIs. For most microcontroller firmware this is normally fine since you are just reading some register in the address space to access of set state which is effectively immediate.

However this API falls apart when IO expanders enter the equation. Something like the MCP23017 may need to perform an I2C transaction to get the state of GPIO. Blocking during an I2C transaction can take some time and blocking may be undesirable.

I also am working some firmware that would let you turn an MCU into an I/O expander using postcard. Setting an output pin to a high level would need to transmit a packet over some transport to the target MCU. A workaround would be to queue the operation for sending asynchronously later and return Ok. However the workaround can lead to race conditions if the transport could lose packets in transit. To me it seems less error prone to be able to await a async form of toggle.

embedded_hal_async::digital does exist, but this only contains the Wait trait. This does let you listen for events, but does not let you set output pin state or ask for the current pin state asynchronously. The best way I can think of to make IO expanders act correctly would be to add traits to embedded_hal_async which are async equivalents of the traits under the digital module.

Another concern I have is crates using embedded-hal already depending on types like OutputPin. I don't think it's possible to express a trait bound like implement one of embedded_hal::digital::OutputPin or embedded_hal_async::digital::OutputPin. Even though this could be added without a semver breakage, I imagine the new traits might ripple into a lot of broken downstream crates.

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.

1 participant