Simple USBHost library for LPC4088. Backward compatibility of official-USBHost.

Dependencies:   FATFileSystem mbed-rtos

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

Import programLPC4088-USBHostMSD_HelloWorld

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

Import programLPC4088-BTstack_example

BTstack for EA LPC4088 QSB example program

Import programLPC4088-USBHostC270_example

Simple USBHost WebCam for EA LPC4088 QSB/LPC1768 test program

Committer:
va009039
Date:
Fri Apr 25 05:18:55 2014 +0000
Revision:
0:148fca6fd246
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:148fca6fd246 1 /* mbed USBHost Library
va009039 0:148fca6fd246 2 * Copyright (c) 2006-2013 ARM Limited
va009039 0:148fca6fd246 3 *
va009039 0:148fca6fd246 4 * Licensed under the Apache License, Version 2.0 (the "License");
va009039 0:148fca6fd246 5 * you may not use this file except in compliance with the License.
va009039 0:148fca6fd246 6 * You may obtain a copy of the License at
va009039 0:148fca6fd246 7 *
va009039 0:148fca6fd246 8 * http://www.apache.org/licenses/LICENSE-2.0
va009039 0:148fca6fd246 9 *
va009039 0:148fca6fd246 10 * Unless required by applicable law or agreed to in writing, software
va009039 0:148fca6fd246 11 * distributed under the License is distributed on an "AS IS" BASIS,
va009039 0:148fca6fd246 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
va009039 0:148fca6fd246 13 * See the License for the specific language governing permissions and
va009039 0:148fca6fd246 14 * limitations under the License.
va009039 0:148fca6fd246 15 */
va009039 0:148fca6fd246 16
va009039 0:148fca6fd246 17 #pragma once
va009039 0:148fca6fd246 18 #include "mbed.h"
va009039 0:148fca6fd246 19 #include "USBHALHost.h"
va009039 0:148fca6fd246 20 #include "USBDeviceConnected.h"
va009039 0:148fca6fd246 21 #include "IUSBEnumerator.h"
va009039 0:148fca6fd246 22 #include "USBHostConf.h"
va009039 0:148fca6fd246 23 #include "dbg.h"
va009039 0:148fca6fd246 24 #include "myvector.h"
va009039 0:148fca6fd246 25
va009039 0:148fca6fd246 26 // USB STANDARD REQUEST DEFINITIONS
va009039 0:148fca6fd246 27 #define USB_DESCRIPTOR_TYPE_STRING 3
va009039 0:148fca6fd246 28 #define USB_DESCRIPTOR_TYPE_HUB 0x29
va009039 0:148fca6fd246 29
va009039 0:148fca6fd246 30 #pragma pack(push,1)
va009039 0:148fca6fd246 31 struct StringDescriptor {// offset
va009039 0:148fca6fd246 32 uint8_t bLength; // +0
va009039 0:148fca6fd246 33 uint8_t bDescriptorType; // +1
va009039 0:148fca6fd246 34 char bString[0]; // +2
va009039 0:148fca6fd246 35 };
va009039 0:148fca6fd246 36 #pragma pack(pop)
va009039 0:148fca6fd246 37
va009039 0:148fca6fd246 38 /**
va009039 0:148fca6fd246 39 * USBHost class
va009039 0:148fca6fd246 40 * This class is a singleton. All drivers have a reference on the static USBHost instance
va009039 0:148fca6fd246 41 */
va009039 0:148fca6fd246 42 class USBHost : public USBHALHost {
va009039 0:148fca6fd246 43 public:
va009039 0:148fca6fd246 44 /**
va009039 0:148fca6fd246 45 * Static method to create or retrieve the single USBHost instance
va009039 0:148fca6fd246 46 */
va009039 0:148fca6fd246 47 static USBHost* getHostInst();
va009039 0:148fca6fd246 48
va009039 0:148fca6fd246 49 /**
va009039 0:148fca6fd246 50 * Control read: setup stage, data stage and status stage
va009039 0:148fca6fd246 51 *
va009039 0:148fca6fd246 52 * @param dev the control read will be done for this device
va009039 0:148fca6fd246 53 * @param requestType request type
va009039 0:148fca6fd246 54 * @param request request
va009039 0:148fca6fd246 55 * @param value value
va009039 0:148fca6fd246 56 * @param index index
va009039 0:148fca6fd246 57 * @param buf pointer on a buffer where will be store the data received
va009039 0:148fca6fd246 58 * @param len length of the transfer
va009039 0:148fca6fd246 59 *
va009039 0:148fca6fd246 60 * @returns status of the control read
va009039 0:148fca6fd246 61 */
va009039 0:148fca6fd246 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:148fca6fd246 63
va009039 0:148fca6fd246 64 /**
va009039 0:148fca6fd246 65 * Control write: setup stage, data stage and status stage
va009039 0:148fca6fd246 66 *
va009039 0:148fca6fd246 67 * @param dev the control write will be done for this device
va009039 0:148fca6fd246 68 * @param requestType request type
va009039 0:148fca6fd246 69 * @param request request
va009039 0:148fca6fd246 70 * @param value value
va009039 0:148fca6fd246 71 * @param index index
va009039 0:148fca6fd246 72 * @param buf pointer on a buffer which will be written
va009039 0:148fca6fd246 73 * @param len length of the transfer
va009039 0:148fca6fd246 74 *
va009039 0:148fca6fd246 75 * @returns status of the control write
va009039 0:148fca6fd246 76 */
va009039 0:148fca6fd246 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:148fca6fd246 78
va009039 0:148fca6fd246 79 /**
va009039 0:148fca6fd246 80 * Bulk read
va009039 0:148fca6fd246 81 *
va009039 0:148fca6fd246 82 * @param dev the bulk transfer will be done for this device
va009039 0:148fca6fd246 83 * @param ep USBEndpoint which will be used to read a packet
va009039 0:148fca6fd246 84 * @param buf pointer on a buffer where will be store the data received
va009039 0:148fca6fd246 85 * @param len length of the transfer
va009039 0:148fca6fd246 86 * @param blocking if true, the read is blocking (wait for completion)
va009039 0:148fca6fd246 87 *
va009039 0:148fca6fd246 88 * @returns status of the bulk read
va009039 0:148fca6fd246 89 */
va009039 0:148fca6fd246 90 USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 0:148fca6fd246 91
va009039 0:148fca6fd246 92 /**
va009039 0:148fca6fd246 93 * Bulk write
va009039 0:148fca6fd246 94 *
va009039 0:148fca6fd246 95 * @param dev the bulk transfer will be done for this device
va009039 0:148fca6fd246 96 * @param ep USBEndpoint which will be used to write a packet
va009039 0:148fca6fd246 97 * @param buf pointer on a buffer which will be written
va009039 0:148fca6fd246 98 * @param len length of the transfer
va009039 0:148fca6fd246 99 * @param blocking if true, the write is blocking (wait for completion)
va009039 0:148fca6fd246 100 *
va009039 0:148fca6fd246 101 * @returns status of the bulk write
va009039 0:148fca6fd246 102 */
va009039 0:148fca6fd246 103 USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 0:148fca6fd246 104
va009039 0:148fca6fd246 105 /**
va009039 0:148fca6fd246 106 * Interrupt read
va009039 0:148fca6fd246 107 *
va009039 0:148fca6fd246 108 * @param dev the interrupt transfer will be done for this device
va009039 0:148fca6fd246 109 * @param ep USBEndpoint which will be used to write a packet
va009039 0:148fca6fd246 110 * @param buf pointer on a buffer which will be written
va009039 0:148fca6fd246 111 * @param len length of the transfer
va009039 0:148fca6fd246 112 * @param blocking if true, the read is blocking (wait for completion)
va009039 0:148fca6fd246 113 *
va009039 0:148fca6fd246 114 * @returns status of the interrupt read
va009039 0:148fca6fd246 115 */
va009039 0:148fca6fd246 116 USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 0:148fca6fd246 117
va009039 0:148fca6fd246 118 /**
va009039 0:148fca6fd246 119 * Interrupt write
va009039 0:148fca6fd246 120 *
va009039 0:148fca6fd246 121 * @param dev the interrupt transfer will be done for this device
va009039 0:148fca6fd246 122 * @param ep USBEndpoint which will be used to write a packet
va009039 0:148fca6fd246 123 * @param buf pointer on a buffer which will be written
va009039 0:148fca6fd246 124 * @param len length of the transfer
va009039 0:148fca6fd246 125 * @param blocking if true, the write is blocking (wait for completion)
va009039 0:148fca6fd246 126 *
va009039 0:148fca6fd246 127 * @returns status of the interrupt write
va009039 0:148fca6fd246 128 */
va009039 0:148fca6fd246 129 USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 0:148fca6fd246 130
va009039 0:148fca6fd246 131 /**
va009039 0:148fca6fd246 132 * Enumerate a device.
va009039 0:148fca6fd246 133 *
va009039 0:148fca6fd246 134 * @param dev device which will be enumerated
va009039 0:148fca6fd246 135 *
va009039 0:148fca6fd246 136 * @returns status of the enumeration
va009039 0:148fca6fd246 137 */
va009039 0:148fca6fd246 138 USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator);
va009039 0:148fca6fd246 139
va009039 0:148fca6fd246 140 /**
va009039 0:148fca6fd246 141 * Get a device
va009039 0:148fca6fd246 142 *
va009039 0:148fca6fd246 143 * @param index index of the device which will be returned
va009039 0:148fca6fd246 144 *
va009039 0:148fca6fd246 145 * @returns pointer on the "index" device
va009039 0:148fca6fd246 146 */
va009039 0:148fca6fd246 147 USBDeviceConnected * getDevice(uint8_t index) {
va009039 0:148fca6fd246 148 return index < DeviceLists.size() ? DeviceLists[index] : NULL;
va009039 0:148fca6fd246 149 }
va009039 0:148fca6fd246 150
va009039 0:148fca6fd246 151 /**
va009039 0:148fca6fd246 152 * register a driver into the host associated with a callback function called when the device is disconnected
va009039 0:148fca6fd246 153 *
va009039 0:148fca6fd246 154 * @param dev device
va009039 0:148fca6fd246 155 * @param intf interface number
va009039 0:148fca6fd246 156 * @param tptr pointer to the object to call the member function on
va009039 0:148fca6fd246 157 * @param mptr pointer to the member function to be called
va009039 0:148fca6fd246 158 */
va009039 0:148fca6fd246 159 template<typename T>
va009039 0:148fca6fd246 160 void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
va009039 0:148fca6fd246 161 }
va009039 0:148fca6fd246 162
va009039 0:148fca6fd246 163 // LPC4088-USBHost extensions
va009039 0:148fca6fd246 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:148fca6fd246 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:148fca6fd246 166 int interruptReadNB(USBEndpoint* ep, uint8_t* data, int size);
va009039 0:148fca6fd246 167 int bulkReadNB(USBEndpoint*ep, uint8_t* data, int size);
va009039 0:148fca6fd246 168 /**
va009039 0:148fca6fd246 169 *
va009039 0:148fca6fd246 170 */
va009039 0:148fca6fd246 171 static void poll();
va009039 0:148fca6fd246 172
va009039 0:148fca6fd246 173 private:
va009039 0:148fca6fd246 174 USBHost();
va009039 0:148fca6fd246 175 static USBHost* inst;
va009039 0:148fca6fd246 176 virtual bool addDevice(USBDeviceConnected* parent, int port, bool lowSpeed);
va009039 0:148fca6fd246 177 void root_enumeration(USBDeviceConnected* dev);
va009039 0:148fca6fd246 178 void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator);
va009039 0:148fca6fd246 179 myvector<USBDeviceConnected*>DeviceLists;
va009039 0:148fca6fd246 180
va009039 0:148fca6fd246 181 void task();
va009039 0:148fca6fd246 182 EndpointQueue ep_queue;
va009039 0:148fca6fd246 183
va009039 0:148fca6fd246 184 // USB HUB
va009039 0:148fca6fd246 185 bool Hub(USBDeviceConnected* dev);
va009039 0:148fca6fd246 186 int SetPortPower(USBDeviceConnected* dev, int port);
va009039 0:148fca6fd246 187 int ClearPortPower(USBDeviceConnected* dev, int port);
va009039 0:148fca6fd246 188 int PortReset(USBDeviceConnected* dev, int port);
va009039 0:148fca6fd246 189 int SetPortFeature(USBDeviceConnected* dev, int feature, int index);
va009039 0:148fca6fd246 190 int ClearPortFeature(USBDeviceConnected* dev, int feature, int index);
va009039 0:148fca6fd246 191 int SetPortReset(USBDeviceConnected* dev, int port);
va009039 0:148fca6fd246 192 int GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status);
va009039 0:148fca6fd246 193 };
va009039 0:148fca6fd246 194