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

GPIO: wakeup_enable: Return error instead of panic #2916

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `ClockSource` enums are now `#[non_exhaustive]` (#2912)

- `gpio::{Input, Flex}::wakeup_enable` now returns an error instead of panicking. (#2916)

### Fixed

- Xtensa devices now correctly enable the `esp-hal-procmacros/rtc-slow` feature (#2594)
Expand Down
46 changes: 38 additions & 8 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
//! [embedded-hal-async]: https://docs.rs/embedded-hal-async/latest/embedded_hal_async/index.html
//! [Inverting TX and RX Pins]: crate::uart#inverting-rx-and-tx-pins

use core::fmt::Display;

use portable_atomic::{AtomicPtr, AtomicU32, Ordering};
use procmacros::ram;
use strum::EnumCount;
Expand Down Expand Up @@ -206,6 +208,30 @@ impl From<Level> for bool {
}
}

/// Errors that can occur when configuring a pin to be a wakeup source.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WakeConfigError {
/// Returned when trying to configure a pin to wake up from light sleep on
/// an edge trigger, which is not supported.
EdgeTriggeringNotSupported,
}

impl Display for WakeConfigError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
WakeConfigError::EdgeTriggeringNotSupported => {
write!(
f,
"Edge triggering is not supported for wake-up from light sleep"
)
}
}
}
}

impl core::error::Error for WakeConfigError {}

/// Pull setting for an input.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
Expand Down Expand Up @@ -1617,8 +1643,8 @@ where
/// This will unlisten for interrupts
#[instability::unstable]
#[inline]
pub fn wakeup_enable(&mut self, enable: bool, event: WakeEvent) {
self.pin.wakeup_enable(enable, event);
pub fn wakeup_enable(&mut self, enable: bool, event: WakeEvent) -> Result<(), WakeConfigError> {
self.pin.wakeup_enable(enable, event)
}
}

Expand Down Expand Up @@ -1959,11 +1985,11 @@ where
int_enable: bool,
nmi_enable: bool,
wake_up_from_light_sleep: bool,
) {
) -> Result<(), WakeConfigError> {
if wake_up_from_light_sleep {
match event {
Event::AnyEdge | Event::RisingEdge | Event::FallingEdge => {
panic!("Edge triggering is not supported for wake-up from light sleep");
return Err(WakeConfigError::EdgeTriggeringNotSupported);
}
_ => {}
}
Expand All @@ -1974,15 +2000,19 @@ where
Some(gpio_intr_enable(int_enable, nmi_enable)),
event as u8,
wake_up_from_light_sleep,
)
);

Ok(())
}

/// Listen for interrupts.
///
/// See [`Input::listen`] for more information and an example.
#[inline]
pub fn listen(&mut self, event: Event) {
self.listen_with_options(event, true, false, false)
// Unwrap can't fail currently as listen_with_options is only supposed to return
// an error if wake_up_from_light_sleep is true.
unwrap!(self.listen_with_options(event, true, false, false));
}

/// Stop listening for interrupts.
Expand Down Expand Up @@ -2017,8 +2047,8 @@ where
/// This will unlisten for interrupts
#[instability::unstable]
#[inline]
pub fn wakeup_enable(&mut self, enable: bool, event: WakeEvent) {
self.listen_with_options(event.into(), false, false, enable);
pub fn wakeup_enable(&mut self, enable: bool, event: WakeEvent) -> Result<(), WakeConfigError> {
self.listen_with_options(event.into(), false, false, enable)
}
}

Expand Down
Loading