pub struct Aw9523<A: AddressMode, I2C: I2c<A>> { /* private fields */ }Expand description
AW9523 driver instance
Manages communication with an AW9523 GPIO expander over I2C. The generic parameters allow flexibility in I2C implementation and addressing mode.
Implementations§
Source§impl<A, I2C> Aw9523<A, I2C>
impl<A, I2C> Aw9523<A, I2C>
Sourcepub fn init(&mut self) -> Result<(), Aw9523Error<I2C::Error>>
pub fn init(&mut self) -> Result<(), Aw9523Error<I2C::Error>>
Initializes the AW9523 with a default configuration.
Sets up output values, pin directions, and enables push-pull mode. Call this after creating the device to ensure a known state.
§Errors
Returns an error if any I2C write operation fails.
Sourcepub fn output_gpio(&mut self, pins: u16) -> Result<(), Aw9523Error<I2C::Error>>
pub fn output_gpio(&mut self, pins: u16) -> Result<(), Aw9523Error<I2C::Error>>
Sets the output state for all 16 GPIO pins at once.
Each bit in the 16-bit value represents one pin’s state (1 = high, 0 = low). Bit 0 corresponds to pin 0, bit 15 to pin 15.
§Arguments
pins- 16-bit value where each bit sets the corresponding pin’s output
§Example
// Set pins 0 and 3 high, all others low
gpio.output_gpio(AW9523_PIN_0 | AW9523_PIN_3).unwrap();Sourcepub fn input_gpio(&mut self) -> Result<u16, Aw9523Error<I2C::Error>>
pub fn input_gpio(&mut self) -> Result<u16, Aw9523Error<I2C::Error>>
Sourcepub fn interrupt_enable_gpio(
&mut self,
pins: u16,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn interrupt_enable_gpio( &mut self, pins: u16, ) -> Result<(), Aw9523Error<I2C::Error>>
Enables interrupt detection for all 16 GPIO pins at once.
Each bit in the 16-bit value enables (1) or disables (0) interrupt detection for the corresponding pin.
§Arguments
pins- 16-bit value where each bit enables the corresponding pin’s interrupt
§Note
The AW9523 uses inverted logic internally, but this is handled automatically.
Sourcepub fn configure_direction(
&mut self,
pins: u16,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn configure_direction( &mut self, pins: u16, ) -> Result<(), Aw9523Error<I2C::Error>>
Configures pin direction for all 16 GPIO pins at once.
Each bit in the 16-bit value sets the direction for the corresponding pin: 1 = output, 0 = input.
§Arguments
pins- 16-bit value where each bit sets the corresponding pin as output (1) or input (0)
§Note
The AW9523 uses inverted logic internally, but this is handled automatically.
Sourcepub fn configure_led_mode(
&mut self,
pins: u16,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn configure_led_mode( &mut self, pins: u16, ) -> Result<(), Aw9523Error<I2C::Error>>
Configures LED/constant-current mode for all 16 pins at once.
Each bit in the 16-bit value enables (1) or disables (0) LED mode
for the corresponding pin. When enabled, the pin can be used with
analog_write for LED dimming.
§Arguments
pins- 16-bit value where each bit enables LED mode for the corresponding pin
§Note
The AW9523 uses inverted logic internally, but this is handled automatically.
Sourcepub fn analog_write(
&mut self,
pin: u8,
val: u8,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn analog_write( &mut self, pin: u8, val: u8, ) -> Result<(), Aw9523Error<I2C::Error>>
Sets the LED brightness/current level for a single pin.
The pin must be configured in LED mode first using pin_mode
or configure_led_mode.
§Arguments
pin- Pin number (0-15)val- Brightness level (0 = off, 255 = maximum current)
§Errors
Returns InvalidArgument if the pin number is greater than 15.
§Example
gpio.pin_mode(0, PinMode::LedMode).unwrap();
gpio.analog_write(0, 128).unwrap(); // 50% brightnessSourcepub fn digital_write(
&mut self,
pin: u8,
state: PinState,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn digital_write( &mut self, pin: u8, state: PinState, ) -> Result<(), Aw9523Error<I2C::Error>>
Sets the digital output state for a single pin.
The pin must be configured as an output first using pin_mode
or configure_direction.
§Arguments
pin- Pin number (0-15)state- Output state (HIGH/LOWorPinState::High/PinState::Low)
§Errors
Returns InvalidArgument if the pin number is greater than 15.
§Example
gpio.pin_mode(3, PinMode::Output).unwrap();
gpio.digital_write(3, HIGH).unwrap(); // Set pin 3 high
gpio.digital_write(3, LOW).unwrap(); // Set pin 3 lowSourcepub fn digital_read(&mut self, pin: u8) -> Result<bool, Aw9523Error<I2C::Error>>
pub fn digital_read(&mut self, pin: u8) -> Result<bool, Aw9523Error<I2C::Error>>
Reads the digital input state for a single pin.
The pin must be configured as an input first using pin_mode
or configure_direction.
§Arguments
pin- Pin number (0-15)
§Returns
Returns true if the pin is high, false if low.
§Errors
Returns InvalidArgument if the pin number is greater than 15.
§Example
gpio.pin_mode(8, PinMode::Input).unwrap();
let state = gpio.digital_read(8).unwrap();Sourcepub fn enable_interrupt(
&mut self,
pin: u8,
en: bool,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn enable_interrupt( &mut self, pin: u8, en: bool, ) -> Result<(), Aw9523Error<I2C::Error>>
Enables or disables interrupt detection for a single pin.
§Arguments
pin- Pin number (0-15)en- true to enable interrupt detection, false to disable
§Errors
Returns InvalidArgument if the pin number is greater than 15.
§Note
The AW9523 uses inverted logic internally, but this is handled automatically.
Sourcepub fn pin_mode(
&mut self,
pin: u8,
mode: PinMode,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn pin_mode( &mut self, pin: u8, mode: PinMode, ) -> Result<(), Aw9523Error<I2C::Error>>
Configures the mode for a single pin.
This is the main method for setting up pins. It configures both the direction (input/output) and whether the pin operates as a GPIO or LED driver.
§Arguments
pin- Pin number (0-15)mode- Pin configuration mode:PinMode::Input- Digital inputPinMode::Output- Digital outputPinMode::LedMode- LED driver with current control
§Errors
Returns InvalidArgument if the pin number is greater than 15.
§Example
gpio.pin_mode(0, PinMode::Output).unwrap(); // Digital output
gpio.pin_mode(5, PinMode::Input).unwrap(); // Digital input
gpio.pin_mode(12, PinMode::LedMode).unwrap(); // LED driverSourcepub fn open_drain_port0(
&mut self,
od: bool,
) -> Result<(), Aw9523Error<I2C::Error>>
pub fn open_drain_port0( &mut self, od: bool, ) -> Result<(), Aw9523Error<I2C::Error>>
Configures output mode for all port 0 pins (pins 0-7).
Sets whether port 0 pins use open-drain or push-pull output mode. This affects all 8 pins in port 0 simultaneously.
§Arguments
od- true for open-drain mode, false for push-pull mode
§Note
Port 1 (pins 8-15) always operates in push-pull mode and cannot be changed. The AW9523 uses inverted logic internally, but this is handled automatically.