
A USB to UART bridge
Dependencies: USBDevice BufferedSerial mbed
main.cpp@2:427b69ad737c, 2013-12-25 (annotated)
- Committer:
- yihui
- Date:
- Wed Dec 25 03:04:09 2013 +0000
- Revision:
- 2:427b69ad737c
- Parent:
- 1:efa9f62a12c4
- Child:
- 3:2b4d2284bab0
add led indicator
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:427b69ad737c | 10 | DigitalOut led1(LED1); |
yihui | 2:427b69ad737c | 11 | DigitalOut led2(LED2); |
yihui | 2:427b69ad737c | 12 | DigitalOut led3(LED3); |
yihui | 0:8c4eea221dcf | 13 | |
yihui | 0:8c4eea221dcf | 14 | // Called by ISR |
yihui | 0:8c4eea221dcf | 15 | void settingsChanged(int baud, int bits, int parity, int stop) |
yihui | 0:8c4eea221dcf | 16 | { |
yihui | 0:8c4eea221dcf | 17 | const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1}; |
yihui | 0:8c4eea221dcf | 18 | |
yihui | 2:427b69ad737c | 19 | |
yihui | 2:427b69ad737c | 20 | led1 = 1; |
yihui | 0:8c4eea221dcf | 21 | if (stop != 2) { |
yihui | 0:8c4eea221dcf | 22 | stop = 1; // stop bit(s) = 1 or 1.5 |
yihui | 0:8c4eea221dcf | 23 | } |
yihui | 0:8c4eea221dcf | 24 | uart.baud(baud); |
yihui | 0:8c4eea221dcf | 25 | uart.format(bits, parityTable[parity], stop); |
yihui | 2:427b69ad737c | 26 | led1 = 0; |
yihui | 0:8c4eea221dcf | 27 | } |
yihui | 0:8c4eea221dcf | 28 | |
yihui | 0:8c4eea221dcf | 29 | int main() |
yihui | 0:8c4eea221dcf | 30 | { |
yihui | 0:8c4eea221dcf | 31 | pc.attach(settingsChanged); |
yihui | 0:8c4eea221dcf | 32 | |
yihui | 0:8c4eea221dcf | 33 | while (1) { |
yihui | 0:8c4eea221dcf | 34 | while (uart.readable()) { |
yihui | 2:427b69ad737c | 35 | led2 = 1; |
yihui | 2:427b69ad737c | 36 | pc.putc(uart.getc()); |
yihui | 2:427b69ad737c | 37 | led2 = 0; |
yihui | 0:8c4eea221dcf | 38 | } |
yihui | 0:8c4eea221dcf | 39 | |
yihui | 0:8c4eea221dcf | 40 | while (pc.readable()) { |
yihui | 2:427b69ad737c | 41 | led3 = 1; |
yihui | 0:8c4eea221dcf | 42 | uart.putc(pc.getc()); |
yihui | 2:427b69ad737c | 43 | led3 = 0; |
yihui | 0:8c4eea221dcf | 44 | } |
yihui | 0:8c4eea221dcf | 45 | } |
yihui | 0:8c4eea221dcf | 46 | } |