Xiaohai Li / Mbed 2 deprecated USB_VCP_Passthrough_F429

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBSerial.h"
00003 #include "USBMSD.h"
00004 
00005 // Ring buffer for serial RX data - used by interrupt routines
00006 #define SERIAL_RX_BUFF_LEN 2047
00007 // might need to increase buffer size for high baud rates
00008 char serial_rx_buffer[SERIAL_RX_BUFF_LEN+1];
00009 // Ring buffer pointers
00010 // volatile makes read-modify-write atomic 
00011 volatile int serial_rx_in_inx=0;
00012 volatile int serial_rx_out_inx=0;
00013 
00014 // might need to increase buffer size for high baud rates
00015 char vcp_rx_buffer[SERIAL_RX_BUFF_LEN+1];
00016 // Ring buffer pointers
00017 // volatile makes read-modify-write atomic 
00018 volatile int vcp_rx_in_inx=0;
00019 volatile int vcp_rx_out_inx=0;
00020 
00021 USBSerial vcp2;
00022 USBSerial vcp3;
00023 USBSerial vcp;
00024 
00025 RawSerial pc(SERIAL_TX, SERIAL_RX, 115200);
00026 
00027 DigitalOut led1(LED1), led2(LED2), led3(LED3);
00028 
00029 // Interupt Routine to read in data from serial port
00030 void vcp_rx_isr_handler( void )
00031 {
00032     led2 = !led2;
00033 // Loop just in case more than one character is in UART's receive FIFO buffer
00034 // Stop if buffer full
00035     while( vcp.readable() && ( (vcp_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN != vcp_rx_out_inx ) )
00036     {
00037         vcp_rx_buffer[vcp_rx_in_inx] = vcp.getc();        
00038         vcp_rx_in_inx = (vcp_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN;
00039     }
00040     return;
00041 }
00042 
00043 // Interupt Routine to read in data from serial port
00044 void serial_rx_isr_handler( void )
00045 {
00046     led3 = !led3;
00047 // Loop just in case more than one character is in UART's receive FIFO buffer
00048 // Stop if buffer full
00049     while( pc.readable() && ( (serial_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN != serial_rx_out_inx ) )
00050     {
00051         serial_rx_buffer[serial_rx_in_inx] = pc.getc();        
00052         serial_rx_in_inx = (serial_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN;
00053     }
00054     return;
00055 }
00056 
00057 
00058 int main(void)
00059 {   
00060     pc.attach(&serial_rx_isr_handler, Serial::RxIrq);
00061     vcp.attach(&vcp_rx_isr_handler);
00062     pc.printf("\n\r\n\r\n\r========== USB Device Test ==========\n\r\n\r");    
00063     vcp.printf("\n\r\n\r\n\r========== USB Device Test ==========\n\r\n\r");
00064     while (1)
00065     {
00066         while( serial_rx_in_inx != serial_rx_out_inx )
00067         {
00068             vcp.putc(serial_rx_buffer[serial_rx_out_inx]);
00069             serial_rx_out_inx = (serial_rx_out_inx + 1) % SERIAL_RX_BUFF_LEN;
00070             led1 = !led1;
00071         }
00072         while( vcp_rx_in_inx != vcp_rx_out_inx )
00073         {
00074             pc.putc(vcp_rx_buffer[vcp_rx_out_inx]);
00075             vcp_rx_out_inx = (vcp_rx_out_inx + 1) % SERIAL_RX_BUFF_LEN;
00076             led1 = !led1;
00077         }
00078     }
00079 }