Takuya Urakawa / F401RE-USBHost

Dependencies:   FATFileSystem

Dependents:   F401RE-USBHostMIDI_RecieveExample

Fork of F401RE-USBHost by Norimasa Okamoto

Revision:
3:a3872f7593e2
Parent:
2:0cdac6bcc534
Child:
4:21d651ad6987
diff -r 0cdac6bcc534 -r a3872f7593e2 USBHost.cpp
--- a/USBHost.cpp	Thu Jan 23 08:32:54 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,226 +0,0 @@
-// 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_DBG_HEX(A,B) debug_hex(A,B)
-void debug_hex(uint8_t* buf, int size);
-#else
-#define USB_DBG(...) while(0)
-#define USB_DBG_HEX(A,B) 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))
-
-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);
-        return false;
-    }
-    USB_DBG_HEX(desc, result);
-    MaxPacketSize0 = desc[7];
-
-    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);
-        return false;
-    }
-    wait_ms(100);
-    dev_addr = 1;
-
-    result = ControlRead(&setup_get_descriptor, desc, sizeof(desc));
-    if (result < 8) {
-        USB_DBG("result=%d", result);
-        return false;
-    }
-    USB_DBG_HEX(desc, result);
-
-    setup_get_descriptor.wValue = 2<<8; // config descriptor
-    result = ControlRead(&setup_get_descriptor, desc, 4);
-    if (result != 4) {
-        USB_DBG("result=%d", result);
-        return false;
-    }
-    USB_DBG_HEX(desc, 4);
-
-    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);
-        return false;
-    }
-    USB_DBG_HEX(buf, TotalLength);
-
-    for(int i = 0; i < TotalLength; ) {
-        int Length = buf[i];
-        uint8_t DescriptorType = buf[i+1];
-        if (DescriptorType == 0x05) { // endpoint
-            uint8_t EndpointAddress = buf[i+2];
-            uint8_t Attributes = buf[i+3];
-            if (Attributes == 0x03) { // interrupt
-                if (EndpointAddress & 0x80) {
-                    ep_int_in = EndpointAddress;
-                }
-            } else if (Attributes == 0x02) { // bulk
-                if (EndpointAddress & 0x80) {
-                    ep_bulk_in = EndpointAddress;
-                } else {
-                    ep_bulk_out = EndpointAddress;
-                }
-            }
-        }
-        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);
-        if (lowSpeed && LastStatus == STALL) { // TODO:
-            wait_ms(100);
-            return true;
-        }
-        return false;
-    }
-    wait_ms(100);
-    return true;
-}
-
-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;
-    }
-    rx_data01[0] = DATA1;
-    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;
-        }
-    }    
-    tx_data01[0] = rx_data01[0];
-    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;
-    }
-
-    tx_data01[0] = DATA1;
-    int write_len = 0;
-    if (data != NULL) {
-        write_len = token_out(0, data, size);
-        if (write_len < 0) {
-            return -1;
-        }
-    }
-    rx_data01[0] = tx_data01[0];
-    int result = token_in(0); // status stage
-    if (result < 0) {
-        return result;
-    }
-    return write_len;
-}
-
-int USBHost::InterruptRead(uint8_t* data, int size) {
-    setAddr(dev_addr);
-    setEndpoint();
-    const int retryLimit = 0;
-    return token_in(ep_int_in & 0x7f, data, size, retryLimit);
-}
-
-int USBHost::BulkRead(uint8_t* data, int size) {
-    setAddr(dev_addr);
-    setEndpoint();
-    const int max_packet_size = 64;
-    int read_len = 0;
-    while(read_len < size) {
-        int size2 = std::min(size-read_len, max_packet_size);
-        int result = token_in(ep_bulk_in & 0x7f, data+read_len, size2);
-        if (result < 0) {
-            //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::BulkWrite(const uint8_t* data, int size) {
-    setAddr(dev_addr);
-    setEndpoint();
-    const int max_packet_size = 64;
-    int write_len = 0;
-    while(write_len < size) {
-        int size2 = std::min(size-write_len, max_packet_size);
-        int result = token_out(ep_bulk_out, data+write_len, size2);
-        if (result < 0) {
-            //USB_DBG("token_in result=%d %02x", result, LastStatus);
-            return result;
-        }
-        write_len += result;
-        if (result < max_packet_size) {
-            break;
-        }
-    }
-    return write_len;
-}
-