libmcp23016  1.1
Linux userspace driver for Microchip MCP23016 I/O expanders
libmcp23016 Documentation

libmcp23016 provides a simple and straightforward API for accessing Microchip MCP23016 I/O expanders from userspace. It relies on the libgpiod and libi2cd libraries to interface with the Linux kernel.

libmcp23016 development is hosted on GitHub. A code coverage report is also available.

Getting Started

libmcp23016's main data structures and functions are documented in the Main API module. Most uses of the API are straightforward: a MCP23016 device handle is first created by calling mcp23016_open(), which is then passed to supporting functions followed by a call to mcp23016_close() to close the handle and free associated memory. As the MCP23016 lacks a hardware reset, it is advised that a software reset be issued by calling mcp23016_reset() after opening the device handle to ensure the device is in a consistent state. Interrupt output is managed separately to support multiple devices. See the Interrupt Output module for more details.

The following example demonstrates getting the port value from a MCP23016 device at position 0 (I2C slave address 0x20):

#include <stdio.h>
#include <stdlib.h>
#include <mcp23016.h>
int main(void)
{
struct mcp23016_device *dev;
uint16_t port;
/* Open MCP23016 device */
dev = mcp23016_open("/dev/i2c-1", 0);
if (dev == NULL) {
perror(NULL);
return EXIT_FAILURE;
}
/* Reset registers to POR defaults */
if (mcp23016_reset(dev) < 0) {
perror(NULL);
return EXIT_FAILURE;
}
/* Get the port value */
if (mcp23016_get_port(dev, &port) < 0) {
perror(NULL);
return EXIT_FAILURE;
}
printf("port value: %#" PRIx16 "\n", port);
/* Close MCP23016 device handle */
return EXIT_SUCCESS;
}

Care should be taken if the device handle is shared between threads as libmcp23016 is not inherently thread-safe. Calls using the same handle should be restricted to a single thread or synchronized using a mutual exclusion mechanism.

License

libmcp23016 is distributed under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. See COPYING for more details.

mcp23016_device
Handle to a MCP23016 device.
mcp23016_get_port
int mcp23016_get_port(struct mcp23016_device *dev, uint16_t *val)
Get the port value.
mcp23016_reset
int mcp23016_reset(struct mcp23016_device *dev)
Issue a software reset, which resets registers to POR defaults and clears pending interrupts.
mcp23016_open
struct mcp23016_device * mcp23016_open(const char *path, unsigned int num)
Open the MCP23016 device specified by path and num.
mcp23016_close
void mcp23016_close(struct mcp23016_device *dev)
Close a MCP23016 device handle and free associated memory.