Simple USBHost MSD(USB flash drive) for EA LPC4088 QSB test program
Dependencies: LPC4088-USBHost mbed
EA LPC4088をUSBホストにしてUSBフラッシュメモリ(USB flash drive)を読み書きするテストプログラムです。
https://bitbucket.org/va009039/lpc4088_usbhost
LPC4088-USBHost/USBHost/USBHost.h@0:11152e69fc05, 2014-04-22 (annotated)
- 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?
User | Revision | Line number | New 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 "mbed.h" |
va009039 | 0:11152e69fc05 | 19 | #include "USBHALHost.h" |
va009039 | 0:11152e69fc05 | 20 | #include "USBDeviceConnected.h" |
va009039 | 0:11152e69fc05 | 21 | #include "IUSBEnumerator.h" |
va009039 | 0:11152e69fc05 | 22 | #include "USBHostConf.h" |
va009039 | 0:11152e69fc05 | 23 | #include "dbg.h" |
va009039 | 0:11152e69fc05 | 24 | #include "myvector.h" |
va009039 | 0:11152e69fc05 | 25 | |
va009039 | 0:11152e69fc05 | 26 | // USB STANDARD REQUEST DEFINITIONS |
va009039 | 0:11152e69fc05 | 27 | #define USB_DESCRIPTOR_TYPE_STRING 3 |
va009039 | 0:11152e69fc05 | 28 | #define USB_DESCRIPTOR_TYPE_HUB 0x29 |
va009039 | 0:11152e69fc05 | 29 | |
va009039 | 0:11152e69fc05 | 30 | #pragma pack(push,1) |
va009039 | 0:11152e69fc05 | 31 | struct StringDescriptor {// offset |
va009039 | 0:11152e69fc05 | 32 | uint8_t bLength; // +0 |
va009039 | 0:11152e69fc05 | 33 | uint8_t bDescriptorType; // +1 |
va009039 | 0:11152e69fc05 | 34 | char bString[0]; // +2 |
va009039 | 0:11152e69fc05 | 35 | }; |
va009039 | 0:11152e69fc05 | 36 | #pragma pack(pop) |
va009039 | 0:11152e69fc05 | 37 | |
va009039 | 0:11152e69fc05 | 38 | /** |
va009039 | 0:11152e69fc05 | 39 | * USBHost class |
va009039 | 0:11152e69fc05 | 40 | * This class is a singleton. All drivers have a reference on the static USBHost instance |
va009039 | 0:11152e69fc05 | 41 | */ |
va009039 | 0:11152e69fc05 | 42 | class USBHost : public USBHALHost { |
va009039 | 0:11152e69fc05 | 43 | public: |
va009039 | 0:11152e69fc05 | 44 | /** |
va009039 | 0:11152e69fc05 | 45 | * Static method to create or retrieve the single USBHost instance |
va009039 | 0:11152e69fc05 | 46 | */ |
va009039 | 0:11152e69fc05 | 47 | static USBHost* getHostInst(); |
va009039 | 0:11152e69fc05 | 48 | |
va009039 | 0:11152e69fc05 | 49 | /** |
va009039 | 0:11152e69fc05 | 50 | * Control read: setup stage, data stage and status stage |
va009039 | 0:11152e69fc05 | 51 | * |
va009039 | 0:11152e69fc05 | 52 | * @param dev the control read will be done for this device |
va009039 | 0:11152e69fc05 | 53 | * @param requestType request type |
va009039 | 0:11152e69fc05 | 54 | * @param request request |
va009039 | 0:11152e69fc05 | 55 | * @param value value |
va009039 | 0:11152e69fc05 | 56 | * @param index index |
va009039 | 0:11152e69fc05 | 57 | * @param buf pointer on a buffer where will be store the data received |
va009039 | 0:11152e69fc05 | 58 | * @param len length of the transfer |
va009039 | 0:11152e69fc05 | 59 | * |
va009039 | 0:11152e69fc05 | 60 | * @returns status of the control read |
va009039 | 0:11152e69fc05 | 61 | */ |
va009039 | 0:11152e69fc05 | 62 | USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len); |
va009039 | 0:11152e69fc05 | 63 | |
va009039 | 0:11152e69fc05 | 64 | /** |
va009039 | 0:11152e69fc05 | 65 | * Control write: setup stage, data stage and status stage |
va009039 | 0:11152e69fc05 | 66 | * |
va009039 | 0:11152e69fc05 | 67 | * @param dev the control write will be done for this device |
va009039 | 0:11152e69fc05 | 68 | * @param requestType request type |
va009039 | 0:11152e69fc05 | 69 | * @param request request |
va009039 | 0:11152e69fc05 | 70 | * @param value value |
va009039 | 0:11152e69fc05 | 71 | * @param index index |
va009039 | 0:11152e69fc05 | 72 | * @param buf pointer on a buffer which will be written |
va009039 | 0:11152e69fc05 | 73 | * @param len length of the transfer |
va009039 | 0:11152e69fc05 | 74 | * |
va009039 | 0:11152e69fc05 | 75 | * @returns status of the control write |
va009039 | 0:11152e69fc05 | 76 | */ |
va009039 | 0:11152e69fc05 | 77 | USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len); |
va009039 | 0:11152e69fc05 | 78 | |
va009039 | 0:11152e69fc05 | 79 | /** |
va009039 | 0:11152e69fc05 | 80 | * Bulk read |
va009039 | 0:11152e69fc05 | 81 | * |
va009039 | 0:11152e69fc05 | 82 | * @param dev the bulk transfer will be done for this device |
va009039 | 0:11152e69fc05 | 83 | * @param ep USBEndpoint which will be used to read a packet |
va009039 | 0:11152e69fc05 | 84 | * @param buf pointer on a buffer where will be store the data received |
va009039 | 0:11152e69fc05 | 85 | * @param len length of the transfer |
va009039 | 0:11152e69fc05 | 86 | * @param blocking if true, the read is blocking (wait for completion) |
va009039 | 0:11152e69fc05 | 87 | * |
va009039 | 0:11152e69fc05 | 88 | * @returns status of the bulk read |
va009039 | 0:11152e69fc05 | 89 | */ |
va009039 | 0:11152e69fc05 | 90 | USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
va009039 | 0:11152e69fc05 | 91 | |
va009039 | 0:11152e69fc05 | 92 | /** |
va009039 | 0:11152e69fc05 | 93 | * Bulk write |
va009039 | 0:11152e69fc05 | 94 | * |
va009039 | 0:11152e69fc05 | 95 | * @param dev the bulk transfer will be done for this device |
va009039 | 0:11152e69fc05 | 96 | * @param ep USBEndpoint which will be used to write a packet |
va009039 | 0:11152e69fc05 | 97 | * @param buf pointer on a buffer which will be written |
va009039 | 0:11152e69fc05 | 98 | * @param len length of the transfer |
va009039 | 0:11152e69fc05 | 99 | * @param blocking if true, the write is blocking (wait for completion) |
va009039 | 0:11152e69fc05 | 100 | * |
va009039 | 0:11152e69fc05 | 101 | * @returns status of the bulk write |
va009039 | 0:11152e69fc05 | 102 | */ |
va009039 | 0:11152e69fc05 | 103 | USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
va009039 | 0:11152e69fc05 | 104 | |
va009039 | 0:11152e69fc05 | 105 | /** |
va009039 | 0:11152e69fc05 | 106 | * Interrupt read |
va009039 | 0:11152e69fc05 | 107 | * |
va009039 | 0:11152e69fc05 | 108 | * @param dev the interrupt transfer will be done for this device |
va009039 | 0:11152e69fc05 | 109 | * @param ep USBEndpoint which will be used to write a packet |
va009039 | 0:11152e69fc05 | 110 | * @param buf pointer on a buffer which will be written |
va009039 | 0:11152e69fc05 | 111 | * @param len length of the transfer |
va009039 | 0:11152e69fc05 | 112 | * @param blocking if true, the read is blocking (wait for completion) |
va009039 | 0:11152e69fc05 | 113 | * |
va009039 | 0:11152e69fc05 | 114 | * @returns status of the interrupt read |
va009039 | 0:11152e69fc05 | 115 | */ |
va009039 | 0:11152e69fc05 | 116 | USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
va009039 | 0:11152e69fc05 | 117 | |
va009039 | 0:11152e69fc05 | 118 | /** |
va009039 | 0:11152e69fc05 | 119 | * Interrupt write |
va009039 | 0:11152e69fc05 | 120 | * |
va009039 | 0:11152e69fc05 | 121 | * @param dev the interrupt transfer will be done for this device |
va009039 | 0:11152e69fc05 | 122 | * @param ep USBEndpoint which will be used to write a packet |
va009039 | 0:11152e69fc05 | 123 | * @param buf pointer on a buffer which will be written |
va009039 | 0:11152e69fc05 | 124 | * @param len length of the transfer |
va009039 | 0:11152e69fc05 | 125 | * @param blocking if true, the write is blocking (wait for completion) |
va009039 | 0:11152e69fc05 | 126 | * |
va009039 | 0:11152e69fc05 | 127 | * @returns status of the interrupt write |
va009039 | 0:11152e69fc05 | 128 | */ |
va009039 | 0:11152e69fc05 | 129 | USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
va009039 | 0:11152e69fc05 | 130 | |
va009039 | 0:11152e69fc05 | 131 | /** |
va009039 | 0:11152e69fc05 | 132 | * Enumerate a device. |
va009039 | 0:11152e69fc05 | 133 | * |
va009039 | 0:11152e69fc05 | 134 | * @param dev device which will be enumerated |
va009039 | 0:11152e69fc05 | 135 | * |
va009039 | 0:11152e69fc05 | 136 | * @returns status of the enumeration |
va009039 | 0:11152e69fc05 | 137 | */ |
va009039 | 0:11152e69fc05 | 138 | USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator); |
va009039 | 0:11152e69fc05 | 139 | |
va009039 | 0:11152e69fc05 | 140 | /** |
va009039 | 0:11152e69fc05 | 141 | * Get a device |
va009039 | 0:11152e69fc05 | 142 | * |
va009039 | 0:11152e69fc05 | 143 | * @param index index of the device which will be returned |
va009039 | 0:11152e69fc05 | 144 | * |
va009039 | 0:11152e69fc05 | 145 | * @returns pointer on the "index" device |
va009039 | 0:11152e69fc05 | 146 | */ |
va009039 | 0:11152e69fc05 | 147 | USBDeviceConnected * getDevice(uint8_t index) { |
va009039 | 0:11152e69fc05 | 148 | return index < DeviceLists.size() ? DeviceLists[index] : NULL; |
va009039 | 0:11152e69fc05 | 149 | } |
va009039 | 0:11152e69fc05 | 150 | |
va009039 | 0:11152e69fc05 | 151 | /** |
va009039 | 0:11152e69fc05 | 152 | * register a driver into the host associated with a callback function called when the device is disconnected |
va009039 | 0:11152e69fc05 | 153 | * |
va009039 | 0:11152e69fc05 | 154 | * @param dev device |
va009039 | 0:11152e69fc05 | 155 | * @param intf interface number |
va009039 | 0:11152e69fc05 | 156 | * @param tptr pointer to the object to call the member function on |
va009039 | 0:11152e69fc05 | 157 | * @param mptr pointer to the member function to be called |
va009039 | 0:11152e69fc05 | 158 | */ |
va009039 | 0:11152e69fc05 | 159 | template<typename T> |
va009039 | 0:11152e69fc05 | 160 | void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) { |
va009039 | 0:11152e69fc05 | 161 | } |
va009039 | 0:11152e69fc05 | 162 | |
va009039 | 0:11152e69fc05 | 163 | // LPC4088-USBHost extensions |
va009039 | 0:11152e69fc05 | 164 | USB_TYPE controlRead(USBEndpoint* ep, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t* buf, uint32_t len); |
va009039 | 0:11152e69fc05 | 165 | USB_TYPE controlWrite(USBEndpoint* ep, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t* buf, uint32_t len); |
va009039 | 0:11152e69fc05 | 166 | static void poll(); |
va009039 | 0:11152e69fc05 | 167 | |
va009039 | 0:11152e69fc05 | 168 | private: |
va009039 | 0:11152e69fc05 | 169 | USBHost(); |
va009039 | 0:11152e69fc05 | 170 | static USBHost* inst; |
va009039 | 0:11152e69fc05 | 171 | virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed); |
va009039 | 0:11152e69fc05 | 172 | void root_enumeration(USBDeviceConnected* dev); |
va009039 | 0:11152e69fc05 | 173 | void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator); |
va009039 | 0:11152e69fc05 | 174 | myvector<USBDeviceConnected*>DeviceLists; |
va009039 | 0:11152e69fc05 | 175 | |
va009039 | 0:11152e69fc05 | 176 | void task(); |
va009039 | 0:11152e69fc05 | 177 | EndpointQueue ep_queue; |
va009039 | 0:11152e69fc05 | 178 | |
va009039 | 0:11152e69fc05 | 179 | // USB HUB |
va009039 | 0:11152e69fc05 | 180 | bool Hub(USBDeviceConnected* dev); |
va009039 | 0:11152e69fc05 | 181 | int SetPortPower(USBDeviceConnected* dev, int port); |
va009039 | 0:11152e69fc05 | 182 | int ClearPortPower(USBDeviceConnected* dev, int port); |
va009039 | 0:11152e69fc05 | 183 | int PortReset(USBDeviceConnected* dev, int port); |
va009039 | 0:11152e69fc05 | 184 | int SetPortFeature(USBDeviceConnected* dev, int feature, int index); |
va009039 | 0:11152e69fc05 | 185 | int ClearPortFeature(USBDeviceConnected* dev, int feature, int index); |
va009039 | 0:11152e69fc05 | 186 | int SetPortReset(USBDeviceConnected* dev, int port); |
va009039 | 0:11152e69fc05 | 187 | int GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status); |
va009039 | 0:11152e69fc05 | 188 | }; |
va009039 | 0:11152e69fc05 | 189 | |
va009039 | 0:11152e69fc05 | 190 | // --- HUB -------------------------------------------------- |
va009039 | 0:11152e69fc05 | 191 | class UsbHub { |
va009039 | 0:11152e69fc05 | 192 | public: |
va009039 | 0:11152e69fc05 | 193 | UsbHub(ControlEp* ctlEp = NULL); |
va009039 | 0:11152e69fc05 | 194 | static bool check(ControlEp* ctlEp); |
va009039 | 0:11152e69fc05 | 195 | USB_TYPE SetPortPower(int port); |
va009039 | 0:11152e69fc05 | 196 | USB_TYPE ClearPortPower(int port); |
va009039 | 0:11152e69fc05 | 197 | myvector<ControlEp*> PortEp; |
va009039 | 0:11152e69fc05 | 198 | template<class T> ControlEp* search(int skip = 0) { |
va009039 | 0:11152e69fc05 | 199 | for(int i = 0; i < PortEp.size(); i++) { |
va009039 | 0:11152e69fc05 | 200 | if (T::check(PortEp[i])) { |
va009039 | 0:11152e69fc05 | 201 | if (skip-- <= 0) { |
va009039 | 0:11152e69fc05 | 202 | return PortEp[i]; |
va009039 | 0:11152e69fc05 | 203 | } |
va009039 | 0:11152e69fc05 | 204 | } |
va009039 | 0:11152e69fc05 | 205 | } |
va009039 | 0:11152e69fc05 | 206 | return NULL; |
va009039 | 0:11152e69fc05 | 207 | } |
va009039 | 0:11152e69fc05 | 208 | template<class T> ControlEp* assign(int port) { |
va009039 | 0:11152e69fc05 | 209 | if (port >= 0 && port < PortEp.size()) { |
va009039 | 0:11152e69fc05 | 210 | return PortEp[port]; |
va009039 | 0:11152e69fc05 | 211 | } |
va009039 | 0:11152e69fc05 | 212 | return NULL; |
va009039 | 0:11152e69fc05 | 213 | } |
va009039 | 0:11152e69fc05 | 214 | private: |
va009039 | 0:11152e69fc05 | 215 | USB_TYPE PortReset(int port); |
va009039 | 0:11152e69fc05 | 216 | USB_TYPE SetPortFeature(int feature, int index); |
va009039 | 0:11152e69fc05 | 217 | USB_TYPE ClearPortFeature(int feature, int index); |
va009039 | 0:11152e69fc05 | 218 | USB_TYPE SetPortReset(int port); |
va009039 | 0:11152e69fc05 | 219 | USB_TYPE GetPortStatus(int port, uint32_t* status); |
va009039 | 0:11152e69fc05 | 220 | ControlEp* m_ctlEp; |
va009039 | 0:11152e69fc05 | 221 | }; |
va009039 | 0:11152e69fc05 | 222 | |
va009039 | 0:11152e69fc05 | 223 | // --- UVC -------------------------------------------------- |
va009039 | 0:11152e69fc05 | 224 | #define _30FPS 333333 |
va009039 | 0:11152e69fc05 | 225 | #define _25FPS 400000 |
va009039 | 0:11152e69fc05 | 226 | #define _20FPS 500000 |
va009039 | 0:11152e69fc05 | 227 | #define _15FPS 666666 |
va009039 | 0:11152e69fc05 | 228 | #define _10FPS 1000000 |
va009039 | 0:11152e69fc05 | 229 | #define _5FPS 2000000 |
va009039 | 0:11152e69fc05 | 230 | #define _1FPS 10000000 |
va009039 | 0:11152e69fc05 | 231 | |
va009039 | 0:11152e69fc05 | 232 | #define SET_CUR 0x01 |
va009039 | 0:11152e69fc05 | 233 | #define GET_CUR 0x81 |
va009039 | 0:11152e69fc05 | 234 | #define GET_MIN 0x82 |
va009039 | 0:11152e69fc05 | 235 | #define GET_MAX 0x83 |
va009039 | 0:11152e69fc05 | 236 | #define GET_RES 0x84 |
va009039 | 0:11152e69fc05 | 237 | #define GET_LEN 0x85 |
va009039 | 0:11152e69fc05 | 238 | #define GET_INFO 0x86 |
va009039 | 0:11152e69fc05 | 239 | #define GET_DEF 0x87 |
va009039 | 0:11152e69fc05 | 240 | |
va009039 | 0:11152e69fc05 | 241 | #define VS_PROBE_CONTROL 0x01 |
va009039 | 0:11152e69fc05 | 242 | #define VS_COMMIT_CONTROL 0x02 |
va009039 | 0:11152e69fc05 | 243 | |
va009039 | 0:11152e69fc05 | 244 | class BaseUvc { |
va009039 | 0:11152e69fc05 | 245 | public: |
va009039 | 0:11152e69fc05 | 246 | BaseUvc(){clearOnResult();} |
va009039 | 0:11152e69fc05 | 247 | void poll(int millisec=osWaitForever); |
va009039 | 0:11152e69fc05 | 248 | USB_TYPE Control(int req, int cs, int index, uint8_t* buf, int size); |
va009039 | 0:11152e69fc05 | 249 | ControlEp* m_ctlEp; |
va009039 | 0:11152e69fc05 | 250 | IsochronousEp* m_isoEp; |
va009039 | 0:11152e69fc05 | 251 | uint32_t report_cc_count[16]; // ConditionCode |
va009039 | 0:11152e69fc05 | 252 | uint32_t report_ps_cc_count[16]; // Packt Status ConditionCode |
va009039 | 0:11152e69fc05 | 253 | // callback |
va009039 | 0:11152e69fc05 | 254 | void onResult(uint16_t frame, uint8_t* buf, int len); |
va009039 | 0:11152e69fc05 | 255 | void setOnResult( void (*pMethod)(uint16_t, uint8_t*, int) ); |
va009039 | 0:11152e69fc05 | 256 | class CDummy; |
va009039 | 0:11152e69fc05 | 257 | template<class T> |
va009039 | 0:11152e69fc05 | 258 | void setOnResult( T* pItem, void (T::*pMethod)(uint16_t, uint8_t*, int) ) |
va009039 | 0:11152e69fc05 | 259 | { |
va009039 | 0:11152e69fc05 | 260 | m_pCb = NULL; |
va009039 | 0:11152e69fc05 | 261 | m_pCbItem = (CDummy*) pItem; |
va009039 | 0:11152e69fc05 | 262 | m_pCbMeth = (void (CDummy::*)(uint16_t, uint8_t*, int)) pMethod; |
va009039 | 0:11152e69fc05 | 263 | } |
va009039 | 0:11152e69fc05 | 264 | void clearOnResult(); |
va009039 | 0:11152e69fc05 | 265 | CDummy* m_pCbItem; |
va009039 | 0:11152e69fc05 | 266 | void (CDummy::*m_pCbMeth)(uint16_t, uint8_t*, int); |
va009039 | 0:11152e69fc05 | 267 | void (*m_pCb)(uint16_t, uint8_t*, int); |
va009039 | 0:11152e69fc05 | 268 | }; |
va009039 | 0:11152e69fc05 | 269 |