USB-UART bridge for XBee with XCTU.

Dependencies:   USBDevice mbed

USB to UART bridge for XBee with XCTU.
320 350
Windows serial driver:
https://developer.mbed.org/handbook/USBSerial
https://developer.mbed.org/media/uploads/samux/serial.zip

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Mon May 12 15:53:39 2014 +0000
Child:
1:3d02f8b77d68
Commit message:
first commit

Changed in this revision

BaseUsbUartBridge.cpp Show annotated file Show diff for this revision Revisions of this file
BaseUsbUartBridge.h Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BaseUsbUartBridge.cpp	Mon May 12 15:53:39 2014 +0000
@@ -0,0 +1,59 @@
+#include "BaseUsbUartBridge.h"
+
+// http://mbed.org/handbook/USBSerial
+// http://mbed.org/media/uploads/samux/serial.zip
+
+BaseUsbUartBridge::BaseUsbUartBridge(PinName tx, PinName rx, int uart_buf_size, int usb_buf_size)
+    : SerialBase(tx, rx), USBCDC(0x1f00, 0x2012, 0x0001, true),
+    uart_buf(uart_buf_size), usb_buf(usb_buf_size)
+{
+    SerialBase::attach(this, &BaseUsbUartBridge::uart_handler, RxIrq);
+}
+
+void BaseUsbUartBridge::poll()
+{
+    uint8_t c;
+    int size = uart_buf.available();
+    if (size > 0) {
+        uint8_t buf[MAX_PACKET_SIZE_EPBULK];
+        if (size > sizeof(buf)) {
+            size = sizeof(buf);
+        }
+        for(int i = 0; i < size; i++) {
+            uart_buf.dequeue(&c);
+            buf[i] = c;
+            monitor(_UART, c);
+        }
+        if (terminal_connected) {
+            USBCDC::send(buf, size);
+        }
+    }
+    
+    if (usb_buf.dequeue(&c)) {
+        SerialBase::_base_putc(c);
+        monitor(_USB, c);
+    }
+}
+
+void BaseUsbUartBridge::uart_handler(void)
+{
+    uint8_t c = SerialBase::_base_getc();
+    uart_buf.queue(c);
+}
+
+bool BaseUsbUartBridge::bulk_callback()
+{
+    uint8_t c[65];
+    uint32_t size = 0;
+
+    //we read the packet received and put it on the circular buffer
+    readEP(c, &size);
+    for (uint32_t i = 0; i < size; i++) {
+        usb_buf.queue(c[i]);
+    }
+
+    // We reactivate the endpoint to receive next characters
+    readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
+    return true;    
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BaseUsbUartBridge.h	Mon May 12 15:53:39 2014 +0000
@@ -0,0 +1,29 @@
+#pragma once
+#include "mbed.h"
+#include "USBCDC.h"
+#include "CircBuffer.h"
+
+typedef enum {
+    _USB,_UART,
+} src_t;
+
+class BaseUsbUartBridge : public SerialBase, public USBCDC {
+public:
+    BaseUsbUartBridge(PinName tx, PinName rx, int uart_buf_size = 256, int usb_buf_size = 1024);
+    void poll();
+
+protected:
+    virtual bool EP2_OUT_callback() { return bulk_callback(); }
+    virtual void lineCodingChanged(int baud, int bits, int parity, int stop){
+        SerialBase::baud(baud);
+    }
+    virtual void controlLineStateChanged(int rts, int dtr){}
+    virtual void monitor(src_t src, uint8_t c) {}
+
+private:
+    bool bulk_callback();
+    void uart_handler(void);
+    CircBuffer<uint8_t> uart_buf;
+    CircBuffer<uint8_t> usb_buf;
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Mon May 12 15:53:39 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/va009039/code/USBDevice/#c4c8fd0b0f12
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 12 15:53:39 2014 +0000
@@ -0,0 +1,64 @@
+#include "BaseUsbUartBridge.h"
+DigitalOut led_xbee(LED1),led_usb(LED2),led_reset(LED3),led_heartbeat(LED4);
+RawSerial pc(MBED_UARTUSB);
+#if defined(TARGET_LPC4088)
+#define XBEE_UART P4_22,P4_23
+DigitalOut xbee_rts(P4_21);
+DigitalOut xbee_dtr(P4_19);
+DigitalOut xbee_reset(P4_17);
+DigitalIn reset_sw(P2_10);
+#define SW_MODE PullUp
+#elif defined(TARGET_LPC1768)
+#define XBEE_UART p9,p10
+DigitalOut xbee_rts(p21);
+DigitalOut xbee_dtr(p22);
+DigitalOut xbee_reset(p30);
+DigitalIn reset_sw(p14);
+#define SW_MODE PullDown
+#else
+#error "target error"
+#endif
+
+class USB_XBee : public BaseUsbUartBridge {
+public:
+    USB_XBee(PinName tx, PinName rx) : BaseUsbUartBridge(tx, rx) {}
+    virtual void controlLineStateChanged(int rts, int dtr){
+        xbee_rts = rts ^ 1;
+        xbee_dtr = dtr ^ 1;
+    }
+    virtual void monitor(src_t src, uint8_t c) {
+        switch(src) {
+            case _UART: led_xbee = !led_xbee; break;
+            case _USB: led_usb = !led_usb; break;
+        }
+        //pc.putc(c);// debug
+    }
+};
+
+USB_XBee usb_xbee(XBEE_UART);
+
+int main() {
+    pc.baud(115200);
+
+    xbee_rts = 0;
+    xbee_dtr = 0;
+    xbee_reset = 1;
+    reset_sw.mode(SW_MODE);
+    int prev_sw = 1;
+
+    Timer heartbeat_t;
+    heartbeat_t.reset();
+    heartbeat_t.start();
+    while(1) {
+        if (reset_sw ^ prev_sw) {
+            prev_sw = reset_sw;
+            xbee_reset = prev_sw ^ (SW_MODE == PullDown ? 1 : 0);
+            led_reset = xbee_reset;
+        }
+        if (heartbeat_t.read_ms() > 500) {
+            heartbeat_t.reset();
+            led_heartbeat = !led_heartbeat;
+        }
+        usb_xbee.poll();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon May 12 15:53:39 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776
\ No newline at end of file