To support GR-PEACH

Fork of USBHost by mbed official

Revision:
4:b320d68e98e7
Parent:
0:a554658735bf
Child:
5:e48791a1ef18
--- a/USBHostSerial/USBHostSerial.cpp	Wed Mar 06 17:50:07 2013 +0000
+++ b/USBHostSerial/USBHostSerial.cpp	Tue Mar 12 17:23:37 2013 +0000
@@ -22,6 +22,8 @@
 
 #include "dbg.h"
 
+#define SET_LINE_CODING 0x20
+
 USBHostSerial::USBHostSerial(): circ_buf() {
     host = USBHost::getHostInst();
     size_bulk_in = 0;
@@ -36,6 +38,8 @@
     dev_connected = false;
     serial_intf = -1;
     serial_device_found = false;
+    memcpy(&line_coding, 0, sizeof(LINE_CODING));
+    circ_buf.flush();
 }
 
 bool USBHostSerial::connected()
@@ -50,6 +54,8 @@
     }
     for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
         if ((dev = host->getDevice(i)) != NULL) {
+            
+            USB_DBG("Trying to connect serial device\r\n");
 
             if(host->enumerate(dev, this))
                 break;
@@ -61,10 +67,12 @@
                 if (!bulk_in || !bulk_out)
                     break;
                 
-                USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p]", dev->getVid(), dev->getPid(), dev);
-                dev->setName("Serial");
+                USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf);
+                dev->setName("Serial", serial_intf);
                 host->registerDriver(dev, serial_intf, this, &USBHostSerial::init);
                 
+                baud(9600);
+                
                 size_bulk_in = bulk_in->getSize();
                 size_bulk_out = bulk_out->getSize();
                 
@@ -89,8 +97,8 @@
                 circ_buf.queue(buf[i]);
             }
             rx.call();
+            host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
         }
-        host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
     }
 }
 
@@ -111,13 +119,30 @@
     return -1;
 }
 
+void USBHostSerial::baud(int baudrate) {
+    line_coding.baudrate = baudrate;
+    format();
+}
+
+void USBHostSerial::format(int bits, Parity parity, int stop_bits) {
+    line_coding.data_bits = bits;
+    line_coding.parity = parity;
+    line_coding.stop_bits = (stop_bits == 1) ? 0 : 2;
+    
+    // set line coding
+    int res = host->controlWrite(   dev,
+                                    USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
+                                    SET_LINE_CODING,
+                                    0, serial_intf, (uint8_t *)&line_coding, 7);
+}
 
 int USBHostSerial::_getc() {
     uint8_t c = 0;
-    while ((bulk_in != NULL) && (circ_buf.isEmpty()));
     if (bulk_in == NULL) {
+        init();
         return -1;
     }
+    while (circ_buf.isEmpty());
     circ_buf.dequeue(&c);
     return c;
 }
@@ -156,5 +181,3 @@
 }
 
 #endif
-
-