PICO I2C FW

Dependencies:   USBDevice

Revision:
27:72e40a8d3001
Parent:
26:89920e2454f8
--- a/main.cpp	Wed Mar 13 22:01:22 2019 +0000
+++ b/main.cpp	Thu Mar 14 23:12:52 2019 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "USBHID.h"
+#include "PicoCommands.h"
 
 // Virtual serial port over USB TODO NEW VID PID NEEDED!!
 USBHID hid(64, 64);
@@ -9,7 +10,7 @@
 HID_REPORT recv_report;
 
 //debug port setup
-Serial pc(P3_0, P3_1);
+Serial db(P3_0, P3_1);
 
 // I2C setup
 I2C i2c(P1_6, P1_7);            // P1_6 -> I2C1_SDA,  P1_7-> I2C1_SCL
@@ -99,17 +100,73 @@
     error = i2c.write(I2C_add, &reg_add, 1, 1);
     if (error)return error;
     error = i2c.read(I2C_add, bytes, n);
-    //if(DEBUG)db.printf("rr e[%d]\r\n",error);
     return error;
 }
 
+void process_command(uint8_t command)
+{
+    char data[130];
+    switch (command) 
+    {
+        case I2CReadReg:
+        {                                       
+            uint8_t slaveAddr = recv_report.data[2];
+            uint8_t regAddr = recv_report.data[3];
+            uint8_t numBytes = recv_report.data[4];
+            
+            int error = I2C_read_register(slaveAddr, regAddr, data, numBytes);
+            db.printf("slaveAddr = %02X regAddr = %02X data[0] = %02X numBytes = %d error = %d\r\n", slaveAddr, regAddr, data[0], numBytes, error);
+            bool ack = !error;
+            
+            send_report.data[0] = I2CReadReg;//OpCode = I2CReadReg
+            if(ack) 
+                send_report.data[1] = Success;//CmdStatus = Success
+            else
+                send_report.data[1] = Failure;//CmdStatus = Failure
+            for(int i = 0; i < numBytes; i++)
+            {
+                send_report.data[2 + i] = data[i];
+            }
+            
+            //Send the report
+            hid.send(&send_report);
+            break;
+        }
+        case I2CWriteReg:
+        {
+            uint8_t slaveAddr = recv_report.data[2];
+            uint8_t regAddr = recv_report.data[3];
+            uint8_t numBytes = recv_report.data[4];
+            
+            for(int i = 0; i < numBytes; i++)
+            {
+                data[i] = recv_report.data[5 + i];
+            }
+            
+            int error = I2C_write_register(slaveAddr, regAddr, data, numBytes);
+            db.printf("slaveAddr = %02X regAddr = %02X data[0] = %02X numBytes = %d error = %d\r\n", slaveAddr, regAddr, data[0], numBytes, error);
+            bool ack = !error;
+            
+            send_report.data[0] = I2CWriteReg;//OpCode = I2CWriteReg
+            if(ack) 
+                send_report.data[1] = Success;//CmdStatus = Success
+            else
+                send_report.data[1] = Failure;//CmdStatus = Failure
+            
+            //Send the report
+            hid.send(&send_report);
+            break;
+        }
+    }//end switch(c)
+}
+
 //******************************************************************************
 //        main()
 //******************************************************************************
 
 int main()
 {
-    pc.baud(9600);
+    db.baud(9600);
 
     //************* init ticker timer callbacks  ****************
     timer_1.attach(&LED_blink_callback, 0.5);     //start ticker, once per  sec.      
@@ -121,77 +178,15 @@
     bLED = LED_ON;
 
     send_report.length = 64;
-    //pc.printf("<HID_TEST_FW>\r\n");
     
-    
- 
-    while (1) {
+    while (1) 
+    {
         
-        //try to read a msg
-        if(hid.readNB(&recv_report)) {
-            
-        uint8_t command = recv_report.data[0];
-        char data[130];
-        switch (command) 
+        //if HID report is received...
+        if(hid.readNB(&recv_report)) 
         {
-            case 0x13:
-            {                                       
-                uint8_t slaveAddr = recv_report.data[2];
-                uint8_t regAddr = recv_report.data[3];
-                uint8_t numBytes = recv_report.data[4];
-                
-                int error = I2C_read_register(slaveAddr, regAddr, data, numBytes);
-                bool ack = !error;
-                
-                send_report.data[0] = 0x13;//OpCode = I2CReadReg
-                if(ack) 
-                    send_report.data[1] = 0x00; //CmdStatus = Success
-                else
-                    send_report.data[1] = 0x01;//CmdStatus = Failure
-                    for(int i = 0; i < numBytes; i++)
-                    {
-                        send_report.data[2 + i] = data[i];
-                    }
-                
-                //Send the report
-                hid.send(&send_report);
-                break;
-            }
-            case 0x12:
-            {
-                uint8_t slaveAddr = recv_report.data[2];
-                uint8_t regAddr = recv_report.data[3];
-                uint8_t numBytes = recv_report.data[4];
-                
-                for(int i = 0; i < numBytes; i++)
-                {
-                    data[i] = recv_report.data[5 + i];
-                }
-                
-                int error = I2C_write_register(slaveAddr, regAddr, data, numBytes);
-                bool ack = !error;
-                
-                send_report.data[0] = 0x12;//OpCode = I2CWriteReg
-                if(ack) 
-                    send_report.data[1] = 0x00; //CmdStatus = Success
-                else
-                    send_report.data[1] = 0x01;//CmdStatus = Failure
-                
-                //Send the report
-                hid.send(&send_report);
-                break;
-            }
-        }//end switch(c)
-            
-            /////send reply
-            /*for (int i = 0; i < send_report.length; i++) {
-            send_report.data[i] = i;
-            }*/
-            //Send the report
-            //hid.send(&send_report);
-            ///////////////
+            uint8_t command = recv_report.data[0];
+            process_command(command);
         }
-        
-        wait_ms(1);
     }
 }