Lcd companion boards support (VKLCD50RTA & VKLCD70RT)

What is this ?

This is a demo program using Renesas RGA library & USB Camera to demonstrate VK-RZ/A1H's companion boards workability.


Supported companion Boards:

VKLCD50RTA

/media/uploads/tvendov/front_view_hmi_50.png /media/uploads/tvendov/side_view_hmi_50.png

VKLCD70RT

/media/uploads/tvendov/front_view_hmi_70.png/media/uploads/tvendov/side_view_hmi_70.png /media/uploads/tvendov/front_view_lvds.png/media/uploads/tvendov/back_view_lvds.png


How to Configure ?

You can choose which display is installed by altering the lcd_panel.h file

Leave the active one & comment out the others:

#define     LCD_VDC5_CH0_PANEL                  LCD_CH0_PANEL_VKLCD50RTA
//#define     LCD_VDC5_CH0_PANEL                  LCD_CH0_PANEL_VKLCD70RT

You can alter the whole demo with your pictures if you like:


How to compile ?

  • The Demo can be compiled in 3 modes:
    • I. Execution from the internal 10-MB on-chip SRAM.
      • After import in the online compiler just leave only the VKRZA1H_RAM.sct & delete all others linker files in the TOOLCHAIN_ARM_STD folder.
      • Save the result binary in the SD Card (<SD>:\vkrza1\lcd_sample ), altering vkrza1h.ini by this way
    • II. Execution from the on-board serial FALSH in dual (32-MB) mode.
      • After import in the online compiler just leave only the VKRZA1H_DOUBLE.sct & delete all others linker files in the TOOLCHAIN_ARM_STD folder.
      • Drag & drop the result binary in MBED disk, (previously inited in double flash mode)
    • III. Execution from the on-board serial FALSH in single (16-MB) mode.
      • After import in the online compiler just leave only the VKRZA1H_SINGLE.sct & delete all others linker files in the TOOLCHAIN_ARM_STD folder.
      • Drag & drop the result binary in MBED disk, (previously inited in single flash mode )

Quick presentation:


Other demos ?

More demos you can find on our FTP

Committer:
tvendov
Date:
Thu Feb 16 10:23:48 2017 +0000
Revision:
0:6435b67ad23c
Initial lcd support (VKLCD50RTA & VKLCD70RT companion boards)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tvendov 0:6435b67ad23c 1 /* mbed USBHost Library
tvendov 0:6435b67ad23c 2 * Copyright (c) 2006-2013 ARM Limited
tvendov 0:6435b67ad23c 3 *
tvendov 0:6435b67ad23c 4 * Licensed under the Apache License, Version 2.0 (the "License");
tvendov 0:6435b67ad23c 5 * you may not use this file except in compliance with the License.
tvendov 0:6435b67ad23c 6 * You may obtain a copy of the License at
tvendov 0:6435b67ad23c 7 *
tvendov 0:6435b67ad23c 8 * http://www.apache.org/licenses/LICENSE-2.0
tvendov 0:6435b67ad23c 9 *
tvendov 0:6435b67ad23c 10 * Unless required by applicable law or agreed to in writing, software
tvendov 0:6435b67ad23c 11 * distributed under the License is distributed on an "AS IS" BASIS,
tvendov 0:6435b67ad23c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tvendov 0:6435b67ad23c 13 * See the License for the specific language governing permissions and
tvendov 0:6435b67ad23c 14 * limitations under the License.
tvendov 0:6435b67ad23c 15 */
tvendov 0:6435b67ad23c 16
tvendov 0:6435b67ad23c 17
tvendov 0:6435b67ad23c 18 #include "USBHost.h"
tvendov 0:6435b67ad23c 19 #include "USBHostHub.h"
tvendov 0:6435b67ad23c 20 #if(1) /* Isochronous */
tvendov 0:6435b67ad23c 21 #include "USBIsochronous.h"
tvendov 0:6435b67ad23c 22 #endif
tvendov 0:6435b67ad23c 23
tvendov 0:6435b67ad23c 24 USBHost * USBHost::instHost = NULL;
tvendov 0:6435b67ad23c 25
tvendov 0:6435b67ad23c 26 #define DEVICE_CONNECTED_EVENT (1 << 0)
tvendov 0:6435b67ad23c 27 #define DEVICE_DISCONNECTED_EVENT (1 << 1)
tvendov 0:6435b67ad23c 28 #define TD_PROCESSED_EVENT (1 << 2)
tvendov 0:6435b67ad23c 29
tvendov 0:6435b67ad23c 30 #define MAX_TRY_ENUMERATE_HUB 3
tvendov 0:6435b67ad23c 31
tvendov 0:6435b67ad23c 32 #define MIN(a, b) ((a > b) ? b : a)
tvendov 0:6435b67ad23c 33
tvendov 0:6435b67ad23c 34 /**
tvendov 0:6435b67ad23c 35 * How interrupts are processed:
tvendov 0:6435b67ad23c 36 * - new device connected:
tvendov 0:6435b67ad23c 37 * - a message is queued in queue_usb_event with the id DEVICE_CONNECTED_EVENT
tvendov 0:6435b67ad23c 38 * - when the usb_thread receives the event, it:
tvendov 0:6435b67ad23c 39 * - resets the device
tvendov 0:6435b67ad23c 40 * - reads the device descriptor
tvendov 0:6435b67ad23c 41 * - sets the address of the device
tvendov 0:6435b67ad23c 42 * - if it is a hub, enumerates it
tvendov 0:6435b67ad23c 43 * - device disconnected:
tvendov 0:6435b67ad23c 44 * - a message is queued in queue_usb_event with the id DEVICE_DISCONNECTED_EVENT
tvendov 0:6435b67ad23c 45 * - when the usb_thread receives the event, it:
tvendov 0:6435b67ad23c 46 * - free the device and all its children (hub)
tvendov 0:6435b67ad23c 47 * - td processed
tvendov 0:6435b67ad23c 48 * - a message is queued in queue_usb_event with the id TD_PROCESSED_EVENT
tvendov 0:6435b67ad23c 49 * - when the usb_thread receives the event, it:
tvendov 0:6435b67ad23c 50 * - call the callback attached to the endpoint where the td is attached
tvendov 0:6435b67ad23c 51 */
tvendov 0:6435b67ad23c 52 void USBHost::usb_process() {
tvendov 0:6435b67ad23c 53
tvendov 0:6435b67ad23c 54 bool controlListState;
tvendov 0:6435b67ad23c 55 bool bulkListState;
tvendov 0:6435b67ad23c 56 bool interruptListState;
tvendov 0:6435b67ad23c 57 USBEndpoint * ep;
tvendov 0:6435b67ad23c 58 uint8_t i, j, res, timeout_set_addr = 10;
tvendov 0:6435b67ad23c 59 uint8_t buf[8];
tvendov 0:6435b67ad23c 60 bool too_many_hub;
tvendov 0:6435b67ad23c 61 int idx;
tvendov 0:6435b67ad23c 62
tvendov 0:6435b67ad23c 63 #if DEBUG_TRANSFER
tvendov 0:6435b67ad23c 64 uint8_t * buf_transfer;
tvendov 0:6435b67ad23c 65 #endif
tvendov 0:6435b67ad23c 66
tvendov 0:6435b67ad23c 67 #if MAX_HUB_NB
tvendov 0:6435b67ad23c 68 uint8_t k;
tvendov 0:6435b67ad23c 69 #endif
tvendov 0:6435b67ad23c 70
tvendov 0:6435b67ad23c 71 while(1) {
tvendov 0:6435b67ad23c 72 osEvent evt = mail_usb_event.get();
tvendov 0:6435b67ad23c 73
tvendov 0:6435b67ad23c 74 if (evt.status == osEventMail) {
tvendov 0:6435b67ad23c 75
tvendov 0:6435b67ad23c 76 message_t * usb_msg = (message_t*)evt.value.p;
tvendov 0:6435b67ad23c 77
tvendov 0:6435b67ad23c 78 switch (usb_msg->event_id) {
tvendov 0:6435b67ad23c 79
tvendov 0:6435b67ad23c 80 // a new device has been connected
tvendov 0:6435b67ad23c 81 case DEVICE_CONNECTED_EVENT:
tvendov 0:6435b67ad23c 82 too_many_hub = false;
tvendov 0:6435b67ad23c 83 buf[4] = 0;
tvendov 0:6435b67ad23c 84
tvendov 0:6435b67ad23c 85 do
tvendov 0:6435b67ad23c 86 {
tvendov 0:6435b67ad23c 87 Lock lock(this);
tvendov 0:6435b67ad23c 88
tvendov 0:6435b67ad23c 89 for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
tvendov 0:6435b67ad23c 90 if (!deviceInUse[i]) {
tvendov 0:6435b67ad23c 91 USB_DBG_EVENT("new device connected: %p\r\n", &devices[i]);
tvendov 0:6435b67ad23c 92 devices[i].init(usb_msg->hub, usb_msg->port, usb_msg->lowSpeed);
tvendov 0:6435b67ad23c 93 deviceReset[i] = false;
tvendov 0:6435b67ad23c 94 deviceInited[i] = true;
tvendov 0:6435b67ad23c 95 break;
tvendov 0:6435b67ad23c 96 }
tvendov 0:6435b67ad23c 97 }
tvendov 0:6435b67ad23c 98
tvendov 0:6435b67ad23c 99 if (i == MAX_DEVICE_CONNECTED) {
tvendov 0:6435b67ad23c 100 USB_ERR("Too many device connected!!\r\n");
tvendov 0:6435b67ad23c 101 continue;
tvendov 0:6435b67ad23c 102 }
tvendov 0:6435b67ad23c 103
tvendov 0:6435b67ad23c 104 if (!controlEndpointAllocated) {
tvendov 0:6435b67ad23c 105 control = newEndpoint(CONTROL_ENDPOINT, OUT, 0x08, 0x00);
tvendov 0:6435b67ad23c 106 addEndpoint(NULL, 0, (USBEndpoint*)control);
tvendov 0:6435b67ad23c 107 controlEndpointAllocated = true;
tvendov 0:6435b67ad23c 108 }
tvendov 0:6435b67ad23c 109
tvendov 0:6435b67ad23c 110 #if MAX_HUB_NB
tvendov 0:6435b67ad23c 111 if (usb_msg->hub_parent)
tvendov 0:6435b67ad23c 112 devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent));
tvendov 0:6435b67ad23c 113 #endif
tvendov 0:6435b67ad23c 114
tvendov 0:6435b67ad23c 115 for (j = 0; j < timeout_set_addr; j++) {
tvendov 0:6435b67ad23c 116
tvendov 0:6435b67ad23c 117 resetDevice(&devices[i]);
tvendov 0:6435b67ad23c 118
tvendov 0:6435b67ad23c 119 // set size of control endpoint
tvendov 0:6435b67ad23c 120 devices[i].setSizeControlEndpoint(8);
tvendov 0:6435b67ad23c 121
tvendov 0:6435b67ad23c 122 devices[i].activeAddress(false);
tvendov 0:6435b67ad23c 123
tvendov 0:6435b67ad23c 124 // get first 8 bit of device descriptor
tvendov 0:6435b67ad23c 125 // and check if we deal with a hub
tvendov 0:6435b67ad23c 126 USB_DBG("usb_thread read device descriptor on dev: %p\r\n", &devices[i]);
tvendov 0:6435b67ad23c 127 res = getDeviceDescriptor(&devices[i], buf, 8);
tvendov 0:6435b67ad23c 128
tvendov 0:6435b67ad23c 129 if (res != USB_TYPE_OK) {
tvendov 0:6435b67ad23c 130 USB_ERR("usb_thread could not read dev descr");
tvendov 0:6435b67ad23c 131 continue;
tvendov 0:6435b67ad23c 132 }
tvendov 0:6435b67ad23c 133
tvendov 0:6435b67ad23c 134 // set size of control endpoint
tvendov 0:6435b67ad23c 135 devices[i].setSizeControlEndpoint(buf[7]);
tvendov 0:6435b67ad23c 136
tvendov 0:6435b67ad23c 137 // second step: set an address to the device
tvendov 0:6435b67ad23c 138 res = setAddress(&devices[i], devices[i].getAddress());
tvendov 0:6435b67ad23c 139
tvendov 0:6435b67ad23c 140 if (res != USB_TYPE_OK) {
tvendov 0:6435b67ad23c 141 USB_ERR("SET ADDR FAILED");
tvendov 0:6435b67ad23c 142 continue;
tvendov 0:6435b67ad23c 143 }
tvendov 0:6435b67ad23c 144 devices[i].activeAddress(true);
tvendov 0:6435b67ad23c 145 USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress());
tvendov 0:6435b67ad23c 146
tvendov 0:6435b67ad23c 147 // try to read again the device descriptor to check if the device
tvendov 0:6435b67ad23c 148 // answers to its new address
tvendov 0:6435b67ad23c 149 res = getDeviceDescriptor(&devices[i], buf, 8);
tvendov 0:6435b67ad23c 150
tvendov 0:6435b67ad23c 151 if (res == USB_TYPE_OK) {
tvendov 0:6435b67ad23c 152 break;
tvendov 0:6435b67ad23c 153 }
tvendov 0:6435b67ad23c 154
tvendov 0:6435b67ad23c 155 Thread::wait(100);
tvendov 0:6435b67ad23c 156 }
tvendov 0:6435b67ad23c 157
tvendov 0:6435b67ad23c 158 USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port);
tvendov 0:6435b67ad23c 159
tvendov 0:6435b67ad23c 160 #if MAX_HUB_NB
tvendov 0:6435b67ad23c 161 if (buf[4] == HUB_CLASS) {
tvendov 0:6435b67ad23c 162 for (k = 0; k < MAX_HUB_NB; k++) {
tvendov 0:6435b67ad23c 163 if (hub_in_use[k] == false) {
tvendov 0:6435b67ad23c 164 for (uint8_t j = 0; j < MAX_TRY_ENUMERATE_HUB; j++) {
tvendov 0:6435b67ad23c 165 if (hubs[k].connect(&devices[i])) {
tvendov 0:6435b67ad23c 166 devices[i].hub = &hubs[k];
tvendov 0:6435b67ad23c 167 hub_in_use[k] = true;
tvendov 0:6435b67ad23c 168 break;
tvendov 0:6435b67ad23c 169 }
tvendov 0:6435b67ad23c 170 }
tvendov 0:6435b67ad23c 171 if (hub_in_use[k] == true)
tvendov 0:6435b67ad23c 172 break;
tvendov 0:6435b67ad23c 173 }
tvendov 0:6435b67ad23c 174 }
tvendov 0:6435b67ad23c 175
tvendov 0:6435b67ad23c 176 if (k == MAX_HUB_NB) {
tvendov 0:6435b67ad23c 177 USB_ERR("Too many hubs connected!!\r\n");
tvendov 0:6435b67ad23c 178 too_many_hub = true;
tvendov 0:6435b67ad23c 179 }
tvendov 0:6435b67ad23c 180 }
tvendov 0:6435b67ad23c 181
tvendov 0:6435b67ad23c 182 if (usb_msg->hub_parent)
tvendov 0:6435b67ad23c 183 ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]);
tvendov 0:6435b67ad23c 184 #endif
tvendov 0:6435b67ad23c 185
tvendov 0:6435b67ad23c 186 if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) {
tvendov 0:6435b67ad23c 187 deviceInUse[i] = true;
tvendov 0:6435b67ad23c 188 }
tvendov 0:6435b67ad23c 189
tvendov 0:6435b67ad23c 190 } while(0);
tvendov 0:6435b67ad23c 191
tvendov 0:6435b67ad23c 192 break;
tvendov 0:6435b67ad23c 193
tvendov 0:6435b67ad23c 194 // a device has been disconnected
tvendov 0:6435b67ad23c 195 case DEVICE_DISCONNECTED_EVENT:
tvendov 0:6435b67ad23c 196
tvendov 0:6435b67ad23c 197 do
tvendov 0:6435b67ad23c 198 {
tvendov 0:6435b67ad23c 199 Lock lock(this);
tvendov 0:6435b67ad23c 200
tvendov 0:6435b67ad23c 201 controlListState = disableList(CONTROL_ENDPOINT);
tvendov 0:6435b67ad23c 202 bulkListState = disableList(BULK_ENDPOINT);
tvendov 0:6435b67ad23c 203 interruptListState = disableList(INTERRUPT_ENDPOINT);
tvendov 0:6435b67ad23c 204
tvendov 0:6435b67ad23c 205 idx = findDevice(usb_msg->hub, usb_msg->port, (USBHostHub *)(usb_msg->hub_parent));
tvendov 0:6435b67ad23c 206 if (idx != -1) {
tvendov 0:6435b67ad23c 207 freeDevice((USBDeviceConnected*)&devices[idx]);
tvendov 0:6435b67ad23c 208 }
tvendov 0:6435b67ad23c 209
tvendov 0:6435b67ad23c 210 if (controlListState) enableList(CONTROL_ENDPOINT);
tvendov 0:6435b67ad23c 211 if (bulkListState) enableList(BULK_ENDPOINT);
tvendov 0:6435b67ad23c 212 if (interruptListState) enableList(INTERRUPT_ENDPOINT);
tvendov 0:6435b67ad23c 213
tvendov 0:6435b67ad23c 214 } while(0);
tvendov 0:6435b67ad23c 215
tvendov 0:6435b67ad23c 216 break;
tvendov 0:6435b67ad23c 217
tvendov 0:6435b67ad23c 218 // a td has been processed
tvendov 0:6435b67ad23c 219 // call callback on the ed associated to the td
tvendov 0:6435b67ad23c 220 // we are not in ISR -> users can use printf in their callback method
tvendov 0:6435b67ad23c 221 case TD_PROCESSED_EVENT:
tvendov 0:6435b67ad23c 222 ep = (USBEndpoint *) ((HCTD *)usb_msg->td_addr)->ep;
tvendov 0:6435b67ad23c 223 if (usb_msg->td_state == USB_TYPE_IDLE) {
tvendov 0:6435b67ad23c 224 USB_DBG_EVENT("call callback on td %p [ep: %p state: %s - dev: %p - %s]", usb_msg->td_addr, ep, ep->getStateString(), ep->dev, ep->dev->getName(ep->getIntfNb()));
tvendov 0:6435b67ad23c 225
tvendov 0:6435b67ad23c 226 #if DEBUG_TRANSFER
tvendov 0:6435b67ad23c 227 if (ep->getDir() == IN) {
tvendov 0:6435b67ad23c 228 buf_transfer = ep->getBufStart();
tvendov 0:6435b67ad23c 229 printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress());
tvendov 0:6435b67ad23c 230 for (int i = 0; i < ep->getLengthTransferred(); i++)
tvendov 0:6435b67ad23c 231 printf("%02X ", buf_transfer[i]);
tvendov 0:6435b67ad23c 232 printf("\r\n\r\n");
tvendov 0:6435b67ad23c 233 }
tvendov 0:6435b67ad23c 234 #endif
tvendov 0:6435b67ad23c 235 ep->call();
tvendov 0:6435b67ad23c 236 } else {
tvendov 0:6435b67ad23c 237 idx = findDevice(ep->dev);
tvendov 0:6435b67ad23c 238 if (idx != -1) {
tvendov 0:6435b67ad23c 239 if (deviceInUse[idx]) {
tvendov 0:6435b67ad23c 240 USB_WARN("td %p processed but not in idle state: %s [ep: %p - dev: %p - %s]", usb_msg->td_addr, ep->getStateString(), ep, ep->dev, ep->dev->getName(ep->getIntfNb()));
tvendov 0:6435b67ad23c 241 ep->setState(USB_TYPE_IDLE);
tvendov 0:6435b67ad23c 242 }
tvendov 0:6435b67ad23c 243 }
tvendov 0:6435b67ad23c 244 }
tvendov 0:6435b67ad23c 245 break;
tvendov 0:6435b67ad23c 246 }
tvendov 0:6435b67ad23c 247
tvendov 0:6435b67ad23c 248 mail_usb_event.free(usb_msg);
tvendov 0:6435b67ad23c 249 }
tvendov 0:6435b67ad23c 250 }
tvendov 0:6435b67ad23c 251 }
tvendov 0:6435b67ad23c 252
tvendov 0:6435b67ad23c 253 /* static */void USBHost::usb_process_static(void const * arg) {
tvendov 0:6435b67ad23c 254 ((USBHost *)arg)->usb_process();
tvendov 0:6435b67ad23c 255 }
tvendov 0:6435b67ad23c 256
tvendov 0:6435b67ad23c 257 USBHost::USBHost() : usbThread(USBHost::usb_process_static, (void *)this, osPriorityNormal, USB_THREAD_STACK)
tvendov 0:6435b67ad23c 258 {
tvendov 0:6435b67ad23c 259 headControlEndpoint = NULL;
tvendov 0:6435b67ad23c 260 headBulkEndpoint = NULL;
tvendov 0:6435b67ad23c 261 headInterruptEndpoint = NULL;
tvendov 0:6435b67ad23c 262 tailControlEndpoint = NULL;
tvendov 0:6435b67ad23c 263 tailBulkEndpoint = NULL;
tvendov 0:6435b67ad23c 264 tailInterruptEndpoint = NULL;
tvendov 0:6435b67ad23c 265
tvendov 0:6435b67ad23c 266 lenReportDescr = 0;
tvendov 0:6435b67ad23c 267
tvendov 0:6435b67ad23c 268 controlEndpointAllocated = false;
tvendov 0:6435b67ad23c 269
tvendov 0:6435b67ad23c 270 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
tvendov 0:6435b67ad23c 271 deviceInUse[i] = false;
tvendov 0:6435b67ad23c 272 devices[i].setAddress(i + 1);
tvendov 0:6435b67ad23c 273 deviceReset[i] = false;
tvendov 0:6435b67ad23c 274 deviceInited[i] = false;
tvendov 0:6435b67ad23c 275 for (uint8_t j = 0; j < MAX_INTF; j++)
tvendov 0:6435b67ad23c 276 deviceAttachedDriver[i][j] = false;
tvendov 0:6435b67ad23c 277 }
tvendov 0:6435b67ad23c 278
tvendov 0:6435b67ad23c 279 #if MAX_HUB_NB
tvendov 0:6435b67ad23c 280 for (uint8_t i = 0; i < MAX_HUB_NB; i++) {
tvendov 0:6435b67ad23c 281 hubs[i].setHost(this);
tvendov 0:6435b67ad23c 282 hub_in_use[i] = false;
tvendov 0:6435b67ad23c 283 }
tvendov 0:6435b67ad23c 284 #endif
tvendov 0:6435b67ad23c 285 plug_status = false;
tvendov 0:6435b67ad23c 286 }
tvendov 0:6435b67ad23c 287
tvendov 0:6435b67ad23c 288 USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost)
tvendov 0:6435b67ad23c 289 {
tvendov 0:6435b67ad23c 290 m_pHost->usb_mutex.lock();
tvendov 0:6435b67ad23c 291 }
tvendov 0:6435b67ad23c 292
tvendov 0:6435b67ad23c 293 USBHost::Lock::~Lock()
tvendov 0:6435b67ad23c 294 {
tvendov 0:6435b67ad23c 295 m_pHost->usb_mutex.unlock();
tvendov 0:6435b67ad23c 296 }
tvendov 0:6435b67ad23c 297
tvendov 0:6435b67ad23c 298 void USBHost::transferCompleted(volatile uint32_t addr)
tvendov 0:6435b67ad23c 299 {
tvendov 0:6435b67ad23c 300 uint8_t state;
tvendov 0:6435b67ad23c 301
tvendov 0:6435b67ad23c 302 if(addr == 0)
tvendov 0:6435b67ad23c 303 return;
tvendov 0:6435b67ad23c 304
tvendov 0:6435b67ad23c 305 volatile HCTD* tdList = NULL;
tvendov 0:6435b67ad23c 306
tvendov 0:6435b67ad23c 307 //First we must reverse the list order and dequeue each TD
tvendov 0:6435b67ad23c 308 do {
tvendov 0:6435b67ad23c 309 volatile HCTD* td = (volatile HCTD*)addr;
tvendov 0:6435b67ad23c 310 addr = (uint32_t)td->nextTD; //Dequeue from physical list
tvendov 0:6435b67ad23c 311 td->nextTD = (hcTd*)tdList; //Enqueue into reversed list
tvendov 0:6435b67ad23c 312 tdList = td;
tvendov 0:6435b67ad23c 313 } while(addr);
tvendov 0:6435b67ad23c 314
tvendov 0:6435b67ad23c 315 while(tdList != NULL) {
tvendov 0:6435b67ad23c 316 volatile HCTD* td = tdList;
tvendov 0:6435b67ad23c 317 tdList = (volatile HCTD*)td->nextTD; //Dequeue element now as it could be modified below
tvendov 0:6435b67ad23c 318 #if(1) /* Isochronous */
tvendov 0:6435b67ad23c 319 if (!isTD((uint8_t*)td)) { // ITD?
tvendov 0:6435b67ad23c 320 HCITD* itd = (HCITD*)td;
tvendov 0:6435b67ad23c 321 IsochronousEp* ep = itd->ep;
tvendov 0:6435b67ad23c 322 if (ep) {
tvendov 0:6435b67ad23c 323 ep->irqWdhHandler(itd);
tvendov 0:6435b67ad23c 324 }
tvendov 0:6435b67ad23c 325 continue;
tvendov 0:6435b67ad23c 326 }
tvendov 0:6435b67ad23c 327 #endif
tvendov 0:6435b67ad23c 328 if (td->ep != NULL) {
tvendov 0:6435b67ad23c 329 USBEndpoint * ep = (USBEndpoint *)(td->ep);
tvendov 0:6435b67ad23c 330
tvendov 0:6435b67ad23c 331 if (((HCTD *)td)->control >> 28) {
tvendov 0:6435b67ad23c 332 state = ((HCTD *)td)->control >> 28;
tvendov 0:6435b67ad23c 333 } else {
tvendov 0:6435b67ad23c 334 if (td->currBufPtr)
tvendov 0:6435b67ad23c 335 ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart());
tvendov 0:6435b67ad23c 336 state = 16 /*USB_TYPE_IDLE*/;
tvendov 0:6435b67ad23c 337 }
tvendov 0:6435b67ad23c 338
tvendov 0:6435b67ad23c 339 ep->unqueueTransfer(td);
tvendov 0:6435b67ad23c 340
tvendov 0:6435b67ad23c 341 if (ep->getType() != CONTROL_ENDPOINT) {
tvendov 0:6435b67ad23c 342 // callback on the processed td will be called from the usb_thread (not in ISR)
tvendov 0:6435b67ad23c 343 message_t * usb_msg = mail_usb_event.alloc();
tvendov 0:6435b67ad23c 344 usb_msg->event_id = TD_PROCESSED_EVENT;
tvendov 0:6435b67ad23c 345 usb_msg->td_addr = (void *)td;
tvendov 0:6435b67ad23c 346 usb_msg->td_state = state;
tvendov 0:6435b67ad23c 347 mail_usb_event.put(usb_msg);
tvendov 0:6435b67ad23c 348 }
tvendov 0:6435b67ad23c 349 ep->setState(state);
tvendov 0:6435b67ad23c 350 ep->ep_queue.put((uint8_t*)1);
tvendov 0:6435b67ad23c 351 }
tvendov 0:6435b67ad23c 352 }
tvendov 0:6435b67ad23c 353 }
tvendov 0:6435b67ad23c 354
tvendov 0:6435b67ad23c 355 USBHost * USBHost::getHostInst()
tvendov 0:6435b67ad23c 356 {
tvendov 0:6435b67ad23c 357 if (instHost == NULL) {
tvendov 0:6435b67ad23c 358 instHost = new USBHost();
tvendov 0:6435b67ad23c 359 instHost->init();
tvendov 0:6435b67ad23c 360 }
tvendov 0:6435b67ad23c 361 return instHost;
tvendov 0:6435b67ad23c 362 }
tvendov 0:6435b67ad23c 363
tvendov 0:6435b67ad23c 364
tvendov 0:6435b67ad23c 365 /*
tvendov 0:6435b67ad23c 366 * Called when a device has been connected
tvendov 0:6435b67ad23c 367 * Called in ISR!!!! (no printf)
tvendov 0:6435b67ad23c 368 */
tvendov 0:6435b67ad23c 369 /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent)
tvendov 0:6435b67ad23c 370 {
tvendov 0:6435b67ad23c 371 // be sure that the new device connected is not already connected...
tvendov 0:6435b67ad23c 372 int idx = findDevice(hub, port, hub_parent);
tvendov 0:6435b67ad23c 373 if (idx != -1) {
tvendov 0:6435b67ad23c 374 if (deviceInited[idx])
tvendov 0:6435b67ad23c 375 return;
tvendov 0:6435b67ad23c 376 }
tvendov 0:6435b67ad23c 377
tvendov 0:6435b67ad23c 378 message_t * usb_msg = mail_usb_event.alloc();
tvendov 0:6435b67ad23c 379 usb_msg->event_id = DEVICE_CONNECTED_EVENT;
tvendov 0:6435b67ad23c 380 usb_msg->hub = hub;
tvendov 0:6435b67ad23c 381 usb_msg->port = port;
tvendov 0:6435b67ad23c 382 usb_msg->lowSpeed = lowSpeed;
tvendov 0:6435b67ad23c 383 usb_msg->hub_parent = hub_parent;
tvendov 0:6435b67ad23c 384 mail_usb_event.put(usb_msg);
tvendov 0:6435b67ad23c 385 plug_status = true;
tvendov 0:6435b67ad23c 386 }
tvendov 0:6435b67ad23c 387
tvendov 0:6435b67ad23c 388 /*
tvendov 0:6435b67ad23c 389 * Called when a device has been disconnected
tvendov 0:6435b67ad23c 390 * Called in ISR!!!! (no printf)
tvendov 0:6435b67ad23c 391 */
tvendov 0:6435b67ad23c 392 /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr)
tvendov 0:6435b67ad23c 393 {
tvendov 0:6435b67ad23c 394 // be sure that the device disconnected is connected...
tvendov 0:6435b67ad23c 395 int idx = findDevice(hub, port, hub_parent);
tvendov 0:6435b67ad23c 396 if (idx != -1) {
tvendov 0:6435b67ad23c 397 if (!deviceInUse[idx])
tvendov 0:6435b67ad23c 398 return;
tvendov 0:6435b67ad23c 399 } else {
tvendov 0:6435b67ad23c 400 return;
tvendov 0:6435b67ad23c 401 }
tvendov 0:6435b67ad23c 402
tvendov 0:6435b67ad23c 403 message_t * usb_msg = mail_usb_event.alloc();
tvendov 0:6435b67ad23c 404 usb_msg->event_id = DEVICE_DISCONNECTED_EVENT;
tvendov 0:6435b67ad23c 405 usb_msg->hub = hub;
tvendov 0:6435b67ad23c 406 usb_msg->port = port;
tvendov 0:6435b67ad23c 407 usb_msg->hub_parent = hub_parent;
tvendov 0:6435b67ad23c 408 mail_usb_event.put(usb_msg);
tvendov 0:6435b67ad23c 409 plug_status = false;
tvendov 0:6435b67ad23c 410 }
tvendov 0:6435b67ad23c 411
tvendov 0:6435b67ad23c 412 void USBHost::freeDevice(USBDeviceConnected * dev)
tvendov 0:6435b67ad23c 413 {
tvendov 0:6435b67ad23c 414 USBEndpoint * ep = NULL;
tvendov 0:6435b67ad23c 415 HCED * ed = NULL;
tvendov 0:6435b67ad23c 416
tvendov 0:6435b67ad23c 417 #if MAX_HUB_NB
tvendov 0:6435b67ad23c 418 if (dev->getClass() == HUB_CLASS) {
tvendov 0:6435b67ad23c 419 if (dev->hub == NULL) {
tvendov 0:6435b67ad23c 420 USB_ERR("HUB NULL!!!!!\r\n");
tvendov 0:6435b67ad23c 421 } else {
tvendov 0:6435b67ad23c 422 dev->hub->hubDisconnected();
tvendov 0:6435b67ad23c 423 for (uint8_t i = 0; i < MAX_HUB_NB; i++) {
tvendov 0:6435b67ad23c 424 if (dev->hub == &hubs[i]) {
tvendov 0:6435b67ad23c 425 hub_in_use[i] = false;
tvendov 0:6435b67ad23c 426 break;
tvendov 0:6435b67ad23c 427 }
tvendov 0:6435b67ad23c 428 }
tvendov 0:6435b67ad23c 429 }
tvendov 0:6435b67ad23c 430 }
tvendov 0:6435b67ad23c 431
tvendov 0:6435b67ad23c 432 // notify hub parent that this device has been disconnected
tvendov 0:6435b67ad23c 433 if (dev->getHubParent())
tvendov 0:6435b67ad23c 434 dev->getHubParent()->deviceDisconnected(dev);
tvendov 0:6435b67ad23c 435
tvendov 0:6435b67ad23c 436 #endif
tvendov 0:6435b67ad23c 437
tvendov 0:6435b67ad23c 438 int idx = findDevice(dev);
tvendov 0:6435b67ad23c 439 if (idx != -1) {
tvendov 0:6435b67ad23c 440 deviceInUse[idx] = false;
tvendov 0:6435b67ad23c 441 deviceReset[idx] = false;
tvendov 0:6435b67ad23c 442
tvendov 0:6435b67ad23c 443 for (uint8_t j = 0; j < MAX_INTF; j++) {
tvendov 0:6435b67ad23c 444 deviceAttachedDriver[idx][j] = false;
tvendov 0:6435b67ad23c 445 if (dev->getInterface(j) != NULL) {
tvendov 0:6435b67ad23c 446 USB_DBG("FREE INTF %d on dev: %p, %p, nb_endpot: %d, %s", j, (void *)dev->getInterface(j), dev, dev->getInterface(j)->nb_endpoint, dev->getName(j));
tvendov 0:6435b67ad23c 447 for (int i = 0; i < dev->getInterface(j)->nb_endpoint; i++) {
tvendov 0:6435b67ad23c 448 if ((ep = dev->getEndpoint(j, i)) != NULL) {
tvendov 0:6435b67ad23c 449 ed = (HCED *)ep->getHCED();
tvendov 0:6435b67ad23c 450 ed->control |= (1 << 14); //sKip bit
tvendov 0:6435b67ad23c 451 unqueueEndpoint(ep);
tvendov 0:6435b67ad23c 452
tvendov 0:6435b67ad23c 453 freeTD((volatile uint8_t*)ep->getTDList()[0]);
tvendov 0:6435b67ad23c 454 freeTD((volatile uint8_t*)ep->getTDList()[1]);
tvendov 0:6435b67ad23c 455
tvendov 0:6435b67ad23c 456 freeED((uint8_t *)ep->getHCED());
tvendov 0:6435b67ad23c 457 }
tvendov 0:6435b67ad23c 458 printList(BULK_ENDPOINT);
tvendov 0:6435b67ad23c 459 printList(INTERRUPT_ENDPOINT);
tvendov 0:6435b67ad23c 460 }
tvendov 0:6435b67ad23c 461 USB_INFO("Device disconnected [%p - %s - hub: %d - port: %d]", dev, dev->getName(j), dev->getHub(), dev->getPort());
tvendov 0:6435b67ad23c 462 }
tvendov 0:6435b67ad23c 463 }
tvendov 0:6435b67ad23c 464 dev->disconnect();
tvendov 0:6435b67ad23c 465 }
tvendov 0:6435b67ad23c 466 }
tvendov 0:6435b67ad23c 467
tvendov 0:6435b67ad23c 468
tvendov 0:6435b67ad23c 469 void USBHost::unqueueEndpoint(USBEndpoint * ep)
tvendov 0:6435b67ad23c 470 {
tvendov 0:6435b67ad23c 471 USBEndpoint * prec = NULL;
tvendov 0:6435b67ad23c 472 USBEndpoint * current = NULL;
tvendov 0:6435b67ad23c 473
tvendov 0:6435b67ad23c 474 for (int i = 0; i < 2; i++) {
tvendov 0:6435b67ad23c 475 current = (i == 0) ? (USBEndpoint*)headBulkEndpoint : (USBEndpoint*)headInterruptEndpoint;
tvendov 0:6435b67ad23c 476 prec = current;
tvendov 0:6435b67ad23c 477 while (current != NULL) {
tvendov 0:6435b67ad23c 478 if (current == ep) {
tvendov 0:6435b67ad23c 479 if (current->nextEndpoint() != NULL) {
tvendov 0:6435b67ad23c 480 prec->queueEndpoint(current->nextEndpoint());
tvendov 0:6435b67ad23c 481 if (current == headBulkEndpoint) {
tvendov 0:6435b67ad23c 482 updateBulkHeadED((uint32_t)current->nextEndpoint()->getHCED());
tvendov 0:6435b67ad23c 483 headBulkEndpoint = current->nextEndpoint();
tvendov 0:6435b67ad23c 484 } else if (current == headInterruptEndpoint) {
tvendov 0:6435b67ad23c 485 updateInterruptHeadED((uint32_t)current->nextEndpoint()->getHCED());
tvendov 0:6435b67ad23c 486 headInterruptEndpoint = current->nextEndpoint();
tvendov 0:6435b67ad23c 487 }
tvendov 0:6435b67ad23c 488 }
tvendov 0:6435b67ad23c 489 // here we are dequeuing the queue of ed
tvendov 0:6435b67ad23c 490 // we need to update the tail pointer
tvendov 0:6435b67ad23c 491 else {
tvendov 0:6435b67ad23c 492 prec->queueEndpoint(NULL);
tvendov 0:6435b67ad23c 493 if (current == headBulkEndpoint) {
tvendov 0:6435b67ad23c 494 updateBulkHeadED(0);
tvendov 0:6435b67ad23c 495 headBulkEndpoint = current->nextEndpoint();
tvendov 0:6435b67ad23c 496 } else if (current == headInterruptEndpoint) {
tvendov 0:6435b67ad23c 497 updateInterruptHeadED(0);
tvendov 0:6435b67ad23c 498 headInterruptEndpoint = current->nextEndpoint();
tvendov 0:6435b67ad23c 499 }
tvendov 0:6435b67ad23c 500
tvendov 0:6435b67ad23c 501 // modify tail
tvendov 0:6435b67ad23c 502 switch (current->getType()) {
tvendov 0:6435b67ad23c 503 case BULK_ENDPOINT:
tvendov 0:6435b67ad23c 504 tailBulkEndpoint = prec;
tvendov 0:6435b67ad23c 505 break;
tvendov 0:6435b67ad23c 506 case INTERRUPT_ENDPOINT:
tvendov 0:6435b67ad23c 507 tailInterruptEndpoint = prec;
tvendov 0:6435b67ad23c 508 break;
tvendov 0:6435b67ad23c 509 default:
tvendov 0:6435b67ad23c 510 break;
tvendov 0:6435b67ad23c 511 }
tvendov 0:6435b67ad23c 512 }
tvendov 0:6435b67ad23c 513 current->setState(USB_TYPE_FREE);
tvendov 0:6435b67ad23c 514 return;
tvendov 0:6435b67ad23c 515 }
tvendov 0:6435b67ad23c 516 prec = current;
tvendov 0:6435b67ad23c 517 current = current->nextEndpoint();
tvendov 0:6435b67ad23c 518 }
tvendov 0:6435b67ad23c 519 }
tvendov 0:6435b67ad23c 520 }
tvendov 0:6435b67ad23c 521
tvendov 0:6435b67ad23c 522
tvendov 0:6435b67ad23c 523 USBDeviceConnected * USBHost::getDevice(uint8_t index)
tvendov 0:6435b67ad23c 524 {
tvendov 0:6435b67ad23c 525 if ((index >= MAX_DEVICE_CONNECTED) || (!deviceInUse[index])) {
tvendov 0:6435b67ad23c 526 return NULL;
tvendov 0:6435b67ad23c 527 }
tvendov 0:6435b67ad23c 528 return (USBDeviceConnected*)&devices[index];
tvendov 0:6435b67ad23c 529 }
tvendov 0:6435b67ad23c 530
tvendov 0:6435b67ad23c 531 // create an USBEndpoint descriptor. the USBEndpoint is not linked
tvendov 0:6435b67ad23c 532 USBEndpoint * USBHost::newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr)
tvendov 0:6435b67ad23c 533 {
tvendov 0:6435b67ad23c 534 int i = 0;
tvendov 0:6435b67ad23c 535 HCED * ed = (HCED *)getED();
tvendov 0:6435b67ad23c 536 HCTD* td_list[2] = { (HCTD*)getTD(), (HCTD*)getTD() };
tvendov 0:6435b67ad23c 537
tvendov 0:6435b67ad23c 538 memset((void *)td_list[0], 0x00, sizeof(HCTD));
tvendov 0:6435b67ad23c 539 memset((void *)td_list[1], 0x00, sizeof(HCTD));
tvendov 0:6435b67ad23c 540
tvendov 0:6435b67ad23c 541 // search a free USBEndpoint
tvendov 0:6435b67ad23c 542 for (i = 0; i < MAX_ENDPOINT; i++) {
tvendov 0:6435b67ad23c 543 if (endpoints[i].getState() == USB_TYPE_FREE) {
tvendov 0:6435b67ad23c 544 endpoints[i].init(ed, type, dir, size, addr, td_list);
tvendov 0:6435b67ad23c 545 USB_DBG("USBEndpoint created (%p): type: %d, dir: %d, size: %d, addr: %d, state: %s", &endpoints[i], type, dir, size, addr, endpoints[i].getStateString());
tvendov 0:6435b67ad23c 546 return &endpoints[i];
tvendov 0:6435b67ad23c 547 }
tvendov 0:6435b67ad23c 548 }
tvendov 0:6435b67ad23c 549 USB_ERR("could not allocate more endpoints!!!!");
tvendov 0:6435b67ad23c 550 return NULL;
tvendov 0:6435b67ad23c 551 }
tvendov 0:6435b67ad23c 552
tvendov 0:6435b67ad23c 553
tvendov 0:6435b67ad23c 554 USB_TYPE USBHost::resetDevice(USBDeviceConnected * dev)
tvendov 0:6435b67ad23c 555 {
tvendov 0:6435b67ad23c 556 int index = findDevice(dev);
tvendov 0:6435b67ad23c 557 if (index != -1) {
tvendov 0:6435b67ad23c 558 USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort());
tvendov 0:6435b67ad23c 559 Thread::wait(100);
tvendov 0:6435b67ad23c 560 if (dev->getHub() == 0) {
tvendov 0:6435b67ad23c 561 resetRootHub();
tvendov 0:6435b67ad23c 562 }
tvendov 0:6435b67ad23c 563 #if MAX_HUB_NB
tvendov 0:6435b67ad23c 564 else {
tvendov 0:6435b67ad23c 565 dev->getHubParent()->portReset(dev->getPort());
tvendov 0:6435b67ad23c 566 }
tvendov 0:6435b67ad23c 567 #endif
tvendov 0:6435b67ad23c 568 Thread::wait(100);
tvendov 0:6435b67ad23c 569 deviceReset[index] = true;
tvendov 0:6435b67ad23c 570 return USB_TYPE_OK;
tvendov 0:6435b67ad23c 571 }
tvendov 0:6435b67ad23c 572
tvendov 0:6435b67ad23c 573 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 574 }
tvendov 0:6435b67ad23c 575
tvendov 0:6435b67ad23c 576 // link the USBEndpoint to the linked list and attach an USBEndpoint to a device
tvendov 0:6435b67ad23c 577 bool USBHost::addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep)
tvendov 0:6435b67ad23c 578 {
tvendov 0:6435b67ad23c 579
tvendov 0:6435b67ad23c 580 if (ep == NULL) {
tvendov 0:6435b67ad23c 581 return false;
tvendov 0:6435b67ad23c 582 }
tvendov 0:6435b67ad23c 583
tvendov 0:6435b67ad23c 584 HCED * prevEd;
tvendov 0:6435b67ad23c 585
tvendov 0:6435b67ad23c 586 // set device address in the USBEndpoint descriptor
tvendov 0:6435b67ad23c 587 if (dev == NULL) {
tvendov 0:6435b67ad23c 588 ep->setDeviceAddress(0);
tvendov 0:6435b67ad23c 589 } else {
tvendov 0:6435b67ad23c 590 ep->setDeviceAddress(dev->getAddress());
tvendov 0:6435b67ad23c 591 }
tvendov 0:6435b67ad23c 592
tvendov 0:6435b67ad23c 593 if ((dev != NULL) && dev->getSpeed()) {
tvendov 0:6435b67ad23c 594 ep->setSpeed(dev->getSpeed());
tvendov 0:6435b67ad23c 595 }
tvendov 0:6435b67ad23c 596
tvendov 0:6435b67ad23c 597 ep->setIntfNb(intf_nb);
tvendov 0:6435b67ad23c 598
tvendov 0:6435b67ad23c 599 // queue the new USBEndpoint on the ED list
tvendov 0:6435b67ad23c 600 switch (ep->getType()) {
tvendov 0:6435b67ad23c 601
tvendov 0:6435b67ad23c 602 case CONTROL_ENDPOINT:
tvendov 0:6435b67ad23c 603 prevEd = ( HCED*) controlHeadED();
tvendov 0:6435b67ad23c 604 if (!prevEd) {
tvendov 0:6435b67ad23c 605 updateControlHeadED((uint32_t) ep->getHCED());
tvendov 0:6435b67ad23c 606 USB_DBG_TRANSFER("First control USBEndpoint: %08X", (uint32_t) ep->getHCED());
tvendov 0:6435b67ad23c 607 headControlEndpoint = ep;
tvendov 0:6435b67ad23c 608 tailControlEndpoint = ep;
tvendov 0:6435b67ad23c 609 return true;
tvendov 0:6435b67ad23c 610 }
tvendov 0:6435b67ad23c 611 tailControlEndpoint->queueEndpoint(ep);
tvendov 0:6435b67ad23c 612 tailControlEndpoint = ep;
tvendov 0:6435b67ad23c 613 return true;
tvendov 0:6435b67ad23c 614
tvendov 0:6435b67ad23c 615 case BULK_ENDPOINT:
tvendov 0:6435b67ad23c 616 prevEd = ( HCED*) bulkHeadED();
tvendov 0:6435b67ad23c 617 if (!prevEd) {
tvendov 0:6435b67ad23c 618 updateBulkHeadED((uint32_t) ep->getHCED());
tvendov 0:6435b67ad23c 619 USB_DBG_TRANSFER("First bulk USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED());
tvendov 0:6435b67ad23c 620 headBulkEndpoint = ep;
tvendov 0:6435b67ad23c 621 tailBulkEndpoint = ep;
tvendov 0:6435b67ad23c 622 break;
tvendov 0:6435b67ad23c 623 }
tvendov 0:6435b67ad23c 624 USB_DBG_TRANSFER("Queue BULK Ed %p after %p\r\n",ep->getHCED(), prevEd);
tvendov 0:6435b67ad23c 625 tailBulkEndpoint->queueEndpoint(ep);
tvendov 0:6435b67ad23c 626 tailBulkEndpoint = ep;
tvendov 0:6435b67ad23c 627 break;
tvendov 0:6435b67ad23c 628
tvendov 0:6435b67ad23c 629 case INTERRUPT_ENDPOINT:
tvendov 0:6435b67ad23c 630 prevEd = ( HCED*) interruptHeadED();
tvendov 0:6435b67ad23c 631 if (!prevEd) {
tvendov 0:6435b67ad23c 632 updateInterruptHeadED((uint32_t) ep->getHCED());
tvendov 0:6435b67ad23c 633 USB_DBG_TRANSFER("First interrupt USBEndpoint: %08X\r\n", (uint32_t) ep->getHCED());
tvendov 0:6435b67ad23c 634 headInterruptEndpoint = ep;
tvendov 0:6435b67ad23c 635 tailInterruptEndpoint = ep;
tvendov 0:6435b67ad23c 636 break;
tvendov 0:6435b67ad23c 637 }
tvendov 0:6435b67ad23c 638 USB_DBG_TRANSFER("Queue INTERRUPT Ed %p after %p\r\n",ep->getHCED(), prevEd);
tvendov 0:6435b67ad23c 639 tailInterruptEndpoint->queueEndpoint(ep);
tvendov 0:6435b67ad23c 640 tailInterruptEndpoint = ep;
tvendov 0:6435b67ad23c 641 break;
tvendov 0:6435b67ad23c 642 default:
tvendov 0:6435b67ad23c 643 return false;
tvendov 0:6435b67ad23c 644 }
tvendov 0:6435b67ad23c 645
tvendov 0:6435b67ad23c 646 ep->dev = dev;
tvendov 0:6435b67ad23c 647 dev->addEndpoint(intf_nb, ep);
tvendov 0:6435b67ad23c 648
tvendov 0:6435b67ad23c 649 return true;
tvendov 0:6435b67ad23c 650 }
tvendov 0:6435b67ad23c 651
tvendov 0:6435b67ad23c 652
tvendov 0:6435b67ad23c 653 int USBHost::findDevice(USBDeviceConnected * dev)
tvendov 0:6435b67ad23c 654 {
tvendov 0:6435b67ad23c 655 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
tvendov 0:6435b67ad23c 656 if (dev == &devices[i]) {
tvendov 0:6435b67ad23c 657 return i;
tvendov 0:6435b67ad23c 658 }
tvendov 0:6435b67ad23c 659 }
tvendov 0:6435b67ad23c 660 return -1;
tvendov 0:6435b67ad23c 661 }
tvendov 0:6435b67ad23c 662
tvendov 0:6435b67ad23c 663 int USBHost::findDevice(uint8_t hub, uint8_t port, USBHostHub * hub_parent)
tvendov 0:6435b67ad23c 664 {
tvendov 0:6435b67ad23c 665 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
tvendov 0:6435b67ad23c 666 if (devices[i].getHub() == hub && devices[i].getPort() == port) {
tvendov 0:6435b67ad23c 667 if (hub_parent != NULL) {
tvendov 0:6435b67ad23c 668 if (hub_parent == devices[i].getHubParent())
tvendov 0:6435b67ad23c 669 return i;
tvendov 0:6435b67ad23c 670 } else {
tvendov 0:6435b67ad23c 671 return i;
tvendov 0:6435b67ad23c 672 }
tvendov 0:6435b67ad23c 673 }
tvendov 0:6435b67ad23c 674 }
tvendov 0:6435b67ad23c 675 return -1;
tvendov 0:6435b67ad23c 676 }
tvendov 0:6435b67ad23c 677
tvendov 0:6435b67ad23c 678 void USBHost::printList(ENDPOINT_TYPE type)
tvendov 0:6435b67ad23c 679 {
tvendov 0:6435b67ad23c 680 #if DEBUG_EP_STATE
tvendov 0:6435b67ad23c 681 volatile HCED * hced;
tvendov 0:6435b67ad23c 682 switch(type) {
tvendov 0:6435b67ad23c 683 case CONTROL_ENDPOINT:
tvendov 0:6435b67ad23c 684 hced = (HCED *)controlHeadED();
tvendov 0:6435b67ad23c 685 break;
tvendov 0:6435b67ad23c 686 case BULK_ENDPOINT:
tvendov 0:6435b67ad23c 687 hced = (HCED *)bulkHeadED();
tvendov 0:6435b67ad23c 688 break;
tvendov 0:6435b67ad23c 689 case INTERRUPT_ENDPOINT:
tvendov 0:6435b67ad23c 690 hced = (HCED *)interruptHeadED();
tvendov 0:6435b67ad23c 691 break;
tvendov 0:6435b67ad23c 692 }
tvendov 0:6435b67ad23c 693 volatile HCTD * hctd = NULL;
tvendov 0:6435b67ad23c 694 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" :
tvendov 0:6435b67ad23c 695 ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" :
tvendov 0:6435b67ad23c 696 ((type == CONTROL_ENDPOINT) ? "CONTROL" : "ISOCHRONOUS"));
tvendov 0:6435b67ad23c 697 printf("State of %s:\r\n", type_str);
tvendov 0:6435b67ad23c 698 while (hced != NULL) {
tvendov 0:6435b67ad23c 699 uint8_t dir = ((hced->control & (3 << 11)) >> 11);
tvendov 0:6435b67ad23c 700 printf("hced: %p [ADDR: %d, DIR: %s, EP_NB: 0x%X]\r\n", hced,
tvendov 0:6435b67ad23c 701 hced->control & 0x7f,
tvendov 0:6435b67ad23c 702 (dir == 1) ? "OUT" : ((dir == 0) ? "FROM_TD":"IN"),
tvendov 0:6435b67ad23c 703 (hced->control & (0xf << 7)) >> 7);
tvendov 0:6435b67ad23c 704 hctd = (HCTD *)((uint32_t)(hced->headTD) & ~(0xf));
tvendov 0:6435b67ad23c 705 while (hctd != hced->tailTD) {
tvendov 0:6435b67ad23c 706 printf("\thctd: %p [DIR: %s]\r\n", hctd, ((hctd->control & (3 << 19)) >> 19) == 1 ? "OUT" : "IN");
tvendov 0:6435b67ad23c 707 hctd = hctd->nextTD;
tvendov 0:6435b67ad23c 708 }
tvendov 0:6435b67ad23c 709 printf("\thctd: %p\r\n", hctd);
tvendov 0:6435b67ad23c 710 hced = hced->nextED;
tvendov 0:6435b67ad23c 711 }
tvendov 0:6435b67ad23c 712 printf("\r\n\r\n");
tvendov 0:6435b67ad23c 713 #endif
tvendov 0:6435b67ad23c 714 }
tvendov 0:6435b67ad23c 715
tvendov 0:6435b67ad23c 716
tvendov 0:6435b67ad23c 717 // add a transfer on the TD linked list
tvendov 0:6435b67ad23c 718 USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
tvendov 0:6435b67ad23c 719 {
tvendov 0:6435b67ad23c 720 td_mutex.lock();
tvendov 0:6435b67ad23c 721
tvendov 0:6435b67ad23c 722 // allocate a TD which will be freed in TDcompletion
tvendov 0:6435b67ad23c 723 volatile HCTD * td = ed->getNextTD();
tvendov 0:6435b67ad23c 724 if (td == NULL) {
tvendov 0:6435b67ad23c 725 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 726 }
tvendov 0:6435b67ad23c 727
tvendov 0:6435b67ad23c 728 uint32_t token = (ed->isSetup() ? TD_SETUP : ( (ed->getDir() == IN) ? TD_IN : TD_OUT ));
tvendov 0:6435b67ad23c 729
tvendov 0:6435b67ad23c 730 uint32_t td_toggle;
tvendov 0:6435b67ad23c 731
tvendov 0:6435b67ad23c 732 if (ed->getType() == CONTROL_ENDPOINT) {
tvendov 0:6435b67ad23c 733 if (ed->isSetup()) {
tvendov 0:6435b67ad23c 734 td_toggle = TD_TOGGLE_0;
tvendov 0:6435b67ad23c 735 } else {
tvendov 0:6435b67ad23c 736 td_toggle = TD_TOGGLE_1;
tvendov 0:6435b67ad23c 737 }
tvendov 0:6435b67ad23c 738 } else {
tvendov 0:6435b67ad23c 739 td_toggle = 0;
tvendov 0:6435b67ad23c 740 }
tvendov 0:6435b67ad23c 741
tvendov 0:6435b67ad23c 742 td->control = (TD_ROUNDING | token | TD_DELAY_INT(0) | td_toggle | TD_CC);
tvendov 0:6435b67ad23c 743 td->currBufPtr = buf;
tvendov 0:6435b67ad23c 744 td->bufEnd = (buf + (len - 1));
tvendov 0:6435b67ad23c 745
tvendov 0:6435b67ad23c 746 ENDPOINT_TYPE type = ed->getType();
tvendov 0:6435b67ad23c 747
tvendov 0:6435b67ad23c 748 disableList(type);
tvendov 0:6435b67ad23c 749 ed->queueTransfer();
tvendov 0:6435b67ad23c 750 printList(type);
tvendov 0:6435b67ad23c 751 enableList(type);
tvendov 0:6435b67ad23c 752
tvendov 0:6435b67ad23c 753 td_mutex.unlock();
tvendov 0:6435b67ad23c 754
tvendov 0:6435b67ad23c 755 return USB_TYPE_PROCESSING;
tvendov 0:6435b67ad23c 756 }
tvendov 0:6435b67ad23c 757
tvendov 0:6435b67ad23c 758
tvendov 0:6435b67ad23c 759
tvendov 0:6435b67ad23c 760 USB_TYPE USBHost::getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_dev_descr)
tvendov 0:6435b67ad23c 761 {
tvendov 0:6435b67ad23c 762 USB_TYPE t = controlRead( dev,
tvendov 0:6435b67ad23c 763 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
tvendov 0:6435b67ad23c 764 GET_DESCRIPTOR,
tvendov 0:6435b67ad23c 765 (DEVICE_DESCRIPTOR << 8) | (0),
tvendov 0:6435b67ad23c 766 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf));
tvendov 0:6435b67ad23c 767 if (len_dev_descr)
tvendov 0:6435b67ad23c 768 *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf);
tvendov 0:6435b67ad23c 769
tvendov 0:6435b67ad23c 770 return t;
tvendov 0:6435b67ad23c 771 }
tvendov 0:6435b67ad23c 772
tvendov 0:6435b67ad23c 773 USB_TYPE USBHost::getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t max_len_buf, uint16_t * len_conf_descr)
tvendov 0:6435b67ad23c 774 {
tvendov 0:6435b67ad23c 775 USB_TYPE res;
tvendov 0:6435b67ad23c 776 uint16_t total_conf_descr_length = 0;
tvendov 0:6435b67ad23c 777
tvendov 0:6435b67ad23c 778 // fourth step: get the beginning of the configuration descriptor to have the total length of the conf descr
tvendov 0:6435b67ad23c 779 res = controlRead( dev,
tvendov 0:6435b67ad23c 780 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
tvendov 0:6435b67ad23c 781 GET_DESCRIPTOR,
tvendov 0:6435b67ad23c 782 (CONFIGURATION_DESCRIPTOR << 8) | (0),
tvendov 0:6435b67ad23c 783 0, buf, CONFIGURATION_DESCRIPTOR_LENGTH);
tvendov 0:6435b67ad23c 784
tvendov 0:6435b67ad23c 785 if (res != USB_TYPE_OK) {
tvendov 0:6435b67ad23c 786 USB_ERR("GET CONF 1 DESCR FAILED");
tvendov 0:6435b67ad23c 787 return res;
tvendov 0:6435b67ad23c 788 }
tvendov 0:6435b67ad23c 789 total_conf_descr_length = buf[2] | (buf[3] << 8);
tvendov 0:6435b67ad23c 790 total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length);
tvendov 0:6435b67ad23c 791
tvendov 0:6435b67ad23c 792 if (len_conf_descr)
tvendov 0:6435b67ad23c 793 *len_conf_descr = total_conf_descr_length;
tvendov 0:6435b67ad23c 794
tvendov 0:6435b67ad23c 795 USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]);
tvendov 0:6435b67ad23c 796
tvendov 0:6435b67ad23c 797 return controlRead( dev,
tvendov 0:6435b67ad23c 798 USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE,
tvendov 0:6435b67ad23c 799 GET_DESCRIPTOR,
tvendov 0:6435b67ad23c 800 (CONFIGURATION_DESCRIPTOR << 8) | (0),
tvendov 0:6435b67ad23c 801 0, buf, total_conf_descr_length);
tvendov 0:6435b67ad23c 802 }
tvendov 0:6435b67ad23c 803
tvendov 0:6435b67ad23c 804
tvendov 0:6435b67ad23c 805 USB_TYPE USBHost::setAddress(USBDeviceConnected * dev, uint8_t address) {
tvendov 0:6435b67ad23c 806 return controlWrite( dev,
tvendov 0:6435b67ad23c 807 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
tvendov 0:6435b67ad23c 808 SET_ADDRESS,
tvendov 0:6435b67ad23c 809 address,
tvendov 0:6435b67ad23c 810 0, NULL, 0);
tvendov 0:6435b67ad23c 811
tvendov 0:6435b67ad23c 812 }
tvendov 0:6435b67ad23c 813
tvendov 0:6435b67ad23c 814 USB_TYPE USBHost::setConfiguration(USBDeviceConnected * dev, uint8_t conf)
tvendov 0:6435b67ad23c 815 {
tvendov 0:6435b67ad23c 816 return controlWrite( dev,
tvendov 0:6435b67ad23c 817 USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE,
tvendov 0:6435b67ad23c 818 SET_CONFIGURATION,
tvendov 0:6435b67ad23c 819 conf,
tvendov 0:6435b67ad23c 820 0, NULL, 0);
tvendov 0:6435b67ad23c 821 }
tvendov 0:6435b67ad23c 822
tvendov 0:6435b67ad23c 823 uint8_t USBHost::numberDriverAttached(USBDeviceConnected * dev) {
tvendov 0:6435b67ad23c 824 int index = findDevice(dev);
tvendov 0:6435b67ad23c 825 uint8_t cnt = 0;
tvendov 0:6435b67ad23c 826 if (index == -1)
tvendov 0:6435b67ad23c 827 return 0;
tvendov 0:6435b67ad23c 828 for (uint8_t i = 0; i < MAX_INTF; i++) {
tvendov 0:6435b67ad23c 829 if (deviceAttachedDriver[index][i])
tvendov 0:6435b67ad23c 830 cnt++;
tvendov 0:6435b67ad23c 831 }
tvendov 0:6435b67ad23c 832 return cnt;
tvendov 0:6435b67ad23c 833 }
tvendov 0:6435b67ad23c 834
tvendov 0:6435b67ad23c 835 // enumerate a device with the control USBEndpoint
tvendov 0:6435b67ad23c 836 USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator)
tvendov 0:6435b67ad23c 837 {
tvendov 0:6435b67ad23c 838 uint16_t total_conf_descr_length = 0;
tvendov 0:6435b67ad23c 839 USB_TYPE res;
tvendov 0:6435b67ad23c 840
tvendov 0:6435b67ad23c 841 do
tvendov 0:6435b67ad23c 842 {
tvendov 0:6435b67ad23c 843 Lock lock(this);
tvendov 0:6435b67ad23c 844
tvendov 0:6435b67ad23c 845 // don't enumerate a device which all interfaces are registered to a specific driver
tvendov 0:6435b67ad23c 846 int index = findDevice(dev);
tvendov 0:6435b67ad23c 847
tvendov 0:6435b67ad23c 848 if (index == -1) {
tvendov 0:6435b67ad23c 849 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 850 }
tvendov 0:6435b67ad23c 851
tvendov 0:6435b67ad23c 852 uint8_t nb_intf_attached = numberDriverAttached(dev);
tvendov 0:6435b67ad23c 853 USB_DBG("dev: %p nb_intf: %d", dev, dev->getNbIntf());
tvendov 0:6435b67ad23c 854 USB_DBG("dev: %p nb_intf_attached: %d", dev, nb_intf_attached);
tvendov 0:6435b67ad23c 855 if ((nb_intf_attached != 0) && (dev->getNbIntf() == nb_intf_attached)) {
tvendov 0:6435b67ad23c 856 USB_DBG("Don't enumerate dev: %p because all intf are registered with a driver", dev);
tvendov 0:6435b67ad23c 857 return USB_TYPE_OK;
tvendov 0:6435b67ad23c 858 }
tvendov 0:6435b67ad23c 859
tvendov 0:6435b67ad23c 860 USB_DBG("Enumerate dev: %p", dev);
tvendov 0:6435b67ad23c 861
tvendov 0:6435b67ad23c 862 // third step: get the whole device descriptor to see vid, pid
tvendov 0:6435b67ad23c 863 res = getDeviceDescriptor(dev, data, DEVICE_DESCRIPTOR_LENGTH);
tvendov 0:6435b67ad23c 864
tvendov 0:6435b67ad23c 865 if (res != USB_TYPE_OK) {
tvendov 0:6435b67ad23c 866 USB_DBG("GET DEV DESCR FAILED");
tvendov 0:6435b67ad23c 867 return res;
tvendov 0:6435b67ad23c 868 }
tvendov 0:6435b67ad23c 869
tvendov 0:6435b67ad23c 870 dev->setClass(data[4]);
tvendov 0:6435b67ad23c 871 dev->setSubClass(data[5]);
tvendov 0:6435b67ad23c 872 dev->setProtocol(data[6]);
tvendov 0:6435b67ad23c 873 dev->setVid(data[8] | (data[9] << 8));
tvendov 0:6435b67ad23c 874 dev->setPid(data[10] | (data[11] << 8));
tvendov 0:6435b67ad23c 875 USB_DBG("CLASS: %02X \t VID: %04X \t PID: %04X", data[4], data[8] | (data[9] << 8), data[10] | (data[11] << 8));
tvendov 0:6435b67ad23c 876
tvendov 0:6435b67ad23c 877 pEnumerator->setVidPid( data[8] | (data[9] << 8), data[10] | (data[11] << 8) );
tvendov 0:6435b67ad23c 878
tvendov 0:6435b67ad23c 879 res = getConfigurationDescriptor(dev, data, sizeof(data), &total_conf_descr_length);
tvendov 0:6435b67ad23c 880 if (res != USB_TYPE_OK) {
tvendov 0:6435b67ad23c 881 return res;
tvendov 0:6435b67ad23c 882 }
tvendov 0:6435b67ad23c 883
tvendov 0:6435b67ad23c 884 #if (DEBUG > 3)
tvendov 0:6435b67ad23c 885 USB_DBG("CONFIGURATION DESCRIPTOR:\r\n");
tvendov 0:6435b67ad23c 886 for (int i = 0; i < total_conf_descr_length; i++)
tvendov 0:6435b67ad23c 887 printf("%02X ", data[i]);
tvendov 0:6435b67ad23c 888 printf("\r\n\r\n");
tvendov 0:6435b67ad23c 889 #endif
tvendov 0:6435b67ad23c 890
tvendov 0:6435b67ad23c 891 // Parse the configuration descriptor
tvendov 0:6435b67ad23c 892 parseConfDescr(dev, data, total_conf_descr_length, pEnumerator);
tvendov 0:6435b67ad23c 893
tvendov 0:6435b67ad23c 894 // only set configuration if not enumerated before
tvendov 0:6435b67ad23c 895 if (!dev->isEnumerated()) {
tvendov 0:6435b67ad23c 896
tvendov 0:6435b67ad23c 897 USB_DBG("Set configuration 1 on dev: %p", dev);
tvendov 0:6435b67ad23c 898 // sixth step: set configuration (only 1 supported)
tvendov 0:6435b67ad23c 899 res = setConfiguration(dev, 1);
tvendov 0:6435b67ad23c 900
tvendov 0:6435b67ad23c 901 if (res != USB_TYPE_OK) {
tvendov 0:6435b67ad23c 902 USB_DBG("SET CONF FAILED");
tvendov 0:6435b67ad23c 903 return res;
tvendov 0:6435b67ad23c 904 }
tvendov 0:6435b67ad23c 905 }
tvendov 0:6435b67ad23c 906
tvendov 0:6435b67ad23c 907 dev->setEnumerated();
tvendov 0:6435b67ad23c 908
tvendov 0:6435b67ad23c 909 // Now the device is enumerated!
tvendov 0:6435b67ad23c 910 USB_DBG("dev %p is enumerated\r\n", dev);
tvendov 0:6435b67ad23c 911
tvendov 0:6435b67ad23c 912 } while(0);
tvendov 0:6435b67ad23c 913
tvendov 0:6435b67ad23c 914 // Some devices may require this delay
tvendov 0:6435b67ad23c 915 Thread::wait(100);
tvendov 0:6435b67ad23c 916
tvendov 0:6435b67ad23c 917 return USB_TYPE_OK;
tvendov 0:6435b67ad23c 918 }
tvendov 0:6435b67ad23c 919 // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor.
tvendov 0:6435b67ad23c 920 void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator)
tvendov 0:6435b67ad23c 921 {
tvendov 0:6435b67ad23c 922 uint32_t index = 0;
tvendov 0:6435b67ad23c 923 uint32_t len_desc = 0;
tvendov 0:6435b67ad23c 924 uint8_t id = 0;
tvendov 0:6435b67ad23c 925 int nb_endpoints_used = 0;
tvendov 0:6435b67ad23c 926 USBEndpoint * ep = NULL;
tvendov 0:6435b67ad23c 927 uint8_t intf_nb = 0;
tvendov 0:6435b67ad23c 928 bool parsing_intf = false;
tvendov 0:6435b67ad23c 929 uint8_t current_intf = 0;
tvendov 0:6435b67ad23c 930
tvendov 0:6435b67ad23c 931 #if(1) /* Isochronous */
tvendov 0:6435b67ad23c 932 lenCnfdDescr = len;
tvendov 0:6435b67ad23c 933 indexCnfdDescr = 0;
tvendov 0:6435b67ad23c 934 #endif
tvendov 0:6435b67ad23c 935
tvendov 0:6435b67ad23c 936 while (index < len) {
tvendov 0:6435b67ad23c 937 len_desc = conf_descr[index];
tvendov 0:6435b67ad23c 938 id = conf_descr[index+1];
tvendov 0:6435b67ad23c 939 switch (id) {
tvendov 0:6435b67ad23c 940 case CONFIGURATION_DESCRIPTOR:
tvendov 0:6435b67ad23c 941 USB_DBG("dev: %p has %d intf", dev, conf_descr[4]);
tvendov 0:6435b67ad23c 942 dev->setNbIntf(conf_descr[4]);
tvendov 0:6435b67ad23c 943 break;
tvendov 0:6435b67ad23c 944 case INTERFACE_DESCRIPTOR:
tvendov 0:6435b67ad23c 945 if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) {
tvendov 0:6435b67ad23c 946 if (intf_nb++ <= MAX_INTF) {
tvendov 0:6435b67ad23c 947 current_intf = conf_descr[index + 2];
tvendov 0:6435b67ad23c 948 dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]);
tvendov 0:6435b67ad23c 949 nb_endpoints_used = 0;
tvendov 0:6435b67ad23c 950 USB_DBG("ADD INTF %d on device %p: class: %d, subclass: %d, proto: %d", current_intf, dev, conf_descr[index + 5],conf_descr[index + 6],conf_descr[index + 7]);
tvendov 0:6435b67ad23c 951 } else {
tvendov 0:6435b67ad23c 952 USB_DBG("Drop intf...");
tvendov 0:6435b67ad23c 953 }
tvendov 0:6435b67ad23c 954 parsing_intf = true;
tvendov 0:6435b67ad23c 955 } else {
tvendov 0:6435b67ad23c 956 parsing_intf = false;
tvendov 0:6435b67ad23c 957 }
tvendov 0:6435b67ad23c 958 break;
tvendov 0:6435b67ad23c 959 case ENDPOINT_DESCRIPTOR:
tvendov 0:6435b67ad23c 960 if (parsing_intf && (intf_nb <= MAX_INTF) ) {
tvendov 0:6435b67ad23c 961 if (nb_endpoints_used < MAX_ENDPOINT_PER_INTERFACE) {
tvendov 0:6435b67ad23c 962 if( pEnumerator->useEndpoint(current_intf, (ENDPOINT_TYPE)(conf_descr[index + 3] & 0x03), (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1)) ) {
tvendov 0:6435b67ad23c 963 // if the USBEndpoint is isochronous -> skip it (TODO: fix this)
tvendov 0:6435b67ad23c 964 if ((conf_descr[index + 3] & 0x03) != ISOCHRONOUS_ENDPOINT) {
tvendov 0:6435b67ad23c 965 ep = newEndpoint((ENDPOINT_TYPE)(conf_descr[index+3] & 0x03),
tvendov 0:6435b67ad23c 966 (ENDPOINT_DIRECTION)((conf_descr[index + 2] >> 7) + 1),
tvendov 0:6435b67ad23c 967 conf_descr[index + 4] | (conf_descr[index + 5] << 8),
tvendov 0:6435b67ad23c 968 conf_descr[index + 2] & 0x0f);
tvendov 0:6435b67ad23c 969 USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev);
tvendov 0:6435b67ad23c 970 if (ep != NULL && dev != NULL) {
tvendov 0:6435b67ad23c 971 addEndpoint(dev, current_intf, ep);
tvendov 0:6435b67ad23c 972 } else {
tvendov 0:6435b67ad23c 973 USB_DBG("EP NULL");
tvendov 0:6435b67ad23c 974 }
tvendov 0:6435b67ad23c 975 nb_endpoints_used++;
tvendov 0:6435b67ad23c 976 } else {
tvendov 0:6435b67ad23c 977 USB_DBG("ISO USBEndpoint NOT SUPPORTED");
tvendov 0:6435b67ad23c 978 }
tvendov 0:6435b67ad23c 979 }
tvendov 0:6435b67ad23c 980 }
tvendov 0:6435b67ad23c 981 }
tvendov 0:6435b67ad23c 982 break;
tvendov 0:6435b67ad23c 983 case HID_DESCRIPTOR:
tvendov 0:6435b67ad23c 984 lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8);
tvendov 0:6435b67ad23c 985 break;
tvendov 0:6435b67ad23c 986 default:
tvendov 0:6435b67ad23c 987 break;
tvendov 0:6435b67ad23c 988 }
tvendov 0:6435b67ad23c 989 index += len_desc;
tvendov 0:6435b67ad23c 990 #if(1) /* Isochronous */
tvendov 0:6435b67ad23c 991 indexCnfdDescr = index;
tvendov 0:6435b67ad23c 992 #endif
tvendov 0:6435b67ad23c 993 }
tvendov 0:6435b67ad23c 994 }
tvendov 0:6435b67ad23c 995
tvendov 0:6435b67ad23c 996
tvendov 0:6435b67ad23c 997 USB_TYPE USBHost::bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
tvendov 0:6435b67ad23c 998 {
tvendov 0:6435b67ad23c 999 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, true);
tvendov 0:6435b67ad23c 1000 }
tvendov 0:6435b67ad23c 1001
tvendov 0:6435b67ad23c 1002 USB_TYPE USBHost::bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
tvendov 0:6435b67ad23c 1003 {
tvendov 0:6435b67ad23c 1004 return generalTransfer(dev, ep, buf, len, blocking, BULK_ENDPOINT, false);
tvendov 0:6435b67ad23c 1005 }
tvendov 0:6435b67ad23c 1006
tvendov 0:6435b67ad23c 1007 USB_TYPE USBHost::interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
tvendov 0:6435b67ad23c 1008 {
tvendov 0:6435b67ad23c 1009 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, true);
tvendov 0:6435b67ad23c 1010 }
tvendov 0:6435b67ad23c 1011
tvendov 0:6435b67ad23c 1012 USB_TYPE USBHost::interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking)
tvendov 0:6435b67ad23c 1013 {
tvendov 0:6435b67ad23c 1014 return generalTransfer(dev, ep, buf, len, blocking, INTERRUPT_ENDPOINT, false);
tvendov 0:6435b67ad23c 1015 }
tvendov 0:6435b67ad23c 1016
tvendov 0:6435b67ad23c 1017 USB_TYPE USBHost::generalTransfer(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking, ENDPOINT_TYPE type, bool write) {
tvendov 0:6435b67ad23c 1018
tvendov 0:6435b67ad23c 1019 #if DEBUG_TRANSFER
tvendov 0:6435b67ad23c 1020 const char * type_str = (type == BULK_ENDPOINT) ? "BULK" : ((type == INTERRUPT_ENDPOINT) ? "INTERRUPT" : "ISOCHRONOUS");
tvendov 0:6435b67ad23c 1021 USB_DBG_TRANSFER("----- %s %s [dev: %p - %s - hub: %d - port: %d - addr: %d - ep: %02X]------", type_str, (write) ? "WRITE" : "READ", dev, dev->getName(ep->getIntfNb()), dev->getHub(), dev->getPort(), dev->getAddress(), ep->getAddress());
tvendov 0:6435b67ad23c 1022 #endif
tvendov 0:6435b67ad23c 1023
tvendov 0:6435b67ad23c 1024 Lock lock(this);
tvendov 0:6435b67ad23c 1025
tvendov 0:6435b67ad23c 1026 USB_TYPE res;
tvendov 0:6435b67ad23c 1027 ENDPOINT_DIRECTION dir = (write) ? OUT : IN;
tvendov 0:6435b67ad23c 1028
tvendov 0:6435b67ad23c 1029 if (dev == NULL) {
tvendov 0:6435b67ad23c 1030 USB_ERR("dev NULL");
tvendov 0:6435b67ad23c 1031 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 1032 }
tvendov 0:6435b67ad23c 1033
tvendov 0:6435b67ad23c 1034 if (ep == NULL) {
tvendov 0:6435b67ad23c 1035 USB_ERR("ep NULL");
tvendov 0:6435b67ad23c 1036 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 1037 }
tvendov 0:6435b67ad23c 1038
tvendov 0:6435b67ad23c 1039 if (ep->getState() != USB_TYPE_IDLE) {
tvendov 0:6435b67ad23c 1040 USB_WARN("[ep: %p - dev: %p - %s] NOT IDLE: %s", ep, ep->dev, ep->dev->getName(ep->getIntfNb()), ep->getStateString());
tvendov 0:6435b67ad23c 1041 return ep->getState();
tvendov 0:6435b67ad23c 1042 }
tvendov 0:6435b67ad23c 1043
tvendov 0:6435b67ad23c 1044 if ((ep->getDir() != dir) || (ep->getType() != type)) {
tvendov 0:6435b67ad23c 1045 USB_ERR("[ep: %p - dev: %p] wrong dir or bad USBEndpoint type", ep, ep->dev);
tvendov 0:6435b67ad23c 1046 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 1047 }
tvendov 0:6435b67ad23c 1048
tvendov 0:6435b67ad23c 1049 if (dev->getAddress() != ep->getDeviceAddress()) {
tvendov 0:6435b67ad23c 1050 USB_ERR("[ep: %p - dev: %p] USBEndpoint addr and device addr don't match", ep, ep->dev);
tvendov 0:6435b67ad23c 1051 return USB_TYPE_ERROR;
tvendov 0:6435b67ad23c 1052 }
tvendov 0:6435b67ad23c 1053
tvendov 0:6435b67ad23c 1054 #if DEBUG_TRANSFER
tvendov 0:6435b67ad23c 1055 if (write) {
tvendov 0:6435b67ad23c 1056 USB_DBG_TRANSFER("%s WRITE buffer", type_str);
tvendov 0:6435b67ad23c 1057 for (int i = 0; i < ep->getLengthTransferred(); i++)
tvendov 0:6435b67ad23c 1058 printf("%02X ", buf[i]);
tvendov 0:6435b67ad23c 1059 printf("\r\n\r\n");
tvendov 0:6435b67ad23c 1060 }
tvendov 0:6435b67ad23c 1061 #endif
tvendov 0:6435b67ad23c 1062 addTransfer(ep, buf, len);
tvendov 0:6435b67ad23c 1063
tvendov 0:6435b67ad23c 1064 if (blocking) {
tvendov 0:6435b67ad23c 1065
tvendov 0:6435b67ad23c 1066 ep->ep_queue.get();
tvendov 0:6435b67ad23c 1067 res = ep->getState();
tvendov 0:6435b67ad23c 1068
tvendov 0:6435b67ad23c 1069 USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep);
tvendov 0:6435b67ad23c 1070
tvendov 0:6435b67ad23c 1071 if (res != USB_TYPE_IDLE) {
tvendov 0:6435b67ad23c 1072 return res;
tvendov 0:6435b67ad23c 1073 }
tvendov 0:6435b67ad23c 1074
tvendov 0:6435b67ad23c 1075 return USB_TYPE_OK;
tvendov 0:6435b67ad23c 1076 }
tvendov 0:6435b67ad23c 1077
tvendov 0:6435b67ad23c 1078 return USB_TYPE_PROCESSING;
tvendov 0:6435b67ad23c 1079
tvendov 0:6435b67ad23c 1080 }
tvendov 0:6435b67ad23c 1081
tvendov 0:6435b67ad23c 1082
tvendov 0:6435b67ad23c 1083 USB_TYPE USBHost::controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
tvendov 0:6435b67ad23c 1084 return controlTransfer(dev, requestType, request, value, index, buf, len, false);
tvendov 0:6435b67ad23c 1085 }
tvendov 0:6435b67ad23c 1086
tvendov 0:6435b67ad23c 1087 USB_TYPE USBHost::controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
tvendov 0:6435b67ad23c 1088 return controlTransfer(dev, requestType, request, value, index, buf, len, true);
tvendov 0:6435b67ad23c 1089 }
tvendov 0:6435b67ad23c 1090
tvendov 0:6435b67ad23c 1091 USB_TYPE USBHost::controlTransfer(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len, bool write)
tvendov 0:6435b67ad23c 1092 {
tvendov 0:6435b67ad23c 1093 Lock lock(this);
tvendov 0:6435b67ad23c 1094 USB_DBG_TRANSFER("----- CONTROL %s [dev: %p - hub: %d - port: %d] ------", (write) ? "WRITE" : "READ", dev, dev->getHub(), dev->getPort());
tvendov 0:6435b67ad23c 1095
tvendov 0:6435b67ad23c 1096 int length_transfer = len;
tvendov 0:6435b67ad23c 1097 USB_TYPE res;
tvendov 0:6435b67ad23c 1098 uint32_t token;
tvendov 0:6435b67ad23c 1099
tvendov 0:6435b67ad23c 1100 control->setSpeed(dev->getSpeed());
tvendov 0:6435b67ad23c 1101 control->setSize(dev->getSizeControlEndpoint());
tvendov 0:6435b67ad23c 1102 if (dev->isActiveAddress()) {
tvendov 0:6435b67ad23c 1103 control->setDeviceAddress(dev->getAddress());
tvendov 0:6435b67ad23c 1104 } else {
tvendov 0:6435b67ad23c 1105 control->setDeviceAddress(0);
tvendov 0:6435b67ad23c 1106 }
tvendov 0:6435b67ad23c 1107
tvendov 0:6435b67ad23c 1108 USB_DBG_TRANSFER("Control transfer on device: %d\r\n", control->getDeviceAddress());
tvendov 0:6435b67ad23c 1109 fillControlBuf(requestType, request, value, index, len);
tvendov 0:6435b67ad23c 1110
tvendov 0:6435b67ad23c 1111 #if DEBUG_TRANSFER
tvendov 0:6435b67ad23c 1112 USB_DBG_TRANSFER("SETUP PACKET: ");
tvendov 0:6435b67ad23c 1113 for (int i = 0; i < 8; i++)
tvendov 0:6435b67ad23c 1114 printf("%01X ", setupPacket[i]);
tvendov 0:6435b67ad23c 1115 printf("\r\n");
tvendov 0:6435b67ad23c 1116 #endif
tvendov 0:6435b67ad23c 1117
tvendov 0:6435b67ad23c 1118 control->setNextToken(TD_SETUP);
tvendov 0:6435b67ad23c 1119 addTransfer(control, (uint8_t*)setupPacket, 8);
tvendov 0:6435b67ad23c 1120
tvendov 0:6435b67ad23c 1121 control->ep_queue.get();
tvendov 0:6435b67ad23c 1122 res = control->getState();
tvendov 0:6435b67ad23c 1123
tvendov 0:6435b67ad23c 1124 USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString());
tvendov 0:6435b67ad23c 1125
tvendov 0:6435b67ad23c 1126 if (res != USB_TYPE_IDLE) {
tvendov 0:6435b67ad23c 1127 return res;
tvendov 0:6435b67ad23c 1128 }
tvendov 0:6435b67ad23c 1129
tvendov 0:6435b67ad23c 1130 if (length_transfer) {
tvendov 0:6435b67ad23c 1131 token = (write) ? TD_OUT : TD_IN;
tvendov 0:6435b67ad23c 1132 control->setNextToken(token);
tvendov 0:6435b67ad23c 1133 addTransfer(control, (uint8_t *)buf, length_transfer);
tvendov 0:6435b67ad23c 1134
tvendov 0:6435b67ad23c 1135 control->ep_queue.get();
tvendov 0:6435b67ad23c 1136 res = control->getState();
tvendov 0:6435b67ad23c 1137
tvendov 0:6435b67ad23c 1138 #if DEBUG_TRANSFER
tvendov 0:6435b67ad23c 1139 USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString());
tvendov 0:6435b67ad23c 1140 if (write) {
tvendov 0:6435b67ad23c 1141 USB_DBG_TRANSFER("CONTROL WRITE buffer");
tvendov 0:6435b67ad23c 1142 for (int i = 0; i < control->getLengthTransferred(); i++)
tvendov 0:6435b67ad23c 1143 printf("%02X ", buf[i]);
tvendov 0:6435b67ad23c 1144 printf("\r\n\r\n");
tvendov 0:6435b67ad23c 1145 } else {
tvendov 0:6435b67ad23c 1146 USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred());
tvendov 0:6435b67ad23c 1147 for (int i = 0; i < control->getLengthTransferred(); i++)
tvendov 0:6435b67ad23c 1148 printf("%02X ", buf[i]);
tvendov 0:6435b67ad23c 1149 printf("\r\n\r\n");
tvendov 0:6435b67ad23c 1150 }
tvendov 0:6435b67ad23c 1151 #endif
tvendov 0:6435b67ad23c 1152
tvendov 0:6435b67ad23c 1153 if (res != USB_TYPE_IDLE) {
tvendov 0:6435b67ad23c 1154 return res;
tvendov 0:6435b67ad23c 1155 }
tvendov 0:6435b67ad23c 1156 }
tvendov 0:6435b67ad23c 1157
tvendov 0:6435b67ad23c 1158 token = (write) ? TD_IN : TD_OUT;
tvendov 0:6435b67ad23c 1159 control->setNextToken(token);
tvendov 0:6435b67ad23c 1160 addTransfer(control, NULL, 0);
tvendov 0:6435b67ad23c 1161
tvendov 0:6435b67ad23c 1162 control->ep_queue.get();
tvendov 0:6435b67ad23c 1163 res = control->getState();
tvendov 0:6435b67ad23c 1164
tvendov 0:6435b67ad23c 1165 USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString());
tvendov 0:6435b67ad23c 1166
tvendov 0:6435b67ad23c 1167 if (res != USB_TYPE_IDLE)
tvendov 0:6435b67ad23c 1168 return res;
tvendov 0:6435b67ad23c 1169
tvendov 0:6435b67ad23c 1170 return USB_TYPE_OK;
tvendov 0:6435b67ad23c 1171 }
tvendov 0:6435b67ad23c 1172
tvendov 0:6435b67ad23c 1173
tvendov 0:6435b67ad23c 1174 void USBHost::fillControlBuf(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, int len)
tvendov 0:6435b67ad23c 1175 {
tvendov 0:6435b67ad23c 1176 setupPacket[0] = requestType;
tvendov 0:6435b67ad23c 1177 setupPacket[1] = request;
tvendov 0:6435b67ad23c 1178 setupPacket[2] = (uint8_t) value;
tvendov 0:6435b67ad23c 1179 setupPacket[3] = (uint8_t) (value >> 8);
tvendov 0:6435b67ad23c 1180 setupPacket[4] = (uint8_t) index;
tvendov 0:6435b67ad23c 1181 setupPacket[5] = (uint8_t) (index >> 8);
tvendov 0:6435b67ad23c 1182 setupPacket[6] = (uint8_t) len;
tvendov 0:6435b67ad23c 1183 setupPacket[7] = (uint8_t) (len >> 8);
tvendov 0:6435b67ad23c 1184 }