Simple USBHost library for Nucleo F446RE/F411RE/F401RE FRDM-KL46Z/KL25Z/F64F LPC4088/LPC1768

Dependencies:   FATFileSystem

Dependents:   F401RE-BTstack_example F401RE-USBHostMSD_HelloWorld

Fork of KL46Z-USBHost by Norimasa Okamoto

簡易USBホストライブラリです。
official-USBHostの下位互換で対応プログラムを僅かな修正で動かすことが出来ます。

Platforms

  • Nucleo F446RE
  • Nucleo F411RE
  • Nucleo F401RE
  • FRDM-K64F
  • FRDM-KL46Z
  • FRDM-KL25Z
  • LPC4088
  • LPC1768

Nucleo F446RE/F411RE/F401REのUSB接続方法

ST morphoUSB
U5V (CN10-8)VBUS (1 RED)
PA11 (CN10-14)DM  (2 WHITE)
PA12 (CN10-12)DP  (3 GREEN)
GND (CN10-20)GND (4 BLACK)

Examples

Import programF446RE-USBHostMouse_HelloWorld

USBHostMouse Hello World for ST-Nucleo-F446RE

Import programF401RE-USBHostMSD_HelloWorld

Simple USBHost MSD(USB flash drive) for Nucleo F401RE/FRDM-KL46Z test program

Import programF401RE-USBHostC270_example

Simple USBHost WebCam test program

Import programK64F_USBHostC270_example

Simple USBHost C270 example

Import programF401RE-BTstack_example

BTstack for Nucleo F401RE/FRDM-KL46Z example program

Import programUSBHostRSSI_example

Bluetooth device discovery example program.

Import programKL46Z-USBHostGPS_HelloWorld

Simple USBHost GPS Dongle Receiver for FRDM-KL46Z test program

USBHost.cpp

Committer:
va009039
Date:
2014-01-23
Revision:
2:0cdac6bcc534
Parent:
1:c072d9e580b0

File content as of revision 2:0cdac6bcc534:

// 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;
}