A USB to UART bridge

Dependencies:   USBDevice BufferedSerial mbed

Committer:
yihui
Date:
Wed Oct 30 02:01:50 2013 +0000
Revision:
0:8c4eea221dcf
Child:
1:efa9f62a12c4
a USB to UART bridge

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 0:8c4eea221dcf 7
yihui 0:8c4eea221dcf 8 Serial uart(USBTX, USBRX);
yihui 0:8c4eea221dcf 9 USBSerial pc;
yihui 0:8c4eea221dcf 10
yihui 0:8c4eea221dcf 11 // Called by ISR
yihui 0:8c4eea221dcf 12 void settingsChanged(int baud, int bits, int parity, int stop)
yihui 0:8c4eea221dcf 13 {
yihui 0:8c4eea221dcf 14 const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
yihui 0:8c4eea221dcf 15
yihui 0:8c4eea221dcf 16 if (stop != 2) {
yihui 0:8c4eea221dcf 17 stop = 1; // stop bit(s) = 1 or 1.5
yihui 0:8c4eea221dcf 18 }
yihui 0:8c4eea221dcf 19
yihui 0:8c4eea221dcf 20 uart.baud(baud);
yihui 0:8c4eea221dcf 21 uart.format(bits, parityTable[parity], stop);
yihui 0:8c4eea221dcf 22 }
yihui 0:8c4eea221dcf 23
yihui 0:8c4eea221dcf 24 int main()
yihui 0:8c4eea221dcf 25 {
yihui 0:8c4eea221dcf 26 pc.attach(settingsChanged);
yihui 0:8c4eea221dcf 27
yihui 0:8c4eea221dcf 28 while (1) {
yihui 0:8c4eea221dcf 29 while (uart.readable()) {
yihui 0:8c4eea221dcf 30 pc.putc(uart.getc());
yihui 0:8c4eea221dcf 31 }
yihui 0:8c4eea221dcf 32
yihui 0:8c4eea221dcf 33 while (pc.readable()) {
yihui 0:8c4eea221dcf 34 uart.putc(pc.getc());
yihui 0:8c4eea221dcf 35 }
yihui 0:8c4eea221dcf 36 }
yihui 0:8c4eea221dcf 37 }