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/USBEndpoint.h

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

File content as of revision 0:11152e69fc05:

/* mbed USBHost Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#pragma once
#include "rtos.h"
#include "FunctionPointer.h"
#include "USBHostTypes.h"
#include "USBDeviceConnected.h"

class USBDeviceConnected;

#define HCTD_QUEUE_SIZE 3

struct HCTD;
struct HCED;
struct HCITD;
class USBHost;

/**
* USBEndpoint class
*/
class USBEndpoint {
public:
    /**
    * Constructor
    */
    USBEndpoint(USBDeviceConnected* _dev) {
        init(CONTROL_ENDPOINT, IN, 8, 0);
        dev = _dev;
    }

    /**
    * Initialize an endpoint
    *
    * @param type endpoint type
    * @param dir endpoint direction
    * @param size endpoint size
    * @param ep_number endpoint number
    */
    void init(ENDPOINT_TYPE _type, ENDPOINT_DIRECTION _dir, uint32_t size, uint8_t ep_number) {
        setType(_type);
        dir = _dir;
        MaxPacketSize = size;
        address = ep_number;
        m_pED = NULL;
        //data01_toggle = DATA0;
    }

    USBEndpoint(int addr, uint8_t ep = 0, uint16_t size = 8, int lowSpeed = 0);

    /**
     *  Attach a member function to call when a transfer is finished
     *
     *  @param tptr pointer to the object to call the member function on
     *  @param mptr pointer to the member function to be called
     */
    template<typename T>
    void attach(T* tptr, void (T::*mptr)(void)) {
        if((mptr != NULL) && (tptr != NULL)) {
            rx.attach(tptr, mptr);
        }
    }

    /**
     * Attach a callback called when a transfer is finished
     *
     * @param fptr function pointer
     */
    void attach(void (*fptr)(void)) {
        if(fptr != NULL) {
            rx.attach(fptr);
        }
    }

    /**
    * Call the handler associted to the end of a transfer
    */
    void call() {
        rx.call();
    };

    int GetAddr();
    int GetLowSpeed();
    void update_FunctionAddress(int addr);
    void update_MaxPacketSize(uint16_t size);
    //
    virtual void irqWdhHandler(HCTD* td) {m_queue.put(td);} // WDH
    HCTD* get_queue_HCTD(uint32_t millisec=osWaitForever);
    HCED* m_pED;
    // report
    uint8_t m_ConditionCode;
    int m_report_queue_error;

    void setType(ENDPOINT_TYPE _type) { type = _type; };
    void setState(uint8_t st){}; // dummy
    void setLengthTransferred(int len) { transferred = len; };
    void setBuffer(uint8_t* buf, int size) { buf_start = buf, buf_size = size; }
    void setSize(int size) { MaxPacketSize = size; }
    void setNextEndpoint(USBEndpoint* ep) { nextEp = ep; };

    USBDeviceConnected* getDevice() { return dev; }
    ENDPOINT_TYPE getType() { return type; };
    int getLengthTransferred() { return transferred; }
    uint8_t *getBufStart() { return buf_start; }
    int getBufSize() { return buf_size; }
    uint8_t getAddress(){ return address; };
    int getSize() { return MaxPacketSize; }
    ENDPOINT_DIRECTION getDir() { return dir; }
    USBEndpoint* nextEndpoint() { return nextEp; };

private:
    ENDPOINT_TYPE type;
    ENDPOINT_DIRECTION dir;
    USBDeviceConnected* dev;
    uint8_t address;
    int transferred;
    uint8_t * buf_start;
    int buf_size;
    FunctionPointer rx;
    int MaxPacketSize;
    USBEndpoint* nextEp;

protected:
    Queue<HCTD, HCTD_QUEUE_SIZE> m_queue; // TD done queue
    int m_td_queue_count;
    USBHost* host;
};

class ControlEp : public USBEndpoint {
public:
    ControlEp(int lowSpeed = 0);
    USB_TYPE GetDescriptor(int descType, int descIndex, uint8_t* data, int length);
    USB_TYPE SetAddress(int addr); // device address 
    USB_TYPE SetConfiguration(int config);
    USB_TYPE GetConfiguration(int *config);
    USB_TYPE SetInterfaceAlternate(int interface, int alternate);
    USB_TYPE GetInterface(int interface, int *alternate);
    USB_TYPE controlSend(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex=0, const uint8_t* data=NULL, int length=0);
    USB_TYPE controlReceive(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t* data, int length);
    USB_TYPE GetStringDescriptor(char* s, int index);

private:
    USB_TYPE open(int addr);

};

class BulkEp : public USBEndpoint {
public:
    BulkEp(int addr, uint8_t ep, uint16_t size);
};

class InterruptEp : public USBEndpoint {
public:
    InterruptEp(int addr, uint8_t ep, uint16_t size, int lowSpeed=0);
};

class IsochronousEp : public USBEndpoint {
public:
    IsochronousEp(int addr, uint8_t ep, uint16_t size);
    void reset(int delay_ms = 100);
    HCITD* isochronousReveive(int millisec=osWaitForever);
    int isochronousSend(uint8_t* buf, int len, int millisec=osWaitForever);
    HCITD* get_queue_HCITD(int millisec);
    uint16_t m_PacketSize;
private:
    HCITD* new_HCITD(USBEndpoint* obj);
    int m_itd_queue_count;
    uint16_t m_FrameNumber;
    int m_FrameCount; // 1-8
};

class EndpointQueue {
public:
    EndpointQueue():head(NULL),tail(NULL) {}
    void push(USBEndpoint* ep) {
        if (head) {
            tail->setNextEndpoint(ep);
        } else {
            head = ep;
        }
        tail = ep;
        ep->setNextEndpoint(NULL);
    }
    USBEndpoint* pop() {
        USBEndpoint* ep = head;
        if (ep) {
            head = ep->nextEndpoint();
        }
        return ep;
    }
    bool empty() { return head == NULL; }

private:
    USBEndpoint* head;
    USBEndpoint* tail;
};