Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices

Dependents:   MCP41xxxApp MCP320xApp MCP41xxxApp

Revision:
7:12a0d89aa72f
Parent:
6:ded0d8a6729c
--- a/MCP4xxxx_SPI.cpp	Thu Jun 06 07:59:16 2013 +0000
+++ b/MCP4xxxx_SPI.cpp	Thu Sep 05 07:29:00 2013 +0000
@@ -23,7 +23,7 @@
 
     unsigned char CMCP4xxxx_SPI::SPIModuleRefCounter = 0;
 
-    CMCP4xxxx_SPI::CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs, const PinName p_reset, const PinName p_shdn, const Mcp4xxxFamilly p_familly, const unsigned int p_frequency) : _internalId("") {
+    CMCP4xxxx_SPI::CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs, const PinName p_reset, const PinName p_shdn, const Mcp4xxxFamily p_family, const unsigned int p_frequency) : _internalId("") {
         DEBUG_ENTER("CMCP4xxxx_SPI")
 
         if (CMCP4xxxx_SPI::SPIModuleRefCounter != 0) {
@@ -63,8 +63,8 @@
             _shdn = NULL; // Not used
         }
         
-        _familly = p_familly;
-        SetFamilly(p_familly);
+        _family = p_family;
+        SetFamilly(p_family);
         
         _incStep = 0x80; // Set to middle step
         
@@ -101,7 +101,7 @@
     }
     
     unsigned short CMCP4xxxx_SPI::WriteRegister(const Addresses p_address, const unsigned char p_value) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::WriteRegister: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_address, p_value, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::WriteRegister: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_address, p_value, (unsigned char)_family)
         
         // Sanity check
         if (p_address == CMCP4xxxx_SPI::Status) {
@@ -117,8 +117,7 @@
             _cs->write(0);
         }
         unsigned short result = _spiInstance->write(command);
-        DEBUG("CMCP4xxxx_SPI::WriteRegister: returned value=%d", result)
-        if (_cs != NULL) {
+         if (_cs != NULL) {
             _cs->write(1);
         }
         
@@ -127,7 +126,7 @@
     }
     
     unsigned short CMCP4xxxx_SPI::ReadRegister(const Addresses p_address) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::ReadRegister: 0x%02x - 0x%02x", (unsigned char)p_address, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::ReadRegister: 0x%02x", (unsigned char)p_address)
         
         unsigned short command = ((p_address & 0x0f) << 4 | (0x03/*TODO Use 'define' for Read command*/ << 2)) << 8;
         
@@ -145,7 +144,7 @@
         return result & 0x00ff;
     }
     
-    unsigned short CMCP4xxxx_SPI::Incrememt(const Commands p_command) { 
+    unsigned short CMCP4xxxx_SPI::Increment(const Commands p_command) { 
         // Sanity check
         if (_incStep != 0xff) { 
             _incStep += 1; // TODO Change increment 1 by a parametrized increment step
@@ -163,7 +162,7 @@
 
     
     unsigned short CMCP4xxxx_SPI::Write(const Commands p_command, const unsigned char p_value) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_command, p_value, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_command, p_value, (unsigned char)_family)
         
         // Sanity check
         if ((p_command != WriteToDigiPot1) && (p_command != WriteToDigiPot2) && (p_command != WriteToBoth)) {
@@ -172,7 +171,7 @@
         }
         
         unsigned short command = 0;
-        if ((_familly == CMCP4xxxx_SPI::_41xxx) || (_familly == CMCP4xxxx_SPI::_42xxx)) {  // See DS11195C
+        if ((_family == CMCP4xxxx_SPI::_41xxx) || (_family == CMCP4xxxx_SPI::_42xxx)) {  // See DS11195C
             switch (p_command) {
                 case WriteToDigiPot1: 
                     command = (0x11 << 8 | p_value);
@@ -193,7 +192,7 @@
             } // End of 'switch' statement
         }
         
-        DEBUG("CMCP4xxxx_SPI::Write: Send command: 0x%04x", (unsigned char)command, (unsigned char)_familly)
+        DEBUG("CMCP4xxxx_SPI::Write: Send command: 0x%04x", (unsigned char)command)
         if (_cs != NULL) {
             _cs->write(0);
         }
@@ -207,7 +206,7 @@
     }
 
     unsigned short CMCP4xxxx_SPI::Shutdown(const Commands p_command, const bool p_set) {
-        DEBUG_ENTER("CMCP4xxxx_SPI::Shutdown: 0x%02x - 0x%02x", (unsigned char)p_command, (unsigned char)_familly)
+        DEBUG_ENTER("CMCP4xxxx_SPI::Shutdown: 0x%02x - 0x%02x", (unsigned char)p_command, (unsigned char)_family)
         
         // Sanity check
         if ((p_command != ShutdownDigiPot1) && (p_command != ShutdownDigiPot2) && (p_command != ShutdownBoth)) {
@@ -216,7 +215,7 @@
         }
         
        unsigned short command = 0;
-       if ((_familly == CMCP4xxxx_SPI::_41xxx) || (_familly == CMCP4xxxx_SPI::_42xxx)) { // See DS11195C
+       if ((_family == CMCP4xxxx_SPI::_41xxx) || (_family == CMCP4xxxx_SPI::_42xxx)) { // See DS11195C
            switch (p_command) {
                 case ShutdownDigiPot1:
                     command = (0x21 << 8);
@@ -266,19 +265,20 @@
         DEBUG_LEAVE("CMCP4xxxx_SPI::Shutdown: %d", result)
         return result;
     }
-    unsigned short CMCP4xxxx_SPI::Write() {
-        return _spiInstance->write(0);
-    }
+    
+    //unsigned short CMCP4xxxx_SPI::Write() {
+    //    return _spiInstance->write(0);
+    //}
     
-    CMCP4xxxx_SPI::Mcp4xxxFamilly CMCP4xxxx_SPI::SetFamilly(const CMCP4xxxx_SPI::Mcp4xxxFamilly p_familly) { 
-        DEBUG_ENTER("CMCP4xxxx_SPI::SetFamilly: 0x%02x", (unsigned char)p_familly)
+    CMCP4xxxx_SPI::Mcp4xxxFamily CMCP4xxxx_SPI::SetFamilly(const CMCP4xxxx_SPI::Mcp4xxxFamily p_family) { 
+        DEBUG_ENTER("CMCP4xxxx_SPI::SetFamilly: 0x%02x", (unsigned char)p_family)
         
-        Mcp4xxxFamilly old = _familly;
-        _familly = p_familly;
-        if ((_familly != CMCP4xxxx_SPI::_41xxx) && (_familly != CMCP4xxxx_SPI::_42xxx)) {
+        Mcp4xxxFamily old = _family;
+        _family = p_family;
+        if ((_family != CMCP4xxxx_SPI::_41xxx) && (_family != CMCP4xxxx_SPI::_42xxx)) {
             // Setup TCON register 
             DEBUG("CMCP4xxxx_SPI::SetFamilly: Setup TCON register")
-            WriteRegister(CMCP4xxxx_SPI::TCon, 0x7f); // See DS22060B-page 36 REGISTER 4-2: TCON BITS
+            WriteRegister(CMCP4xxxx_SPI::TCon, 0xff); // See DS22060B-page 36 REGISTER 4-2: TCON BITS
                                                       // TODO Use 'define' for command
         }