A USB to UART bridge

Dependencies:   USBDevice BufferedSerial mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * USB to UART Bridge
00003  */
00004  
00005 #include "mbed.h"
00006 #include "USBSerial.h"
00007 #include "BufferedSerial.h"
00008 
00009 BufferedSerial uart(P0_19, P0_18, 512);
00010 USBSerial pc;
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DigitalOut led3(LED3);
00014 DigitalOut led4(LED4);
00015 
00016 Ticker ticker;
00017 volatile bool rxflag = false;
00018 volatile bool txflag = false;
00019 
00020 
00021 #ifdef TARGET_LPC11UXX
00022 #include "LPC11Uxx.h"
00023 void enable_hardware_flow_control()
00024 {
00025     LPC_IOCON->PIO0_17 = 0x01 | (0x1 << 3);     // RTS, 
00026     LPC_IOCON->PIO0_7  = 0x01 | (0x1 << 3);     // CTS, pull-down
00027     
00028     // enable auto RTS and CTS
00029     LPC_USART->MCR = (1 << 6) | (1 << 7);
00030 }
00031 #endif
00032 
00033 
00034 void indicate()
00035 {
00036     if (rxflag) {
00037         led3 = !led3;
00038         rxflag = false;
00039     } else {
00040         if (led3) {
00041             led3 = 0;
00042         }
00043     }
00044     
00045     if (txflag) {
00046         led4 = !led4;
00047         txflag = false;
00048     } else {
00049         if (led4) {
00050             led4 = 0;
00051         }
00052     }   
00053 }
00054 
00055 // Called by ISR
00056 void settings_changed(int baud, int bits, int parity, int stop)
00057 {
00058     const Serial::Parity parityTable[] = {Serial::None, Serial::Odd, Serial::Even, Serial::Forced0, Serial::Forced1};
00059     
00060     
00061     led1 = 1;
00062     if (stop != 2) {
00063         stop = 1;   // stop bit(s) = 1 or 1.5
00064     }
00065     uart.baud(baud);
00066     uart.format(bits, parityTable[parity], stop);
00067     led1 = 0;
00068 }
00069 
00070 
00071 int main()
00072 {
00073 #ifdef TARGET_LPC11UXX
00074     enable_hardware_flow_control();
00075 #endif
00076     
00077     pc.attach(settings_changed);
00078     ticker.attach_us(indicate, 500);
00079     
00080     while (1) {
00081         while (uart.readable()) {
00082             rxflag = true;
00083             char r = uart.getc();
00084             pc.putc(r);
00085         }
00086         
00087         while (pc.readable()) {
00088             char r = pc.getc();
00089             
00090             txflag = true;
00091             uart.putc(r);
00092         }
00093     }
00094 }