Create a UART to USB Serial bridge with the DipCortex
Fork of DipCortex-USB-CDC by
main.cpp@1:b9eededaba19, 2013-11-01 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Fri Nov 01 08:27:31 2013 +0000
- Revision:
- 1:b9eededaba19
- Parent:
- 0:8c4eea221dcf
- Child:
- 2:ec470dd97c6e
Example code show you how to create a USB CDC Device with the DipCortex
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 | |
SolderSplashLabs | 1:b9eededaba19 | 8 | //Serial uart(USBTX, USBRX); |
SolderSplashLabs | 1:b9eededaba19 | 9 | Serial uart(p19, p20); |
yihui | 0:8c4eea221dcf | 10 | USBSerial pc; |
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 | 0:8c4eea221dcf | 27 | pc.attach(settingsChanged); |
yihui | 0:8c4eea221dcf | 28 | |
yihui | 0:8c4eea221dcf | 29 | while (1) { |
yihui | 0:8c4eea221dcf | 30 | while (uart.readable()) { |
yihui | 0:8c4eea221dcf | 31 | pc.putc(uart.getc()); |
yihui | 0:8c4eea221dcf | 32 | } |
yihui | 0:8c4eea221dcf | 33 | |
yihui | 0:8c4eea221dcf | 34 | while (pc.readable()) { |
yihui | 0:8c4eea221dcf | 35 | uart.putc(pc.getc()); |
yihui | 0:8c4eea221dcf | 36 | } |
yihui | 0:8c4eea221dcf | 37 | } |
yihui | 0:8c4eea221dcf | 38 | } |