AW9523 0.0.3
AW9523 GPIO expander component
Loading...
Searching...
No Matches
aw9523.h File Reference

AW9523 GPIO expander component. More...

#include <stdbool.h>
#include <stdint.h>
#include "driver/i2c_types.h"
#include "esp_err.h"

Go to the source code of this file.

Macros

#define AW9523_ID   0x23
 ID returned by the AW9523 GPIO expander when reading from the AW9523_REG_ID register.

Typedefs

typedef bool aw9523_pin_data_digital_t
 GPIO digital pin data to read/write.
typedef uint8_t aw9523_pin_num_t
 Pin number, ranging from 0 to 15 inclusive.
typedef aw9523_pin_data_digital_t aw9523_pins_data_digital_t[16]
 GPIO digital pin data to read/write for all 16 pins.
typedef aw9523_pin_interrupt_mode_t aw9523_pins_interrupt_mode_t[16]
 Interrupt mode for all 16 GPIO input pins.
typedef aw9523_pin_mode_t aw9523_pins_mode_t[16]
 Pin mode of all 16 pins.
typedef uint8_t aw9523_reg_value_t
 Register value read from and/or to write to the AW9523 GPIO expander.
typedef i2c_master_dev_handle_t aw9523_t
 AW9523 GPIO expander's I2C device handle.

Enumerations

enum  aw9523_gpio_output_mode_t { AW9523_GPIO_OUTPUT_MODE_OPEN_DRAIN = 0b0 , AW9523_GPIO_OUTPUT_MODE_PUSH_PULL = 0b1 }
 GPIO output mode for all port 0 pins (pins 0 to 7). More...
enum  aw9523_i2c_addr_t { AW9523_I2C_ADDR_AD0_GND_AD1_GND = 0x58 , AW9523_I2C_ADDR_AD0_VCC_AD1_GND = 0x59 , AW9523_I2C_ADDR_AD0_GND_AD1_VCC = 0x5A , AW9523_I2C_ADDR_AD0_VCC_AD1_VCC = 0x5B }
 I2C address of the AW9523 GPIO expander. More...
enum  aw9523_led_max_current_t { AW9523_LED_MAX_mA_37 = 0x0 , AW9523_LED_MAX_mA_27_75 = 0x1 , AW9523_LED_MAX_mA_18_5 = 0x2 , AW9523_LED_MAX_mA_9_25 = 0x3 }
 Maximum current for all LED output pins. More...
enum  aw9523_pin_interrupt_mode_t { AW9523_PIN_INTERRUPT_MODE_ENABLE = 0b0 , AW9523_PIN_INTERRUPT_MODE_DISABLE = 0b1 }
 Interrupt mode on/off for a GPIO input pin. More...
enum  aw9523_pin_mode_t { AW9523_PIN_GPIO_OUTPUT = 0 , AW9523_PIN_GPIO_INPUT = 1 , AW9523_PIN_LED = 2 }
 Pin mode of a specific pin. More...
enum  aw9523_reg_addr_t {
  AW9523_REG_GPIO_INPUT_P0 = 0x00 , AW9523_REG_GPIO_INPUT_P1 = 0x01 , AW9523_REG_GPIO_OUTPUT_P0 = 0x02 , AW9523_REG_GPIO_OUTPUT_P1 = 0x03 ,
  AW9523_REG_GPIO_DIR_P0 = 0x04 , AW9523_REG_GPIO_DIR_P1 = 0x05 , AW9523_REG_GPIO_INTERRUPT_P0 = 0x06 , AW9523_REG_GPIO_INTERRUPT_P1 = 0x07 ,
  AW9523_REG_ID = 0x10 , AW9523_REG_CONTROL = 0x11 , AW9523_REG_MODE_P0 = 0x12 , AW9523_REG_MODE_P1 = 0x13 ,
  AW9523_REG_SOFT_RESET = 0x7F
}
 Register address constants to read from and/or write to for the AW9523 GPIO expander. More...

Functions

esp_err_t aw9523_destroy (aw9523_t *dev)
 Soft reset and remove the I2C instance of the AW9523 GPIO expander.
esp_err_t aw9523_gpio_read_pin (const aw9523_t *dev, const aw9523_pin_num_t pin, const aw9523_pin_mode_t pin_mode, aw9523_pin_data_digital_t *data)
 Read digital pin data from a specific GPIO pin.
esp_err_t aw9523_gpio_read_pins (const aw9523_t *dev, aw9523_pins_data_digital_t *pins_data)
 Read all GPIO pins.
esp_err_t aw9523_gpio_write_pin (const aw9523_t *dev, const aw9523_pin_num_t pin, const aw9523_pin_data_digital_t data)
 Write digital pin data to specific GPIO output pin.
esp_err_t aw9523_gpio_write_pins (const aw9523_t *dev, const aw9523_pins_data_digital_t *data)
 Write digital pin data to all GPIO output pins.
esp_err_t aw9523_init (const i2c_master_bus_handle_t *bus_handle, const aw9523_i2c_addr_t address, aw9523_t *dev)
 Initialise the AW9523 GPIO expander.
esp_err_t aw9523_read_reg (const aw9523_t *dev, const aw9523_reg_addr_t reg, aw9523_reg_value_t *value)
 Read the specified register from the AW9523 GPIO expander.
esp_err_t aw9523_set_gpio_interrupt_pin (const aw9523_t *dev, const aw9523_pin_num_t pin, const aw9523_pin_interrupt_mode_t pin_interrupt_mode)
 Set interrupt pin mode for specific GPIO input pin.
esp_err_t aw9523_set_gpio_interrupt_pins (const aw9523_t *dev, const aw9523_pins_interrupt_mode_t *pins_interrupt_mode)
 Set interrupt pin mode for all GPIO input pins.
esp_err_t aw9523_set_gpio_output_mode_p0 (const aw9523_t *dev, const aw9523_gpio_output_mode_t gpio_output_mode)
 Set GPIO output mode of all pins in port 0 (pins 0 to 7).
esp_err_t aw9523_set_led_brightness (const aw9523_t *dev, const aw9523_pin_num_t pin, const aw9523_reg_value_t brightness)
 Set LED brightness of specific LED pin in LED mode.
esp_err_t aw9523_set_led_max_current (const aw9523_t *dev, const aw9523_led_max_current_t max_current)
 Set max current output of LED pins in LED mode.
esp_err_t aw9523_set_pin (const aw9523_t *dev, const aw9523_pin_num_t pin, const aw9523_pin_mode_t pin_mode)
 Set pin mode of a specific pin.
esp_err_t aw9523_set_pins (const aw9523_t *dev, const aw9523_pins_mode_t *pins_mode)
 Set pin mode of all 16 pins.
esp_err_t aw9523_soft_reset (const aw9523_t *dev)
 Soft reset the AW9523 GPIO expander.
esp_err_t aw9523_write_reg (const aw9523_t *dev, const aw9523_reg_addr_t reg, const aw9523_reg_value_t value)
 Write the specified register to the AW9523 GPIO expander.

Detailed Description

AW9523 GPIO expander component.

Author
Yik Jin (@yikjin) yikji.nosp@m.n@nu.nosp@m.shack.nosp@m.ers..nosp@m.org
Version
0.0.3
Date
2026-01-05

This component simplifies communication with the AW9523 GPIO expander module, and abstracts away having to deal with the 2 physical ports (P0 and P1) of 8 pins (Pn_0..7) each in the device, representing them as 16 virtual pins instead (0..15) for ease of use.

Enumeration Type Documentation

◆ aw9523_gpio_output_mode_t

GPIO output mode for all port 0 pins (pins 0 to 7).

Enumerator
AW9523_GPIO_OUTPUT_MODE_OPEN_DRAIN 0b0 

Open drain GPIO output mode

AW9523_GPIO_OUTPUT_MODE_PUSH_PULL 0b1 

Push pull GPIO output mode

◆ aw9523_i2c_addr_t

I2C address of the AW9523 GPIO expander.

Enumerator
AW9523_I2C_ADDR_AD0_GND_AD1_GND 0x58 

AD0 and AD1 connected to GND

AW9523_I2C_ADDR_AD0_VCC_AD1_GND 0x59 

AD0 connected to VCC, and AD1 connected to GND

AW9523_I2C_ADDR_AD0_GND_AD1_VCC 0x5A 

AD0 connected to GND, and AD1 connected to VCC

AW9523_I2C_ADDR_AD0_VCC_AD1_VCC 0x5B 

AD0 and AD1 connected to VCC

◆ aw9523_led_max_current_t

Maximum current for all LED output pins.

Enumerator
AW9523_LED_MAX_mA_37 0x0 

37 mA max current for all LED output pins

AW9523_LED_MAX_mA_27_75 0x1 

27.75 mA max current for all LED output pins

AW9523_LED_MAX_mA_18_5 0x2 

18.5 mA max current for all LED output pins

AW9523_LED_MAX_mA_9_25 0x3 

9.25 mA max current for all LED output pins

◆ aw9523_pin_interrupt_mode_t

Interrupt mode on/off for a GPIO input pin.

Enumerator
AW9523_PIN_INTERRUPT_MODE_ENABLE 0b0 

Enable interrupt for GPIO input pin

AW9523_PIN_INTERRUPT_MODE_DISABLE 0b1 

Disable interrupt for GPIO input pin

◆ aw9523_pin_mode_t

Pin mode of a specific pin.

Enumerator
AW9523_PIN_GPIO_OUTPUT 

GPIO output pin mode

AW9523_PIN_GPIO_INPUT 

GPIO input pin mode

AW9523_PIN_LED 

LED pin mode

◆ aw9523_reg_addr_t

Register address constants to read from and/or write to for the AW9523 GPIO expander.

Enumerator
AW9523_REG_GPIO_INPUT_P0 0x00 

GPIO pins' input values for port 0 (pins 0 to 7) (read-only)

AW9523_REG_GPIO_INPUT_P1 0x01 

GPIO pins' input values for port 1 (pins 8 to 15) (read-only)

AW9523_REG_GPIO_OUTPUT_P0 0x02 

GPIO pins' output values for port 0 (pins 0 to 7) (read/write)

AW9523_REG_GPIO_OUTPUT_P1 0x03 

GPIO pins' output values for port 1 (pins 8 to 15) (read/write)

AW9523_REG_GPIO_DIR_P0 0x04 

GPIO pins' direction (input/output) for port 0 (pins 0 to 7) (read/write)

AW9523_REG_GPIO_DIR_P1 0x05 

GPIO pins' direction (input/output) for port 1 (pins 8 to 15) (read/write)

AW9523_REG_GPIO_INTERRUPT_P0 0x06 

GPIO input pins' interrupt status for port 0 (pins 0 to 7) (read/write)

AW9523_REG_GPIO_INTERRUPT_P1 0x07 

GPIO input pins' interrupt status for port 1 (pins 8 to 15) (read/write)

AW9523_REG_ID 0x10 

Constant ID value (read-only)

AW9523_REG_CONTROL 0x11 

Control register (read/write)

AW9523_REG_MODE_P0 0x12 

GPIO or LED pins mode for port 0 (pins 0 to 7) (read/write)

AW9523_REG_MODE_P1 0x13 

GPIO or LED pins mode for port 1 (pins 8 to 15) (read/write)

AW9523_REG_SOFT_RESET 0x7F 

Soft reset to default state (write-only)

Function Documentation

◆ aw9523_destroy()

esp_err_t aw9523_destroy ( aw9523_t * dev)

Soft reset and remove the I2C instance of the AW9523 GPIO expander.

Parameters
[in,out]devI2C device handle of the AW9523 GPIO expander
Returns
esp_err_t ESP error constants

◆ aw9523_gpio_read_pin()

esp_err_t aw9523_gpio_read_pin ( const aw9523_t * dev,
const aw9523_pin_num_t pin,
const aw9523_pin_mode_t pin_mode,
aw9523_pin_data_digital_t * data )

Read digital pin data from a specific GPIO pin.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pinPin number to read from
[in]pin_modeIf the specified pin is in GPIO input or output mode
[out]dataGPIO digital pin data read from the pin number
Returns
esp_err_t ESP error constants

◆ aw9523_gpio_read_pins()

esp_err_t aw9523_gpio_read_pins ( const aw9523_t * dev,
aw9523_pins_data_digital_t * pins_data )

Read all GPIO pins.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[out]pins_dataPin mode read from all 16 pins
Returns
esp_err_t ESP error constants

◆ aw9523_gpio_write_pin()

esp_err_t aw9523_gpio_write_pin ( const aw9523_t * dev,
const aw9523_pin_num_t pin,
const aw9523_pin_data_digital_t data )

Write digital pin data to specific GPIO output pin.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pinGPIO output pin number to write to
[in]dataDigital data to write to the specified GPIO output pin
Returns
esp_err_t ESP error constants

◆ aw9523_gpio_write_pins()

esp_err_t aw9523_gpio_write_pins ( const aw9523_t * dev,
const aw9523_pins_data_digital_t * data )

Write digital pin data to all GPIO output pins.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]dataDigital data to write to each GPIO output pin
Returns
esp_err_t ESP error constants

◆ aw9523_init()

esp_err_t aw9523_init ( const i2c_master_bus_handle_t * bus_handle,
const aw9523_i2c_addr_t address,
aw9523_t * dev )

Initialise the AW9523 GPIO expander.

Parameters
[in]bus_handleI2C master bus handle
[in]addressI2C address of the AW9523 GPIO expander
[out]devI2C device handle of the AW9523 GPIO expander
Returns
esp_err_t ESP error constants

◆ aw9523_read_reg()

esp_err_t aw9523_read_reg ( const aw9523_t * dev,
const aw9523_reg_addr_t reg,
aw9523_reg_value_t * value )

Read the specified register from the AW9523 GPIO expander.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]regRegister address to read from
[out]valueValue read from the specified register
Returns
esp_err_t ESP error constants

◆ aw9523_set_gpio_interrupt_pin()

esp_err_t aw9523_set_gpio_interrupt_pin ( const aw9523_t * dev,
const aw9523_pin_num_t pin,
const aw9523_pin_interrupt_mode_t pin_interrupt_mode )

Set interrupt pin mode for specific GPIO input pin.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pinGPIO input pin to set interrupt pin mode of
[in]pin_interrupt_modeInterrupt mode to set the specified pin to
Returns
esp_err_t ESP error constants

◆ aw9523_set_gpio_interrupt_pins()

esp_err_t aw9523_set_gpio_interrupt_pins ( const aw9523_t * dev,
const aw9523_pins_interrupt_mode_t * pins_interrupt_mode )

Set interrupt pin mode for all GPIO input pins.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pins_interrupt_modeInterrupt mode to set for each GPIO input pin
Returns
esp_err_t ESP error constants

◆ aw9523_set_gpio_output_mode_p0()

esp_err_t aw9523_set_gpio_output_mode_p0 ( const aw9523_t * dev,
const aw9523_gpio_output_mode_t gpio_output_mode )

Set GPIO output mode of all pins in port 0 (pins 0 to 7).

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]gpio_output_modeGPIO output mode to set all of pins 0 to 7 to
Returns
esp_err_t ESP error constants

◆ aw9523_set_led_brightness()

esp_err_t aw9523_set_led_brightness ( const aw9523_t * dev,
const aw9523_pin_num_t pin,
const aw9523_reg_value_t brightness )

Set LED brightness of specific LED pin in LED mode.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pinLED pin number to set LED brightness of
[in]brightnessLED brightness value from 0 (0x00) to 255 (0xFF)
Returns
esp_err_t ESP error constants

◆ aw9523_set_led_max_current()

esp_err_t aw9523_set_led_max_current ( const aw9523_t * dev,
const aw9523_led_max_current_t max_current )

Set max current output of LED pins in LED mode.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]max_currentMax current output of LED pins
Returns
esp_err_t ESP error constants

◆ aw9523_set_pin()

esp_err_t aw9523_set_pin ( const aw9523_t * dev,
const aw9523_pin_num_t pin,
const aw9523_pin_mode_t pin_mode )

Set pin mode of a specific pin.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pinPin number to update the pin mode of
[in]pin_modeNew pin mode for the specified pin number
Returns
esp_err_t ESP error constants

◆ aw9523_set_pins()

esp_err_t aw9523_set_pins ( const aw9523_t * dev,
const aw9523_pins_mode_t * pins_mode )

Set pin mode of all 16 pins.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]pins_modePin mode of all 16 pins
Returns
esp_err_t ESP error constants

◆ aw9523_soft_reset()

esp_err_t aw9523_soft_reset ( const aw9523_t * dev)

Soft reset the AW9523 GPIO expander.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
Returns
esp_err_t ESP error constants

◆ aw9523_write_reg()

esp_err_t aw9523_write_reg ( const aw9523_t * dev,
const aw9523_reg_addr_t reg,
const aw9523_reg_value_t value )

Write the specified register to the AW9523 GPIO expander.

Parameters
[in]devI2C device handle of the AW9523 GPIO expander
[in]regRegister address to write to
[in]valueValue to write to the specified register
Returns
esp_err_t ESP error constants