Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Revision:
7:9a20482c9a7a
Child:
8:6463cd1964c0
diff -r 03ef38d6e1ba -r 9a20482c9a7a USBHost/USBHost.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHost/USBHost.cpp	Tue Jan 28 06:50:12 2014 +0000
@@ -0,0 +1,343 @@
+// Simple USBHost for FRDM-KL46Z
+#include "USBHost.h"
+#include <algorithm>
+
+template <bool>struct CtAssert;
+template <>struct CtAssert<true> {};
+#define CTASSERT(A) CtAssert<A>();
+
+
+#ifdef _USB_DBG
+#define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0);
+#define USB_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0);
+#define USB_DBG_HEX(A,B) debug_hex(A,B)
+#define USB_DBG_ERRSTAT() report.print_errstat();
+void debug_hex(uint8_t* buf, int size);
+#else
+#define USB_DBG(...) while(0)
+#define USB_DBG2(...) while(0)
+#define USB_DBG_HEX(A,B) while(0)
+#define USB_DBG_ERRSTAT() while(0)
+#endif
+
+#define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
+#define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A))
+
+#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0);
+
+USBHost* USBHost::inst = NULL;
+
+USBHost* USBHost::getHostInst()
+{
+    if (inst == NULL) {
+        inst = new USBHost();
+        inst->init();
+    }
+    return inst;
+}
+
+USBHost::USBHost() {
+}
+
+/* virtual */ bool USBHost::enumeration() {
+    USBEndpoint* ep = &ep_ctl_in_out;
+    uint8_t desc[64];
+    ep->setAddress(0);
+    ep->setSize(8); // max packet size
+    dev.addr = 0;
+    wait_ms(100);
+    SETUP_PACKET setup_get_descriptor = {0x80, GET_DESCRIPTOR, 1<<8, 0, 0};
+    int result = ControlRead(&setup_get_descriptor, desc, ep->getSize());
+    if (result < ep->getSize()) {
+        USB_DBG("result=%d %02x", result, LastStatus);
+        USB_DBG_ERRSTAT();
+        return false;
+    }
+    USB_DBG_HEX(desc, result);
+    USB_DBG_ERRSTAT();
+    DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc);
+    ep->setSize(dev_desc->bMaxPacketSize);
+
+    SETUP_PACKET setup_set_address = {0x00, SET_ADDRESS, 1, 0, 0};
+    result = ControlWrite(&setup_set_address);
+    if (result < 0) {
+        USB_DBG("result=%d %02x", result, LastStatus);
+        USB_DBG_ERRSTAT();
+        return false;
+    }
+    wait_ms(100);
+    dev.addr = 1;
+
+    result = ControlRead(&setup_get_descriptor, desc, sizeof(desc));
+    if (result < 8) {
+        USB_DBG("result=%d", result);
+        USB_DBG_ERRSTAT();
+        return false;
+    }
+    USB_DBG_HEX(desc, result);
+    USB_DBG_ERRSTAT();
+
+    dev.vid = dev_desc->idVendor;
+    dev.pid = dev_desc->idProduct;
+    USB_INFO("USB VID: %04x, PID: %04x\n", dev.vid, dev.pid);
+    if (dev_desc->bDeviceClass == HUB_CLASS) {
+        USB_INFO("USB hub not supported.\n\n");
+        exit(1);
+    }
+
+    setup_get_descriptor.wValue = 2<<8; // config descriptor
+    result = ControlRead(&setup_get_descriptor, desc, 4);
+    if (result != 4) {
+        USB_DBG("result=%d", result);
+        USB_DBG_ERRSTAT();
+        return false;
+    }
+    USB_DBG_HEX(desc, 4);
+    USB_DBG_ERRSTAT();
+
+    int TotalLength = desc[2]|desc[3]<<8;
+    uint8_t* buf = new uint8_t[TotalLength];
+    result = ControlRead(&setup_get_descriptor, buf, TotalLength);
+    if (result != TotalLength) {
+        USB_DBG("result=%d TotalLength=%d %02x", result, TotalLength, LastStatus);
+        USB_DBG_ERRSTAT();
+        return false;
+    }
+    USB_DBG_HEX(buf, TotalLength);
+    USB_DBG_ERRSTAT();
+
+    for(int i = 0; i < TotalLength; ) {
+        int Length = buf[i];
+        uint8_t DescriptorType = buf[i+1];
+        if (DescriptorType == 0x05) { // endpoint
+            EndpointDescriptor* desc = reinterpret_cast<EndpointDescriptor*>(buf+i);
+            USBEndpoint* ep = NULL;
+            if (desc->bmAttributes == 0x03) { // interrupt
+                if (desc->bEndpointAddress & 0x80) {
+                    ep = &ep_int_in;
+                }
+            } else if (desc->bmAttributes == 0x02) { // bulk
+                ep = (desc->bEndpointAddress & 0x80) ? &ep_bulk_in : &ep_bulk_out;
+            }
+            if (ep) {
+                ep->setAddress(desc->bEndpointAddress);
+                ep->setSize(desc->wMaxPacketSize);
+            }
+        }
+        USB_DBG_HEX(buf+i, Length);
+        i += Length;
+    }
+    delete[] buf;
+
+    // config = 1
+    SETUP_PACKET setup_set_config = {0x00, SET_CONFIGURATION, 1, 0, 0};
+    result = ControlWrite(&setup_set_config);
+    if (result < 0) {
+        USB_DBG("set config: %02x", LastStatus);
+        USB_DBG_ERRSTAT();
+        if (lowSpeed && LastStatus == STALL) { // TODO:
+            wait_ms(100);
+            return true;
+        }
+        return false;
+    }
+    wait_ms(100);
+    return true;
+}
+
+USB_TYPE USBHost::controlRead(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
+    SETUP_PACKET setup = {requestType, request, value, index};
+    int result = ControlRead(&setup, buf, len);
+    USB_DBG2("result=%d %02x", result, LastStatus);
+    return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR;
+}
+
+USB_TYPE USBHost::controlWrite(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
+    SETUP_PACKET setup = {requestType, request, value, index};
+    int result = ControlWrite(&setup, buf, len);
+    if (result >= 0) {
+        return USB_TYPE_OK;
+    }
+    USB_DBG("result=%d %02x", result, LastStatus);
+    USB_DBG_ERRSTAT();
+    USB_DBG_HEX(buf, len);
+    return USB_TYPE_ERROR;
+}
+
+USB_TYPE USBHost::bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) {
+    USB_TEST_ASSERT(blocking);
+    int result = BulkRead(buf, len);
+    if (result >= 0) {
+        return USB_TYPE_OK;
+    }
+    //USB_DBG2("result=%d %02x", result, host->LastStatus);
+    return USB_TYPE_ERROR;
+}
+
+USB_TYPE USBHost::bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) {
+    USB_TEST_ASSERT(blocking);
+    int result = BulkWrite(buf, len);
+    if (result >= 0) {
+        return USB_TYPE_OK;
+    }
+    USB_DBG2("result=%d %02x", result, LastStatus);
+    return USB_TYPE_ERROR;
+}
+
+USB_TYPE USBHost::interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking) {
+    USB_TEST_ASSERT(blocking);
+    int result = InterruptRead(buf, len);
+    if (result >= 0) {
+        return USB_TYPE_OK;
+    }
+    return USB_TYPE_ERROR;
+}
+
+int USBHost::ControlRead(SETUP_PACKET* setup, uint8_t* data, int size) {
+    USBEndpoint* ep = &ep_ctl_in_out;
+    setAddr(dev.addr);
+    token_setup(ep, setup, size); // setup stage
+    if (LastStatus != ACK) {
+        USB_DBG("setup %02x", LastStatus);
+        return -1;
+    }
+    int read_len = 0;
+    while(read_len < size) {
+        int size2 = std::min(size-read_len, ep->getSize());
+        int result = token_in(ep, data+read_len, size2);
+        //USB_DBG("token_in result=%d %02x", result, LastStatus);
+        if (result < 0) {
+            USB_DBG("token_in %d/%d %02x", read_len, size, LastStatus);
+            return result;
+        }
+        read_len += result;
+        if (result < ep->getSize()) {
+            break;
+        }
+    }    
+    ep->setData01(DATA1);
+    int result = token_out(ep); // status stage
+    if (result < 0) {
+        USB_DBG("status token_out %02x", LastStatus);
+        if (LastStatus == STALL) {
+            return read_len;
+        }
+        return result;
+    }
+    return read_len;
+}
+
+int USBHost::ControlWrite(SETUP_PACKET* setup, uint8_t* data, int size) {
+    USBEndpoint* ep = &ep_ctl_in_out;
+    setAddr(dev.addr);
+    token_setup(ep, setup, size); // setup stage
+    if (LastStatus != ACK) {
+        USB_DBG("setup %02x", LastStatus);
+        return -1;
+    }
+    int write_len = 0;
+    if (data != NULL) {
+        write_len = token_out(ep, data, size);
+        if (write_len < 0) {
+            return -1;
+        }
+    }
+    ep->setData01(DATA1);
+    int result = token_in(ep); // status stage
+    if (result < 0) {
+        USB_DBG("result=%d %02x", result, LastStatus);
+        //return result;
+    }
+    return write_len;
+}
+
+int USBHost::InterruptRead(uint8_t* data, int size) {
+    USBEndpoint* ep = &ep_int_in;
+    setAddr(dev.addr);
+    setEndpoint();
+    const int retryLimit = 0;
+    int read_len = 0;
+    for(int n = 0; read_len < size; n++) {
+        int size2 = std::min(size-read_len, ep->getSize());
+        int result = token_in(ep, data+read_len, size2, retryLimit);
+        if (result < 0) {
+            if (LastStatus == NAK) {
+                if (n == 0) {
+                    return -1;
+                }
+                break;
+            }
+            USB_DBG("token_in result=%d %02x", result, LastStatus);
+            return result;
+        }
+        read_len += result;
+        if (result < ep->getSize()) {
+            break;
+        }
+    }
+    return read_len;
+}
+
+int USBHost::BulkRead(uint8_t* data, int size, int timeout_ms) {
+    USBEndpoint* ep = &ep_bulk_in;
+    setAddr(dev.addr);
+    setEndpoint();
+    int retryLimit = (timeout_ms == 0) ? 0 : 10;
+    int read_len = 0;
+    Timer t;
+    for(int n = 0; read_len < size; n++) {
+        int size2 = std::min(size-read_len, ep->getSize());
+        int result = token_in(ep, data+read_len, size2, retryLimit);
+        if (result < 0) {
+            if (LastStatus == NAK) {
+                if (n == 0) {
+                    return -1;
+                }
+                break;
+            }
+            USB_DBG("token_in result=%d %02x", result, LastStatus);
+            return result;
+        }
+        read_len += result;
+        if (result < ep->getSize()) {
+            break;
+        }
+        if (timeout_ms > 0 && t.read_ms() > timeout_ms) {
+            USB_DBG("timeout_ms: %d", timeout_ms);
+            break;
+        }
+    }
+    return read_len;
+}
+
+int USBHost::BulkWrite(const uint8_t* data, int size) {
+    USBEndpoint* ep = &ep_bulk_out;
+    setAddr(dev.addr);
+    setEndpoint();
+    int write_len = 0;
+    for(int n = 0; write_len < size; n++) {
+        int size2 = std::min(size-write_len, ep->getSize());
+        int result = token_out(ep, data+write_len, size2);
+        if (result < 0) {
+            if (LastStatus == NAK) {
+                if (n == 0) {
+                    return -1;
+                }
+                break;
+            }
+            USB_DBG("token_out result=%d %02x", result, LastStatus);
+            return result;
+        }
+        write_len += result;
+        if (result < ep->getSize()) {
+            break;
+        }
+    }
+    return write_len;
+}
+
+int USBHost::IsochronousRead(USBEndpoint* ep, uint8_t* data, int size) {
+    setAddr(dev.addr);
+    return token_iso_in(ep, data, size);
+}
+