only for STM32F769NI-DISCOVERY, porting from mbed OS5 unsupport functions
USBHost/USBHost.h@0:dae1ac0c0a7b, 2019-08-07 (annotated)
- Committer:
- kenjiArai
- Date:
- Wed Aug 07 05:33:53 2019 +0000
- Revision:
- 0:dae1ac0c0a7b
only for STM32F769NI-DISCOVERY, porting from mbed OS5 unsupport functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:dae1ac0c0a7b | 1 | /* mbed USBHost Library |
kenjiArai | 0:dae1ac0c0a7b | 2 | * Copyright (c) 2006-2013 ARM Limited |
kenjiArai | 0:dae1ac0c0a7b | 3 | * |
kenjiArai | 0:dae1ac0c0a7b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 0:dae1ac0c0a7b | 5 | * you may not use this file except in compliance with the License. |
kenjiArai | 0:dae1ac0c0a7b | 6 | * You may obtain a copy of the License at |
kenjiArai | 0:dae1ac0c0a7b | 7 | * |
kenjiArai | 0:dae1ac0c0a7b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:dae1ac0c0a7b | 9 | * |
kenjiArai | 0:dae1ac0c0a7b | 10 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:dae1ac0c0a7b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 0:dae1ac0c0a7b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:dae1ac0c0a7b | 13 | * See the License for the specific language governing permissions and |
kenjiArai | 0:dae1ac0c0a7b | 14 | * limitations under the License. |
kenjiArai | 0:dae1ac0c0a7b | 15 | */ |
kenjiArai | 0:dae1ac0c0a7b | 16 | |
kenjiArai | 0:dae1ac0c0a7b | 17 | #ifndef USBHOST_H |
kenjiArai | 0:dae1ac0c0a7b | 18 | #define USBHOST_H |
kenjiArai | 0:dae1ac0c0a7b | 19 | #ifdef TARGET_STM |
kenjiArai | 0:dae1ac0c0a7b | 20 | #include "mbed.h" |
kenjiArai | 0:dae1ac0c0a7b | 21 | #endif |
kenjiArai | 0:dae1ac0c0a7b | 22 | #include "USBHALHost.h" |
kenjiArai | 0:dae1ac0c0a7b | 23 | #include "USBDeviceConnected.h" |
kenjiArai | 0:dae1ac0c0a7b | 24 | #include "IUSBEnumerator.h" |
kenjiArai | 0:dae1ac0c0a7b | 25 | #include "USBHostConf.h" |
kenjiArai | 0:dae1ac0c0a7b | 26 | #include "rtos.h" |
kenjiArai | 0:dae1ac0c0a7b | 27 | #include "dbg.h" |
kenjiArai | 0:dae1ac0c0a7b | 28 | #include "USBHostHub.h" |
kenjiArai | 0:dae1ac0c0a7b | 29 | |
kenjiArai | 0:dae1ac0c0a7b | 30 | /** |
kenjiArai | 0:dae1ac0c0a7b | 31 | * USBHost class |
kenjiArai | 0:dae1ac0c0a7b | 32 | * This class is a singleton. All drivers have a reference on the static USBHost instance |
kenjiArai | 0:dae1ac0c0a7b | 33 | */ |
kenjiArai | 0:dae1ac0c0a7b | 34 | class USBHost : public USBHALHost { |
kenjiArai | 0:dae1ac0c0a7b | 35 | public: |
kenjiArai | 0:dae1ac0c0a7b | 36 | /** |
kenjiArai | 0:dae1ac0c0a7b | 37 | * Static method to create or retrieve the single USBHost instance |
kenjiArai | 0:dae1ac0c0a7b | 38 | */ |
kenjiArai | 0:dae1ac0c0a7b | 39 | static USBHost * getHostInst(); |
kenjiArai | 0:dae1ac0c0a7b | 40 | |
kenjiArai | 0:dae1ac0c0a7b | 41 | /** |
kenjiArai | 0:dae1ac0c0a7b | 42 | * Control read: setup stage, data stage and status stage |
kenjiArai | 0:dae1ac0c0a7b | 43 | * |
kenjiArai | 0:dae1ac0c0a7b | 44 | * @param dev the control read will be done for this device |
kenjiArai | 0:dae1ac0c0a7b | 45 | * @param requestType request type |
kenjiArai | 0:dae1ac0c0a7b | 46 | * @param request request |
kenjiArai | 0:dae1ac0c0a7b | 47 | * @param value value |
kenjiArai | 0:dae1ac0c0a7b | 48 | * @param index index |
kenjiArai | 0:dae1ac0c0a7b | 49 | * @param buf pointer on a buffer where will be store the data received |
kenjiArai | 0:dae1ac0c0a7b | 50 | * @param len length of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 51 | * |
kenjiArai | 0:dae1ac0c0a7b | 52 | * @returns status of the control read |
kenjiArai | 0:dae1ac0c0a7b | 53 | */ |
kenjiArai | 0:dae1ac0c0a7b | 54 | USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len); |
kenjiArai | 0:dae1ac0c0a7b | 55 | |
kenjiArai | 0:dae1ac0c0a7b | 56 | /** |
kenjiArai | 0:dae1ac0c0a7b | 57 | * Control write: setup stage, data stage and status stage |
kenjiArai | 0:dae1ac0c0a7b | 58 | * |
kenjiArai | 0:dae1ac0c0a7b | 59 | * @param dev the control write will be done for this device |
kenjiArai | 0:dae1ac0c0a7b | 60 | * @param requestType request type |
kenjiArai | 0:dae1ac0c0a7b | 61 | * @param request request |
kenjiArai | 0:dae1ac0c0a7b | 62 | * @param value value |
kenjiArai | 0:dae1ac0c0a7b | 63 | * @param index index |
kenjiArai | 0:dae1ac0c0a7b | 64 | * @param buf pointer on a buffer which will be written |
kenjiArai | 0:dae1ac0c0a7b | 65 | * @param len length of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 66 | * |
kenjiArai | 0:dae1ac0c0a7b | 67 | * @returns status of the control write |
kenjiArai | 0:dae1ac0c0a7b | 68 | */ |
kenjiArai | 0:dae1ac0c0a7b | 69 | USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len); |
kenjiArai | 0:dae1ac0c0a7b | 70 | |
kenjiArai | 0:dae1ac0c0a7b | 71 | /** |
kenjiArai | 0:dae1ac0c0a7b | 72 | * Bulk read |
kenjiArai | 0:dae1ac0c0a7b | 73 | * |
kenjiArai | 0:dae1ac0c0a7b | 74 | * @param dev the bulk transfer will be done for this device |
kenjiArai | 0:dae1ac0c0a7b | 75 | * @param ep USBEndpoint which will be used to read a packet |
kenjiArai | 0:dae1ac0c0a7b | 76 | * @param buf pointer on a buffer where will be store the data received |
kenjiArai | 0:dae1ac0c0a7b | 77 | * @param len length of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 78 | * @param blocking if true, the read is blocking (wait for completion) |
kenjiArai | 0:dae1ac0c0a7b | 79 | * |
kenjiArai | 0:dae1ac0c0a7b | 80 | * @returns status of the bulk read |
kenjiArai | 0:dae1ac0c0a7b | 81 | */ |
kenjiArai | 0:dae1ac0c0a7b | 82 | USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
kenjiArai | 0:dae1ac0c0a7b | 83 | |
kenjiArai | 0:dae1ac0c0a7b | 84 | /** |
kenjiArai | 0:dae1ac0c0a7b | 85 | * Bulk write |
kenjiArai | 0:dae1ac0c0a7b | 86 | * |
kenjiArai | 0:dae1ac0c0a7b | 87 | * @param dev the bulk transfer will be done for this device |
kenjiArai | 0:dae1ac0c0a7b | 88 | * @param ep USBEndpoint which will be used to write a packet |
kenjiArai | 0:dae1ac0c0a7b | 89 | * @param buf pointer on a buffer which will be written |
kenjiArai | 0:dae1ac0c0a7b | 90 | * @param len length of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 91 | * @param blocking if true, the write is blocking (wait for completion) |
kenjiArai | 0:dae1ac0c0a7b | 92 | * |
kenjiArai | 0:dae1ac0c0a7b | 93 | * @returns status of the bulk write |
kenjiArai | 0:dae1ac0c0a7b | 94 | */ |
kenjiArai | 0:dae1ac0c0a7b | 95 | USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
kenjiArai | 0:dae1ac0c0a7b | 96 | |
kenjiArai | 0:dae1ac0c0a7b | 97 | /** |
kenjiArai | 0:dae1ac0c0a7b | 98 | * Interrupt read |
kenjiArai | 0:dae1ac0c0a7b | 99 | * |
kenjiArai | 0:dae1ac0c0a7b | 100 | * @param dev the bulk transfer will be done for this device |
kenjiArai | 0:dae1ac0c0a7b | 101 | * @param ep USBEndpoint which will be used to write a packet |
kenjiArai | 0:dae1ac0c0a7b | 102 | * @param buf pointer on a buffer which will be written |
kenjiArai | 0:dae1ac0c0a7b | 103 | * @param len length of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 104 | * @param blocking if true, the read is blocking (wait for completion) |
kenjiArai | 0:dae1ac0c0a7b | 105 | * |
kenjiArai | 0:dae1ac0c0a7b | 106 | * @returns status of the interrupt read |
kenjiArai | 0:dae1ac0c0a7b | 107 | */ |
kenjiArai | 0:dae1ac0c0a7b | 108 | USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
kenjiArai | 0:dae1ac0c0a7b | 109 | |
kenjiArai | 0:dae1ac0c0a7b | 110 | /** |
kenjiArai | 0:dae1ac0c0a7b | 111 | * Interrupt write |
kenjiArai | 0:dae1ac0c0a7b | 112 | * |
kenjiArai | 0:dae1ac0c0a7b | 113 | * @param dev the bulk transfer will be done for this device |
kenjiArai | 0:dae1ac0c0a7b | 114 | * @param ep USBEndpoint which will be used to write a packet |
kenjiArai | 0:dae1ac0c0a7b | 115 | * @param buf pointer on a buffer which will be written |
kenjiArai | 0:dae1ac0c0a7b | 116 | * @param len length of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 117 | * @param blocking if true, the write is blocking (wait for completion) |
kenjiArai | 0:dae1ac0c0a7b | 118 | * |
kenjiArai | 0:dae1ac0c0a7b | 119 | * @returns status of the interrupt write |
kenjiArai | 0:dae1ac0c0a7b | 120 | */ |
kenjiArai | 0:dae1ac0c0a7b | 121 | USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true); |
kenjiArai | 0:dae1ac0c0a7b | 122 | |
kenjiArai | 0:dae1ac0c0a7b | 123 | /** |
kenjiArai | 0:dae1ac0c0a7b | 124 | * Enumerate a device. |
kenjiArai | 0:dae1ac0c0a7b | 125 | * |
kenjiArai | 0:dae1ac0c0a7b | 126 | * @param dev device which will be enumerated |
kenjiArai | 0:dae1ac0c0a7b | 127 | * |
kenjiArai | 0:dae1ac0c0a7b | 128 | * @returns status of the enumeration |
kenjiArai | 0:dae1ac0c0a7b | 129 | */ |
kenjiArai | 0:dae1ac0c0a7b | 130 | USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator); |
kenjiArai | 0:dae1ac0c0a7b | 131 | |
kenjiArai | 0:dae1ac0c0a7b | 132 | /** |
kenjiArai | 0:dae1ac0c0a7b | 133 | * reset a specific device |
kenjiArai | 0:dae1ac0c0a7b | 134 | * |
kenjiArai | 0:dae1ac0c0a7b | 135 | * @param dev device which will be resetted |
kenjiArai | 0:dae1ac0c0a7b | 136 | */ |
kenjiArai | 0:dae1ac0c0a7b | 137 | USB_TYPE resetDevice(USBDeviceConnected * dev); |
kenjiArai | 0:dae1ac0c0a7b | 138 | |
kenjiArai | 0:dae1ac0c0a7b | 139 | /** |
kenjiArai | 0:dae1ac0c0a7b | 140 | * Get a device |
kenjiArai | 0:dae1ac0c0a7b | 141 | * |
kenjiArai | 0:dae1ac0c0a7b | 142 | * @param index index of the device which will be returned |
kenjiArai | 0:dae1ac0c0a7b | 143 | * |
kenjiArai | 0:dae1ac0c0a7b | 144 | * @returns pointer on the "index" device |
kenjiArai | 0:dae1ac0c0a7b | 145 | */ |
kenjiArai | 0:dae1ac0c0a7b | 146 | USBDeviceConnected * getDevice(uint8_t index); |
kenjiArai | 0:dae1ac0c0a7b | 147 | |
kenjiArai | 0:dae1ac0c0a7b | 148 | /* |
kenjiArai | 0:dae1ac0c0a7b | 149 | * If there is a HID device connected, the host stores the length of the report descriptor. |
kenjiArai | 0:dae1ac0c0a7b | 150 | * This avoid to the driver to re-ask the configuration descriptor to request the report descriptor |
kenjiArai | 0:dae1ac0c0a7b | 151 | * |
kenjiArai | 0:dae1ac0c0a7b | 152 | * @returns length of the report descriptor |
kenjiArai | 0:dae1ac0c0a7b | 153 | */ |
kenjiArai | 0:dae1ac0c0a7b | 154 | inline uint16_t getLengthReportDescr() { |
kenjiArai | 0:dae1ac0c0a7b | 155 | return lenReportDescr; |
kenjiArai | 0:dae1ac0c0a7b | 156 | }; |
kenjiArai | 0:dae1ac0c0a7b | 157 | |
kenjiArai | 0:dae1ac0c0a7b | 158 | /** |
kenjiArai | 0:dae1ac0c0a7b | 159 | * register a driver into the host associated with a callback function called when the device is disconnected |
kenjiArai | 0:dae1ac0c0a7b | 160 | * |
kenjiArai | 0:dae1ac0c0a7b | 161 | * @param dev device |
kenjiArai | 0:dae1ac0c0a7b | 162 | * @param intf interface number |
kenjiArai | 0:dae1ac0c0a7b | 163 | * @param tptr pointer to the object to call the member function on |
kenjiArai | 0:dae1ac0c0a7b | 164 | * @param mptr pointer to the member function to be called |
kenjiArai | 0:dae1ac0c0a7b | 165 | */ |
kenjiArai | 0:dae1ac0c0a7b | 166 | template<typename T> |
kenjiArai | 0:dae1ac0c0a7b | 167 | inline void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) { |
kenjiArai | 0:dae1ac0c0a7b | 168 | int index = findDevice(dev); |
kenjiArai | 0:dae1ac0c0a7b | 169 | if ((index != -1) && (mptr != NULL) && (tptr != NULL)) { |
kenjiArai | 0:dae1ac0c0a7b | 170 | USB_DBG("register driver for dev: %p on intf: %d", dev, intf); |
kenjiArai | 0:dae1ac0c0a7b | 171 | deviceAttachedDriver[index][intf] = true; |
kenjiArai | 0:dae1ac0c0a7b | 172 | dev->onDisconnect(intf, tptr, mptr); |
kenjiArai | 0:dae1ac0c0a7b | 173 | } |
kenjiArai | 0:dae1ac0c0a7b | 174 | } |
kenjiArai | 0:dae1ac0c0a7b | 175 | |
kenjiArai | 0:dae1ac0c0a7b | 176 | /** |
kenjiArai | 0:dae1ac0c0a7b | 177 | * register a driver into the host associated with a callback function called when the device is disconnected |
kenjiArai | 0:dae1ac0c0a7b | 178 | * |
kenjiArai | 0:dae1ac0c0a7b | 179 | * @param dev device |
kenjiArai | 0:dae1ac0c0a7b | 180 | * @param intf interface number |
kenjiArai | 0:dae1ac0c0a7b | 181 | * @param fn callback called when the specified device has been disconnected |
kenjiArai | 0:dae1ac0c0a7b | 182 | */ |
kenjiArai | 0:dae1ac0c0a7b | 183 | inline void registerDriver(USBDeviceConnected * dev, uint8_t intf, void (*fn)(void)) { |
kenjiArai | 0:dae1ac0c0a7b | 184 | int index = findDevice(dev); |
kenjiArai | 0:dae1ac0c0a7b | 185 | if ((index != -1) && (fn != NULL)) { |
kenjiArai | 0:dae1ac0c0a7b | 186 | USB_DBG("register driver for dev: %p on intf: %d", dev, intf); |
kenjiArai | 0:dae1ac0c0a7b | 187 | deviceAttachedDriver[index][intf] = true; |
kenjiArai | 0:dae1ac0c0a7b | 188 | dev->onDisconnect(intf, fn); |
kenjiArai | 0:dae1ac0c0a7b | 189 | } |
kenjiArai | 0:dae1ac0c0a7b | 190 | } |
kenjiArai | 0:dae1ac0c0a7b | 191 | |
kenjiArai | 0:dae1ac0c0a7b | 192 | /** |
kenjiArai | 0:dae1ac0c0a7b | 193 | * Instantiate to protect USB thread from accessing shared objects (USBConnectedDevices and Interfaces) |
kenjiArai | 0:dae1ac0c0a7b | 194 | */ |
kenjiArai | 0:dae1ac0c0a7b | 195 | class Lock |
kenjiArai | 0:dae1ac0c0a7b | 196 | { |
kenjiArai | 0:dae1ac0c0a7b | 197 | public: |
kenjiArai | 0:dae1ac0c0a7b | 198 | Lock(USBHost* pHost); |
kenjiArai | 0:dae1ac0c0a7b | 199 | ~Lock(); |
kenjiArai | 0:dae1ac0c0a7b | 200 | private: |
kenjiArai | 0:dae1ac0c0a7b | 201 | USBHost* m_pHost; |
kenjiArai | 0:dae1ac0c0a7b | 202 | }; |
kenjiArai | 0:dae1ac0c0a7b | 203 | |
kenjiArai | 0:dae1ac0c0a7b | 204 | friend class USBHostHub; |
kenjiArai | 0:dae1ac0c0a7b | 205 | |
kenjiArai | 0:dae1ac0c0a7b | 206 | protected: |
kenjiArai | 0:dae1ac0c0a7b | 207 | |
kenjiArai | 0:dae1ac0c0a7b | 208 | /** |
kenjiArai | 0:dae1ac0c0a7b | 209 | * Virtual method called when a transfer has been completed |
kenjiArai | 0:dae1ac0c0a7b | 210 | * |
kenjiArai | 0:dae1ac0c0a7b | 211 | * @param addr list of the TDs which have been completed |
kenjiArai | 0:dae1ac0c0a7b | 212 | */ |
kenjiArai | 0:dae1ac0c0a7b | 213 | virtual void transferCompleted(volatile uint32_t addr); |
kenjiArai | 0:dae1ac0c0a7b | 214 | |
kenjiArai | 0:dae1ac0c0a7b | 215 | /** |
kenjiArai | 0:dae1ac0c0a7b | 216 | * Virtual method called when a device has been connected |
kenjiArai | 0:dae1ac0c0a7b | 217 | * |
kenjiArai | 0:dae1ac0c0a7b | 218 | * @param hub hub number of the device |
kenjiArai | 0:dae1ac0c0a7b | 219 | * @param port port number of the device |
kenjiArai | 0:dae1ac0c0a7b | 220 | * @param lowSpeed 1 if low speed, 0 otherwise |
kenjiArai | 0:dae1ac0c0a7b | 221 | * @param hub_parent reference on the parent hub |
kenjiArai | 0:dae1ac0c0a7b | 222 | */ |
kenjiArai | 0:dae1ac0c0a7b | 223 | virtual void deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent = NULL); |
kenjiArai | 0:dae1ac0c0a7b | 224 | |
kenjiArai | 0:dae1ac0c0a7b | 225 | /** |
kenjiArai | 0:dae1ac0c0a7b | 226 | * Virtuel method called when a device has been disconnected |
kenjiArai | 0:dae1ac0c0a7b | 227 | * |
kenjiArai | 0:dae1ac0c0a7b | 228 | * @param hub hub number of the device |
kenjiArai | 0:dae1ac0c0a7b | 229 | * @param port port number of the device |
kenjiArai | 0:dae1ac0c0a7b | 230 | * @param addr list of the TDs which have been completed to dequeue freed TDs |
kenjiArai | 0:dae1ac0c0a7b | 231 | */ |
kenjiArai | 0:dae1ac0c0a7b | 232 | virtual void deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr); |
kenjiArai | 0:dae1ac0c0a7b | 233 | |
kenjiArai | 0:dae1ac0c0a7b | 234 | |
kenjiArai | 0:dae1ac0c0a7b | 235 | private: |
kenjiArai | 0:dae1ac0c0a7b | 236 | // singleton class -> constructor is private |
kenjiArai | 0:dae1ac0c0a7b | 237 | USBHost(); |
kenjiArai | 0:dae1ac0c0a7b | 238 | static USBHost * instHost; |
kenjiArai | 0:dae1ac0c0a7b | 239 | uint16_t lenReportDescr; |
kenjiArai | 0:dae1ac0c0a7b | 240 | |
kenjiArai | 0:dae1ac0c0a7b | 241 | // endpoints |
kenjiArai | 0:dae1ac0c0a7b | 242 | void unqueueEndpoint(USBEndpoint * ep) ; |
kenjiArai | 0:dae1ac0c0a7b | 243 | USBEndpoint endpoints[MAX_ENDPOINT]; |
kenjiArai | 0:dae1ac0c0a7b | 244 | USBEndpoint* volatile control; |
kenjiArai | 0:dae1ac0c0a7b | 245 | |
kenjiArai | 0:dae1ac0c0a7b | 246 | USBEndpoint* volatile headControlEndpoint; |
kenjiArai | 0:dae1ac0c0a7b | 247 | USBEndpoint* volatile headBulkEndpoint; |
kenjiArai | 0:dae1ac0c0a7b | 248 | USBEndpoint* volatile headInterruptEndpoint; |
kenjiArai | 0:dae1ac0c0a7b | 249 | |
kenjiArai | 0:dae1ac0c0a7b | 250 | USBEndpoint* volatile tailControlEndpoint; |
kenjiArai | 0:dae1ac0c0a7b | 251 | USBEndpoint* volatile tailBulkEndpoint; |
kenjiArai | 0:dae1ac0c0a7b | 252 | USBEndpoint* volatile tailInterruptEndpoint; |
kenjiArai | 0:dae1ac0c0a7b | 253 | |
kenjiArai | 0:dae1ac0c0a7b | 254 | bool controlEndpointAllocated; |
kenjiArai | 0:dae1ac0c0a7b | 255 | |
kenjiArai | 0:dae1ac0c0a7b | 256 | // devices connected |
kenjiArai | 0:dae1ac0c0a7b | 257 | USBDeviceConnected devices[MAX_DEVICE_CONNECTED]; |
kenjiArai | 0:dae1ac0c0a7b | 258 | bool deviceInUse[MAX_DEVICE_CONNECTED]; |
kenjiArai | 0:dae1ac0c0a7b | 259 | bool deviceAttachedDriver[MAX_DEVICE_CONNECTED][MAX_INTF]; |
kenjiArai | 0:dae1ac0c0a7b | 260 | bool deviceReset[MAX_DEVICE_CONNECTED]; |
kenjiArai | 0:dae1ac0c0a7b | 261 | bool deviceInited[MAX_DEVICE_CONNECTED]; |
kenjiArai | 0:dae1ac0c0a7b | 262 | |
kenjiArai | 0:dae1ac0c0a7b | 263 | #if MAX_HUB_NB |
kenjiArai | 0:dae1ac0c0a7b | 264 | USBHostHub hubs[MAX_HUB_NB]; |
kenjiArai | 0:dae1ac0c0a7b | 265 | bool hub_in_use[MAX_HUB_NB]; |
kenjiArai | 0:dae1ac0c0a7b | 266 | #endif |
kenjiArai | 0:dae1ac0c0a7b | 267 | |
kenjiArai | 0:dae1ac0c0a7b | 268 | // to store a setup packet |
kenjiArai | 0:dae1ac0c0a7b | 269 | uint8_t setupPacket[8]; |
kenjiArai | 0:dae1ac0c0a7b | 270 | |
kenjiArai | 0:dae1ac0c0a7b | 271 | typedef struct { |
kenjiArai | 0:dae1ac0c0a7b | 272 | uint8_t event_id; |
kenjiArai | 0:dae1ac0c0a7b | 273 | void * td_addr; |
kenjiArai | 0:dae1ac0c0a7b | 274 | uint8_t hub; |
kenjiArai | 0:dae1ac0c0a7b | 275 | uint8_t port; |
kenjiArai | 0:dae1ac0c0a7b | 276 | uint8_t lowSpeed; |
kenjiArai | 0:dae1ac0c0a7b | 277 | uint8_t td_state; |
kenjiArai | 0:dae1ac0c0a7b | 278 | void * hub_parent; |
kenjiArai | 0:dae1ac0c0a7b | 279 | } message_t; |
kenjiArai | 0:dae1ac0c0a7b | 280 | |
kenjiArai | 0:dae1ac0c0a7b | 281 | Thread usbThread; |
kenjiArai | 0:dae1ac0c0a7b | 282 | void usb_process(); |
kenjiArai | 0:dae1ac0c0a7b | 283 | Mail<message_t, 10> mail_usb_event; |
kenjiArai | 0:dae1ac0c0a7b | 284 | Mutex usb_mutex; |
kenjiArai | 0:dae1ac0c0a7b | 285 | Mutex td_mutex; |
kenjiArai | 0:dae1ac0c0a7b | 286 | |
kenjiArai | 0:dae1ac0c0a7b | 287 | // buffer for conf descriptor |
kenjiArai | 0:dae1ac0c0a7b | 288 | uint8_t data[415]; |
kenjiArai | 0:dae1ac0c0a7b | 289 | |
kenjiArai | 0:dae1ac0c0a7b | 290 | /** |
kenjiArai | 0:dae1ac0c0a7b | 291 | * Add a transfer on the TD linked list associated to an ED |
kenjiArai | 0:dae1ac0c0a7b | 292 | * |
kenjiArai | 0:dae1ac0c0a7b | 293 | * @param ed the transfer is associated to this ed |
kenjiArai | 0:dae1ac0c0a7b | 294 | * @param buf pointer on a buffer where will be read/write data to send or receive |
kenjiArai | 0:dae1ac0c0a7b | 295 | * @param len transfer length |
kenjiArai | 0:dae1ac0c0a7b | 296 | * |
kenjiArai | 0:dae1ac0c0a7b | 297 | * @return status of the transfer |
kenjiArai | 0:dae1ac0c0a7b | 298 | */ |
kenjiArai | 0:dae1ac0c0a7b | 299 | USB_TYPE addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len) ; |
kenjiArai | 0:dae1ac0c0a7b | 300 | |
kenjiArai | 0:dae1ac0c0a7b | 301 | /** |
kenjiArai | 0:dae1ac0c0a7b | 302 | * Link the USBEndpoint to the linked list and attach an USBEndpoint this USBEndpoint to a device |
kenjiArai | 0:dae1ac0c0a7b | 303 | * |
kenjiArai | 0:dae1ac0c0a7b | 304 | * @param dev pointer on a USBDeviceConnected object |
kenjiArai | 0:dae1ac0c0a7b | 305 | * @param ep pointer on the USBEndpoint which will be added |
kenjiArai | 0:dae1ac0c0a7b | 306 | * |
kenjiArai | 0:dae1ac0c0a7b | 307 | * return true if successful |
kenjiArai | 0:dae1ac0c0a7b | 308 | */ |
kenjiArai | 0:dae1ac0c0a7b | 309 | bool addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep) ; |
kenjiArai | 0:dae1ac0c0a7b | 310 | |
kenjiArai | 0:dae1ac0c0a7b | 311 | /** |
kenjiArai | 0:dae1ac0c0a7b | 312 | * Create an USBEndpoint descriptor. Warning: the USBEndpoint is not linked. |
kenjiArai | 0:dae1ac0c0a7b | 313 | * |
kenjiArai | 0:dae1ac0c0a7b | 314 | * @param type USBEndpoint type (CONTROL_ENDPOINT, BULK_ENDPOINT, INTERRUPT_ENDPOINT) |
kenjiArai | 0:dae1ac0c0a7b | 315 | * @param dir USBEndpoint direction (no meaning for CONTROL_ENDPOINT) |
kenjiArai | 0:dae1ac0c0a7b | 316 | * @param size USBEndpoint max packet size |
kenjiArai | 0:dae1ac0c0a7b | 317 | * @param addr USBEndpoint address |
kenjiArai | 0:dae1ac0c0a7b | 318 | * |
kenjiArai | 0:dae1ac0c0a7b | 319 | * @returns pointer on the USBEndpoint created |
kenjiArai | 0:dae1ac0c0a7b | 320 | */ |
kenjiArai | 0:dae1ac0c0a7b | 321 | USBEndpoint * newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr) ; |
kenjiArai | 0:dae1ac0c0a7b | 322 | |
kenjiArai | 0:dae1ac0c0a7b | 323 | /** |
kenjiArai | 0:dae1ac0c0a7b | 324 | * Request the device descriptor |
kenjiArai | 0:dae1ac0c0a7b | 325 | * |
kenjiArai | 0:dae1ac0c0a7b | 326 | * @param dev request the device descriptor on this device |
kenjiArai | 0:dae1ac0c0a7b | 327 | * @param buf buffer to store the device descriptor |
kenjiArai | 0:dae1ac0c0a7b | 328 | * @param max_len_buf maximum size of buf |
kenjiArai | 0:dae1ac0c0a7b | 329 | * @param len_dev_descr pointer to store the length of the packet transferred |
kenjiArai | 0:dae1ac0c0a7b | 330 | */ |
kenjiArai | 0:dae1ac0c0a7b | 331 | USB_TYPE getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr = NULL); |
kenjiArai | 0:dae1ac0c0a7b | 332 | |
kenjiArai | 0:dae1ac0c0a7b | 333 | /** |
kenjiArai | 0:dae1ac0c0a7b | 334 | * Request the configuration descriptor |
kenjiArai | 0:dae1ac0c0a7b | 335 | * |
kenjiArai | 0:dae1ac0c0a7b | 336 | * @param dev request the configuration descriptor on this device |
kenjiArai | 0:dae1ac0c0a7b | 337 | * @param buf buffer to store the configuration descriptor |
kenjiArai | 0:dae1ac0c0a7b | 338 | * @param max_len_buf maximum size of buf |
kenjiArai | 0:dae1ac0c0a7b | 339 | * @param len_conf_descr pointer to store the length of the packet transferred |
kenjiArai | 0:dae1ac0c0a7b | 340 | */ |
kenjiArai | 0:dae1ac0c0a7b | 341 | USB_TYPE getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_conf_descr = NULL); |
kenjiArai | 0:dae1ac0c0a7b | 342 | |
kenjiArai | 0:dae1ac0c0a7b | 343 | /** |
kenjiArai | 0:dae1ac0c0a7b | 344 | * Set the address of a specific device |
kenjiArai | 0:dae1ac0c0a7b | 345 | * |
kenjiArai | 0:dae1ac0c0a7b | 346 | * @param dev device to set the address |
kenjiArai | 0:dae1ac0c0a7b | 347 | * @param address address |
kenjiArai | 0:dae1ac0c0a7b | 348 | */ |
kenjiArai | 0:dae1ac0c0a7b | 349 | USB_TYPE setAddress(USBDeviceConnected * dev, uint8_t address); |
kenjiArai | 0:dae1ac0c0a7b | 350 | |
kenjiArai | 0:dae1ac0c0a7b | 351 | /** |
kenjiArai | 0:dae1ac0c0a7b | 352 | * Set the configuration of a device |
kenjiArai | 0:dae1ac0c0a7b | 353 | * |
kenjiArai | 0:dae1ac0c0a7b | 354 | * @param dev device on which the specified configuration will be activated |
kenjiArai | 0:dae1ac0c0a7b | 355 | * @param conf configuration number to activate (usually 1) |
kenjiArai | 0:dae1ac0c0a7b | 356 | */ |
kenjiArai | 0:dae1ac0c0a7b | 357 | USB_TYPE setConfiguration(USBDeviceConnected * dev, uint8_t conf); |
kenjiArai | 0:dae1ac0c0a7b | 358 | |
kenjiArai | 0:dae1ac0c0a7b | 359 | /** |
kenjiArai | 0:dae1ac0c0a7b | 360 | * Free a specific device |
kenjiArai | 0:dae1ac0c0a7b | 361 | * |
kenjiArai | 0:dae1ac0c0a7b | 362 | * @param dev device to be freed |
kenjiArai | 0:dae1ac0c0a7b | 363 | */ |
kenjiArai | 0:dae1ac0c0a7b | 364 | void freeDevice(USBDeviceConnected * dev); |
kenjiArai | 0:dae1ac0c0a7b | 365 | |
kenjiArai | 0:dae1ac0c0a7b | 366 | USB_TYPE controlTransfer( USBDeviceConnected * dev, |
kenjiArai | 0:dae1ac0c0a7b | 367 | uint8_t requestType, |
kenjiArai | 0:dae1ac0c0a7b | 368 | uint8_t request, |
kenjiArai | 0:dae1ac0c0a7b | 369 | uint32_t value, |
kenjiArai | 0:dae1ac0c0a7b | 370 | uint32_t index, |
kenjiArai | 0:dae1ac0c0a7b | 371 | uint8_t * buf, |
kenjiArai | 0:dae1ac0c0a7b | 372 | uint32_t len, |
kenjiArai | 0:dae1ac0c0a7b | 373 | bool write); |
kenjiArai | 0:dae1ac0c0a7b | 374 | |
kenjiArai | 0:dae1ac0c0a7b | 375 | USB_TYPE generalTransfer( USBDeviceConnected * dev, |
kenjiArai | 0:dae1ac0c0a7b | 376 | USBEndpoint * ep, |
kenjiArai | 0:dae1ac0c0a7b | 377 | uint8_t * buf, |
kenjiArai | 0:dae1ac0c0a7b | 378 | uint32_t len, |
kenjiArai | 0:dae1ac0c0a7b | 379 | bool blocking, |
kenjiArai | 0:dae1ac0c0a7b | 380 | ENDPOINT_TYPE type, |
kenjiArai | 0:dae1ac0c0a7b | 381 | bool write) ; |
kenjiArai | 0:dae1ac0c0a7b | 382 | |
kenjiArai | 0:dae1ac0c0a7b | 383 | void fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len) ; |
kenjiArai | 0:dae1ac0c0a7b | 384 | void parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) ; |
kenjiArai | 0:dae1ac0c0a7b | 385 | int findDevice(USBDeviceConnected * dev) ; |
kenjiArai | 0:dae1ac0c0a7b | 386 | int findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent = NULL) ; |
kenjiArai | 0:dae1ac0c0a7b | 387 | uint8_t numberDriverAttached(USBDeviceConnected * dev); |
kenjiArai | 0:dae1ac0c0a7b | 388 | |
kenjiArai | 0:dae1ac0c0a7b | 389 | ///////////////////////// |
kenjiArai | 0:dae1ac0c0a7b | 390 | /// FOR DEBUG |
kenjiArai | 0:dae1ac0c0a7b | 391 | ///////////////////////// |
kenjiArai | 0:dae1ac0c0a7b | 392 | void printList(ENDPOINT_TYPE type); |
kenjiArai | 0:dae1ac0c0a7b | 393 | |
kenjiArai | 0:dae1ac0c0a7b | 394 | }; |
kenjiArai | 0:dae1ac0c0a7b | 395 | |
kenjiArai | 0:dae1ac0c0a7b | 396 | #endif |