Skip to content

Conversation

@de-vri-es
Copy link
Contributor

This PR turns gpiote::InputChannel::wait() into a fn wait() -> impl Future instead of an async fn. This guarantees that the interrupt is enabled and the event flag is cleared directly when the function is called, and not delayed until the future is polled.

This enables use cases like this:

let wait = input_channel.wait();
if input.pin().is_low() {
  return;
}
wait.await;

Which should give race-free waiting for a pin to become low.

Note: I also think this function should actually take &mut self. It clears the event flag, so multiple concurrent calls will interfere with each-other.

However, making it &mut self will actually break the use case above :p

So.. do we 🙈 about the &self or should I maybe directly implement functions like wait_for_high() and wait_for_low() to avoid races and still support the use case above?

Those functions might also actually want to change the event polarity though, or they would need to report an error if the polarity is wrong. Or we would need separate types for the different polarities, but that feels a bit overkill.

@de-vri-es de-vri-es force-pushed the nrf-gpiote-enable-interrupts-before-being-polled branch 2 times, most recently from 8d5f69a to 1552990 Compare October 30, 2025 11:35
@lulf
Copy link
Member

lulf commented Oct 30, 2025

I think my preference would be to add wait_for_low, wait_for_high to the API and require &mut. The example code both using wait and input seems like something we'd want to avoid.

@de-vri-es de-vri-es force-pushed the nrf-gpiote-enable-interrupts-before-being-polled branch from 1552990 to ca7cf0a Compare October 30, 2025 13:33
@de-vri-es de-vri-es changed the title embassy-nrf: catch GPIOTE events directly when wait() is called embassy-nrf: add gpiote::InputChannel::wait_for_high/low() Oct 30, 2025
@de-vri-es de-vri-es force-pushed the nrf-gpiote-enable-interrupts-before-being-polled branch 3 times, most recently from dd6bc8e to 5033f59 Compare October 30, 2025 13:46
Also catch GPIOTE events directly when wait() is called, even before
polling the future.
@de-vri-es de-vri-es force-pushed the nrf-gpiote-enable-interrupts-before-being-polled branch from 5033f59 to 3949a86 Compare October 30, 2025 13:55
@de-vri-es
Copy link
Contributor Author

Updated and tested with the nrf9160 :)

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.

2 participants