Simple USBHost MSD(USB flash drive) for EA LPC4088 QSB test program

Dependencies:   LPC4088-USBHost mbed

EA LPC4088をUSBホストにしてUSBフラッシュメモリ(USB flash drive)を読み書きするテストプログラムです。
/media/uploads/va009039/lpc4088-msd-1.jpg
/media/uploads/va009039/lpc4088-msd-2.png

https://bitbucket.org/va009039/lpc4088_usbhost

LPC4088-USBHost/USBHost/BaseUsbHostCtlEp.cpp

Committer:
va009039
Date:
2014-04-22
Revision:
0:11152e69fc05

File content as of revision 0:11152e69fc05:

// BaseUsbHostCtlEp.cpp 2014/4/21
#include "USBHost.h"
#define USB_DEBUG
#include "BaseUsbHostDebug.h"
#define TEST
#include "BaseUsbHostTest.h"

static uint8_t device_addr = 1;
ControlEp::ControlEp(int lowSpeed):USBEndpoint(0, 0, 8, lowSpeed) {
    setType(CONTROL_ENDPOINT);
    USB_TYPE r = open(device_addr);
    if (r == USB_TYPE_OK) {
        device_addr++;
    }
}

USB_TYPE ControlEp::SetAddress(int addr)
{
    return controlSend(0x00, 5, addr);
}

USB_TYPE ControlEp::GetDescriptor(int descType, int descIndex, uint8_t* data, int length)
{
    return controlReceive(0x80, 6, (descType<<8)|descIndex, 0, data, length);
}

USB_TYPE ControlEp::SetConfiguration(int config)
{
    return controlSend(0x00, 9, config);
}

USB_TYPE ControlEp::GetConfiguration(int *config)
{
    uint8_t buf[1];
    USB_TYPE rc = controlReceive(0x80, 8, 0, 0, buf, 1);
    *config = buf[0];
    return rc;
}

USB_TYPE ControlEp::SetInterfaceAlternate(int interface, int alternate)
{
    return controlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_INTERFACE, 
                     SET_INTERFACE, alternate, interface, NULL, 0);
}

USB_TYPE ControlEp::GetInterface(int interface, int *alternate)
{
    uint8_t buf[1];
    USB_TYPE rc = controlReceive(0x81, 10, 0, interface, buf, 1);
    *alternate = buf[0];
    return rc;
}

USB_TYPE ControlEp::GetStringDescriptor(char* s, int index)
{
    uint8_t buf[128];
    USB_TYPE r = GetDescriptor(USB_DESCRIPTOR_TYPE_STRING, index, buf, sizeof(buf));
    if (r != USB_TYPE_OK) {
        return r;
    }
    DBG_HEX(buf, sizeof(buf));
    StringDescriptor* desc = reinterpret_cast<StringDescriptor*>(buf);
    if (desc->bLength <= 2 || desc->bDescriptorType != 3) {
        return USB_TYPE_ERROR;
    }
    for(int i = 0; i < desc->bLength-2; i += 2) {
        *s++ = desc->bString[i];
    }
    *s = '\0';
    return USB_TYPE_OK;
}

USB_TYPE ControlEp::open(int addr)
{
    TEST_ASSERT(addr >= 1 && addr <= 127);
    uint8_t buf[8];
    USB_TYPE r = GetDescriptor(1, 0, buf, 8);
    if (r != USB_TYPE_OK) {
        return r;
    }
    TEST_ASSERT(buf[0] == 0x12);
    TEST_ASSERT(buf[1] == 0x01);
    TEST_ASSERT(buf[7] >= 8);
    update_MaxPacketSize(buf[7]);
    r = SetAddress(addr);    
    TEST_ASSERT(r == USB_TYPE_OK);
    if (r != USB_TYPE_OK) {
        return r;
    }
    wait_ms(2);
    update_FunctionAddress(addr);
    return USB_TYPE_OK;
}

USB_TYPE ControlEp::controlReceive(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
                     uint8_t* data, int length)
{
    return host->controlRead(this, bmRequestType, bRequest, wValue, wIndex, data, length);
}

USB_TYPE ControlEp::controlSend(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
                     const uint8_t* data, int length)
{
    return host->controlWrite(this, bmRequestType, bRequest, wValue, wIndex, (uint8_t*)data, length);
}