mcp23008 - Driver for 8-bit I2C GPIO expander MCP23008

group mcp23008

ESP-IDF driver for I2C 8 bit GPIO expander MCP23008.

Copyright (C) 2018 Ruslan V. Uss https://github.com/UncleRus

BSD Licensed as described in the file LICENSE

Defines

MCP23008_I2C_ADDR_BASE

Enums

enum mcp23008_gpio_mode_t

GPIO mode.

Values:

enumerator MCP23008_GPIO_OUTPUT
enumerator MCP23008_GPIO_INPUT
enum mcp23008_int_out_mode_t

INTA/INTB pins mode.

Values:

enumerator MCP23008_ACTIVE_LOW

Low level on interrupt.

enumerator MCP23008_ACTIVE_HIGH

High level on interrupt.

enumerator MCP23008_OPEN_DRAIN

Open drain.

enum mcp23008_gpio_intr_t

Interrupt mode.

Values:

enumerator MCP23008_INT_DISABLED

No interrupt.

enumerator MCP23008_INT_LOW_EDGE

Interrupt on low edge.

enumerator MCP23008_INT_HIGH_EDGE

Interrupt on high edge.

enumerator MCP23008_INT_ANY_EDGE

Interrupt on any edge.

Functions

esp_err_t mcp23008_init_desc(i2c_dev_t *dev, i2c_port_t port, uint8_t addr, gpio_num_t sda_gpio, gpio_num_t scl_gpio)

Initialize device descriptior SCL frequency is 1MHz.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • port: I2C port number

  • addr: I2C address,

  • sda_gpio: SDA GPIO

  • scl_gpio: SCL GPIO

esp_err_t mcp23008_free_desc(i2c_dev_t *dev)

Free device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

esp_err_t mcp23008_get_int_out_mode(i2c_dev_t *dev, mcp23008_int_out_mode_t *mode)

Get INT pins mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • [out] mode: Buffer to store mode

esp_err_t mcp23008_set_int_out_mode(i2c_dev_t *dev, mcp23008_int_out_mode_t mode)

Set INT pins mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • mode: INT pins mode

esp_err_t mcp23008_port_get_mode(i2c_dev_t *dev, uint8_t *val)

Get GPIO pins mode 0 - output, 1 - input for each bit in val

Return

Parameters
  • dev: Pointer to I2C device descriptor

  • [out] val: Buffer to store mode, 0 bit for GPIO0..7 bit for GPIO7

esp_err_t mcp23008_port_set_mode(i2c_dev_t *dev, uint8_t val)

Set GPIO pins mode 0 - output, 1 - input for each bit in val

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • val: Mode, 0 bit for GPIO0..7 bit for GPIO7

esp_err_t mcp23008_port_get_pullup(i2c_dev_t *dev, uint8_t *val)

Get GPIO pullups status 0 - pullup disabled, 1 - pullup enabled for each bit in val

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • [out] val: Pullup status, 0 bit for GPIO0..7 bit for GPIO7

esp_err_t mcp23008_port_set_pullup(i2c_dev_t *dev, uint8_t val)

Set GPIO pullups status 0 - pullup disabled, 1 - pullup enabled for each bit in val

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • val: Pullup status, 0 bit for GPIO0..7 bit for GPIO7

esp_err_t mcp23008_port_read(i2c_dev_t *dev, uint8_t *val)

Read GPIO port value.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • [out] val: 8-bit GPIO port value, 0 bit for GPIO0..7 bit for GPIO7

esp_err_t mcp23008_port_write(i2c_dev_t *dev, uint8_t val)

Write value to GPIO port.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • val: GPIO port value, 0 bit for GPIO0..7 bit for GPIO7

esp_err_t mcp23008_get_mode(i2c_dev_t *dev, uint8_t pin, mcp23008_gpio_mode_t *mode)

Get GPIO pin mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • [out] mode: GPIO pin mode

esp_err_t mcp23008_set_mode(i2c_dev_t *dev, uint8_t pin, mcp23008_gpio_mode_t mode)

Set GPIO pin mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • mode: GPIO pin mode

esp_err_t mcp23008_get_pullup(i2c_dev_t *dev, uint8_t pin, bool *enable)

Get pullup mode of GPIO pin.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • [out] enable: pullup mode

esp_err_t mcp23008_set_pullup(i2c_dev_t *dev, uint8_t pin, bool enable)

Set pullup mode of GPIO pin.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • enable: true to enable pullup

esp_err_t mcp23008_get_level(i2c_dev_t *dev, uint8_t pin, uint32_t *val)

Read GPIO pin level.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • [out] val: true if pin currently in high state

esp_err_t mcp23008_set_level(i2c_dev_t *dev, uint8_t pin, uint32_t val)

Set GPIO pin level Pin must be set up as output.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • [out] val: true if pin currently in high state

esp_err_t mcp23008_port_set_interrupt(i2c_dev_t *dev, uint8_t mask, mcp23008_gpio_intr_t intr)

Setup interrupt for group of GPIO pins.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • mask: Pins to setup

  • intr: Interrupt mode

esp_err_t mcp23008_set_interrupt(i2c_dev_t *dev, uint8_t pin, mcp23008_gpio_intr_t intr)

Setup interrupt for GPIO pin.

Return

ESP_OK on success

Parameters
  • dev: Pointer to I2C device descriptor

  • pin: Pin number, 0..7

  • intr: Interrupt mode