|
| 1 | +// Licensed under the Apache-2.0 license |
| 2 | + |
| 3 | +/// This represents a common set of GPIO operation errors. Implementations are |
| 4 | +/// free to define more specific or additional error types. However, by providing |
| 5 | +/// a mapping to these common errors, generic code can still react to them. |
| 6 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 7 | +#[non_exhaustive] |
| 8 | +pub enum GpioErrorKind { |
| 9 | + /// The specified GPIO port does not exist |
| 10 | + InvalidPort, |
| 11 | + /// The specified pin(s) do not exist on this port |
| 12 | + InvalidPin, |
| 13 | + /// The requested configuration is not supported |
| 14 | + UnsupportedConfiguration, |
| 15 | + /// The pins cannot be configured as requested (e.g., reserved pins) |
| 16 | + ConfigurationFailed, |
| 17 | + /// Cannot change pins currently used by another peripheral |
| 18 | + PinInUse, |
| 19 | + /// The interrupt requested cannot be configured |
| 20 | + InterruptConfigurationFailed, |
| 21 | + /// The requested operation is not allowed in the current state |
| 22 | + PermissionDenied, |
| 23 | + /// Hardware failure during operation |
| 24 | + HardwareFailure, |
| 25 | + /// Operation timed out |
| 26 | + Timeout, |
| 27 | + /// The pin is not configured for the requested operation |
| 28 | + /// (e.g., reading output value from input pin) |
| 29 | + InvalidMode, |
| 30 | +} |
| 31 | + |
| 32 | +/// Trait for GPIO errors |
| 33 | +pub trait GpioError: core::fmt::Debug { |
| 34 | + /// Convert error to a generic error kind |
| 35 | + /// |
| 36 | + /// By using this method, errors freely defined by GPIO implementations |
| 37 | + /// can be converted to a set of generic errors upon which generic |
| 38 | + /// code can act. |
| 39 | + fn kind(&self) -> GpioErrorKind; |
| 40 | +} |
| 41 | + |
| 42 | +impl GpioError for core::convert::Infallible { |
| 43 | + fn kind(&self) -> GpioErrorKind { |
| 44 | + match *self {} |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Edge sensitivity for interrupt configuration |
| 49 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 50 | +pub enum EdgeSensitivity { |
| 51 | + /// Trigger on rising edge |
| 52 | + RisingEdge, |
| 53 | + /// Trigger on falling edge |
| 54 | + FallingEdge, |
| 55 | + /// Trigger on both rising and falling edges |
| 56 | + BothEdges, |
| 57 | + /// Trigger on high level |
| 58 | + HighLevel, |
| 59 | + /// Trigger on low level |
| 60 | + LowLevel, |
| 61 | +} |
| 62 | + |
| 63 | +/// Operations for interrupt control |
| 64 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 65 | +pub enum InterruptOperation { |
| 66 | + /// Enable interrupts |
| 67 | + Enable, |
| 68 | + /// Disable interrupts |
| 69 | + Disable, |
| 70 | + /// Clear pending interrupts |
| 71 | + Clear, |
| 72 | + /// Check if interrupts are pending |
| 73 | + IsPending, |
| 74 | +} |
| 75 | + |
| 76 | +/// Trait for types that define an error type for GPIO operations |
| 77 | +pub trait GpioErrorType { |
| 78 | + /// Error type for GPIO operations |
| 79 | + type Error: GpioError; |
| 80 | +} |
| 81 | + |
| 82 | +/// Base trait for GPIO port operations with integrated error handling |
| 83 | +pub trait GpioPort: GpioErrorType { |
| 84 | + /// Configuration type for GPIO pins |
| 85 | + type Config; |
| 86 | + |
| 87 | + /// Configure GPIO pins with specified configuration |
| 88 | + fn configure(&mut self, pins: u32, config: Self::Config) -> Result<(), Self::Error>; |
| 89 | + |
| 90 | + /// Set and clear pins atomically using set and reset masks |
| 91 | + fn set_reset(&mut self, set_mask: u32, reset_mask: u32) -> Result<(), Self::Error>; |
| 92 | + |
| 93 | + /// Read current state of input pins |
| 94 | + fn read_input(&self) -> Result<u32, Self::Error>; |
| 95 | + |
| 96 | + /// Toggle specified output pins |
| 97 | + fn toggle(&mut self, pins: u32) -> Result<(), Self::Error>; |
| 98 | +} |
| 99 | + |
| 100 | +/// Trait for GPIO interrupt capabilities with integrated error handling |
| 101 | +pub trait GpioInterrupt: GpioErrorType { |
| 102 | + /// Configure interrupt sensitivity for specified pins |
| 103 | + fn irq_configure(&mut self, mask: u32, sensitivity: EdgeSensitivity) |
| 104 | + -> Result<(), Self::Error>; |
| 105 | + |
| 106 | + /// Control interrupt operations (enable, disable, etc.) |
| 107 | + fn irq_control( |
| 108 | + &mut self, |
| 109 | + mask: u32, |
| 110 | + operation: InterruptOperation, |
| 111 | + ) -> Result<bool, Self::Error>; |
| 112 | + |
| 113 | + /// Register a callback for interrupt handling |
| 114 | + fn register_interrupt_handler<F>(&mut self, mask: u32, handler: F) -> Result<(), Self::Error> |
| 115 | + where |
| 116 | + F: FnMut(u32) + Send + 'static; |
| 117 | +} |
| 118 | + |
| 119 | +/// Trait for splitting a GPIO port into individual pins |
| 120 | +pub trait SplitPort: GpioPort + Sized { |
| 121 | + /// Container type returned when splitting the port |
| 122 | + type PortPins; |
| 123 | + |
| 124 | + /// Split the port into a container of pins |
| 125 | + fn split(self) -> Self::PortPins; |
| 126 | +} |
| 127 | + |
| 128 | +/// Combined trait for full GPIO functionality |
| 129 | +pub trait GpioController: GpioPort + GpioInterrupt {} |
| 130 | + |
| 131 | +/// Automatically implement GpioController for any type implementing both required traits |
| 132 | +impl<T: GpioPort + GpioInterrupt> GpioController for T {} |
0 commit comments