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:
3:e8cc286f9b2e
Parent:
2:efaf8aee55df
Child:
4:13e3e375c0d3
--- a/main.cpp	Mon Jan 19 23:48:21 2015 +0000
+++ b/main.cpp	Tue Jan 20 01:09:41 2015 +0000
@@ -46,6 +46,8 @@
 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];
 int i, j;                           // index variables
 int status = 0x00;                  // return value
 
@@ -68,7 +70,7 @@
     rfid_rst = 0;                                                   // ... taking RFID out of reset
         
     // Prox
-    i2c.frequency(400000);
+    i2c.frequency(400000);                                          // I2C speed = 400Kbps
     prox_int.mode(PullUp);                                          // pull up proximity sensor interrupt at MCU
     
     
@@ -102,8 +104,8 @@
     }
     
     end_mark = FALSE;
-    wait(0.5);
-    while(!uart.readable()) printf(".");                            // wait for data from rfid
+    //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++)
     {
@@ -127,6 +129,51 @@
 
 int prox_msg()
 {
+ 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();
+    
+    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;
+        }
+    }
+    
+    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;    
 }
 
@@ -144,15 +191,16 @@
 {
     // initialize everything
    
-    wait(3.0);
+    wait(3.0);                                                      // debug - gives some time to start terminal program and open COM port
+    
     init_periph();
     
-    cdc.printf("Starting...\n\r");
+    //cdc.printf("Starting...\n\r");                                // debug
     
     while(!cdc.readable());                                         // spin here until a message comes in from the host PC
     bool end_mark = FALSE;
     uint8_t crcCount = sizeof(cdc_buffer_rx);                       // use tx buffer size to start
-    cdc.printf("\n\rCDC Input: ");
+    //cdc.printf("\n\rCDC Input: ");                                // debug
     for (i = 0; i < sizeof(cdc_buffer_rx); i++)
     {
         cdc_buffer_rx[i] = cdc.getc();                              // read data from USB side
@@ -190,7 +238,7 @@
             
             for (i = 0; i < sizeof(cdc_buffer_tx); i++)
             {
-                //cdc.printf("%x, ", cdc_buffer_tx[i]);             // debug 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
                 {
@@ -205,7 +253,36 @@
             }
             break;
         case 0xCC:                                                  // Proximity Sensor
-            prox_msg();
+           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
+            }
+            
+            status = prox_msg();                                    // send buffer to RFID and get response according to RFID board
+            
+            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.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
+                    
+                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;
         case 0xDD:                                                  // GPIO (LEDs and RFID-FE control
             gpio_msg();