#include "PortMCP23S17.h" | #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 | digitalWrite(SS, LOW); //enable Slave Select | ||||
SPI.transfer(command); //write command todo also read command? | SPI.transfer(command); //write command todo also read command? | ||||
SPI.transfer(registerAddr); //register address to write data to | 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 | digitalWrite(SS, HIGH); //disable Slave Select | ||||
return portState; | |||||
} | } |
class PortMCP23S17 | class PortMCP23S17 | ||||
{ | { | ||||
protected: | 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 | #endif |
pullUp = 0; | 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 | //0=pull-up disabled, 1=pull-up enabled | ||||
} | } | ||||
*/ | */ | ||||
uint8_t PortRead_MCP23S17::read() | 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 | |||||
} | } |
SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz | 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. | //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. | /* write() sets pin output to logicLevel. | ||||
port.outputVal |= pin; //set pin output to high | 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 | |||||
} | } |