Super Vision / Mbed 2 deprecated sv_usb_firmware

Dependencies:   MODSERIAL USBDevice_for_Rev_C_HW mbed

Fork of mbed_sv_firmware_with_init by Bob Recny

Revision:
4:13e3e375c0d3
Parent:
3:e8cc286f9b2e
Child:
5:e77529f7ede3
--- a/main.cpp	Tue Jan 20 01:09:41 2015 +0000
+++ b/main.cpp	Tue Jan 20 22:26:26 2015 +0000
@@ -17,6 +17,10 @@
 #define ERR_UART_NOT_WRITEABLE  3   // UART has no buffer space
 #define ERR_UART_NO_TX_ENDMARK  4   // message for UART has no 0x7E end-mark
 #define ERR_UART_NO_RX_ENDMARK  5   // message received from UART has no end-mark
+#define ERR_I2C_NOT_WRITEABLE   6   // UART has no buffer space
+#define ERR_I2C_NO_TX_ENDMARK   7   // message for UART has no 0x7E end-mark
+#define ERR_I2C_NO_RX_ENDMARK   8   // message received from UART has no end-mark
+#define ERR_NOT_IMPLEMENTED     255 // method has not yet been implemented
 
 
 // I2C addresses
@@ -46,8 +50,7 @@
 uint8_t cdc_buffer_tx[32];
 uint8_t uart_buffer_rx[32];         // buffers for uart (RFID-FE board)
 uint8_t uart_buffer_tx[32];
-uint8_t i2c_buffer_rx[32];          // buffers for I2C devices - Proximity sensor and EEPROM
-uint8_t i2c_buffer_tx[32];
+char i2c_buffer[264];               // buffer for I2C devices - Proximity sensor and EEPROM - up to 256 bytes data payload for EEPROM, up to 4 for proximity
 int i, j;                           // index variables
 int status = 0x00;                  // return value
 
@@ -77,6 +80,9 @@
     return 0;
 }
 
+/*
+RFID messages are as defined in the RFID-FE manual.
+ */
 int rfid_msg(void)
 {
     bool end_mark = FALSE;
@@ -127,54 +133,49 @@
 }
 
 
-int prox_msg()
+/*
+I2C-prox messages = 0xCC, r/w, number of data bytes, index (2 bytes), data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
+
+Multiple registers can be read or written with single prox_msg_rd() or prox_msg_wr().  Location address increments for each byte.
+ */
+
+int prox_msg_wr()                                                   // write proximity I2C register
 {
- bool end_mark = FALSE;
-    int i;
-    uint8_t crcCount = sizeof(i2c_buffer_tx);                      // use tx buffer size to start
-    
-    i2c.txBufferFlush();                                           // clear out UART buffers
-    i2c.rxBufferFlush();
+    int i2c_err;
+    i2c_err = i2c.write(PROX, &i2c_buffer[3], i2c_buffer[2] + 2, 0);// I2C Address, pointer to buffer, number of bytes (for index + data), stop at end.
+    return i2c_err;                                                 // 0 = ACK received, 1 = NAK/failure
+}
     
-    for (int i = 0; i < sizeof(uart_buffer_tx); i++)
-    {
-        if (!uart.writeable()) return ERR_UART_NOT_WRITEABLE;       // if no space in uart, return error
-        uart.putc(uart_buffer_tx[i]);                               // send uart message
-                    
-        if (uart_buffer_tx[i] == 0x7E)                              // check for rfid end mark in outbound message
-        {
-            crcCount = 2;                                           // two more bytes for CRC
-            end_mark = TRUE;                                        // end mark was reached
-        }
-        if (crcCount-- == 0)                                        // end of message
-        {
-            if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK;   // no end mark detected
-            break;
-        }
-    }
+int prox_msg_rd()
+{
+    int i2c_err;
+    i2c_err = i2c.write(PROX, &i2c_buffer[3], 2, 1);                // I2C Address, pointer to buffer (just the index), index, number of bytes (2 for index), no stop at end.
+    i2c_err |= i2c.read(PROX, &i2c_buffer[5], i2c_buffer[2], 0);    // I2C Address, pointer to buffer (just the data), number of data bytes, stop at end.
+    return i2c_err;                                                 // 0 = ACK received, 1 = NAK/failure
+}
+
+
+/*
+I2C-EEPROM messages = 0xEE, r/w, number of data bytes, block, address, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
+
+Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr().  Location address increments for each byte.
+This practically the the same as the proximity calls, except the index/location is only one byte and the block select is part of the I2C address byte.
+ */
+
+int eeprom_msg_wr()                                                 // write proximity I2C register
+{
+    int i2c_err;
+    i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], i2c_buffer[2] + 1, 0);  // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end.
+    while (!i2c.write(EEPROM || i2c_buffer[3]));                     // wait until write is done (EEPROM will ACK = 1 for single byte i2c.write)
+    return i2c_err;                                                 // 0 = ACK received, 1 = NAK/failure
+}
     
-    end_mark = FALSE;
-    //wait(0.5);                                                    // debug
-    while(!uart.readable());                                        // wait for data from rfid
-    crcCount = sizeof(uart_buffer_rx);                              // use rx buffer size to start
-    for (i = 0; i < sizeof(uart_buffer_rx); i++)
-    {
-        uart_buffer_rx[i] = uart.getc();                            // read a character
-//        cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]);         // debug
-                
-        if (uart_buffer_rx[i] == 0x7E)                              // check for rfid end mark in inbound message
-        {
-            crcCount = 2;                                           // two more bytes for crc
-            end_mark = TRUE;                                        // end mark was reached
-        }
-        if (crcCount-- == 0)                                        // end of message
-        {
-            if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK;  
-            break;
-        }
-    }
-    return ERR_NONE;
-    return ERR_NONE;    
+int eeprom_msg_rd()
+{
+    int i2c_err;
+    i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], 1, 1);                // I2C Address & block select, pointer to buffer (just the index), index, number of bytes (for address + data), no stop at end.
+    i2c_err |= i2c.read((EEPROM || i2c_buffer[3]), &i2c_buffer[5], i2c_buffer[2], 0);   // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end.
+    return i2c_err;                                                 // 0 = ACK received, 1 = NAK/failure
 }
 
 int gpio_msg()
@@ -182,11 +183,6 @@
     return ERR_NONE;
 }
 
-int eeprom_msg()                                                    // eeprom to be implemented along with next hardware prototype
-{
-    return ERR_NONE;
-}
-
 int main() 
 {
     // initialize everything
@@ -238,7 +234,7 @@
             
             for (i = 0; i < sizeof(cdc_buffer_tx); i++)
             {
-                cdc.putc(), cdc_buffer_tx[i]);                      // send message back to PC
+                cdc.putc(cdc_buffer_tx[i]);                         // send message back to PC
                     
                 if (cdc_buffer_tx[i] == 0x7E)                       // check for rfid end mark in outbound message
                 {
@@ -252,24 +248,26 @@
                 }
             }
             break;
-        case 0xCC:                                                  // Proximity Sensor
-           for (i = 0; i < sizeof(cdc_buffer_rx); i++)
+        case 0xCC:            
+        //I2C-prox messages = 0xCC, r/w, number of data bytes, index, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID                                      // Proximity Sensor
+            for (i = 0; i < sizeof(cdc_buffer_rx); i++)
             {
-                i2c_buffer_tx[i] = cdc_buffer_rx[i + 1];           // copy USB message to buffer for I2C
+                i2c_buffer[i] = cdc_buffer_rx[i];                   // copy USB message to buffer for I2C
             }
             
-            status = prox_msg();                                    // send buffer to RFID and get response according to RFID board
+            if (i2c_buffer[1] == 1)                                 // I2C read = 1
+                status = prox_msg_rd();                             // read the requested data
+            else if (i2c_buffer[1] == 0)                             // I2C write = 0
+                status = prox_msg_wr();                             // send buffer to proximity sensor and get response
             
             for (i = 0; i < sizeof(cdc_buffer_tx); i++)
             {
-                cdc_buffer_tx[i] = uart_buffer_rx[i];               // copy RFID response back to USB buffer
+                cdc_buffer_tx[i] = i2c_buffer[i];                   // copy prox response back to USB buffer
             }
             
-            //cdc.printf("\n\rRFID Response: ");                    // debug
-            
             for (i = 0; i < sizeof(cdc_buffer_tx); i++)
             {
-                cdc.putc(), cdc_buffer_tx[i]);                      // send message back to PC
+                cdc.putc(cdc_buffer_tx[i]);                         // send message back to PC
                     
                 if (cdc_buffer_tx[i] == 0x7E)                       // check for rfid end mark in outbound message
                 {
@@ -282,14 +280,47 @@
                     break;
                 }
             }
-            
             break;
         case 0xDD:                                                  // GPIO (LEDs and RFID-FE control
             gpio_msg();
             break;
         case 0xEE:                                                  // Read/write EEPROM
-            eeprom_msg();
-            break;
+/*
+I2C-EEPROM messages = 0xEE, r/w, number of data bytes, block, address, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
+
+Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr().  Location address increments for each byte.
+This practically the the same as the proximity calls, except the index/location is only one byte and the block select is part of the I2C address byte.
+ */
+            for (i = 0; i < sizeof(cdc_buffer_rx); i++)
+            {
+                i2c_buffer[i] = cdc_buffer_rx[i];                   // copy USB message to buffer for I2C
+            }
+            
+            if (i2c_buffer[1] == 1)                                 // I2C read = 1
+                status = eeprom_msg_rd();                           // read the requested data
+            else if (i2c_buffer[1] == 0)                            // I2C write = 0
+                status = eeprom_msg_wr();                           // write the eeprom location
+            
+            for (i = 0; i < sizeof(cdc_buffer_tx); i++)
+            {
+                cdc_buffer_tx[i] = i2c_buffer[i];                   // copy prox response back to USB buffer
+            }
+            
+            for (i = 0; i < sizeof(cdc_buffer_tx); i++)
+            {
+                cdc.putc(cdc_buffer_tx[i]);                         // send message back to PC
+                    
+                if (cdc_buffer_tx[i] == 0x7E)                       // check for rfid end mark in outbound message
+                {
+                    crcCount = 2;                                   // two more bytes for CRC
+                    end_mark = TRUE;                                // end mark was reached
+                }
+                if (crcCount-- == 0)                                // end of message
+                {
+                    if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
+                    break;
+                }
+            }            break;
         default:
             return ERR_CDC_BAD_CMD;
     }