usb vritual serial to uart

Dependencies:   BufferedSerial USBDevice mbed

Fork of USB2UART by Yihui Xiong

Revision:
6:40182fd79c75
Parent:
5:10fccccbbb11
Child:
7:630d09697776
--- a/main.cpp	Fri Mar 04 14:00:51 2016 +0000
+++ b/main.cpp	Fri May 19 17:30:32 2017 +0000
@@ -6,89 +6,91 @@
 #include "USBSerial.h"
 #include "BufferedSerial.h"
 
-BufferedSerial uart(P0_19, P0_18, 512);
-USBSerial pc;
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
+Ticker txrx_ticker;
+Ticker state_ticker;
+Ticker reset_ticker;
 
-Ticker ticker;
 volatile bool rxflag = false;
 volatile bool txflag = false;
-
+volatile int rx_count = 0;
+volatile int tx_count = 0;
 
-#ifdef TARGET_LPC11UXX
-#include "LPC11Uxx.h"
-void enable_hardware_flow_control()
+DigitalOut tx_indicator(P0_20);
+DigitalOut rx_indicator(P0_21);
+DigitalOut state_indicator(P0_11);
+DigitalOut cc3200_reset(P0_2);
+DigitalIn reset_button(P0_1, PullUp);
+
+USBSerial vcom;
+BufferedSerial uart(P0_19, P0_18, 512);
+
+void update_txrx_indicator()
 {
-    LPC_IOCON->PIO0_17 = 0x01 | (0x1 << 3);     // RTS, 
-    LPC_IOCON->PIO0_7  = 0x01 | (0x1 << 3);     // CTS, pull-down
-    
-    // enable auto RTS and CTS
-    LPC_USART->MCR = (1 << 6) | (1 << 7);
-}
-#endif
-
-
-void indicate()
-{
-    if (rxflag) {
-        led3 = !led3;
+    if (txflag) {
+        tx_indicator = !tx_indicator;
         rxflag = false;
-    } else {
-        if (led3) {
-            led3 = 0;
+    } else { // TX done
+        if (!tx_indicator) {
+            tx_indicator = 1;
         }
     }
     
-    if (txflag) {
-        led4 = !led4;
-        txflag = false;
-    } else {
-        if (led4) {
-            led4 = 0;
+    if (rxflag) {
+        rx_indicator = !rx_indicator;
+        rxflag = false;
+    } else { // RX done
+        if (!rx_indicator) {
+            rx_indicator = 1;
         }
-    }   
+    }
+}
+
+void update_state_indicator()
+{
+    state_indicator = 0;
+}
+
+void check_reset_button()
+{
+    if (!reset_button.read()) {
+        cc3200_reset = 0;
+        state_indicator = 0;
+    }
 }
 
 // Called by ISR
 void settings_changed(int baud, int bits, int parity, int stop)
 {
-    const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
+    static const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
     
-    
-    led1 = 1;
+    state_indicator = 1;
     if (stop != 2) {
         stop = 1;   // stop bit(s) = 1 or 1.5
     }
     uart.baud(baud);
     uart.format(bits, parityTable[parity], stop);
-    led1 = 0;
+    state_indicator = 0;
 }
 
-
 int main()
 {
-#ifdef TARGET_LPC11UXX
-    enable_hardware_flow_control();
-#endif
-    
-    pc.attach(settings_changed);
-    ticker.attach_us(indicate, 500);
-    
+    vcom.attach(settings_changed);
+    state_ticker.attach(update_state_indicator, 0.3);
+    txrx_ticker.attach_us(update_txrx_indicator, 10*1000);
+    reset_ticker.attach_us(check_reset_button, 20*1000);
+
     while (1) {
+        while (vcom.readable()) {
+            char c = vcom.getc();
+            tx_count++;
+            txflag = true;
+            uart.putc(c);
+        }
         while (uart.readable()) {
+            char c = uart.getc();
+            rx_count++;
             rxflag = true;
-            char r = uart.getc();
-            pc.putc(r);
-        }
-        
-        while (pc.readable()) {
-            char r = pc.getc();
-            
-            txflag = true;
-            uart.putc(r);
+            vcom.putc(c);
         }
     }
 }