2016-09-25 03:03:08 +00:00
|
|
|
#include "Port_MCP23S17.h"
|
2016-09-11 22:45:52 +00:00
|
|
|
|
2016-09-11 23:03:00 +00:00
|
|
|
/* transfer() writes data to registerAddr, reads portSate from registerAddr, and returns portState.
|
2016-09-24 19:04:52 +00:00
|
|
|
|
|
|
|
MCP23S17 SPI interface is 10 MHz max.
|
|
|
|
The electrical limitation to bus speed is bus capacitance and the length of the wires involved.
|
2016-09-28 20:37:40 +00:00
|
|
|
Longer wires require lower clock speeds.
|
2016-09-11 22:45:52 +00:00
|
|
|
*/
|
2016-09-25 03:03:08 +00:00
|
|
|
uint8_t Port_MCP23S17::transfer(const uint8_t command, const uint8_t registerAddr,
|
2016-09-28 20:37:40 +00:00
|
|
|
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-24 19:04:52 +00:00
|
|
|
SPI.beginTransaction( SPISettings(5000000, MSBFIRST, SPI_MODE0) ); //control SPI bus, 5 MHz
|
2016-09-11 22:45:52 +00:00
|
|
|
digitalWrite(SS, LOW); //enable Slave Select
|
2016-09-28 20:37:40 +00:00
|
|
|
SPI.transfer(command); //write or read command
|
|
|
|
SPI.transfer(registerAddr); //register address to write data to
|
|
|
|
portState = SPI.transfer(data); //write data, read portState
|
2016-09-11 22:45:52 +00:00
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
2016-09-24 19:04:52 +00:00
|
|
|
SPI.endTransaction();
|
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
|
|
|
|
2016-10-30 08:30:13 +00:00
|
|
|
/* beginProtocol() is called from Scanner_IOE::begin(). Initiates SPI bus.
|
2016-09-23 07:13:12 +00:00
|
|
|
*/
|
2016-09-25 03:03:08 +00:00
|
|
|
void Port_MCP23S17::beginProtocol()
|
2016-09-23 07:13:12 +00:00
|
|
|
{
|
|
|
|
pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
|
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
|
|
|
SPI.begin();
|
2016-09-24 14:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* begin() is called from Scanner_IOE::begin().
|
|
|
|
strobeOn is logic level of strobe on, HIGH or LOW
|
|
|
|
configure IODIR and GPPU.
|
|
|
|
*/
|
2016-09-25 03:03:08 +00:00
|
|
|
void Port_MCP23S17::begin(const uint8_t strobeOn)
|
2016-09-24 14:56:02 +00:00
|
|
|
{
|
|
|
|
uint8_t pullUp; //bits, GPPU 0=pull-up disabled, 1=pull-up enabled
|
2016-09-23 07:13:12 +00:00
|
|
|
|
2016-10-30 08:30:13 +00:00
|
|
|
if (strobeOn == LOW) //if active low
|
2016-09-23 07:13:12 +00:00
|
|
|
{
|
2016-10-30 08:30:13 +00:00
|
|
|
pullUp = readPins; //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
|
2016-09-23 07:13:12 +00:00
|
|
|
}
|
2016-10-30 08:30:13 +00:00
|
|
|
else //if active high
|
2016-09-23 07:13:12 +00:00
|
|
|
{
|
2016-10-30 08:30:13 +00:00
|
|
|
pullUp = 0; //0=pull-up disabled (for external pull-down resistors)
|
2016-09-23 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-24 18:08:47 +00:00
|
|
|
transfer(deviceAddr << 1, portNum, readPins); //configure IODIR
|
|
|
|
transfer(deviceAddr << 1, portNum + 0x0C, pullUp); //configure GPPU
|
2016-09-23 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 07:29:29 +00:00
|
|
|
/* setLow() sets pin output LOW.
|
2016-09-23 07:13:12 +00:00
|
|
|
pin is bit pattern, where pin being set is 1.
|
|
|
|
*/
|
2016-11-14 07:29:29 +00:00
|
|
|
void Port_MCP23S17::setLow(const uint8_t pin)
|
2016-09-23 07:13:12 +00:00
|
|
|
{
|
2016-11-14 07:29:29 +00:00
|
|
|
outputVal &= ~pin; //set pin output to low
|
|
|
|
transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
|
|
|
|
}
|
2016-09-23 07:13:12 +00:00
|
|
|
|
2016-11-14 07:29:29 +00:00
|
|
|
/* setHigh() sets pin output HIGH.
|
|
|
|
pin is bit pattern, where pin being set is 1.
|
|
|
|
*/
|
|
|
|
void Port_MCP23S17::setHigh(const uint8_t pin)
|
|
|
|
{
|
|
|
|
outputVal |= pin; //set pin output to high
|
2016-09-24 18:08:47 +00:00
|
|
|
transfer(deviceAddr << 1, portNum + 0x12, outputVal); //set GPIO port to outputVal
|
2016-09-23 07:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read() returns portState. Only portState pins with pull resistors are valid.
|
|
|
|
*/
|
2016-09-25 03:03:08 +00:00
|
|
|
uint8_t Port_MCP23S17::read()
|
2016-09-23 07:13:12 +00:00
|
|
|
{
|
2016-09-24 18:08:47 +00:00
|
|
|
return transfer( (deviceAddr << 1) | 1, portNum + 0x12, 0); //read from GPIO
|
2016-09-23 07:13:12 +00:00
|
|
|
}
|