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

Committer:
va009039
Date:
Tue Apr 22 10:54:52 2014 +0000
Revision:
0:11152e69fc05
first commit,sync rev.25.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:11152e69fc05 1 /* mbed USBHost Library
va009039 0:11152e69fc05 2 * Copyright (c) 2006-2013 ARM Limited
va009039 0:11152e69fc05 3 *
va009039 0:11152e69fc05 4 * Licensed under the Apache License, Version 2.0 (the "License");
va009039 0:11152e69fc05 5 * you may not use this file except in compliance with the License.
va009039 0:11152e69fc05 6 * You may obtain a copy of the License at
va009039 0:11152e69fc05 7 *
va009039 0:11152e69fc05 8 * http://www.apache.org/licenses/LICENSE-2.0
va009039 0:11152e69fc05 9 *
va009039 0:11152e69fc05 10 * Unless required by applicable law or agreed to in writing, software
va009039 0:11152e69fc05 11 * distributed under the License is distributed on an "AS IS" BASIS,
va009039 0:11152e69fc05 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
va009039 0:11152e69fc05 13 * See the License for the specific language governing permissions and
va009039 0:11152e69fc05 14 * limitations under the License.
va009039 0:11152e69fc05 15 */
va009039 0:11152e69fc05 16
va009039 0:11152e69fc05 17 #pragma once
va009039 0:11152e69fc05 18 #include "rtos.h"
va009039 0:11152e69fc05 19 #include "FunctionPointer.h"
va009039 0:11152e69fc05 20 #include "USBHostTypes.h"
va009039 0:11152e69fc05 21 #include "USBDeviceConnected.h"
va009039 0:11152e69fc05 22
va009039 0:11152e69fc05 23 class USBDeviceConnected;
va009039 0:11152e69fc05 24
va009039 0:11152e69fc05 25 #define HCTD_QUEUE_SIZE 3
va009039 0:11152e69fc05 26
va009039 0:11152e69fc05 27 struct HCTD;
va009039 0:11152e69fc05 28 struct HCED;
va009039 0:11152e69fc05 29 struct HCITD;
va009039 0:11152e69fc05 30 class USBHost;
va009039 0:11152e69fc05 31
va009039 0:11152e69fc05 32 /**
va009039 0:11152e69fc05 33 * USBEndpoint class
va009039 0:11152e69fc05 34 */
va009039 0:11152e69fc05 35 class USBEndpoint {
va009039 0:11152e69fc05 36 public:
va009039 0:11152e69fc05 37 /**
va009039 0:11152e69fc05 38 * Constructor
va009039 0:11152e69fc05 39 */
va009039 0:11152e69fc05 40 USBEndpoint(USBDeviceConnected* _dev) {
va009039 0:11152e69fc05 41 init(CONTROL_ENDPOINT, IN, 8, 0);
va009039 0:11152e69fc05 42 dev = _dev;
va009039 0:11152e69fc05 43 }
va009039 0:11152e69fc05 44
va009039 0:11152e69fc05 45 /**
va009039 0:11152e69fc05 46 * Initialize an endpoint
va009039 0:11152e69fc05 47 *
va009039 0:11152e69fc05 48 * @param type endpoint type
va009039 0:11152e69fc05 49 * @param dir endpoint direction
va009039 0:11152e69fc05 50 * @param size endpoint size
va009039 0:11152e69fc05 51 * @param ep_number endpoint number
va009039 0:11152e69fc05 52 */
va009039 0:11152e69fc05 53 void init(ENDPOINT_TYPE _type, ENDPOINT_DIRECTION _dir, uint32_t size, uint8_t ep_number) {
va009039 0:11152e69fc05 54 setType(_type);
va009039 0:11152e69fc05 55 dir = _dir;
va009039 0:11152e69fc05 56 MaxPacketSize = size;
va009039 0:11152e69fc05 57 address = ep_number;
va009039 0:11152e69fc05 58 m_pED = NULL;
va009039 0:11152e69fc05 59 //data01_toggle = DATA0;
va009039 0:11152e69fc05 60 }
va009039 0:11152e69fc05 61
va009039 0:11152e69fc05 62 USBEndpoint(int addr, uint8_t ep = 0, uint16_t size = 8, int lowSpeed = 0);
va009039 0:11152e69fc05 63
va009039 0:11152e69fc05 64 /**
va009039 0:11152e69fc05 65 * Attach a member function to call when a transfer is finished
va009039 0:11152e69fc05 66 *
va009039 0:11152e69fc05 67 * @param tptr pointer to the object to call the member function on
va009039 0:11152e69fc05 68 * @param mptr pointer to the member function to be called
va009039 0:11152e69fc05 69 */
va009039 0:11152e69fc05 70 template<typename T>
va009039 0:11152e69fc05 71 void attach(T* tptr, void (T::*mptr)(void)) {
va009039 0:11152e69fc05 72 if((mptr != NULL) && (tptr != NULL)) {
va009039 0:11152e69fc05 73 rx.attach(tptr, mptr);
va009039 0:11152e69fc05 74 }
va009039 0:11152e69fc05 75 }
va009039 0:11152e69fc05 76
va009039 0:11152e69fc05 77 /**
va009039 0:11152e69fc05 78 * Attach a callback called when a transfer is finished
va009039 0:11152e69fc05 79 *
va009039 0:11152e69fc05 80 * @param fptr function pointer
va009039 0:11152e69fc05 81 */
va009039 0:11152e69fc05 82 void attach(void (*fptr)(void)) {
va009039 0:11152e69fc05 83 if(fptr != NULL) {
va009039 0:11152e69fc05 84 rx.attach(fptr);
va009039 0:11152e69fc05 85 }
va009039 0:11152e69fc05 86 }
va009039 0:11152e69fc05 87
va009039 0:11152e69fc05 88 /**
va009039 0:11152e69fc05 89 * Call the handler associted to the end of a transfer
va009039 0:11152e69fc05 90 */
va009039 0:11152e69fc05 91 void call() {
va009039 0:11152e69fc05 92 rx.call();
va009039 0:11152e69fc05 93 };
va009039 0:11152e69fc05 94
va009039 0:11152e69fc05 95 int GetAddr();
va009039 0:11152e69fc05 96 int GetLowSpeed();
va009039 0:11152e69fc05 97 void update_FunctionAddress(int addr);
va009039 0:11152e69fc05 98 void update_MaxPacketSize(uint16_t size);
va009039 0:11152e69fc05 99 //
va009039 0:11152e69fc05 100 virtual void irqWdhHandler(HCTD* td) {m_queue.put(td);} // WDH
va009039 0:11152e69fc05 101 HCTD* get_queue_HCTD(uint32_t millisec=osWaitForever);
va009039 0:11152e69fc05 102 HCED* m_pED;
va009039 0:11152e69fc05 103 // report
va009039 0:11152e69fc05 104 uint8_t m_ConditionCode;
va009039 0:11152e69fc05 105 int m_report_queue_error;
va009039 0:11152e69fc05 106
va009039 0:11152e69fc05 107 void setType(ENDPOINT_TYPE _type) { type = _type; };
va009039 0:11152e69fc05 108 void setState(uint8_t st){}; // dummy
va009039 0:11152e69fc05 109 void setLengthTransferred(int len) { transferred = len; };
va009039 0:11152e69fc05 110 void setBuffer(uint8_t* buf, int size) { buf_start = buf, buf_size = size; }
va009039 0:11152e69fc05 111 void setSize(int size) { MaxPacketSize = size; }
va009039 0:11152e69fc05 112 void setNextEndpoint(USBEndpoint* ep) { nextEp = ep; };
va009039 0:11152e69fc05 113
va009039 0:11152e69fc05 114 USBDeviceConnected* getDevice() { return dev; }
va009039 0:11152e69fc05 115 ENDPOINT_TYPE getType() { return type; };
va009039 0:11152e69fc05 116 int getLengthTransferred() { return transferred; }
va009039 0:11152e69fc05 117 uint8_t *getBufStart() { return buf_start; }
va009039 0:11152e69fc05 118 int getBufSize() { return buf_size; }
va009039 0:11152e69fc05 119 uint8_t getAddress(){ return address; };
va009039 0:11152e69fc05 120 int getSize() { return MaxPacketSize; }
va009039 0:11152e69fc05 121 ENDPOINT_DIRECTION getDir() { return dir; }
va009039 0:11152e69fc05 122 USBEndpoint* nextEndpoint() { return nextEp; };
va009039 0:11152e69fc05 123
va009039 0:11152e69fc05 124 private:
va009039 0:11152e69fc05 125 ENDPOINT_TYPE type;
va009039 0:11152e69fc05 126 ENDPOINT_DIRECTION dir;
va009039 0:11152e69fc05 127 USBDeviceConnected* dev;
va009039 0:11152e69fc05 128 uint8_t address;
va009039 0:11152e69fc05 129 int transferred;
va009039 0:11152e69fc05 130 uint8_t * buf_start;
va009039 0:11152e69fc05 131 int buf_size;
va009039 0:11152e69fc05 132 FunctionPointer rx;
va009039 0:11152e69fc05 133 int MaxPacketSize;
va009039 0:11152e69fc05 134 USBEndpoint* nextEp;
va009039 0:11152e69fc05 135
va009039 0:11152e69fc05 136 protected:
va009039 0:11152e69fc05 137 Queue<HCTD, HCTD_QUEUE_SIZE> m_queue; // TD done queue
va009039 0:11152e69fc05 138 int m_td_queue_count;
va009039 0:11152e69fc05 139 USBHost* host;
va009039 0:11152e69fc05 140 };
va009039 0:11152e69fc05 141
va009039 0:11152e69fc05 142 class ControlEp : public USBEndpoint {
va009039 0:11152e69fc05 143 public:
va009039 0:11152e69fc05 144 ControlEp(int lowSpeed = 0);
va009039 0:11152e69fc05 145 USB_TYPE GetDescriptor(int descType, int descIndex, uint8_t* data, int length);
va009039 0:11152e69fc05 146 USB_TYPE SetAddress(int addr); // device address
va009039 0:11152e69fc05 147 USB_TYPE SetConfiguration(int config);
va009039 0:11152e69fc05 148 USB_TYPE GetConfiguration(int *config);
va009039 0:11152e69fc05 149 USB_TYPE SetInterfaceAlternate(int interface, int alternate);
va009039 0:11152e69fc05 150 USB_TYPE GetInterface(int interface, int *alternate);
va009039 0:11152e69fc05 151 USB_TYPE controlSend(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex=0, const uint8_t* data=NULL, int length=0);
va009039 0:11152e69fc05 152 USB_TYPE controlReceive(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t* data, int length);
va009039 0:11152e69fc05 153 USB_TYPE GetStringDescriptor(char* s, int index);
va009039 0:11152e69fc05 154
va009039 0:11152e69fc05 155 private:
va009039 0:11152e69fc05 156 USB_TYPE open(int addr);
va009039 0:11152e69fc05 157
va009039 0:11152e69fc05 158 };
va009039 0:11152e69fc05 159
va009039 0:11152e69fc05 160 class BulkEp : public USBEndpoint {
va009039 0:11152e69fc05 161 public:
va009039 0:11152e69fc05 162 BulkEp(int addr, uint8_t ep, uint16_t size);
va009039 0:11152e69fc05 163 };
va009039 0:11152e69fc05 164
va009039 0:11152e69fc05 165 class InterruptEp : public USBEndpoint {
va009039 0:11152e69fc05 166 public:
va009039 0:11152e69fc05 167 InterruptEp(int addr, uint8_t ep, uint16_t size, int lowSpeed=0);
va009039 0:11152e69fc05 168 };
va009039 0:11152e69fc05 169
va009039 0:11152e69fc05 170 class IsochronousEp : public USBEndpoint {
va009039 0:11152e69fc05 171 public:
va009039 0:11152e69fc05 172 IsochronousEp(int addr, uint8_t ep, uint16_t size);
va009039 0:11152e69fc05 173 void reset(int delay_ms = 100);
va009039 0:11152e69fc05 174 HCITD* isochronousReveive(int millisec=osWaitForever);
va009039 0:11152e69fc05 175 int isochronousSend(uint8_t* buf, int len, int millisec=osWaitForever);
va009039 0:11152e69fc05 176 HCITD* get_queue_HCITD(int millisec);
va009039 0:11152e69fc05 177 uint16_t m_PacketSize;
va009039 0:11152e69fc05 178 private:
va009039 0:11152e69fc05 179 HCITD* new_HCITD(USBEndpoint* obj);
va009039 0:11152e69fc05 180 int m_itd_queue_count;
va009039 0:11152e69fc05 181 uint16_t m_FrameNumber;
va009039 0:11152e69fc05 182 int m_FrameCount; // 1-8
va009039 0:11152e69fc05 183 };
va009039 0:11152e69fc05 184
va009039 0:11152e69fc05 185 class EndpointQueue {
va009039 0:11152e69fc05 186 public:
va009039 0:11152e69fc05 187 EndpointQueue():head(NULL),tail(NULL) {}
va009039 0:11152e69fc05 188 void push(USBEndpoint* ep) {
va009039 0:11152e69fc05 189 if (head) {
va009039 0:11152e69fc05 190 tail->setNextEndpoint(ep);
va009039 0:11152e69fc05 191 } else {
va009039 0:11152e69fc05 192 head = ep;
va009039 0:11152e69fc05 193 }
va009039 0:11152e69fc05 194 tail = ep;
va009039 0:11152e69fc05 195 ep->setNextEndpoint(NULL);
va009039 0:11152e69fc05 196 }
va009039 0:11152e69fc05 197 USBEndpoint* pop() {
va009039 0:11152e69fc05 198 USBEndpoint* ep = head;
va009039 0:11152e69fc05 199 if (ep) {
va009039 0:11152e69fc05 200 head = ep->nextEndpoint();
va009039 0:11152e69fc05 201 }
va009039 0:11152e69fc05 202 return ep;
va009039 0:11152e69fc05 203 }
va009039 0:11152e69fc05 204 bool empty() { return head == NULL; }
va009039 0:11152e69fc05 205
va009039 0:11152e69fc05 206 private:
va009039 0:11152e69fc05 207 USBEndpoint* head;
va009039 0:11152e69fc05 208 USBEndpoint* tail;
va009039 0:11152e69fc05 209 };
va009039 0:11152e69fc05 210
va009039 0:11152e69fc05 211