Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Revision:
3:a3872f7593e2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHost/USBHost.cpp	Sat Jan 25 12:51:44 2014 +0000
@@ -0,0 +1,331 @@
+// 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() {
+    uint8_t desc[64];
+    MaxPacketSize0 = 8;
+    dev_addr = 0;
+    USB0->ADDR = (lowSpeed ? USB_ADDR_LSEN_MASK : 0x00) | USB_ADDR_ADDR(dev_addr);
+    wait_ms(100);
+    SETUP_PACKET setup_get_descriptor = {0x80, GET_DESCRIPTOR, 1<<8, 0, 0};
+    int result = ControlRead(&setup_get_descriptor, desc, 8);
+    if (result < 8) {
+        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);
+    MaxPacketSize0 = 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();
+
+    USB_INFO("VID: %04x, PID: %04x\n", dev_desc->idVendor, dev_desc->idProduct);
+    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) {
+    setAddr(dev_addr);
+    token_setup(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, MaxPacketSize0);
+        int result = token_in(0, 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 < MaxPacketSize0) {
+            break;
+        }
+    }    
+    int result = token_out(0); // 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) {
+    setAddr(dev_addr);
+    token_setup(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(0, data, size);
+        if (write_len < 0) {
+            return -1;
+        }
+    }
+    int result = token_in(0); // 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) {
+    setAddr(dev_addr);
+    setEndpoint();
+    const int retryLimit = 0;
+    int max_packet_size = ep_int_in.getSize();
+    int read_len = 0;
+    for(int n = 0; read_len < size; n++) {
+        int size2 = std::min(size-read_len, max_packet_size);
+        int result = token_in(ep_int_in.getAddress() & 0x7f, 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 < max_packet_size) {
+            break;
+        }
+    }
+    return read_len;
+}
+
+int USBHost::BulkRead(uint8_t* data, int size, int timeout_ms) {
+    setAddr(dev_addr);
+    setEndpoint();
+    int max_packet_size = ep_bulk_in.getSize();
+    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, max_packet_size);
+        int result = token_in(ep_bulk_in.getAddress() & 0x7f, 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 < max_packet_size) {
+            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) {
+    setAddr(dev_addr);
+    setEndpoint();
+    int max_packet_size = ep_bulk_out.getSize();
+    int write_len = 0;
+    for(int n = 0; write_len < size; n++) {
+        int size2 = std::min(size-write_len, max_packet_size);
+        int result = token_out(ep_bulk_out.getAddress(), data+write_len, size2);
+        if (result < 0) {
+            if (LastStatus == NAK) {
+                if (n == 0) {
+                    return -1;
+                }
+                break;
+            }
+            //USB_DBG("token_in result=%d %02x", result, LastStatus);
+            return result;
+        }
+        write_len += result;
+        if (result < max_packet_size) {
+            break;
+        }
+    }
+    return write_len;
+}
+