A USB to UART bridge

Dependencies:   USBDevice BufferedSerial mbed

Committer:
yihui
Date:
Wed Dec 04 01:28:54 2013 +0000
Revision:
1:efa9f62a12c4
Parent:
0:8c4eea221dcf
Child:
2:427b69ad737c
settings changed callback function is merged into official USB lib, switch to use official USB lib

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