
A USB to UART bridge
Dependencies: USBDevice BufferedSerial mbed
main.cpp
- Committer:
- yihui
- Date:
- 2014-07-23
- Revision:
- 3:2b4d2284bab0
- Parent:
- 2:427b69ad737c
- Child:
- 4:9c038f12d460
File content as of revision 3:2b4d2284bab0:
/** * USB to UART Bridge */ #include "mbed.h" #include "USBSerial.h" #include "Buffer.h" Serial uart(USBTX, USBRX); USBSerial pc; DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); Buffer <char> rxbuf; Buffer <char> txbuf; Ticker ticker; volatile bool rxflag = false; volatile bool txflag = false; void indicate() { if (rxflag) { led3 = !led3; rxflag = false; } else { if (led3) { led3 = 0; } } if (txflag) { led4 = !led4; txflag = false; } else { if (led4) { led4 = 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}; led1 = 1; if (stop != 2) { stop = 1; // stop bit(s) = 1 or 1.5 } uart.baud(baud); uart.format(bits, parityTable[parity], stop); led1 = 0; } int main() { pc.attach(settings_changed); ticker.attach_us(indicate, 10000); while (1) { while (uart.readable()) { rxflag = true; char r = uart.getc(); rxbuf.put(r); } while (txbuf.available() && uart.writeable()) { txflag = true; uart.putc(txbuf.get()); } if (pc.readable()) { char r = pc.getc(); txbuf.put(r); } if (rxbuf.available() && pc.writeable()) { pc.putc(rxbuf.get()); } } }