Create a UART to USB Serial bridge with the DipCortex
Fork of DipCortex-USB-CDC by
main.cpp@2:ec470dd97c6e, 2014-01-30 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Thu Jan 30 21:33:45 2014 +0000
- Revision:
- 2:ec470dd97c6e
- Parent:
- 1:b9eededaba19
- Child:
- 3:5fffa4cb4ca1
Switch RX and TX pins to use port name and pin
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 | 2:ec470dd97c6e | 8 | // Serial TX Pin19, Serial RX Pin20 |
SolderSplashLabs | 2:ec470dd97c6e | 9 | // Using port and pin names as the mbed definitions pin defs for the M0 are incorrect |
SolderSplashLabs | 2:ec470dd97c6e | 10 | Serial uart(P1_13, P1_14); |
yihui | 0:8c4eea221dcf | 11 | USBSerial pc; |
yihui | 0:8c4eea221dcf | 12 | |
yihui | 0:8c4eea221dcf | 13 | // Called by ISR |
yihui | 0:8c4eea221dcf | 14 | void settingsChanged(int baud, int bits, int parity, int stop) |
yihui | 0:8c4eea221dcf | 15 | { |
yihui | 0:8c4eea221dcf | 16 | const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1}; |
yihui | 0:8c4eea221dcf | 17 | |
yihui | 0:8c4eea221dcf | 18 | if (stop != 2) { |
yihui | 0:8c4eea221dcf | 19 | stop = 1; // stop bit(s) = 1 or 1.5 |
yihui | 0:8c4eea221dcf | 20 | } |
yihui | 0:8c4eea221dcf | 21 | |
yihui | 0:8c4eea221dcf | 22 | uart.baud(baud); |
yihui | 0:8c4eea221dcf | 23 | uart.format(bits, parityTable[parity], stop); |
yihui | 0:8c4eea221dcf | 24 | } |
yihui | 0:8c4eea221dcf | 25 | |
yihui | 0:8c4eea221dcf | 26 | int main() |
yihui | 0:8c4eea221dcf | 27 | { |
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 | } |