@@ -1,12 +1,16 @@ | |||
#include "PortMCP23S17.h" | |||
/* push() writes data to registerAddr. | |||
/* transfer() writes data to registerAddr, reads portSate from registerAddr, and returns portState. | |||
*/ | |||
void PortMCP23S17::push(const uint8_t command, const uint8_t registerAddr, const uint8_t data) | |||
uint8_t PortMCP23S17::transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data) | |||
{ | |||
uint8_t portState; //bit wise | |||
digitalWrite(SS, LOW); //enable Slave Select | |||
SPI.transfer(command); //write command todo also read command? | |||
SPI.transfer(registerAddr); //register address to write data to | |||
SPI.transfer(data); //write the data | |||
portState = SPI.transfer(data); //write data, read portState | |||
digitalWrite(SS, HIGH); //disable Slave Select | |||
return portState; | |||
} |
@@ -7,6 +7,6 @@ | |||
class PortMCP23S17 | |||
{ | |||
protected: | |||
void push(const uint8_t command, const uint8_t registerAddr, const uint8_t data); | |||
uint8_t transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data); | |||
}; | |||
#endif |
@@ -14,8 +14,8 @@ void PortRead_MCP23S17::begin(const uint8_t strobeOn) | |||
pullUp = 0; | |||
} | |||
push(port.DEVICE_ADDR << 1, port.num, readPins); //write, configure IODIR, 0=output, 1=input | |||
push(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //write, configure GPPU, | |||
transfer(port.DEVICE_ADDR << 1, port.num, readPins); //write, configure IODIR, 0=output, 1=input | |||
transfer(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //write, configure GPPU, | |||
//0=pull-up disabled, 1=pull-up enabled | |||
} | |||
@@ -24,13 +24,5 @@ Only portState bits of readPins are valid. | |||
*/ | |||
uint8_t PortRead_MCP23S17::read() | |||
{ | |||
uint8_t portState; //bit wise | |||
digitalWrite(SS, LOW); //enable Slave Select | |||
SPI.transfer( (port.DEVICE_ADDR << 1) | 1); //read command | |||
SPI.transfer(port.num + 0x12); //GPIO register address to read data from | |||
portState = SPI.transfer(0); //save the data (0 is dummy data to send) | |||
digitalWrite(SS, HIGH); //disable Slave Select | |||
return portState; | |||
return transfer( (port.DEVICE_ADDR << 1) | 1, port.num + 0x12, 0); //read from GPIO | |||
} |
@@ -12,7 +12,7 @@ void PortWrite_MCP23S17::begin() | |||
SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz | |||
//SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device. | |||
push(port.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0) | |||
transfer(port.DEVICE_ADDR << 1, port.num, 0); //configure port direction (port.num) to output (0) | |||
} | |||
/* write() sets pin output to logicLevel. | |||
@@ -31,5 +31,5 @@ void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel) | |||
port.outputVal |= pin; //set pin output to high | |||
} | |||
push(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal | |||
transfer(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port to outputVal | |||
} |