A USB to UART bridge

Dependencies:   USBDevice BufferedSerial mbed

Committer:
yihui
Date:
Fri Mar 04 14:00:51 2016 +0000
Revision:
5:10fccccbbb11
Parent:
4:9c038f12d460
add hardware flow control for LPC11Uxx target

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 4:9c038f12d460 7 #include "BufferedSerial.h"
yihui 0:8c4eea221dcf 8
yihui 5:10fccccbbb11 9 BufferedSerial uart(P0_19, P0_18, 512);
yihui 0:8c4eea221dcf 10 USBSerial pc;
yihui 2:427b69ad737c 11 DigitalOut led1(LED1);
yihui 2:427b69ad737c 12 DigitalOut led2(LED2);
yihui 2:427b69ad737c 13 DigitalOut led3(LED3);
yihui 3:2b4d2284bab0 14 DigitalOut led4(LED4);
yihui 3:2b4d2284bab0 15
yihui 3:2b4d2284bab0 16 Ticker ticker;
yihui 3:2b4d2284bab0 17 volatile bool rxflag = false;
yihui 3:2b4d2284bab0 18 volatile bool txflag = false;
yihui 3:2b4d2284bab0 19
yihui 3:2b4d2284bab0 20
yihui 5:10fccccbbb11 21 #ifdef TARGET_LPC11UXX
yihui 5:10fccccbbb11 22 #include "LPC11Uxx.h"
yihui 5:10fccccbbb11 23 void enable_hardware_flow_control()
yihui 5:10fccccbbb11 24 {
yihui 5:10fccccbbb11 25 LPC_IOCON->PIO0_17 = 0x01 | (0x1 << 3); // RTS,
yihui 5:10fccccbbb11 26 LPC_IOCON->PIO0_7 = 0x01 | (0x1 << 3); // CTS, pull-down
yihui 5:10fccccbbb11 27
yihui 5:10fccccbbb11 28 // enable auto RTS and CTS
yihui 5:10fccccbbb11 29 LPC_USART->MCR = (1 << 6) | (1 << 7);
yihui 5:10fccccbbb11 30 }
yihui 5:10fccccbbb11 31 #endif
yihui 5:10fccccbbb11 32
yihui 5:10fccccbbb11 33
yihui 3:2b4d2284bab0 34 void indicate()
yihui 3:2b4d2284bab0 35 {
yihui 3:2b4d2284bab0 36 if (rxflag) {
yihui 3:2b4d2284bab0 37 led3 = !led3;
yihui 3:2b4d2284bab0 38 rxflag = false;
yihui 3:2b4d2284bab0 39 } else {
yihui 3:2b4d2284bab0 40 if (led3) {
yihui 3:2b4d2284bab0 41 led3 = 0;
yihui 3:2b4d2284bab0 42 }
yihui 3:2b4d2284bab0 43 }
yihui 3:2b4d2284bab0 44
yihui 3:2b4d2284bab0 45 if (txflag) {
yihui 3:2b4d2284bab0 46 led4 = !led4;
yihui 3:2b4d2284bab0 47 txflag = false;
yihui 3:2b4d2284bab0 48 } else {
yihui 3:2b4d2284bab0 49 if (led4) {
yihui 3:2b4d2284bab0 50 led4 = 0;
yihui 3:2b4d2284bab0 51 }
yihui 3:2b4d2284bab0 52 }
yihui 3:2b4d2284bab0 53 }
yihui 0:8c4eea221dcf 54
yihui 0:8c4eea221dcf 55 // Called by ISR
yihui 3:2b4d2284bab0 56 void settings_changed(int baud, int bits, int parity, int stop)
yihui 0:8c4eea221dcf 57 {
yihui 0:8c4eea221dcf 58 const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
yihui 0:8c4eea221dcf 59
yihui 2:427b69ad737c 60
yihui 2:427b69ad737c 61 led1 = 1;
yihui 0:8c4eea221dcf 62 if (stop != 2) {
yihui 0:8c4eea221dcf 63 stop = 1; // stop bit(s) = 1 or 1.5
yihui 0:8c4eea221dcf 64 }
yihui 0:8c4eea221dcf 65 uart.baud(baud);
yihui 0:8c4eea221dcf 66 uart.format(bits, parityTable[parity], stop);
yihui 2:427b69ad737c 67 led1 = 0;
yihui 0:8c4eea221dcf 68 }
yihui 0:8c4eea221dcf 69
yihui 5:10fccccbbb11 70
yihui 0:8c4eea221dcf 71 int main()
yihui 0:8c4eea221dcf 72 {
yihui 5:10fccccbbb11 73 #ifdef TARGET_LPC11UXX
yihui 5:10fccccbbb11 74 enable_hardware_flow_control();
yihui 5:10fccccbbb11 75 #endif
yihui 5:10fccccbbb11 76
yihui 3:2b4d2284bab0 77 pc.attach(settings_changed);
yihui 4:9c038f12d460 78 ticker.attach_us(indicate, 500);
yihui 0:8c4eea221dcf 79
yihui 0:8c4eea221dcf 80 while (1) {
yihui 0:8c4eea221dcf 81 while (uart.readable()) {
yihui 3:2b4d2284bab0 82 rxflag = true;
yihui 3:2b4d2284bab0 83 char r = uart.getc();
yihui 4:9c038f12d460 84 pc.putc(r);
yihui 3:2b4d2284bab0 85 }
yihui 3:2b4d2284bab0 86
yihui 4:9c038f12d460 87 while (pc.readable()) {
yihui 3:2b4d2284bab0 88 char r = pc.getc();
yihui 4:9c038f12d460 89
yihui 4:9c038f12d460 90 txflag = true;
yihui 4:9c038f12d460 91 uart.putc(r);
yihui 0:8c4eea221dcf 92 }
yihui 0:8c4eea221dcf 93 }
yihui 0:8c4eea221dcf 94 }