314 lines
7.0 KiB
C++
314 lines
7.0 KiB
C++
#include "MCP23017.h"
|
|
#include "nrf_log.h"
|
|
|
|
|
|
MCP23017::MCP23017(AconnoI2C &i2c, int i2cAddress) : _i2c(i2c)
|
|
{
|
|
MCP23017_i2cAddress = i2cAddress;
|
|
reset(); // initialise chip to power-on condition
|
|
}
|
|
|
|
MCP23017::MCP23017(AconnoI2C &i2c, int i2cAddress,
|
|
int8_t pin,
|
|
GPIO_DIR direction,
|
|
GPIO_PULL pull,
|
|
GPIO_EDGE edge,
|
|
void (*pHandler)() ) : _i2c(i2c)
|
|
{
|
|
MCP23017_i2cAddress = i2cAddress;
|
|
|
|
GPIOPALintInit(pin,
|
|
(uint8_t) direction,
|
|
(uint8_t) pull,
|
|
(uint8_t) edge,
|
|
pHandler
|
|
);
|
|
|
|
reset(); // initialise chip to power-on condition
|
|
}
|
|
|
|
/*
|
|
* reset
|
|
* Set configuration (IOCON) and direction(IODIR) registers to initial state
|
|
*/
|
|
void MCP23017::reset()
|
|
{
|
|
//
|
|
// First make sure that the device is in BANK=0 mode
|
|
//
|
|
writeRegister(0x05, (char)0x00);
|
|
//
|
|
// set direction registers to inputs
|
|
//
|
|
writeRegister(MCP23017_IODIR, (unsigned short)0xFFFF);
|
|
//
|
|
// set all other registers to zero (last of 10 registers is OLAT)
|
|
//
|
|
for (int reg_addr = 2 ; reg_addr <= MCP23017_OLAT ; reg_addr+=2)
|
|
{
|
|
writeRegister(reg_addr, (unsigned short)0x0000);
|
|
}
|
|
}
|
|
|
|
/* Config
|
|
* set direction and pull-up registers
|
|
*/
|
|
void MCP23017::config(unsigned short dirConfig, unsigned short pullupConfig, unsigned short polarityConfig)
|
|
{
|
|
writeRegister(MCP23017_IODIR, dirConfig);
|
|
|
|
writeRegister(MCP23017_GPPU, pullupConfig);
|
|
|
|
writeRegister(MCP23017_IPOL, polarityConfig);
|
|
}
|
|
|
|
/*
|
|
* write_bit
|
|
* Write a 1/0 to a single bit of the 16-bit port
|
|
*/
|
|
void MCP23017::writeBit( int value, int bitNumber, char regAddress)
|
|
{
|
|
unsigned short regData = readRegister(regAddress);
|
|
|
|
if (value == 0) {
|
|
regData &= ~(1 << bitNumber);
|
|
} else {
|
|
regData |= 1 << bitNumber;
|
|
}
|
|
|
|
writeRegister(regAddress, (unsigned short)regData);
|
|
}
|
|
|
|
/*
|
|
* Write a combination of bits to the 16-bit port
|
|
*/
|
|
void MCP23017::writeMask(unsigned short data, unsigned short mask)
|
|
{
|
|
unsigned short gpioData = readRegister(MCP23017_GPIO);
|
|
gpioData = (gpioData & ~mask) | data;
|
|
writeRegister(MCP23017_GPIO, (unsigned short)gpioData);
|
|
}
|
|
|
|
/*
|
|
* read_bit
|
|
* Read a single bit from the 16-bit port
|
|
*/
|
|
int MCP23017::readBit(int bitNumber, char regAddress)
|
|
{
|
|
unsigned short regData = readRegister(regAddress);
|
|
//NRF_LOG_INFO("Expdr -read bit %d", ((regData >> bitNumber) & 0x0001));
|
|
return ((regData >> bitNumber) & 0x0001);
|
|
}
|
|
|
|
/*
|
|
* read_mask
|
|
*/
|
|
int MCP23017::readMask(unsigned short mask)
|
|
{
|
|
unsigned short gpioData = readRegister(MCP23017_GPIO);
|
|
return (gpioData & mask);
|
|
}
|
|
|
|
|
|
/*
|
|
* write_bit
|
|
* Write a 1/0 to a single bit of the 16-bit GPIO port
|
|
*/
|
|
void MCP23017::writeGPIOBit( int value, int bitNumber)
|
|
{
|
|
unsigned short regData = readRegister(MCP23017_GPIO);
|
|
|
|
if (value == 0) {
|
|
regData &= ~(1 << bitNumber);
|
|
} else {
|
|
regData |= 1 << bitNumber;
|
|
}
|
|
|
|
//NRF_LOG_INFO("In write bit, I'll write %d on %hu", (unsigned short)regData, MCP23017_GPIO);
|
|
writeRegister(MCP23017_GPIO, (unsigned short)regData);
|
|
}
|
|
|
|
/*
|
|
* read_bit
|
|
* Read a single bit from the 16-bit port
|
|
*/
|
|
int MCP23017::readGPIOBit(int bitNumber)
|
|
{
|
|
unsigned short regData = readRegister(MCP23017_GPIO);
|
|
//NRF_LOG_INFO("Expdr -read bit %d", ((regData >> bitNumber) & 0x0001));
|
|
return ((regData >> bitNumber) & 0x0001);
|
|
}
|
|
|
|
|
|
/*
|
|
* writeRegister
|
|
* write a byte
|
|
*/
|
|
void MCP23017::writeRegister(char regAddress, char data)
|
|
{
|
|
_i2c.writeToReg(MCP23017_i2cAddress, regAddress, &data, 1);
|
|
}
|
|
|
|
/*
|
|
* write Register
|
|
* write two bytes
|
|
*/
|
|
void MCP23017::writeRegister(char regAddress, unsigned short data)
|
|
{
|
|
char buffer[2];
|
|
tmp_data.value16 = data;
|
|
buffer[0] = tmp_data.value8[0];
|
|
buffer[1] = tmp_data.value8[1];
|
|
char *pbuffer = buffer;
|
|
|
|
_i2c.writeToReg(MCP23017_i2cAddress, regAddress, pbuffer, 2);
|
|
}
|
|
|
|
/*
|
|
* readRegister
|
|
*/
|
|
int MCP23017::readRegister(char regAddress)
|
|
{
|
|
char buffer[2];
|
|
char *pbuffer = buffer;
|
|
_i2c.readFromReg(MCP23017_i2cAddress, regAddress, pbuffer, 2);
|
|
|
|
return ((int)(buffer[0] + (buffer[1]<<8)));
|
|
}
|
|
|
|
/*
|
|
* pinMode
|
|
*/
|
|
void MCP23017::pinMode(int pin, IODIR_MODE mode)
|
|
{
|
|
writeBit(pin, (mode==IODIR_MODE::INPUT), MCP23017_IODIR);
|
|
}
|
|
|
|
|
|
/*
|
|
* digitalRead
|
|
*/
|
|
int MCP23017::digitalRead(int pin)
|
|
{
|
|
return (readRegister(MCP23017_GPIO) >> pin) & 0x1;
|
|
}
|
|
|
|
|
|
/*
|
|
* digitalWrite
|
|
*/
|
|
void MCP23017::digitalWrite(int pin, int val)
|
|
{
|
|
//If this pin is an INPUT pin, a write here will
|
|
//enable the internal pullup
|
|
//otherwise, it will set the OUTPUT voltage
|
|
//as appropriate.
|
|
bool isOutput = !(readRegister(MCP23017_IODIR) & 1<<pin);
|
|
|
|
if (isOutput) {
|
|
//This is an output pin so just write the value
|
|
writeBit(val, pin, MCP23017_GPIO);
|
|
} else {
|
|
//This is an input pin, so we need to enable the pullup
|
|
writeBit(val, pin, MCP23017_GPPU);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* digitalWordRead
|
|
*/
|
|
unsigned short MCP23017::digitalWordRead()
|
|
{
|
|
return readRegister(MCP23017_GPIO);
|
|
}
|
|
|
|
|
|
/*
|
|
* digitalWordWrite
|
|
*/
|
|
void MCP23017::digitalWordWrite(unsigned short word)
|
|
{
|
|
writeRegister(MCP23017_GPIO, word);
|
|
}
|
|
|
|
/*
|
|
* inputPolarityMask
|
|
*/
|
|
void MCP23017::inputPolarityMask(unsigned short mask)
|
|
{
|
|
writeRegister(MCP23017_IPOL, mask);
|
|
}
|
|
|
|
/*
|
|
* inputoutputMask
|
|
*/
|
|
void MCP23017::inputOutputMask(unsigned short mask)
|
|
{
|
|
writeRegister(MCP23017_IODIR, mask);
|
|
}
|
|
|
|
/*
|
|
* internalPullupMask
|
|
*/
|
|
void MCP23017::internalPullupMask(unsigned short mask)
|
|
{
|
|
writeRegister(MCP23017_GPPU, mask);
|
|
}
|
|
|
|
void MCP23017::setuptInterrupts(uint8_t mirroring, uint8_t openDrain, uint8_t polarity)
|
|
{
|
|
//for port A
|
|
writeBit(mirroring, 6, MCP23017_IOCON);
|
|
writeBit(openDrain, 2, MCP23017_IOCON);
|
|
writeBit(polarity, 1, MCP23017_IOCON);
|
|
|
|
//for port B
|
|
writeBit(mirroring, 14, MCP23017_IOCON);
|
|
writeBit(openDrain, 10, MCP23017_IOCON);
|
|
writeBit(polarity, 9, MCP23017_IOCON);
|
|
|
|
}
|
|
|
|
/*
|
|
* setInterruptPin
|
|
*/
|
|
void MCP23017::setupInterruptPin(uint8_t pin, INT_MODE mode)
|
|
{
|
|
// set the pin interrupt control (0 means change, 1 means compare against given value);
|
|
// writeBit((mode!=INT_MODE::CHANGE), pin, MCP23017_INTCON);
|
|
// if the mode is not CHANGE, we need to set up a default value, different value triggers interrupt
|
|
|
|
// In a RISING interrupt the default value is 0, interrupt is triggered when the pin goes to 1.
|
|
// In a FALLING interrupt the default value is 1, interrupt is triggered when pin goes to 0.
|
|
// writeBit((mode==INT_MODE::FALLING), pin, MCP23017_DEFVAL);
|
|
|
|
// enable the pin for interrupt
|
|
writeBit((uint8_t)INT_MODE::HIGH, pin, MCP23017_GPINTEN);
|
|
|
|
}
|
|
|
|
/*
|
|
* clearInterruptPin
|
|
*/
|
|
void MCP23017::clearInterruptPin(uint8_t pin)
|
|
{
|
|
// disable the pin for interrupt
|
|
writeBit( (uint8_t)INT_MODE::LOW, pin, MCP23017_GPINTEN);
|
|
|
|
}
|
|
|
|
/*
|
|
* getLastInterruptPin
|
|
*/
|
|
uint8_t MCP23017::getLastInterruptPin()
|
|
{
|
|
uint8_t intf=readRegister(MCP23017_INTF);
|
|
|
|
for(int i=0;i<16;i++)
|
|
if ( intf & (1 << i) )
|
|
return i;
|
|
|
|
return MCP23017_INT_ERR;
|
|
|
|
} |