usb vritual serial to uart

Dependencies:   BufferedSerial USBDevice mbed

Fork of USB2UART by Yihui Xiong

Committer:
swxu
Date:
Fri May 19 17:30:32 2017 +0000
Revision:
6:40182fd79c75
Parent:
5:10fccccbbb11
Child:
7:630d09697776
update for Uranus

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:8c4eea221dcf 1 /**
yihui 0:8c4eea221dcf 2 * USB to UART Bridge
yihui 0:8c4eea221dcf 3 */
yihui 0:8c4eea221dcf 4
yihui 0:8c4eea221dcf 5 #include "mbed.h"
yihui 0:8c4eea221dcf 6 #include "USBSerial.h"
yihui 4:9c038f12d460 7 #include "BufferedSerial.h"
yihui 0:8c4eea221dcf 8
swxu 6:40182fd79c75 9 Ticker txrx_ticker;
swxu 6:40182fd79c75 10 Ticker state_ticker;
swxu 6:40182fd79c75 11 Ticker reset_ticker;
yihui 3:2b4d2284bab0 12
yihui 3:2b4d2284bab0 13 volatile bool rxflag = false;
yihui 3:2b4d2284bab0 14 volatile bool txflag = false;
swxu 6:40182fd79c75 15 volatile int rx_count = 0;
swxu 6:40182fd79c75 16 volatile int tx_count = 0;
yihui 3:2b4d2284bab0 17
swxu 6:40182fd79c75 18 DigitalOut tx_indicator(P0_20);
swxu 6:40182fd79c75 19 DigitalOut rx_indicator(P0_21);
swxu 6:40182fd79c75 20 DigitalOut state_indicator(P0_11);
swxu 6:40182fd79c75 21 DigitalOut cc3200_reset(P0_2);
swxu 6:40182fd79c75 22 DigitalIn reset_button(P0_1, PullUp);
swxu 6:40182fd79c75 23
swxu 6:40182fd79c75 24 USBSerial vcom;
swxu 6:40182fd79c75 25 BufferedSerial uart(P0_19, P0_18, 512);
swxu 6:40182fd79c75 26
swxu 6:40182fd79c75 27 void update_txrx_indicator()
yihui 5:10fccccbbb11 28 {
swxu 6:40182fd79c75 29 if (txflag) {
swxu 6:40182fd79c75 30 tx_indicator = !tx_indicator;
yihui 3:2b4d2284bab0 31 rxflag = false;
swxu 6:40182fd79c75 32 } else { // TX done
swxu 6:40182fd79c75 33 if (!tx_indicator) {
swxu 6:40182fd79c75 34 tx_indicator = 1;
yihui 3:2b4d2284bab0 35 }
yihui 3:2b4d2284bab0 36 }
yihui 3:2b4d2284bab0 37
swxu 6:40182fd79c75 38 if (rxflag) {
swxu 6:40182fd79c75 39 rx_indicator = !rx_indicator;
swxu 6:40182fd79c75 40 rxflag = false;
swxu 6:40182fd79c75 41 } else { // RX done
swxu 6:40182fd79c75 42 if (!rx_indicator) {
swxu 6:40182fd79c75 43 rx_indicator = 1;
yihui 3:2b4d2284bab0 44 }
swxu 6:40182fd79c75 45 }
swxu 6:40182fd79c75 46 }
swxu 6:40182fd79c75 47
swxu 6:40182fd79c75 48 void update_state_indicator()
swxu 6:40182fd79c75 49 {
swxu 6:40182fd79c75 50 state_indicator = 0;
swxu 6:40182fd79c75 51 }
swxu 6:40182fd79c75 52
swxu 6:40182fd79c75 53 void check_reset_button()
swxu 6:40182fd79c75 54 {
swxu 6:40182fd79c75 55 if (!reset_button.read()) {
swxu 6:40182fd79c75 56 cc3200_reset = 0;
swxu 6:40182fd79c75 57 state_indicator = 0;
swxu 6:40182fd79c75 58 }
yihui 3:2b4d2284bab0 59 }
yihui 0:8c4eea221dcf 60
yihui 0:8c4eea221dcf 61 // Called by ISR
yihui 3:2b4d2284bab0 62 void settings_changed(int baud, int bits, int parity, int stop)
yihui 0:8c4eea221dcf 63 {
swxu 6:40182fd79c75 64 static const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
yihui 0:8c4eea221dcf 65
swxu 6:40182fd79c75 66 state_indicator = 1;
yihui 0:8c4eea221dcf 67 if (stop != 2) {
yihui 0:8c4eea221dcf 68 stop = 1; // stop bit(s) = 1 or 1.5
yihui 0:8c4eea221dcf 69 }
yihui 0:8c4eea221dcf 70 uart.baud(baud);
yihui 0:8c4eea221dcf 71 uart.format(bits, parityTable[parity], stop);
swxu 6:40182fd79c75 72 state_indicator = 0;
yihui 0:8c4eea221dcf 73 }
yihui 0:8c4eea221dcf 74
yihui 0:8c4eea221dcf 75 int main()
yihui 0:8c4eea221dcf 76 {
swxu 6:40182fd79c75 77 vcom.attach(settings_changed);
swxu 6:40182fd79c75 78 state_ticker.attach(update_state_indicator, 0.3);
swxu 6:40182fd79c75 79 txrx_ticker.attach_us(update_txrx_indicator, 10*1000);
swxu 6:40182fd79c75 80 reset_ticker.attach_us(check_reset_button, 20*1000);
swxu 6:40182fd79c75 81
yihui 0:8c4eea221dcf 82 while (1) {
swxu 6:40182fd79c75 83 while (vcom.readable()) {
swxu 6:40182fd79c75 84 char c = vcom.getc();
swxu 6:40182fd79c75 85 tx_count++;
swxu 6:40182fd79c75 86 txflag = true;
swxu 6:40182fd79c75 87 uart.putc(c);
swxu 6:40182fd79c75 88 }
yihui 0:8c4eea221dcf 89 while (uart.readable()) {
swxu 6:40182fd79c75 90 char c = uart.getc();
swxu 6:40182fd79c75 91 rx_count++;
yihui 3:2b4d2284bab0 92 rxflag = true;
swxu 6:40182fd79c75 93 vcom.putc(c);
yihui 0:8c4eea221dcf 94 }
yihui 0:8c4eea221dcf 95 }
yihui 0:8c4eea221dcf 96 }