mcp23x17 - Driver for I2C/SPI 16 bit GPIO expanders MCP23017/MCP23S17

Warning

This component does not support (yet) ESP8266 RTOS SDK!

group mcp23x17

ESP-IDF driver for I2C/SPI 16 bit GPIO expanders MCP23017/MCP23S17.

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

BSD Licensed as described in the file LICENSE

Defines

MCP23X17_ADDR_BASE
MCP23X17_MAX_SPI_FREQ

Enums

enum mcp23x17_gpio_mode_t

GPIO mode.

Values:

enumerator MCP23X17_GPIO_OUTPUT
enumerator MCP23X17_GPIO_INPUT
enum mcp23x17_int_out_mode_t

INTA/INTB pins mode.

Values:

enumerator MCP23X17_ACTIVE_LOW

Low level on interrupt.

enumerator MCP23X17_ACTIVE_HIGH

High level on interrupt.

enumerator MCP23X17_OPEN_DRAIN

Open drain.

enum mcp23x17_gpio_intr_t

Interrupt mode.

Values:

enumerator MCP23X17_INT_DISABLED

No interrupt.

enumerator MCP23X17_INT_LOW_EDGE

Interrupt on low edge.

enumerator MCP23X17_INT_HIGH_EDGE

Interrupt on high edge.

enumerator MCP23X17_INT_ANY_EDGE

Interrupt on any edge.

Functions

esp_err_t mcp23x17_init_desc_spi(mcp23x17_t *dev, spi_host_device_t host, uint32_t clock_speed_hz, uint8_t addr, gpio_num_t cs_pin)

Initialize device descriptior.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • host: SPI host

  • clock_speed_hz: SPI clock speed, Hz (max MCP23X17_MAX_SPI_FREQ)

  • addr: Device address

  • cs_pin: CS pin

esp_err_t mcp23x17_free_desc_spi(mcp23x17_t *dev)

Free device descriptor.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

esp_err_t mcp23x17_get_int_out_mode(mcp23x17_t *dev, mcp23x17_int_out_mode_t *mode)

Get INTA/INTB pins mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • [out] mode: Buffer to store mode

esp_err_t mcp23x17_set_int_out_mode(mcp23x17_t *dev, mcp23x17_int_out_mode_t mode)

Set INTA/INTB pins mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • mode: INTA/INTB pins mode

esp_err_t mcp23x17_port_get_mode(mcp23x17_t *dev, uint16_t *val)

Get GPIO pins mode.

0 - output, 1 - input for each bit in val

Return

Parameters
  • dev: Pointer to device descriptor

  • [out] val: Buffer to store mode, 0 bit for PORTA/GPIO0..15 bit for PORTB/GPIO7

esp_err_t mcp23x17_port_set_mode(mcp23x17_t *dev, uint16_t val)

Set GPIO pins mode.

0 - output, 1 - input for each bit in val

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • val: Mode, 0 bit for PORTA/GPIO0..15 bit for PORTB/GPIO7

esp_err_t mcp23x17_port_get_pullup(mcp23x17_t *dev, uint16_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 device descriptor

  • [out] val: Pullup status, 0 bit for PORTA/GPIO0..15 bit for PORTB/GPIO7

esp_err_t mcp23x17_port_set_pullup(mcp23x17_t *dev, uint16_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 device descriptor

  • val: Pullup status, 0 bit for PORTA/GPIO0..15 bit for PORTB/GPIO7

esp_err_t mcp23x17_port_read(mcp23x17_t *dev, uint16_t *val)

Read GPIO port value.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • [out] val: 16-bit GPIO port value, 0 bit for PORTA/GPIO0..15 bit for PORTB/GPIO7

esp_err_t mcp23x17_port_write(mcp23x17_t *dev, uint16_t val)

Write value to GPIO port.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • val: GPIO port value, 0 bit for PORTA/GPIO0..15 bit for PORTB/GPIO7

esp_err_t mcp23x17_get_mode(mcp23x17_t *dev, uint8_t pin, mcp23x17_gpio_mode_t *mode)

Get GPIO pin mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

  • [out] mode: GPIO pin mode

esp_err_t mcp23x17_set_mode(mcp23x17_t *dev, uint8_t pin, mcp23x17_gpio_mode_t mode)

Set GPIO pin mode.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

  • mode: GPIO pin mode

esp_err_t mcp23x17_get_pullup(mcp23x17_t *dev, uint8_t pin, bool *enable)

Get pullup mode of GPIO pin.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

  • [out] enable: pullup mode

esp_err_t mcp23x17_set_pullup(mcp23x17_t *dev, uint8_t pin, bool enable)

Set pullup mode of GPIO pin.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

  • enable: true to enable pullup

esp_err_t mcp23x17_get_level(mcp23x17_t *dev, uint8_t pin, uint32_t *val)

Read GPIO pin level.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

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

esp_err_t mcp23x17_set_level(mcp23x17_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 device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

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

esp_err_t mcp23x17_port_set_interrupt(mcp23x17_t *dev, uint16_t mask, mcp23x17_gpio_intr_t intr)

Setup interrupt for group of GPIO pins.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • mask: Pins to setup

  • intr: Interrupt mode

esp_err_t mcp23x17_set_interrupt(mcp23x17_t *dev, uint8_t pin, mcp23x17_gpio_intr_t intr)

Setup interrupt for GPIO pin.

Return

ESP_OK on success

Parameters
  • dev: Pointer to device descriptor

  • pin: Pin number, 0 for PORTA/GPIO0..15 for PORTB/GPIO7

  • intr: Interrupt mode

struct mcp23x17_t
#include <mcp23x17.h>