2016-09-11 22:45:52 +00:00
|
|
|
#include "PortMCP23S17.h"
|
|
|
|
|
2016-09-11 23:03:00 +00:00
|
|
|
/* transfer() writes data to registerAddr, reads portSate from registerAddr, and returns portState.
|
2016-09-11 22:45:52 +00:00
|
|
|
*/
|
2016-09-11 23:03:00 +00:00
|
|
|
uint8_t PortMCP23S17::transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data)
|
2016-09-11 22:45:52 +00:00
|
|
|
{
|
2016-09-17 21:47:37 +00:00
|
|
|
uint8_t portState; //bit pattern
|
2016-09-11 23:03:00 +00:00
|
|
|
|
2016-09-11 22:45:52 +00:00
|
|
|
digitalWrite(SS, LOW); //enable Slave Select
|
2016-09-12 06:28:27 +00:00
|
|
|
SPI.transfer(command); //write or read command
|
2016-09-11 22:45:52 +00:00
|
|
|
SPI.transfer(registerAddr); //register address to write data to
|
2016-09-11 23:03:00 +00:00
|
|
|
portState = SPI.transfer(data); //write data, read portState
|
2016-09-11 22:45:52 +00:00
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
2016-09-11 23:03:00 +00:00
|
|
|
|
|
|
|
return portState;
|
2016-09-11 22:45:52 +00:00
|
|
|
}
|
2016-09-23 07:13:12 +00:00
|
|
|
|
|
|
|
/* begin() is called from Scanner_IOE::begin().
|
|
|
|
Initiates SPI bus and configures I/O pins for read and write.
|
|
|
|
strobeOn is logic level of strobe on, HIGH or LOW
|
|
|
|
|
|
|
|
MCP23S17 SPI interface is 10 MHz max.
|
|
|
|
The electrical limitation to bus speed is bus capacitance and the length of the wires involved.
|
|
|
|
Longer wires require lower clock speeds.
|
2016-09-23 20:52:09 +00:00
|
|
|
|
|
|
|
begin() is called from Scanner_IOE::begin() twice, once each for refPortWrite and refPortRead.
|
|
|
|
The first 4 lines only need to be called once, but seem to work OK if called a second time.
|
2016-09-23 07:13:12 +00:00
|
|
|
*/
|
|
|
|
void PortMCP23S17::begin(const uint8_t strobeOn)
|
|
|
|
{
|
|
|
|
uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
|
|
|
|
|
|
|
|
pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
|
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
|
|
|
SPI.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
|
|
|
|
|
|
|
|
if (strobeOn == LOW) //if active low, use internal pull-up resistors
|
|
|
|
{
|
|
|
|
pullUp = readPins;
|
|
|
|
}
|
|
|
|
else //active high requires external pull-down resistors
|
|
|
|
{
|
|
|
|
pullUp = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
transfer(port.DEVICE_ADDR << 1, port.num, readPins); //configure IODIR
|
|
|
|
transfer(port.DEVICE_ADDR << 1, port.num + 0x0C, pullUp); //configure GPPU
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write() sets pin output to logicLevel (useful for strobePin, one LED pin, or multiple pins).
|
|
|
|
pin is bit pattern, where pin being set is 1.
|
|
|
|
logicLevel is HIGH or LOW.
|
|
|
|
write() does not overwrite the other pins.
|
|
|
|
*/
|
|
|
|
void PortMCP23S17::write(const uint8_t pin, const bool logicLevel)
|
|
|
|
{
|
|
|
|
if (logicLevel == LOW)
|
|
|
|
{
|
|
|
|
port.outputVal &= ~pin; //set pin output to low
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
port.outputVal |= pin; //set pin output to high
|
|
|
|
}
|
|
|
|
|
|
|
|
transfer(port.DEVICE_ADDR << 1, port.num + 0x12, port.outputVal); //set GPIO port to outputVal
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read() returns portState. Only portState pins with pull resistors are valid.
|
|
|
|
*/
|
|
|
|
uint8_t PortMCP23S17::read()
|
|
|
|
{
|
|
|
|
return transfer( (port.DEVICE_ADDR << 1) | 1, port.num + 0x12, 0); //read from GPIO
|
|
|
|
}
|