libmcp23s17  0.3.0
A simple static C library for the MCP23S17 port expander.
 All Files Functions
Macros | Functions
mcp23s17.h File Reference

A simple static library for controlling an MCP23S17 port expander over SPI. More...

#include <stdint.h>

Go to the source code of this file.

Macros

#define WRITE_CMD   0
#define READ_CMD   1
#define IODIRA   0x00
#define IODIRB   0x01
#define IPOLA   0x02
#define IPOLB   0x03
#define GPINTENA   0x04
#define GPINTENB   0x05
#define DEFVALA   0x06
#define DEFVALB   0x07
#define INTCONA   0x08
#define INTCONB   0x09
#define IOCON   0x0A
#define GPPUA   0x0C
#define GPPUB   0x0D
#define INTFA   0x0E
#define INTFB   0x0F
#define INTCAPA   0x10
#define INTCAPB   0x11
#define GPIOA   0x12
#define GPIOB   0x13
#define OLATA   0x14
#define OLATB   0x15
#define BANK_OFF   0x00
#define BANK_ON   0x80
#define INT_MIRROR_ON   0x40
#define INT_MIRROR_OFF   0x00
#define SEQOP_OFF   0x20
#define SEQOP_ON   0x00
#define DISSLW_ON   0x10
#define DISSLW_OFF   0x00
#define HAEN_ON   0x08
#define HAEN_OFF   0x00
#define ODR_ON   0x04
#define ODR_OFF   0x00
#define INTPOL_HIGH   0x02
#define INTPOL_LOW   0x00
#define GPIO_INTERRUPT_PIN   25

Functions

int mcp23s17_open (int bus, int chip_select)
uint8_t mcp23s17_read_reg (uint8_t reg, uint8_t hw_addr, int fd)
void mcp23s17_write_reg (uint8_t data, uint8_t reg, uint8_t hw_addr, int fd)
uint8_t mcp23s17_read_bit (uint8_t bit_num, uint8_t reg, uint8_t hw_addr, int fd)
void mcp23s17_write_bit (uint8_t data, uint8_t bit_num, uint8_t reg, uint8_t hw_addr, int fd)
int mcp23s17_enable_interrupts ()
int mcp23s17_disable_interrupts ()
int mcp23s17_wait_for_interrupt (int timeout)

Detailed Description

A simple static library for controlling an MCP23S17 port expander over SPI.

Datasheet: http://ww1.microchip.com/downloads/en/devicedoc/21952b.pdf

Copyright (C) 2013 Thomas Preston thoma.nosp@m.s.pr.nosp@m.eston.nosp@m.@ope.nosp@m.nlx.o.nosp@m.rg.u.nosp@m.k

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Function Documentation

int mcp23s17_disable_interrupts ( )

Disables interrupts and exports to the GPIO connection from the mcp23s17.

Returns
int 0 on success
int mcp23s17_enable_interrupts ( )

Enables interrupts and exports to the GPIO connection from the mcp23s17.

Returns
int 0 on success
int mcp23s17_open ( int  bus,
int  chip_select 
)

Returns a file descriptor for the SPI device through which the MCP23S17 port expander can be accessed.

Parameters
busThe SPI bus.
chip_selectThe SPI chip select.
uint8_t mcp23s17_read_bit ( uint8_t  bit_num,
uint8_t  reg,
uint8_t  hw_addr,
int  fd 
)

Reads a single bit from the register specified. Must also specify which hardware address and file descriptor to use.

Parameters
bit_numThe bit number to read.
regThe register to read from (example: IODIRA, GPIOA).
hw_addrThe hardware address of the MCP23S17.
fdThe file descriptor returned from <mcp23s17_open>"()".
uint8_t mcp23s17_read_reg ( uint8_t  reg,
uint8_t  hw_addr,
int  fd 
)

Returns the 8 bit value from the register specified. Must also specify which hardware address and file descriptor to use.

Parameters
regThe register to read from (example: IODIRA, GPIOA).
hw_addrThe hardware address of the MCP23S17.
fdThe file descriptor returned from <mcp23s17_open>"()".
int mcp23s17_wait_for_interrupt ( int  timeout)

Waits for an interrupt from the mcp23s17 or until timeout is reached.

Note
This method does NOT reset the interrupt - which is done automatically for you by reading the input state register. Calling this method twice in a row without reading the input register will cause it to always wait for your timeout value, regardless of button presses. To avoid this, read the input register after every call to this method.
Parameters
timeoutMaximum ms to wait for input, -1 for forever
Returns
the number of file descriptors ready for the requested I/O, zero if no file descriptor became ready during the requested timeout milliseconds, or -1 on error.
void mcp23s17_write_bit ( uint8_t  data,
uint8_t  bit_num,
uint8_t  reg,
uint8_t  hw_addr,
int  fd 
)

Writes a single bit to the register specified. Must also specify which hardware address and file descriptor to use.

Parameters
dataThe data to write.
bit_numThe bit number to write to.
regThe register to write to (example: IODIRA, GPIOA).
hw_addrThe hardware address of the MCP23S17.
fdThe file descriptor returned from <mcp23s17_open>"()".
void mcp23s17_write_reg ( uint8_t  data,
uint8_t  reg,
uint8_t  hw_addr,
int  fd 
)

Writes an 8 bit value to the register specified. Must also specify which hardware address and file descriptor to use.

Parameters
dataThe data byte to be written.
regThe register to write to (example: IODIRA, GPIOA).
hw_addrThe hardware address of the MCP23S17.
fdThe file descriptor returned from <mcp23s17_open>"()".