32 #include "driver/spi_master.h" 51 #define MCP_SPI_FREQ 10000000 // it seems to work up to 23000000!! (but datasheet specifies 10000000) 52 #define MCP_DMACHANNEL 2 55 #define MCP_MAXDEVICES 2 82 #define MCP_IODIR 0x00 84 #define MCP_GPINTEN 0x04 85 #define MCP_DEFVAL 0x06 86 #define MCP_INTCON 0x08 87 #define MCP_IOCON 0x0A 90 #define MCP_INTCAP 0x10 96 #define MCP_BNK1_IODIR 0x00 97 #define MCP_BNK1_IPOL 0x01 98 #define MCP_BNK1_GPINTEN 0x02 99 #define MCP_BNK1_DEFVAL 0x03 100 #define MCP_BNK1_INTCON 0x04 101 #define MCP_BNK1_IOCON 0x05 102 #define MCP_BNK1_GPPU 0x06 103 #define MCP_BNK1_INTF 0x07 104 #define MCP_BNK1_INTCAP 0x08 105 #define MCP_BNK1_GPIO 0x09 106 #define MCP_BNK1_OLAT 0x0A 110 #define MCP_IOCON_BANK 0x80 // Controls how the registers are addressed (0 = bank0) 111 #define MCP_IOCON_MIRROR 0x40 // INT Pins Mirror bit (1 = mirrored) 112 #define MCP_IOCON_SEQOP 0x20 // Sequential Operation mode bit (1 = not increment) 113 #define MCP_IOCON_DISSLW 0x10 // Slew Rate control bit for SDA output (I2C only) 114 #define MCP_IOCON_HAEN 0x08 // Hardware Address Enable bit 115 #define MCP_IOCON_ODR 0x04 // Configures the INT pin as an open-drain output (1 = open-drain) 116 #define MCP_IOCON_INTPOL 0x02 // This bit sets the polarity of the INT output pin (1 = active-high) 119 #define MCP_GPIO2REG(basereg, gpio) ((basereg) + ((gpio) >> 3)) 120 #define MCP_GPIO2MASK(gpio) (1 << ((gpio) & 7)) 176 bool begin(
int MISO = -1,
int MOSI = -1,
int CLK = -1,
int CS = -1,
int CSActiveState = -1,
int host = HSPI_HOST);
228 void writeReg(uint8_t addr, uint8_t value, uint8_t hwAddr = 0);
238 uint8_t
readReg(uint8_t addr, uint8_t hwAddr = 0);
247 void writeReg16(uint8_t addr, uint16_t value, uint8_t hwAddr = 0);
257 uint16_t
readReg16(uint8_t addr, uint8_t hwAddr = 0);
303 void setPortDir(
int port, uint8_t value, uint8_t hwAddr = 0) {
writeReg(MCP_IODIR + port, value, hwAddr); }
350 void writePort(
int port, uint8_t value, uint8_t hwAddr = 0) {
writeReg(MCP_OLAT + port, value, hwAddr); }
365 uint8_t
readPort(
int port, uint8_t hwAddr = 0) {
return readReg(MCP_GPIO + port, hwAddr); }
424 void writeGPIO(
int gpio,
bool value, uint8_t hwAddr = 0);
438 bool readGPIO(
int gpio, uint8_t hwAddr = 0);
520 void writePort(
int port,
void const * buffer,
size_t length, uint8_t hwAddr = 0);
536 void readPort(
int port,
void * buffer,
size_t length, uint8_t hwAddr = 0);
541 bool SPIBegin(
int CSActiveState);
548 spi_host_device_t m_SPIHost;
550 spi_device_handle_t m_SPIDevHandle;
553 uint8_t m_IOCON[MCP_MAXDEVICES];
void writePort16(uint16_t value, uint8_t hwAddr=0)
Sets status of output pins of combined port A and B.
bool readGPIO(int gpio, uint8_t hwAddr=0)
Reads input status of a pin.
uint8_t getPortIntCaptured(int port, uint8_t hwAddr=0)
Reads status of input port when last interrupt has been triggered.
void setPortInputPolarity(int port, uint8_t value, uint8_t hwAddr=0)
Sets input polarity.
void writeReg16(uint8_t addr, uint16_t value, uint8_t hwAddr=0)
Writes 16 bit value to two consecutive registers.
void enablePortPullUp(int port, uint8_t value, uint8_t hwAddr=0)
Enables/disables port pull-ups.
uint8_t readReg(uint8_t addr, uint8_t hwAddr=0)
Reads 8 bit value from an internal register.
MCPDir
Represents GPIO directioon.
uint8_t readPort(int port, uint8_t hwAddr=0)
Gets status of input pins of specified port.
bool available()
Determines MCP23S17 availability.
bool begin(int MISO=-1, int MOSI=-1, int CLK=-1, int CS=-1, int CSActiveState=-1, int host=HSPI_HOST)
Initializes MCP23S17 driver.
void writeGPIO(int gpio, bool value, uint8_t hwAddr=0)
Sets output status of a pin.
This file contains some utility classes and functions.
void disableInterrupt(int gpio, uint8_t hwAddr=0)
Disables any interrupt on the specified pin.
void end()
Deinitializes MCP23S17 driver.
uint8_t getPortDir(int port, uint8_t hwAddr=0)
Gets port direction.
void setPortDir(int port, uint8_t value, uint8_t hwAddr=0)
Sets port direction.
void writeReg(uint8_t addr, uint8_t value, uint8_t hwAddr=0)
Writes 8 bit value to an internal register.
void enableINTOpenDrain(bool value, uint8_t hwAddr=0)
Enables/disables the INT pin open-drain.
MCPIntTrigger
Represents interrupt trigger mode.
uint16_t readPort16(uint8_t hwAddr=0)
Gets status of input pins of combined port A and B.
void setINTActiveHigh(bool value, uint8_t hwAddr=0)
Sets the polarity of the INT pins.
void configureGPIO(int gpio, MCPDir dir, bool pullup=false, uint8_t hwAddr=0)
Configure a pin direction and pullup.
void writePort(int port, uint8_t value, uint8_t hwAddr=0)
Sets status of output pins of specified port.
void initDevice(uint8_t hwAddr)
Initializes additional MCP23S17 devices connected to the same SPI bus but with a different hardware a...
void enableINTMirroring(bool value, uint8_t hwAddr=0)
Enables/disables INTs pins mirroring.
uint16_t readReg16(uint8_t addr, uint8_t hwAddr=0)
Reads 16 bit value from two consecutive registers.
uint8_t getPortIntFlags(int port, uint8_t hwAddr=0)
Reads interrupt flags for the specified port.
void enableInterrupt(int gpio, MCPIntTrigger trigger, bool defaultValue=false, uint8_t hwAddr=0)
Enables interrupt on the specific pin.