Production Test Program (PTP) for the LPC4088 Experiment Base Board

Dependencies:   EALib I2S LM75B SDFileSystem mbed

Revision:
7:48375cb50f3a
Parent:
1:47680ec5d783
--- a/AR1021I2C.cpp	Tue Sep 09 14:20:12 2014 +0000
+++ b/AR1021I2C.cpp	Thu Sep 25 07:17:44 2014 +0000
@@ -78,6 +78,8 @@
 #define AR1021_TIMEOUT     1000        //how many ms to wait for responce 
 #define AR1021_RETRY          5        //how many times to retry sending command 
 
+#define AR1021_MIN(__a, __b) (((__a)<(__b))?(__a):(__b))
+
 
 AR1021I2C::AR1021I2C(PinName sda, PinName scl, PinName siq) :
 _i2c(sda, scl), _siq(siq), _siqIrq(siq)
@@ -338,7 +340,7 @@
         bool setCsOff) {
 
     int ret = 0;
-    char buff[10];
+    int readLen = (respLen == NULL) ? 0 : *respLen;
     for (int attempt = 1; attempt <= AR1021_RETRY; attempt++) {
         if (attempt > 1) {
             wait_ms(50);
@@ -375,55 +377,52 @@
             // ---------------
             // 0x55 len status cmd data
             // 0x55 = header
-            // len = number of bytes following the len byte
+            // len = number of bytes following the len byte (i.e. including the status&cmd)
             // status = status
             // cmd = command ID
             // data = data to receive
             _i2c.start();
             _i2c.write(AR1021_ADDR + 1);        //send read address 
-            char header = _i2c.read(1);         //header should always be 0x55  
-            char length = _i2c.read(1);         //data length
-
-            //we need to send a NACK on the last read. 
-            int i;
-            for (i = 0; i < (length-1); i++) {
-                buff[i] = _i2c.read(1);
-            }
-            buff[i] = _i2c.read(0);             //last returned data byte             
-            _i2c.stop();
-            
-            
+            char header = _i2c.read(1);         //header should always be 0x55
             if (header != 0x55) {
                 ret = AR1021_ERR_NO_HDR;
                 continue;
             }
+            char length = _i2c.read(1);         //data length
             if (length < 2) {
-                ret = AR1021_ERR_INV_LEN;
+                ret = AR1021_ERR_INV_LEN;       //must have at least status and command bytes
                 continue;
             }
-            if (buff[0] != AR1021_RESP_STAT_OK) {
-                ret = -buff[0];
-                continue;
-            }
-            if (buff[1] != cmd) {
-                ret = AR1021_ERR_INV_RESP;
+            length -= 2;
+            if (length > readLen) {
+                ret = AR1021_ERR_INV_LEN;       //supplied buffer is not enough
                 continue;
             }
+
+            char status = _i2c.read(1);         //command status
+            char usedCmd;
+            if (readLen <= 0) {
+                usedCmd = _i2c.read(0);         //no data => read command byte + NACK
+            } else {
+                usedCmd = _i2c.read(1);         //which command
+
+                //we need to send a NACK on the last read. 
+                int i;
+                for (i = 0; i < (length-1); i++) {
+                    respBuf[i] = _i2c.read(1);
+                }
+                respBuf[i] = _i2c.read(0);      //last returned data byte + NACK
+            }
+            _i2c.stop();
             
-            // copy data into response
-            if (respLen != NULL && respBuf != NULL) {
-                if ((length - 2) > *respLen) {
-                    // respBuf too small, only copy what fits
-                    for (i = 0; i < *respLen; i++) {
-                        respBuf[i] = buff[i+2];
-                    }
-                } else {
-                    // respBuf large enough, copy all
-                    for (i = 0; i < (length-2); i++) {
-                        respBuf[i] = buff[i+2];
-                    }
-                    *respLen = (length-2);
-                }
+            
+            if (status != AR1021_RESP_STAT_OK) {
+                ret = -status;
+                continue;
+            }
+            if (usedCmd != cmd) {
+                ret = AR1021_ERR_INV_RESP;
+                continue;
             }
             
             // success
@@ -559,4 +558,3 @@
     }
     return (res == 0);
 }
-