USB-UART bridge for XBee with XCTU.

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BaseUsbUartBridge.cpp Source File

BaseUsbUartBridge.cpp

00001 #include "BaseUsbUartBridge.h"
00002 
00003 // http://mbed.org/handbook/USBSerial
00004 // http://mbed.org/media/uploads/samux/serial.zip
00005 
00006 BaseUsbUartBridge::BaseUsbUartBridge(PinName tx, PinName rx, int uart_buf_size, int usb_buf_size)
00007     : SerialBase(tx, rx), USBCDC(0x1f00, 0x2012, 0x0001, true),
00008     uart_buf(uart_buf_size), usb_buf(usb_buf_size)
00009 {
00010     SerialBase::attach(this, &BaseUsbUartBridge::uart_handler, RxIrq);
00011 }
00012 
00013 void BaseUsbUartBridge::poll()
00014 {
00015     uint8_t c;
00016     int size = uart_buf.available();
00017     if (size > 0) {
00018         uint8_t buf[MAX_PACKET_SIZE_EPBULK];
00019         if (size > sizeof(buf)) {
00020             size = sizeof(buf);
00021         }
00022         for(int i = 0; i < size; i++) {
00023             uart_buf.dequeue(&c);
00024             buf[i] = c;
00025             monitor(_UART, c);
00026         }
00027         if (terminal_connected) {
00028             USBCDC::send(buf, size);
00029         }
00030     }
00031     
00032     if (usb_buf.dequeue(&c)) {
00033         SerialBase::_base_putc(c);
00034         monitor(_USB, c);
00035     }
00036 }
00037 
00038 void BaseUsbUartBridge::uart_handler(void)
00039 {
00040     uint8_t c = SerialBase::_base_getc();
00041     uart_buf.queue(c);
00042 }
00043 
00044 bool BaseUsbUartBridge::bulk_callback()
00045 {
00046     uint8_t c[65];
00047     uint32_t size = 0;
00048 
00049     //we read the packet received and put it on the circular buffer
00050     readEP(c, &size);
00051     for (uint32_t i = 0; i < size; i++) {
00052         usb_buf.queue(c[i]);
00053     }
00054 
00055     // We reactivate the endpoint to receive next characters
00056     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00057     return true;    
00058 }
00059 
00060 /* virtual */ void BaseUsbUartBridge::lineCodingChanged(int baud, int bits, int parity, int stop)
00061 {
00062     SerialBase::baud(baud);
00063 }
00064 
00065 /* virtual */ void BaseUsbUartBridge::sendBreak(uint16_t duration)
00066 {
00067     SerialBase::send_break();
00068 };
00069