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:
23:52504c8f63c5
Parent:
22:5707b236cbdb
Child:
24:41e424031aba
--- a/main.cpp	Fri Jun 05 01:52:30 2015 +0000
+++ b/main.cpp	Tue Jun 16 23:03:29 2015 +0000
@@ -240,9 +240,9 @@
     // Factory test should initialize the EEPROM
     //
     // First two bytes of bank zero will be "SV" if programmed.
-    // Bank 0x10 holds Manufacturer string
-    // Bank 0x40 holds Serial Number string
-    // Bank 0x70 holds Product Description string
+    // Address 0x10 holds Manufacturer string
+    // Address 0x40 holds Serial Number string
+    // Address 0x70 holds Product Description string
 
     i2c_page = EEPROM | 0 << 1;                                     // set EEPROM page = bank 0
     i2c.write(i2c_page, &temploc, 1, 1);                            // i2c address+bank 0, memory index, 1 byte (just index), no stop at end.
@@ -700,6 +700,31 @@
 }
 
 /**
+ * @name    clrcdctx
+ * @brief   Clear CDC TX buffer
+ *
+ * @returns [none]
+ */
+void clrcdctx(void)
+{
+    for (i = 0; i < sizeof(cdc_buffer_tx); i++){
+        cdc_buffer_tx[i] = 0x00;                                    // clear CDC tx buffer
+    }    
+}
+
+/**
+ * @name    clrcdcrx
+ * @brief   Clear CDC RX buffer
+ *
+ * @returns [none]
+ */
+void clrcdcrx(void)
+{
+    for (i = 0; i < sizeof(cdc_buffer_rx); i++){
+        cdc_buffer_rx[i] = 0x00;                                    // clear CDC tx buffer
+    }    
+}
+/**
  * @name    main
  * @brief   Main firmware loop
  *
@@ -720,6 +745,9 @@
     while(1) {                                                      // main loop
         led_com.write(led_com_state);                               // turn off communication LED unless it was specifically turned on by GPIO command
         if (prox_irq_state == 1) {                                  // process the proximity interrupt
+            clrcdctx();
+
+
             if (prox_irq_msg[0] != 0xBB){                           // no message to send to RFID
                 cdc_buffer_tx[0] = 0xFF;                            // just send a dummy message back to the host
                 cdc_buffer_tx[1] = 0x7E;
@@ -741,6 +769,7 @@
 
         }
         if (uart.readable()) {                                      // message availalbe from rfid (all responses (0x01) and broadcast (0x02))
+            clrcdctx();
             int rfid_len = 0;
             led_com.write(LEDON);
             for (i = 0; i < (RFIDLENLOW); i++) {                    // Get first part of message to find out total count
@@ -758,7 +787,7 @@
         }
         if (usb_irq_state == 1) {                                   // message available from PC
             usb_irq_state = 0;                                      // allow another USB interrupt 
-
+            clrcdcrx();                                             // clear cdc RX buffer
             for (i = 0; i < sizeof(cdc_buffer_rx); i++) {
                 if (cdc.readable()) cdc_buffer_rx[i] = cdc._getc(); // read data from USB side
             }
@@ -777,6 +806,9 @@
             }
 
             switch(cdc_buffer_rx[0]) {                              // check first byte for "destination"
+                case 0xAA:                                          // RFID-FE Firmware Update
+                
+                    break;
                 case 0xBB:                                          // RFID-FE
                     for (i = 0; i < sizeof(cdc_buffer_rx); i++) {
                         uart_buffer_tx[i] = cdc_buffer_rx[i];       // copy USB message to UART for RFID
@@ -786,6 +818,7 @@
                     break;
 
                 case 0xCC:                                          // Proximity Sensor
+                    clrcdctx();
                     led_com.write(LEDON);
                     for (i = 0; i < sizeof(cdc_buffer_rx); i++) {
                         i2c_buffer[i] = cdc_buffer_rx[i];           // copy USB message to buffer for I2C
@@ -815,6 +848,7 @@
                     break;
 
                 case 0xDD:                                          // GPIO (LEDs and RFID-FE control)
+                    clrcdctx();
                     led_com.write(LEDON);
                     for (i = 0; i < sizeof(cdc_buffer_rx); i++) {
                         gpio_buffer[i] = cdc_buffer_rx[i];          // copy USB message to buffer for GPIO
@@ -843,6 +877,7 @@
                     break;
 
                 case 0xEE:                                          // Read/write EEPROM
+                    clrcdctx();
                     led_com.write(LEDON);
                     for (i = 0; i < sizeof(cdc_buffer_rx); i++) {
                         i2c_buffer[i] = cdc_buffer_rx[i];           // copy USB message to buffer for I2C
@@ -853,7 +888,7 @@
                     else if (i2c_buffer[1] == 0)                    // I2C write = 0
                         status = eeprom_msg_wr();                   // send buffer to EEPROM and get response
 
-                    if (status) led_err.write(LEDON);
+                    //if (status) led_err.write(LEDON);
 
                     em_pos = 0;
                     for (i = 0; i < sizeof(cdc_buffer_tx); i++) {
@@ -872,21 +907,22 @@
                     break;
 
                 case 0xFF:
-                    if (cdc_buffer_rx[1] > 0){
+                    clrcdctx();
+                    if (cdc_buffer_rx[1] > 0){                      // FF, 01, 7E, 0F, F0 = read message from EEPROM and turn on prox IRQ
                         // read message sent to RFID on interrupt
                         
                         temploc = 0x12;
-                        i2c.write(i2c_addr, &temploc, 1, 1);        // i2c address+bank 0, RFID message size location, 1 byte, no stop at end
-                        i2c.read(i2c_addr, &msgcount, 1, 0);        // i2c address+bank 0, RFID message size, 1 byte, stop at end
+                        i2c.write(i2c_addr, &temploc, 1, 1);        // i2c address+bank 2, RFID message size location, 1 byte, no stop at end
+                        i2c.read(i2c_addr, &msgcount, 1, 0);        // i2c address+bank 2, RFID message size, 1 byte, stop at end
                         msgcount += 6;                              // get the real size
                         temploc = 0x10;
-                        i2c.write(i2c_addr, &temploc, 1, 1);        // i2c address+bank 0, RFID message location, 1 byte, no stop at end
-                        i2c.read(i2c_addr, prox_irq_msg, msgcount, 0);  // i2c address+bank 0, description string, string size, stop at end
+                        i2c.write(i2c_addr, &temploc, 1, 1);        // i2c address+bank 2, RFID message location, 1 byte, no stop at end
+                        i2c.read(i2c_addr, prox_irq_msg, msgcount, 0);  // i2c address+bank 2, description string, string size, stop at end
 
                         autorange[2] = 0x03;
                         i2c.write(PROX, autorange, 3, 0);           // Start contonuous proximity range read
                     }
-                    else {
+                    else {                                          // FF, 00, 7E, 0F, F0 = turn off prox irq
                         prox_irq_msg[0] = 0xFF;                     // clear first byte
                         autorange[2] = 0x00;
                         i2c.write(PROX, autorange, 3, 0);           // Stop range read
@@ -895,7 +931,7 @@
                     break;
                 default:
                     led_err.write(LEDON);                           // we should never get here
-                    while(1);                                       // halt!
+                    //while(1);                                       // halt!
             }
         }
         led_com.write(led_com_state);