This library provides simplified I2C access to a Microchip MCP23x17 GPIO expender device, including a general interface for any GPIO expender

Dependents:   MCP23017App

Files at this revision

API Documentation at this revision

Comitter:
Yann
Date:
Fri Jan 09 15:35:40 2015 +0000
Parent:
0:ebd3a7cc9b92
Child:
2:3bea48e1505c
Commit message:
Bug fixed in read method; Add bus support

Changed in this revision

AbstractGpioExpender.h Show annotated file Show diff for this revision Revisions of this file
MCP23017_I2C.cpp Show annotated file Show diff for this revision Revisions of this file
MCP23017_I2C.h Show annotated file Show diff for this revision Revisions of this file
--- a/AbstractGpioExpender.h	Fri Jan 09 14:37:42 2015 +0000
+++ b/AbstractGpioExpender.h	Fri Jan 09 15:35:40 2015 +0000
@@ -72,11 +72,11 @@
     
     virtual unsigned char createBus(const std::list<unsigned char> p_lines, const PinMode p_mode = PullNone) = 0;
     virtual void deleteBus(const unsigned char p_busId) = 0;
-    virtual int busRead(const unsigned char p_busId) = 0;
-    virtual int busWrite(const unsigned char p_busId, const unsigned char p_value) = 0;
+    virtual int busRead(const unsigned char p_busId, unsigned short * p_value) = 0;
+    virtual int busWrite(const unsigned char p_busId, const unsigned short p_value) = 0;
     
 protected:
     virtual bool writeRegister(const unsigned char p_registerId, const unsigned char p_value) = 0;
     virtual bool readRegister(const unsigned char p_registerId, unsigned char * p_value) = 0;
 
-}; // End of class AbstractGpioExpender 
\ No newline at end of file
+}; // End of class AbstractGpioExpender 
--- a/MCP23017_I2C.cpp	Fri Jan 09 14:37:42 2015 +0000
+++ b/MCP23017_I2C.cpp	Fri Jan 09 15:35:40 2015 +0000
@@ -1,4 +1,4 @@
-/* mbed simplified access to Microchip MCP28x17 GPIO expender devices (I2C)
+/* mbed simplified access to Microchip MCP23x17 GPIO expender devices (I2C)
  * Copyright (c) 2010-2012 ygarcia, MIT License
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
@@ -405,6 +405,9 @@
         }
         DEBUG("CMCP23017_I2C::read: gpioRegisterValue=%02x", gpioRegisterValue)
         
+        *p_value = (isBitSet(gpioRegisterValue, gpioBit)) ? 0x01 : 0x00;
+        DEBUG("CMCP23017_I2C::read: p_value=%02x", *p_value)
+        
         DEBUG_LEAVE("CMCP23017_I2C::read: 0")
         return 0;
     }
@@ -480,12 +483,47 @@
         _buses.erase(p_busId);
     }
 
-    int CMCP23017_I2C::busRead(const unsigned char p_busId) {
-        return -1;
+    int CMCP23017_I2C::busRead(const unsigned char p_busId, unsigned short * p_value) {
+        // Sanity checks
+        if (_buses.size() == 0) {
+            return -1;
+        }
+        std::map<unsigned char, std::list<unsigned char> >::iterator result = _buses.find(p_busId);
+        if (result == _buses.end()) { // Invalid bus identifier
+            return -1;
+        }
+        
+        std::list<unsigned char>::reverse_iterator rit;
+        for (std::list<unsigned char>::reverse_iterator rit = result->second.rbegin(); rit != result->second.rend(); ++rit) {
+            unsigned char regvalue;
+            if (read(*rit, &regvalue) == 0) {
+                *p_value = (*p_value | regvalue) << 1;
+            } else {
+                *p_value <<= 1;
+            }
+        } // End of 'for' statement
+        
+        return 0;
     }
 
-    int CMCP23017_I2C::busWrite(const unsigned char p_busId, const unsigned char p_value) {
-        return -1;
+    int CMCP23017_I2C::busWrite(const unsigned char p_busId, const unsigned short p_value) {
+        // Sanity checks
+        if (_buses.size() == 0) {
+            return -1;
+        }
+        std::map<unsigned char, std::list<unsigned char> >::iterator result = _buses.find(p_busId);
+        if (result == _buses.end()) { // Invalid bus identifier
+            return -1;
+        }
+        
+        std::list<unsigned char>::reverse_iterator rit;
+        unsigned short value = p_value;
+        for (std::list<unsigned char>::reverse_iterator rit = result->second.rbegin(); rit != result->second.rend(); ++rit) {
+            write(*rit, value & 0x01);
+            value >>= 1;
+        } // End of 'for' statement
+        
+        return 0;
     }
 
     bool CMCP23017_I2C::writeRegister(const unsigned char p_registerId, const unsigned char p_value) {
--- a/MCP23017_I2C.h	Fri Jan 09 14:37:42 2015 +0000
+++ b/MCP23017_I2C.h	Fri Jan 09 15:35:40 2015 +0000
@@ -1,4 +1,4 @@
-/* mbed simplified access to Microchip MCP28x17 GPIO expender devices (I2C)
+/* mbed simplified access to Microchip MCP23x17 GPIO expender devices (I2C)
  * Copyright (c) 2014-2015 ygarcia, MIT License
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
@@ -116,6 +116,12 @@
          * @return 0 on success, negative value otherwise
          */
         virtual int setupInterruptPin(const unsigned char p_gpioId, const InterruptModes p_mode = OnRising);
+        
+        /** Get interrupt information and clear it
+         * @param p_gpioId The IO port identifier where the interrupt occured
+         * @param p_value  The logic value on the pin port where the interrupt occured
+         * @return 0 on success, negative value otherwise
+         */
         virtual int getLastInterruptPin(unsigned char * p_gpioId, unsigned char * p_value);
         
         virtual int read(const unsigned char p_gpioId, unsigned char * p_value);
@@ -123,8 +129,8 @@
         
         virtual unsigned char createBus(const std::list<unsigned char> p_lines, const PinMode p_mode = PullNone);
         virtual void deleteBus(const unsigned char p_busId);
-        virtual int busRead(const unsigned char p_busId);
-        virtual int busWrite(const unsigned char p_busId, const unsigned char p_value);
+        virtual int busRead(const unsigned char p_busId, unsigned short * p_value);
+        virtual int busWrite(const unsigned char p_busId, const unsigned short p_value);
         
         /** Attach a function to call when a interrupt occurs on the GPIOA input ports
          */