USB Serial to physical UART pass through demo. - legacy mbed (no RTOS) - Interrupt mode + ring buffer on both VCP and UART, which can survive in press-test
main.cpp@0:b44aebdb4d65, 2018-09-14 (annotated)
- Committer:
- nightseas
- Date:
- Fri Sep 14 07:31:51 2018 +0000
- Revision:
- 0:b44aebdb4d65
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nightseas | 0:b44aebdb4d65 | 1 | #include "mbed.h" |
nightseas | 0:b44aebdb4d65 | 2 | #include "USBSerial.h" |
nightseas | 0:b44aebdb4d65 | 3 | #include "USBMSD.h" |
nightseas | 0:b44aebdb4d65 | 4 | |
nightseas | 0:b44aebdb4d65 | 5 | // Ring buffer for serial RX data - used by interrupt routines |
nightseas | 0:b44aebdb4d65 | 6 | #define SERIAL_RX_BUFF_LEN 2047 |
nightseas | 0:b44aebdb4d65 | 7 | // might need to increase buffer size for high baud rates |
nightseas | 0:b44aebdb4d65 | 8 | char serial_rx_buffer[SERIAL_RX_BUFF_LEN+1]; |
nightseas | 0:b44aebdb4d65 | 9 | // Ring buffer pointers |
nightseas | 0:b44aebdb4d65 | 10 | // volatile makes read-modify-write atomic |
nightseas | 0:b44aebdb4d65 | 11 | volatile int serial_rx_in_inx=0; |
nightseas | 0:b44aebdb4d65 | 12 | volatile int serial_rx_out_inx=0; |
nightseas | 0:b44aebdb4d65 | 13 | |
nightseas | 0:b44aebdb4d65 | 14 | // might need to increase buffer size for high baud rates |
nightseas | 0:b44aebdb4d65 | 15 | char vcp_rx_buffer[SERIAL_RX_BUFF_LEN+1]; |
nightseas | 0:b44aebdb4d65 | 16 | // Ring buffer pointers |
nightseas | 0:b44aebdb4d65 | 17 | // volatile makes read-modify-write atomic |
nightseas | 0:b44aebdb4d65 | 18 | volatile int vcp_rx_in_inx=0; |
nightseas | 0:b44aebdb4d65 | 19 | volatile int vcp_rx_out_inx=0; |
nightseas | 0:b44aebdb4d65 | 20 | |
nightseas | 0:b44aebdb4d65 | 21 | USBSerial vcp2; |
nightseas | 0:b44aebdb4d65 | 22 | USBSerial vcp3; |
nightseas | 0:b44aebdb4d65 | 23 | USBSerial vcp; |
nightseas | 0:b44aebdb4d65 | 24 | |
nightseas | 0:b44aebdb4d65 | 25 | RawSerial pc(SERIAL_TX, SERIAL_RX, 115200); |
nightseas | 0:b44aebdb4d65 | 26 | |
nightseas | 0:b44aebdb4d65 | 27 | DigitalOut led1(LED1), led2(LED2), led3(LED3); |
nightseas | 0:b44aebdb4d65 | 28 | |
nightseas | 0:b44aebdb4d65 | 29 | // Interupt Routine to read in data from serial port |
nightseas | 0:b44aebdb4d65 | 30 | void vcp_rx_isr_handler( void ) |
nightseas | 0:b44aebdb4d65 | 31 | { |
nightseas | 0:b44aebdb4d65 | 32 | led2 = !led2; |
nightseas | 0:b44aebdb4d65 | 33 | // Loop just in case more than one character is in UART's receive FIFO buffer |
nightseas | 0:b44aebdb4d65 | 34 | // Stop if buffer full |
nightseas | 0:b44aebdb4d65 | 35 | while( vcp.readable() && ( (vcp_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN != vcp_rx_out_inx ) ) |
nightseas | 0:b44aebdb4d65 | 36 | { |
nightseas | 0:b44aebdb4d65 | 37 | vcp_rx_buffer[vcp_rx_in_inx] = vcp.getc(); |
nightseas | 0:b44aebdb4d65 | 38 | vcp_rx_in_inx = (vcp_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN; |
nightseas | 0:b44aebdb4d65 | 39 | } |
nightseas | 0:b44aebdb4d65 | 40 | return; |
nightseas | 0:b44aebdb4d65 | 41 | } |
nightseas | 0:b44aebdb4d65 | 42 | |
nightseas | 0:b44aebdb4d65 | 43 | // Interupt Routine to read in data from serial port |
nightseas | 0:b44aebdb4d65 | 44 | void serial_rx_isr_handler( void ) |
nightseas | 0:b44aebdb4d65 | 45 | { |
nightseas | 0:b44aebdb4d65 | 46 | led3 = !led3; |
nightseas | 0:b44aebdb4d65 | 47 | // Loop just in case more than one character is in UART's receive FIFO buffer |
nightseas | 0:b44aebdb4d65 | 48 | // Stop if buffer full |
nightseas | 0:b44aebdb4d65 | 49 | while( pc.readable() && ( (serial_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN != serial_rx_out_inx ) ) |
nightseas | 0:b44aebdb4d65 | 50 | { |
nightseas | 0:b44aebdb4d65 | 51 | serial_rx_buffer[serial_rx_in_inx] = pc.getc(); |
nightseas | 0:b44aebdb4d65 | 52 | serial_rx_in_inx = (serial_rx_in_inx + 1) % SERIAL_RX_BUFF_LEN; |
nightseas | 0:b44aebdb4d65 | 53 | } |
nightseas | 0:b44aebdb4d65 | 54 | return; |
nightseas | 0:b44aebdb4d65 | 55 | } |
nightseas | 0:b44aebdb4d65 | 56 | |
nightseas | 0:b44aebdb4d65 | 57 | |
nightseas | 0:b44aebdb4d65 | 58 | int main(void) |
nightseas | 0:b44aebdb4d65 | 59 | { |
nightseas | 0:b44aebdb4d65 | 60 | pc.attach(&serial_rx_isr_handler, Serial::RxIrq); |
nightseas | 0:b44aebdb4d65 | 61 | vcp.attach(&vcp_rx_isr_handler); |
nightseas | 0:b44aebdb4d65 | 62 | pc.printf("\n\r\n\r\n\r========== USB Device Test ==========\n\r\n\r"); |
nightseas | 0:b44aebdb4d65 | 63 | vcp.printf("\n\r\n\r\n\r========== USB Device Test ==========\n\r\n\r"); |
nightseas | 0:b44aebdb4d65 | 64 | while (1) |
nightseas | 0:b44aebdb4d65 | 65 | { |
nightseas | 0:b44aebdb4d65 | 66 | while( serial_rx_in_inx != serial_rx_out_inx ) |
nightseas | 0:b44aebdb4d65 | 67 | { |
nightseas | 0:b44aebdb4d65 | 68 | vcp.putc(serial_rx_buffer[serial_rx_out_inx]); |
nightseas | 0:b44aebdb4d65 | 69 | serial_rx_out_inx = (serial_rx_out_inx + 1) % SERIAL_RX_BUFF_LEN; |
nightseas | 0:b44aebdb4d65 | 70 | led1 = !led1; |
nightseas | 0:b44aebdb4d65 | 71 | } |
nightseas | 0:b44aebdb4d65 | 72 | while( vcp_rx_in_inx != vcp_rx_out_inx ) |
nightseas | 0:b44aebdb4d65 | 73 | { |
nightseas | 0:b44aebdb4d65 | 74 | pc.putc(vcp_rx_buffer[vcp_rx_out_inx]); |
nightseas | 0:b44aebdb4d65 | 75 | vcp_rx_out_inx = (vcp_rx_out_inx + 1) % SERIAL_RX_BUFF_LEN; |
nightseas | 0:b44aebdb4d65 | 76 | led1 = !led1; |
nightseas | 0:b44aebdb4d65 | 77 | } |
nightseas | 0:b44aebdb4d65 | 78 | } |
nightseas | 0:b44aebdb4d65 | 79 | } |