Aw9523

Struct Aw9523 

Source
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>
where A: AddressMode + Copy, I2C: I2c<A>,

Source

pub fn new(i2c: I2C, addr: A) -> Self

Creates a new AW9523 driver instance.

§Arguments
  • i2c - An I2C bus implementation
  • addr - The I2C address of the device (typically 0x58)
§Example
let gpio = Aw9523::new(i2c, 0x58);
Source

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.

Source

pub fn reset(&mut self) -> Result<(), Aw9523Error<I2C::Error>>

Performs a soft reset of the device.

All registers return to their default values after a reset. You should call init again after reset.

§Errors

Returns an error if the I2C write fails.

Source

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();
Source

pub fn input_gpio(&mut self) -> Result<u16, Aw9523Error<I2C::Error>>

Reads the input state of all 16 GPIO pins at once.

Returns a 16-bit value where each bit represents one pin’s state (1 = high, 0 = low). Bit 0 corresponds to pin 0, bit 15 to pin 15.

§Example
let inputs = gpio.input_gpio().unwrap();
if inputs & AW9523_PIN_5 != 0 {
    // Pin 5 is high
}
Source

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.

Source

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.

Source

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.

Source

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% brightness
Source

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
§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 low
Source

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();
Source

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.

Source

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
§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 driver
Source

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.

Auto Trait Implementations§

§

impl<A, I2C> Freeze for Aw9523<A, I2C>
where I2C: Freeze, A: Freeze,

§

impl<A, I2C> RefUnwindSafe for Aw9523<A, I2C>

§

impl<A, I2C> Send for Aw9523<A, I2C>
where I2C: Send, A: Send,

§

impl<A, I2C> Sync for Aw9523<A, I2C>
where I2C: Sync, A: Sync,

§

impl<A, I2C> Unpin for Aw9523<A, I2C>
where I2C: Unpin, A: Unpin,

§

impl<A, I2C> UnwindSafe for Aw9523<A, I2C>
where I2C: UnwindSafe, A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.