2016-09-01 02:29:59 +00:00
|
|
|
#include "PortWrite_MCP23S17.h"
|
|
|
|
|
2016-09-11 21:23:47 +00:00
|
|
|
/* push() writes data to registerAddr.
|
2016-09-01 07:08:52 +00:00
|
|
|
*/
|
2016-09-11 21:23:47 +00:00
|
|
|
void PortWrite_MCP23S17::push(const uint8_t registerAddr, const uint8_t data)
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-01 07:08:52 +00:00
|
|
|
digitalWrite(SS, LOW); //enable Slave Select
|
2016-09-11 15:54:10 +00:00
|
|
|
SPI.transfer(port.DEVICE_ADDR << 1); //write command
|
2016-09-01 07:08:52 +00:00
|
|
|
SPI.transfer(registerAddr); //register address to write data to
|
2016-09-11 21:23:47 +00:00
|
|
|
SPI.transfer(data); //write the data
|
2016-09-01 07:08:52 +00:00
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:23:47 +00:00
|
|
|
/* begin() is called from Scanner_IOE::begin().
|
|
|
|
Initiates SPI bus and configures write pins to output.
|
|
|
|
MCP23S17 SPI interface is 10 MHz max.
|
2016-09-01 02:29:59 +00:00
|
|
|
*/
|
|
|
|
void PortWrite_MCP23S17::begin()
|
|
|
|
{
|
2016-09-01 07:08:52 +00:00
|
|
|
pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
|
|
|
|
digitalWrite(SS, HIGH); //disable Slave Select
|
2016-09-01 02:29:59 +00:00
|
|
|
SPI.begin();
|
2016-09-11 21:23:47 +00:00
|
|
|
SPI.beginTransaction(SPISettings (5000000, MSBFIRST, SPI_MODE0)); //control SPI bus, 5 MHz
|
2016-09-11 15:54:10 +00:00
|
|
|
//SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
|
2016-09-01 07:08:52 +00:00
|
|
|
|
2016-09-11 21:23:47 +00:00
|
|
|
push(port.num, 0); //configure port direction (port.num) to output (0)
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:23:47 +00:00
|
|
|
/* write() sets pin output to logicLevel.
|
|
|
|
pin is bitwise, where pin being set is 1.
|
|
|
|
logicLevel is HIGH or LOW.
|
|
|
|
write() does not overwrite the other pins.
|
2016-09-01 02:29:59 +00:00
|
|
|
*/
|
2016-09-11 21:23:47 +00:00
|
|
|
void PortWrite_MCP23S17::write(const uint8_t pin, const bool logicLevel)
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-11 21:23:47 +00:00
|
|
|
if (logicLevel == LOW)
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-11 21:23:47 +00:00
|
|
|
port.outputVal &= ~pin; //set pin output to low
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
2016-09-03 02:24:55 +00:00
|
|
|
else
|
2016-09-01 02:29:59 +00:00
|
|
|
{
|
2016-09-11 21:23:47 +00:00
|
|
|
port.outputVal |= pin; //set pin output to high
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:23:47 +00:00
|
|
|
push(port.num + 0x12, port.outputVal); //set GPIO port pin to outputVal
|
2016-09-01 02:29:59 +00:00
|
|
|
}
|