Norimasa Okamoto / USBLocalFileSystem

Dependencies:   USBDevice

Dependents:   KL46Z-lpc81isp lpcterm2

Revision:
4:8f6857784854
Parent:
0:39eb4d5b97df
diff -r 09b562c87f89 -r 8f6857784854 USBMSD2/USB_CDC.cpp
--- a/USBMSD2/USB_CDC.cpp	Tue May 06 16:05:57 2014 +0900
+++ b/USBMSD2/USB_CDC.cpp	Thu May 08 23:43:46 2014 +0900
@@ -26,6 +26,8 @@
 #define CDC_DBG_HEX(A,B) while(0)
 #endif
 
+static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
+
 #define CDC_SET_LINE_CODING        0x20
 #define CDC_GET_LINE_CODING        0x21
 #define CDC_SET_CONTROL_LINE_STATE 0x22
@@ -34,6 +36,7 @@
 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
 
 USB_CDC::USB_CDC(USBDevice* device) : _device(device), _rx_buf(128)
+    ,settingsChangedCallback(NULL),controlLineStateChangedCallback(NULL),sendBreakCallback(NULL)
 {
     CDC_DBG("device=%p", device);
 
@@ -68,21 +71,6 @@
     return 1;
 }
 
-void USB_CDC::baud_callback(int baudrate)
-{
-    CDC_DBG("baudrate=%d", baudrate);
-}
-
-void USB_CDC::send_break_callback(uint16_t duration)
-{
-    CDC_DBG("duration=%04x", duration);
-}
-
-void USB_CDC::control_line_callback(int rts, int dtr)
-{
-    CDC_DBG("rts=%d, dtr=%d", rts, dtr);
-}
-
 bool USB_CDC::send(uint8_t * buffer, uint32_t size) {
     return _device->write(CDC_EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
 }
@@ -105,8 +93,6 @@
 
 bool USB_CDC::Request_callback(CONTROL_TRANSFER* transfer)
 {
-    static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
-
     if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
         switch (transfer->setup.bRequest) {
             case CDC_SET_LINE_CODING: // 0x20
@@ -122,33 +108,34 @@
                 return true;
 
             case CDC_SET_CONTROL_LINE_STATE: // 0x22
-                control_line_callback((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1);
+            	controlLineStateChanged((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1);
                 terminal_connected = false;
                 return true;
             
             case CDC_SEND_BREAK: // 0x23
-                send_break_callback(transfer->setup.wValue);
+            	sendBreak(transfer->setup.wValue);
                 return true;
         }
     }
     return false;
 }
 
-static uint32_t LD32(uint8_t* buf)
-{
-    return buf[0]|(buf[1]<<8)|(buf[2]<<16)|(buf[3]<<24);
-}
-
 bool USB_CDC::RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length)
 {
     CDC_DBG("transer=%p", transfer);
-    int baudrate;
     if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
-        switch (transfer->setup.bRequest) {
-            case CDC_SET_LINE_CODING: // 0x20
-                baudrate = LD32(buf);
-                baud_callback(baudrate);
+        if (transfer->setup.bRequest == CDC_SET_LINE_CODING) {
+            if (memcmp(cdc_line_coding, buf, 7)) {
+                memcpy(cdc_line_coding, buf, 7);
+
+                int baud = buf[0] + (buf[1] << 8)
+                         + (buf[2] << 16) + (buf[3] << 24);
+                int stop = (buf[4] == 0) ? 1 : 2;
+                int bits = buf[6];
+                int parity = buf[5];
+                lineCodingChanged(baud, bits, parity, stop);
                 return true;
+            }
         }
     }
     CDC_DBG_HEX((uint8_t*)transfer, sizeof(CONTROL_TRANSFER));
@@ -170,3 +157,28 @@
     _device->readStart(CDC_EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
     return true;
 }
+
+void USB_CDC::lineCodingChanged(int baud, int bits, int parity, int stop)
+{
+	CDC_DBG("baud=%d,bits=%d,parity=%d,stop=%d", baud,bits, parity, stop);
+    if (settingsChangedCallback) {
+        settingsChangedCallback(baud, bits, parity, stop);
+    }
+}
+
+void USB_CDC::controlLineStateChanged(int rts, int dtr)
+{
+	CDC_DBG("rts=%d,dtr=%d", rts, dtr);
+    if (controlLineStateChangedCallback) {
+    	controlLineStateChangedCallback(rts, dtr);
+    }
+}
+
+void USB_CDC::sendBreak(uint16_t duration)
+{
+	CDC_DBG("duration=%u", duration)
+    if (sendBreakCallback) {
+    	sendBreakCallback(duration);
+    }
+}
+