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:
19:d3bef4fbab69
Parent:
18:d128c7020292
Child:
22:5707b236cbdb
--- a/main.cpp	Mon Mar 02 17:00:37 2015 +0000
+++ b/main.cpp	Mon Mar 09 18:55:19 2015 +0000
@@ -13,7 +13,7 @@
 
 #include "mbed.h"
 #include "USBSerial.h"
-//#include "MODSERIAL.h"
+#include "MODSERIAL.h"
 
 // Constants
 #define LEDON       0                   // Low active for LEDs - turns LED on
@@ -46,8 +46,7 @@
 
 // Peripherals
 USBSerial cdc;                          // CDC Class USB<>Serial adapter.  Needs custom INF, but uses existing Windows drivers.
-//MODSERIAL uart(PTA2, PTA1);             // UART port connected to RFID-FE board
-Serial uart(PTA2, PTA1);             // UART port connected to RFID-FE board
+MODSERIAL uart(PTA2, PTA1);             // UART port connected to RFID-FE board
 I2C i2c(PTB1, PTB0);                    // I2C port connected to VL6180X and EEPROM - note addresses above)
 
 // GPIO signals
@@ -60,6 +59,10 @@
 DigitalIn rfid_hot(PTE0);               // RFID over-current detection on USB board power switch (active low)
 InterruptIn prox_int(PTD7);             // Proximity sensor interrupt (active low)
 
+DigitalInOut gpio_0(PTC3);              // Extra GPIO on LED connector, pin 6
+DigitalInOut gpio_1(PTC4);              // Extra GPIO on LED connector, pin 7
+DigitalOut gpio_7(PTA4);                // Extra GPIO output on LED connector, pin 3 - pin supports PWM for buzzer, not implemented here
+
 // buffers & variables
 uint8_t gpio_values = 0x00;             // register to read GPIO values
 uint8_t cdc_buffer_rx[BUFFERSIZE];      // buffers for cdc (USB-Serial port on PC)          
@@ -119,9 +122,9 @@
     rfid_rst = 1;                                                   // RFID FE Reset (active high)
 //    rfid_pwr = 1;                                                   // RFID power switch on USB board (active high for prototype 1)
     rfid_pwr = 0;                                                   // RFID power switch on USB board (active low for all others)
-    wait(0.35);                                                     // wait 250ms before...
+    wait(0.25);                                                     // wait 250ms before...
     rfid_rst = 0;                                                   // ... taking RFID out of reset
-    wait(0.35);
+    wait(0.25);
     
     while(!uart.readable())                                           // wait for RESET message from RFID
     {
@@ -131,8 +134,8 @@
         wait(0.1);
     }                                           
     
-    //uart.txBufferFlush();                                           // clear out UART buffers
-    //uart.rxBufferFlush();
+    uart.txBufferFlush();                                           // clear out UART buffers
+    uart.rxBufferFlush();
     
     led_err.write(LEDOFF);
         
@@ -200,41 +203,6 @@
 }    
 
 /**
- * @name    rfid_rd
- * @brief   Sends RFID response to CDC  
- *
- * RFID reader is connected to the KL25Z UART interface.  The host PC will have a USB CDC class COM port device driver.
- * The host PC sends the RFID command over the COM port.  Messages destined for the RFID reader (0xBB leading byte) are 
- * forwarded as-is to the RFID reader.  The reader then responds in kind.  All RFID commands are described in the
- * RFID-FE module manual.
- 
- * @param [in] uart_buffer_rx - messages from the RFID reader
- * 
- * @retval ERR_NONE                No error
- * @example
- * BB 00 03 00 01 02 7E 2E C9 = read 
- */
-int rfid_rd(void)
-{
-    int rfid_len = 0;
-
-    led_com.write(LEDON);
-    for (i = 0; i < (RFIDLENLOW); i++)                              // Get first part of message to find out total count
-    {
-        uart_buffer_rx[i] = uart.getc();                            // get a byte from rfid
-    }
-    rfid_len = ((uart_buffer_rx[i-2]<<8) + (uart_buffer_rx[i-1]));  // location of message length for RFID
-
-    for (i = RFIDLENLOW; i < (RFIDLENLOW + rfid_len + 3); i++)
-    {
-        uart_buffer_rx[i] = uart.getc();                            // get a byte from rfid
-    }
-
-    led_com.write(LEDOFF);
-    return ERR_NONE;
-}
-
-/**
  * @name    prox_msg_wr
  * @brief   Forwards command to VL6180X sensor 
  *
@@ -319,11 +287,11 @@
  * GPIO messages:       
  * - 0xDD                (byte) leading value = 0xDD
  * - r/w#                (byte) 0 = write, 1 = read
- * - data                (byte) see below
+ * - data                (2 bytes) see below
  * - end_mark            (byte) 0x7E
  * - dummy               (2 bytes) values are don't-care - fillers for RFID CRC bytes
  *
- * GPIO data bits:      
+ * GPIO data bits - First Byte
  * - 0 LED - Error           0 = on, 1 = off                  
  * - 1 LED - Comm state      0 = on, 1 = off
  * - 2 RFID interrupt input  0 = off, 1 = on (inverted in h/w)
@@ -333,6 +301,12 @@
  * - 6 RFID over-current     0 = overcurrent detected, 1 = OK
  * - 7 Proximity interrupt   0 = interrupt, 1 = idle (This pin may not return anything meaningful here.  The interrupt is edge triggered).
  *
+ * GPIO data bits - Second Byte
+ * - 0 GPIO on pin 6 of LED connector - I/O
+ * - 1 GPIO on pin 7 of LED connector - I/O
+ * - 2-6 - unused bits, output don't care, input read as zero
+ * - 7 GPIO on pin 3 of LED connector - OUTPUT only, inverted logic, open drain with 1K pull-up, read as zero
+ *
  * @param [in/out] gpio_buffer - GPIO states
  *
  * @retval 0            No error
@@ -349,6 +323,15 @@
     gpio_buffer[2] |= ((rfid_pwr.read() << 5) & 0x20);
     gpio_buffer[2] |= ((rfid_hot.read() << 6) & 0x40);
     gpio_buffer[2] |= ((prox_int.read() << 7) & 0x80);
+
+    gpio_buffer[3]  = (   gpio_0.read()       & 0x01);              // read all of the GPIO pins and store in a single byte
+    gpio_buffer[3] |= ((  gpio_1.read() << 1) & 0x02);              // use of led_com_state allows the led to be ON during this call, but send back the pre-call state.
+    gpio_buffer[3] |= ((              0 << 2) & 0x04);              // bits 2-7 not implemented for read
+    gpio_buffer[3] |= ((              0 << 3) & 0x08);
+    gpio_buffer[3] |= ((              0 << 4) & 0x10);
+    gpio_buffer[3] |= ((              0 << 5) & 0x20);
+    gpio_buffer[3] |= ((              0 << 6) & 0x40);
+    gpio_buffer[2] |= ((              0 << 7) & 0x80);
     return ERR_NONE;
 }
 
@@ -378,6 +361,12 @@
  * - 6 RFID over-current     0 = overcurrent detected, 1 = OK
  * - 7 Proximity interrupt   0 = interrupt, 1 = idle (This pin may not return anything meaningful here.  The interrupt is edge triggered).
  *
+ * GPIO data bits - Second Byte
+ * - 0 GPIO on pin 6 of LED connector - I/O
+ * - 1 GPIO on pin 7 of LED connector - I/O
+ * - 2-6 - unused bits, output don't care, input read as zero
+ * - 7 GPIO on pin 3 of LED connector - OUTPUT only, inverted logic, open drain with 1K pull-up, read as zero
+ *
  * @param [in/out] gpio_buffer - GPIO states
  *
  * @retval 0            No error
@@ -393,12 +382,17 @@
     {
         led_com_state = LEDOFF;
     }
-    led_err.write(gpio_buffer[2] & 0x01);                              
-    led_com.write(led_com_state);                              
+    led_err.write(gpio_buffer[2] & 0x01);             // Write GPIO bits - first byte                 
+    led_com.write(led_com_state);                     // use of led_com_state allows the led to be ON during this call, but send back the pre-call state.         
     rfid_int.write(gpio_buffer[2] & 0x04);                             
     rfid_isp.write(gpio_buffer[2] & 0x08);                             
     rfid_rst.write(gpio_buffer[2] & 0x10);                             
     rfid_pwr.write(gpio_buffer[2] & 0x20);                             
+
+    gpio_0.write(gpio_buffer[3] & 0x01);              // Write GPIO bits - second byte
+    gpio_1.write(gpio_buffer[3] & 0x02);              // Write GPIO bits - second byte
+    gpio_7.write(gpio_buffer[3] & 0x80);              // Write GPIO bits - second byte
+
     return ERR_NONE;
 }
     
@@ -517,26 +511,25 @@
             cdc_buffer_tx[3] = 0xF0;
             cdc.writeBlock(cdc_buffer_tx, 4);
         }
-
         if (uart.readable())                                        // message availalbe from rfid
-//        while (uart.readable())                                        // message availalbe from rfid
         {
-            rfid_rd();
-            em_pos = 0;
-            for (i = 0; i < sizeof(cdc_buffer_tx); i++)
+            int rfid_len = 0;
+            led_com.write(LEDON);
+            for (i = 0; i < (RFIDLENLOW); i++)                              // Get first part of message to find out total count
             {
-                cdc_buffer_tx[i] = uart_buffer_rx[i];               // copy RFID response back to USB buffer
-                if (cdc_buffer_tx[i] == 0x7E) 
-                {
-                    em_pos = (i + 1);                               // allows 0x7E to appear in the data payload - uses last one for end-mark
-                }
+                uart_buffer_rx[i] = uart.getc();                            // get a byte from rfid
+            }
+            rfid_len = ((uart_buffer_rx[i-2]<<8) + (uart_buffer_rx[i-1]));  // location of message length for RFID
+            for (i = RFIDLENLOW; i < (RFIDLENLOW + rfid_len + 3); i++)
+            {
+                uart_buffer_rx[i] = uart.getc();                            // get a byte from rfid
             }
-            if (em_pos == 0) 
+            for (i = 0; i < (RFIDLENLOW + rfid_len + 3); i++)
             {
-                led_err.write(LEDON);                               // end mark never reached
-                break;
+                cdc_buffer_tx[i] = uart_buffer_rx[i];
             }
-            cdc.writeBlock(cdc_buffer_tx, (em_pos + 2));
+            cdc.writeBlock(cdc_buffer_tx, (RFIDLENLOW + rfid_len + 3));
+            led_com.write(LEDOFF);
         }                                         
         if (usb_irq_state == 1)                                     // message available from PC
         {