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 descriptor.

default SCL frequency is 1MHz

Parameters
  • dev – Pointer to I2C device descriptor

  • port – I2C port number

  • addr – I2C address,

  • sda_gpio – SDA GPIO

  • scl_gpio – SCL GPIO

Returns

ESP_OK on success

esp_err_t mcp23008_free_desc(i2c_dev_t *dev)

Free device descriptor.

Parameters

dev – Pointer to I2C device descriptor

Returns

ESP_OK on success

esp_err_t mcp23008_get_int_out_mode(i2c_dev_t *dev, mcp23008_int_out_mode_t *mode)

Get INT pins mode.

Parameters
  • dev – Pointer to I2C device descriptor

  • [out] mode – Buffer to store mode

Returns

ESP_OK on success

esp_err_t mcp23008_set_int_out_mode(i2c_dev_t *dev, mcp23008_int_out_mode_t mode)

Set INT pins mode.

Parameters
  • dev – Pointer to I2C device descriptor

  • mode – INT pins mode

Returns

ESP_OK on success

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

Parameters
  • dev – Pointer to I2C device descriptor

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

Returns

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

Parameters
  • dev – Pointer to I2C device descriptor

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

Returns

ESP_OK on success

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

Parameters
  • dev – Pointer to I2C device descriptor

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

Returns

ESP_OK on success

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

Parameters
  • dev – Pointer to I2C device descriptor

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

Returns

ESP_OK on success

esp_err_t mcp23008_port_read(i2c_dev_t *dev, uint8_t *val)

Read GPIO port value.

Parameters
  • dev – Pointer to I2C device descriptor

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

Returns

ESP_OK on success

esp_err_t mcp23008_port_write(i2c_dev_t *dev, uint8_t val)

Write value to GPIO port.

Parameters
  • dev – Pointer to I2C device descriptor

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

Returns

ESP_OK on success

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

Get GPIO pin mode.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • [out] mode – GPIO pin mode

Returns

ESP_OK on success

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

Set GPIO pin mode.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • mode – GPIO pin mode

Returns

ESP_OK on success

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

Get pullup mode of GPIO pin.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • [out] enable – pullup mode

Returns

ESP_OK on success

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

Set pullup mode of GPIO pin.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • enabletrue to enable pullup

Returns

ESP_OK on success

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

Read GPIO pin level.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • [out] valtrue if pin currently in high state

Returns

ESP_OK on success

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.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • [out] valtrue if pin currently in high state

Returns

ESP_OK on success

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.

Parameters
  • dev – Pointer to I2C device descriptor

  • mask – Pins to setup

  • intr – Interrupt mode

Returns

ESP_OK on success

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

Setup interrupt for GPIO pin.

Parameters
  • dev – Pointer to I2C device descriptor

  • pin – Pin number, 0..7

  • intr – Interrupt mode

Returns

ESP_OK on success