This library provides simplified I2C access to a Microchip MCP23x17 GPIO expender device, including a general interface for any GPIO expender
Diff: MCP23017_I2C.cpp
- Revision:
- 3:b902729a1675
- Parent:
- 2:3bea48e1505c
--- a/MCP23017_I2C.cpp Tue Jan 13 10:09:01 2015 +0000
+++ b/MCP23017_I2C.cpp Tue Jan 13 10:20:02 2015 +0000
@@ -49,7 +49,7 @@
unsigned char CMCP23017_I2C::I2CModuleRefCounter = 0;
CMCP23017_I2C::CMCP23017_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const PinName p_intA, const PinName p_intB, const PinName p_reset, const bool p_internalPullUp, const unsigned int p_frequency) : _gpioAFlags(0x00), _gpioBFlags(0x00), _buses(), _busesIndex(0x00), _internalId("") {
- DEBUG_ENTER("CMCP23017_I2C")
+// DEBUG_ENTER("CMCP23017_I2C")
if (CMCP23017_I2C::I2CModuleRefCounter != 0) {
error("CMCP23017_I2C: Wrong params");
@@ -58,51 +58,51 @@
std::ostringstream out(std::ostringstream::out);
out << "CMCP23017_I2C #" << CMCP23017_I2C::I2CModuleRefCounter;
_internalId.assign(out.str());
- DEBUG("CMCP23017_I2C: _internalId='%s'", _internalId.c_str())
+// DEBUG("CMCP23017_I2C: _internalId='%s'", _internalId.c_str())
#endif // __DEBUG
_i2cInstance = new I2C(p_sda, p_scl);
CMCP23017_I2C::I2CModuleRefCounter += 1;
- DEBUG_ENTER("CMCP23017_I2C: refCounter=%d", CMCP23017_I2C::I2CModuleRefCounter)
+// DEBUG_ENTER("CMCP23017_I2C: refCounter=%d", CMCP23017_I2C::I2CModuleRefCounter)
_slaveAddress = (p_address << 1) | 0x40; // Slave address format is: 0 0 1 0 A3 A2 A1 R/W
- DEBUG("CMCP23017_I2C: I2C slave adress: 0x%02x", _slaveAddress)
+// DEBUG("CMCP23017_I2C: I2C slave adress: 0x%02x", _slaveAddress)
_i2cInstance->frequency(p_frequency); // Set the frequency of the I2C interface
if (p_intA != NC) {
- DEBUG("CMCP23017_I2C: INTA managed");
+// DEBUG("CMCP23017_I2C: INTA managed");
_intA = new InterruptIn(p_intA);
if (p_internalPullUp) _intA->mode(::PullDown);
_intA->enable_irq(); // Enable interrupt
} else {
- DEBUG("CMCP23017_I2C: INTA not managed");
+// DEBUG("CMCP23017_I2C: INTA not managed");
_intA = NULL; // Not used
}
if (p_intB != NC) {
- DEBUG("CMCP23017_I2C: INTB managed");
+// DEBUG("CMCP23017_I2C: INTB managed");
_intB = new InterruptIn(p_intB);
if (p_internalPullUp) _intB->mode(::PullDown);
_intB->enable_irq(); // Enable interrupt
} else {
- DEBUG("CMCP23017_I2C: INTB not managed");
+// DEBUG("CMCP23017_I2C: INTB not managed");
_intB = NULL; // Not used
}
if (p_reset != NC) {
- DEBUG("CMCP23017_I2C: RESET managed");
+// DEBUG("CMCP23017_I2C: RESET managed");
_reset = new DigitalOut(p_reset);
_reset->write(1); // Disable reset
} else {
- DEBUG("CMCP23017_I2C: RESET not managed");
+// DEBUG("CMCP23017_I2C: RESET not managed");
_reset = NULL; // Not used
}
- DEBUG_LEAVE("CMCP23017_I2C")
+// DEBUG_LEAVE("CMCP23017_I2C")
}
CMCP23017_I2C::~CMCP23017_I2C() {
- DEBUG_ENTER("~CMCP23017_I2C")
+// DEBUG_ENTER("~CMCP23017_I2C")
// Release I2C instance
- DEBUG_ENTER("~CMCP23017_I2C: refCounter=%d", CMCP23017_I2C::I2CModuleRefCounter)
+// DEBUG("~CMCP23017_I2C: refCounter=%d", CMCP23017_I2C::I2CModuleRefCounter)
CMCP23017_I2C::I2CModuleRefCounter -= 1;
if (CMCP23017_I2C::I2CModuleRefCounter == 0) {
delete _i2cInstance;
@@ -119,7 +119,7 @@
delete _reset;
}
- DEBUG_LEAVE("~CMCP23017_I2C")
+// DEBUG_LEAVE("~CMCP23017_I2C")
}
bool CMCP23017_I2C::Initialize(const unsigned char p_gpioAFlags, const unsigned char p_gpioBFlags) {
@@ -150,7 +150,7 @@
// Setup interrupt
if (_intA != NULL) {
- DEBUG("CMCP23017_I2C::configure: Setup INTA")
+// DEBUG("CMCP23017_I2C::configure: Setup INTA")
// Setup GPINTEN - See GPINTEN – INTERRUPT-ON-CHANGE PINS
writeRegister(GPINTENA, 0x00); // Disable GPIO interrupt-on-change events
// Setup DEFVAL - See REGISTER 1-4: DEFVAL – DEFAULT VALUE REGISTER
@@ -159,7 +159,7 @@
writeRegister(INTCONA, 0xff); // Pin level change from 0 to 1 raises an interrupt
}
if (_intB != NULL) {
- DEBUG("CMCP23017_I2C::configure: Setup INTB")
+// DEBUG("CMCP23017_I2C::configure: Setup INTB")
// Setup GPINTEN - See GPINTEN – INTERRUPT-ON-CHANGE PINS
writeRegister(GPINTENB, 0x00); // Disable GPIO interrupt-on-change events
// Setup DEFVAL - See REGISTER 1-4: DEFVAL – DEFAULT VALUE REGISTER
@@ -275,7 +275,7 @@
// Retrieve the register address
unsigned char gpioIntconId, gpioDefvalId, gpioGpintenId;
if (!registerIdFromGpioId(p_gpioId, &gpioIntconId)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -1")
return -1;
}
unsigned char gpioFlags;
@@ -296,21 +296,21 @@
unsigned char gpioBit = gpioBitFromGpioId(p_gpioId);
DEBUG("CMCP23017_I2C::setupInterruptPin: gpioBit=%02x", gpioBit)
if (!isBitEqual(gpioFlags, gpioBit, 0x01)) { // Port pin is not configure as input
- DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -1")
return -1;
}
// Read it
unsigned char gpioIntconValue, gpioDefvalValue, gpioGpintenValue;
if (!readRegister(gpioIntconId, &gpioIntconValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
return -2;
}
if (!readRegister(gpioDefvalId, &gpioDefvalValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
return -2;
}
if (!readRegister(gpioGpintenId, &gpioGpintenValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
return -2;
}
DEBUG("CMCP23017_I2C::setupInterruptPin: gpioIntconValue=%02x gpioDefvalValue=%02x gpioGpintenValue=%02x", gpioIntconValue, gpioDefvalValue, gpioGpintenValue)
@@ -340,17 +340,17 @@
writeRegister(gpioIntconId, gpioIntconValue);
writeRegister(gpioGpintenId, gpioGpintenValue);
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: 0")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: 0")
return 0;
}
int CMCP23017_I2C::setupPullPin(const unsigned char p_gpioId, const PullModes p_mode) {
- DEBUG_ENTER("CMCP23017_I2C::setupPullPin: %02x, %02x", p_gpioId, p_mode)
+// DEBUG_ENTER("CMCP23017_I2C::setupPullPin: %02x, %02x", p_gpioId, p_mode)
// Retrieve the register address
unsigned char gpioGppuId;
if (!registerIdFromGpioId(p_gpioId, &gpioGppuId)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -1")
return -1;
}
unsigned char gpioFlags;
@@ -361,23 +361,23 @@
gpioGppuId = GPPUB;
gpioFlags = _gpioBFlags;
}
- DEBUG("CMCP23017_I2C::setupPullPin: gpioGppuId=%02x gpioFlags=%02x", gpioGppuId, gpioFlags)
+// DEBUG("CMCP23017_I2C::setupPullPin: gpioGppuId=%02x gpioFlags=%02x", gpioGppuId, gpioFlags)
// Retrieve the GPIO pin number
unsigned char gpioBit = gpioBitFromGpioId(p_gpioId);
- DEBUG("CMCP23017_I2C::setupPullPin: gpioBit=%02x", gpioBit)
+// DEBUG("CMCP23017_I2C::setupPullPin: gpioBit=%02x", gpioBit)
if (!isBitEqual(gpioFlags, gpioBit, 0x01)) { // Port pin is not configure as input
- DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -1")
return -1;
}
// Read it
unsigned char gpioGppuValue;
if (!readRegister(gpioGppuId, &gpioGppuValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: -2")
return -2;
}
- DEBUG("CMCP23017_I2C::setupPullPin: gpioGppuId=%02x", gpioGppuId)
+// DEBUG("CMCP23017_I2C::setupPullPin: gpioGppuId=%02x", gpioGppuId)
//
switch (static_cast<unsigned char>(p_mode)) {
@@ -393,22 +393,22 @@
} // End of 'switch' statement
// Write register
- DEBUG("CMCP23017_I2C::setupPullPin: gpioGppuValue=%02x", gpioGppuValue)
+// DEBUG("CMCP23017_I2C::setupPullPin: gpioGppuValue=%02x", gpioGppuValue)
writeRegister(gpioGppuId, gpioGppuValue);
- DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: 0")
+// DEBUG_LEAVE("CMCP23017_I2C::setupPullPin: 0")
return 0;
}
int CMCP23017_I2C::getLastInterruptPinAndValue(unsigned char * p_gpioId, unsigned char * p_value) {
- DEBUG_ENTER("CMCP23017_I2C::getLastInterruptPinAndValue")
+// DEBUG_ENTER("CMCP23017_I2C::getLastInterruptPinAndValue")
// Read first INTFA if required
unsigned char vregister;
if (_gpioAFlags != 0x00) {
if (!readRegister(INTFA, &vregister)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -1")
return -1;
}
vregister &= _gpioAFlags; // Cannot have interrupt on output port
@@ -418,7 +418,7 @@
readRegister(INTCAPA, p_value);
*p_value = (*p_value >> bit) & 0x01;
- DEBUG_LEAVE("CMCP23017_I2C::getLastInterruptPinAndValue (A): %02x %02x", *p_gpioId, *p_value)
+// DEBUG_LEAVE("CMCP23017_I2C::getLastInterruptPinAndValue (A): %02x %02x", *p_gpioId, *p_value)
return 0;
}
} // End of 'for' statement
@@ -427,7 +427,7 @@
// If not interrupt on GPIOA, try with GPIOB
if (_gpioBFlags != 0x00) {
if (!readRegister(INTFB, &vregister)) {
- DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::setupInterruptPin: -2")
return -2;
}
vregister &= _gpioBFlags; // Cannot have interrupt on output port
@@ -437,94 +437,94 @@
readRegister(INTCAPB, p_value);
*p_value = (*p_value >> bit) & 0x01;
- DEBUG_LEAVE("CMCP23017_I2C::getLastInterruptPinAndValue (B): %02x %02x", *p_gpioId, *p_value)
+// DEBUG_LEAVE("CMCP23017_I2C::getLastInterruptPinAndValue (B): %02x %02x", *p_gpioId, *p_value)
return 0;
}
} // End of 'for' statement
}
- DEBUG_LEAVE("CMCP23017_I2C::getLastInterruptPinAndValue: 0")
+// DEBUG_LEAVE("CMCP23017_I2C::getLastInterruptPinAndValue: 0")
return 0;
}
int CMCP23017_I2C::read(const unsigned char p_gpioId, unsigned char * p_value) {
- DEBUG_ENTER("CMCP23017_I2C::read: 0x%02x", p_gpioId)
+// DEBUG_ENTER("CMCP23017_I2C::read: 0x%02x", p_gpioId)
// Retrieve the register address
unsigned char gpioRegisterId;
if (!registerIdFromGpioId(p_gpioId, &gpioRegisterId)) {
- DEBUG_LEAVE("CMCP23017_I2C::read: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::read: -1")
return -1;
}
- DEBUG("CMCP23017_I2C::read: gpioRegisterId=%02x", gpioRegisterId)
+// DEBUG("CMCP23017_I2C::read: gpioRegisterId=%02x", gpioRegisterId)
unsigned char gpioBit = gpioBitFromGpioId(p_gpioId);
- DEBUG("CMCP23017_I2C::read: gpioBit=%02x", gpioBit)
+// DEBUG("CMCP23017_I2C::read: gpioBit=%02x", gpioBit)
// Read it
unsigned char gpioRegisterValue;
if (!readRegister(gpioRegisterId, &gpioRegisterValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::read: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::read: -2")
return -2;
}
- DEBUG("CMCP23017_I2C::read: gpioRegisterValue=%02x", gpioRegisterValue)
+// DEBUG("CMCP23017_I2C::read: gpioRegisterValue=%02x", gpioRegisterValue)
*p_value = (isBitSet(gpioRegisterValue, gpioBit)) ? 0x01 : 0x00;
- DEBUG("CMCP23017_I2C::read: p_value=%02x", *p_value)
+// DEBUG("CMCP23017_I2C::read: p_value=%02x", *p_value)
- DEBUG_LEAVE("CMCP23017_I2C::read: 0")
+// DEBUG_LEAVE("CMCP23017_I2C::read: 0")
return 0;
}
bool CMCP23017_I2C::registerIdFromGpioId(const unsigned char p_gpioId, unsigned char * p_gpioRegisterId) {
- DEBUG_ENTER("CMCP23017_I2C::registerIdFromGpioId: 0x%02x", p_gpioId)
+// DEBUG_ENTER("CMCP23017_I2C::registerIdFromGpioId: 0x%02x", p_gpioId)
// Sanity check
if (p_gpioId > GPIO_MAX) {
- DEBUG_ENTER("CMCP23017_I2C::registerIdFromGpioId: false")
+// DEBUG_ENTER("CMCP23017_I2C::registerIdFromGpioId: false")
return false;
}
*p_gpioRegisterId = (p_gpioId < GPIO_MED) ? GPIOA : GPIOB;
- DEBUG_ENTER("CMCP23017_I2C::registerIdFromGpioId: true")
+// DEBUG_ENTER("CMCP23017_I2C::registerIdFromGpioId: true")
return true;
}
int CMCP23017_I2C::write(const unsigned char p_gpioId, const unsigned char p_value) {
- DEBUG_ENTER("CMCP23017_I2C::write: 0x%02x 0x%02x", p_gpioId, p_value)
+// DEBUG_ENTER("CMCP23017_I2C::write: 0x%02x 0x%02x", p_gpioId, p_value)
// Retrieve the register address
unsigned char gpioRegisterId;
if (!registerIdFromGpioId(p_gpioId, &gpioRegisterId)) {
- DEBUG_LEAVE("CMCP23017_I2C::write: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::write: -1")
return -1;
}
- DEBUG("CMCP23017_I2C::write: gpioRegisterId=%02x", gpioRegisterId)
+// DEBUG("CMCP23017_I2C::write: gpioRegisterId=%02x", gpioRegisterId)
// Retrieve the GPIO pin number
unsigned char gpioBit = gpioBitFromGpioId(p_gpioId);
- DEBUG("CMCP23017_I2C::write: gpioBit=%02x", gpioBit)
+// DEBUG("CMCP23017_I2C::write: gpioBit=%02x", gpioBit)
// Read it
unsigned char gpioRegisterValue;
if (!readRegister(gpioRegisterId, &gpioRegisterValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::write: -2")
+// DEBUG_LEAVE("CMCP23017_I2C::write: -2")
return -2;
}
- DEBUG("CMCP23017_I2C::write: gpioRegisterValue=%02x", gpioRegisterValue)
+// DEBUG("CMCP23017_I2C::write: gpioRegisterValue=%02x", gpioRegisterValue)
// Update GPIO bit
if (!isBitEqual(gpioRegisterValue, gpioBit, p_value)) {
// Write it if required
gpioRegisterValue = setBit(gpioRegisterValue, gpioBit, p_value);
- DEBUG("CMCP23017_I2C::write: New gpioRegisterValue=%02x", gpioRegisterValue)
+// DEBUG("CMCP23017_I2C::write: New gpioRegisterValue=%02x", gpioRegisterValue)
if (!writeRegister(gpioRegisterId, gpioRegisterValue)) {
- DEBUG_LEAVE("CMCP23017_I2C::write: -3")
+// DEBUG_LEAVE("CMCP23017_I2C::write: -3")
return -3;
}
}
- DEBUG_LEAVE("CMCP23017_I2C::write: 0")
+// DEBUG_LEAVE("CMCP23017_I2C::write: 0")
return 0;
}
@@ -551,12 +551,12 @@
// Sanity checks
if (_buses.size() == 0) {
- DEBUG_LEAVE("CMCP23017_I2C::busRead: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::busRead: -1")
return -1;
}
std::map<unsigned char, std::list<unsigned char> >::iterator result = _buses.find(p_busId);
if (result == _buses.end()) { // Invalid bus identifier
- DEBUG_LEAVE("CMCP23017_I2C::busRead: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::busRead: -1")
return -1;
}
@@ -583,7 +583,7 @@
}
std::map<unsigned char, std::list<unsigned char> >::iterator result = _buses.find(p_busId);
if (result == _buses.end()) { // Invalid bus identifier
- DEBUG_LEAVE("CMCP23017_I2C::busWrite: -1")
+// DEBUG_LEAVE("CMCP23017_I2C::busWrite: -1")
return -1;
}
@@ -647,7 +647,7 @@
unsigned char value;
for (unsigned int registerId = 0; registerId < 0x16; registerId++) {
readRegister(registerId, &value);
- DEBUG("CMCP23017_I2C::DumpRegisters: register[%d] = 0x%02x", registerId, value)
+// DEBUG("CMCP23017_I2C::DumpRegisters: register[%d] = 0x%02x", registerId, value)
}
}
@@ -655,7 +655,7 @@
unsigned char value;
readRegister(p_registerId, &value);
- DEBUG("CMCP23017_I2C::DumpRegister: register[%d] = 0x%02x", p_registerId, value)
+// DEBUG("CMCP23017_I2C::DumpRegister: register[%d] = 0x%02x", p_registerId, value)
}
} // End of namespace MCP23017_I2C
Yann Garcia