modified to compile for me
Fork of F401RE-USBHost by
USBHost/USBHost.cpp@28:98551d9289a8, 2017-02-24 (annotated)
- Committer:
- Ownasaurus
- Date:
- Fri Feb 24 02:44:44 2017 +0000
- Revision:
- 28:98551d9289a8
- Parent:
- 27:1b65f379ad07
added print to indicate reason for occasional startup failure
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 |
18:61554f238584 | 1 | /* mbed USBHost Library |
va009039 |
18:61554f238584 | 2 | * Copyright (c) 2006-2013 ARM Limited |
va009039 |
18:61554f238584 | 3 | * |
va009039 |
18:61554f238584 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
va009039 |
18:61554f238584 | 5 | * you may not use this file except in compliance with the License. |
va009039 |
18:61554f238584 | 6 | * You may obtain a copy of the License at |
va009039 |
18:61554f238584 | 7 | * |
va009039 |
18:61554f238584 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
va009039 |
18:61554f238584 | 9 | * |
va009039 |
18:61554f238584 | 10 | * Unless required by applicable law or agreed to in writing, software |
va009039 |
18:61554f238584 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
va009039 |
18:61554f238584 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
va009039 |
18:61554f238584 | 13 | * See the License for the specific language governing permissions and |
va009039 |
18:61554f238584 | 14 | * limitations under the License. |
va009039 |
18:61554f238584 | 15 | */ |
va009039 |
18:61554f238584 | 16 | |
va009039 | 12:b91fdea8c0a7 | 17 | #include "USBHost.h" |
va009039 | 12:b91fdea8c0a7 | 18 | |
va009039 |
18:61554f238584 | 19 | #define USB_TRACE1(A) while(0) |
va009039 |
18:61554f238584 | 20 | #undef USB_TEST_ASSERT |
va009039 |
18:61554f238584 | 21 | void usb_test_assert_internal(const char *expr, const char *file, int line); |
va009039 |
18:61554f238584 | 22 | #define USB_TEST_ASSERT(EXPR) while(!(EXPR)){usb_test_assert_internal(#EXPR,__FILE__,__LINE__);} |
va009039 |
18:61554f238584 | 23 | |
va009039 | 12:b91fdea8c0a7 | 24 | USBHost* USBHost::inst = NULL; |
va009039 | 12:b91fdea8c0a7 | 25 | |
va009039 |
18:61554f238584 | 26 | USBHost* USBHost::getHostInst() { |
va009039 | 12:b91fdea8c0a7 | 27 | if (inst == NULL) { |
va009039 | 12:b91fdea8c0a7 | 28 | inst = new USBHost(); |
va009039 | 12:b91fdea8c0a7 | 29 | inst->init(); |
va009039 | 12:b91fdea8c0a7 | 30 | } |
va009039 | 12:b91fdea8c0a7 | 31 | return inst; |
va009039 | 12:b91fdea8c0a7 | 32 | } |
va009039 | 12:b91fdea8c0a7 | 33 | |
va009039 | 12:b91fdea8c0a7 | 34 | void USBHost::poll() |
va009039 | 12:b91fdea8c0a7 | 35 | { |
va009039 | 12:b91fdea8c0a7 | 36 | if (inst) { |
va009039 | 12:b91fdea8c0a7 | 37 | inst->task(); |
va009039 | 12:b91fdea8c0a7 | 38 | } |
va009039 | 12:b91fdea8c0a7 | 39 | } |
va009039 | 12:b91fdea8c0a7 | 40 | |
va009039 | 12:b91fdea8c0a7 | 41 | USBHost::USBHost() { |
va009039 | 12:b91fdea8c0a7 | 42 | } |
va009039 | 12:b91fdea8c0a7 | 43 | |
va009039 | 12:b91fdea8c0a7 | 44 | /* virtual */ bool USBHost::addDevice(USBDeviceConnected* parent, int port, bool lowSpeed) { |
va009039 | 12:b91fdea8c0a7 | 45 | USBDeviceConnected* dev = new USBDeviceConnected; |
va009039 | 12:b91fdea8c0a7 | 46 | USBEndpoint* ep = new USBEndpoint(dev); |
va009039 | 12:b91fdea8c0a7 | 47 | dev->init(0, port, lowSpeed); |
va009039 | 12:b91fdea8c0a7 | 48 | dev->setAddress(0); |
va009039 | 12:b91fdea8c0a7 | 49 | dev->setEpCtl(ep); |
va009039 | 12:b91fdea8c0a7 | 50 | uint8_t desc[18]; |
va009039 | 12:b91fdea8c0a7 | 51 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 52 | |
va009039 | 12:b91fdea8c0a7 | 53 | int rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, 8); |
Ownasaurus | 27:1b65f379ad07 | 54 | USB_TEST_ASSERT(rc == USB_TYPE_OK); // randomly fails if xbone is plugged in when it turns on |
va009039 | 12:b91fdea8c0a7 | 55 | if (rc != USB_TYPE_OK) { |
va009039 | 12:b91fdea8c0a7 | 56 | USB_ERR("ADD DEVICE FAILD"); |
va009039 | 12:b91fdea8c0a7 | 57 | } |
Ownasaurus | 27:1b65f379ad07 | 58 | |
va009039 | 12:b91fdea8c0a7 | 59 | USB_DBG_HEX(desc, 8); |
va009039 | 12:b91fdea8c0a7 | 60 | DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc); |
va009039 | 12:b91fdea8c0a7 | 61 | ep->setSize(dev_desc->bMaxPacketSize); |
va009039 | 12:b91fdea8c0a7 | 62 | |
va009039 | 12:b91fdea8c0a7 | 63 | int new_addr = USBDeviceConnected::getNewAddress(); |
va009039 | 12:b91fdea8c0a7 | 64 | rc = controlWrite(dev, 0x00, SET_ADDRESS, new_addr, 0, NULL, 0); |
va009039 | 12:b91fdea8c0a7 | 65 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 66 | dev->setAddress(new_addr); |
va009039 | 12:b91fdea8c0a7 | 67 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 68 | |
va009039 | 12:b91fdea8c0a7 | 69 | rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 70 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 71 | USB_DBG_HEX(desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 72 | |
va009039 | 12:b91fdea8c0a7 | 73 | dev->setVid(dev_desc->idVendor); |
va009039 | 12:b91fdea8c0a7 | 74 | dev->setPid(dev_desc->idProduct); |
va009039 | 12:b91fdea8c0a7 | 75 | dev->setClass(dev_desc->bDeviceClass); |
va009039 | 12:b91fdea8c0a7 | 76 | USB_INFO("parent:%p port:%d speed:%s VID:%04x PID:%04x class:%02x addr:%d", |
va009039 | 12:b91fdea8c0a7 | 77 | parent, port, (lowSpeed ? "low " : "full"), dev->getVid(), dev->getPid(), dev->getClass(), |
va009039 | 12:b91fdea8c0a7 | 78 | dev->getAddress()); |
va009039 | 12:b91fdea8c0a7 | 79 | |
va009039 | 12:b91fdea8c0a7 | 80 | DeviceLists.push_back(dev); |
va009039 | 12:b91fdea8c0a7 | 81 | |
va009039 | 12:b91fdea8c0a7 | 82 | if (dev->getClass() == HUB_CLASS) { |
va009039 | 12:b91fdea8c0a7 | 83 | const int config = 1; |
va009039 | 12:b91fdea8c0a7 | 84 | int rc = controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); |
va009039 | 12:b91fdea8c0a7 | 85 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 86 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 87 | Hub(dev); |
va009039 | 12:b91fdea8c0a7 | 88 | } |
va009039 | 12:b91fdea8c0a7 | 89 | return true; |
va009039 | 12:b91fdea8c0a7 | 90 | } |
va009039 | 12:b91fdea8c0a7 | 91 | |
va009039 | 12:b91fdea8c0a7 | 92 | // enumerate a device with the control USBEndpoint |
va009039 | 12:b91fdea8c0a7 | 93 | USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator) |
va009039 | 12:b91fdea8c0a7 | 94 | { |
va009039 | 12:b91fdea8c0a7 | 95 | if (dev->getClass() == HUB_CLASS) { // skip hub class |
va009039 | 12:b91fdea8c0a7 | 96 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 97 | } |
va009039 | 12:b91fdea8c0a7 | 98 | uint8_t desc[18]; |
va009039 | 12:b91fdea8c0a7 | 99 | USB_TYPE rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 100 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 101 | USB_DBG_HEX(desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 102 | if (rc != USB_TYPE_OK) { |
va009039 | 12:b91fdea8c0a7 | 103 | return rc; |
va009039 | 12:b91fdea8c0a7 | 104 | } |
va009039 | 12:b91fdea8c0a7 | 105 | DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc); |
va009039 | 12:b91fdea8c0a7 | 106 | dev->setClass(dev_desc->bDeviceClass); |
va009039 | 12:b91fdea8c0a7 | 107 | pEnumerator->setVidPid(dev->getVid(), dev->getPid()); |
va009039 | 12:b91fdea8c0a7 | 108 | |
va009039 | 12:b91fdea8c0a7 | 109 | rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, desc, 4); |
va009039 | 12:b91fdea8c0a7 | 110 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 111 | USB_DBG_HEX(desc, 4); |
va009039 | 12:b91fdea8c0a7 | 112 | |
va009039 | 12:b91fdea8c0a7 | 113 | int TotalLength = desc[2]|desc[3]<<8; |
va009039 | 12:b91fdea8c0a7 | 114 | uint8_t* buf = new uint8_t[TotalLength]; |
va009039 | 12:b91fdea8c0a7 | 115 | rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, buf, TotalLength); |
va009039 | 12:b91fdea8c0a7 | 116 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 117 | //USB_DBG_HEX(buf, TotalLength); |
va009039 | 12:b91fdea8c0a7 | 118 | |
va009039 | 12:b91fdea8c0a7 | 119 | // Parse the configuration descriptor |
va009039 | 12:b91fdea8c0a7 | 120 | parseConfDescr(dev, buf, TotalLength, pEnumerator); |
va009039 | 12:b91fdea8c0a7 | 121 | delete[] buf; |
va009039 | 12:b91fdea8c0a7 | 122 | // only set configuration if not enumerated before |
va009039 | 12:b91fdea8c0a7 | 123 | if (!dev->isEnumerated()) { |
va009039 | 12:b91fdea8c0a7 | 124 | USB_DBG("Set configuration 1 on dev: %p", dev); |
va009039 | 12:b91fdea8c0a7 | 125 | // sixth step: set configuration (only 1 supported) |
va009039 | 12:b91fdea8c0a7 | 126 | int config = 1; |
va009039 | 12:b91fdea8c0a7 | 127 | USB_TYPE res = controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); |
va009039 | 12:b91fdea8c0a7 | 128 | if (res != USB_TYPE_OK) { |
va009039 | 12:b91fdea8c0a7 | 129 | USB_ERR("SET CONF FAILED"); |
va009039 | 12:b91fdea8c0a7 | 130 | return res; |
va009039 | 12:b91fdea8c0a7 | 131 | } |
va009039 | 12:b91fdea8c0a7 | 132 | // Some devices may require this delay |
va009039 | 12:b91fdea8c0a7 | 133 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 134 | dev->setEnumerated(); |
va009039 | 12:b91fdea8c0a7 | 135 | // Now the device is enumerated! |
va009039 | 12:b91fdea8c0a7 | 136 | USB_DBG("dev %p is enumerated", dev); |
va009039 | 12:b91fdea8c0a7 | 137 | } |
va009039 | 12:b91fdea8c0a7 | 138 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 139 | } |
va009039 | 12:b91fdea8c0a7 | 140 | |
va009039 | 12:b91fdea8c0a7 | 141 | // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor. |
va009039 | 12:b91fdea8c0a7 | 142 | void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) |
va009039 | 12:b91fdea8c0a7 | 143 | { |
va009039 | 12:b91fdea8c0a7 | 144 | uint32_t index = 0; |
va009039 | 12:b91fdea8c0a7 | 145 | uint32_t len_desc = 0; |
va009039 | 12:b91fdea8c0a7 | 146 | uint8_t id = 0; |
va009039 | 12:b91fdea8c0a7 | 147 | USBEndpoint * ep = NULL; |
va009039 | 12:b91fdea8c0a7 | 148 | uint8_t intf_nb = 0; |
va009039 | 12:b91fdea8c0a7 | 149 | bool parsing_intf = false; |
va009039 | 12:b91fdea8c0a7 | 150 | uint8_t current_intf = 0; |
va009039 | 12:b91fdea8c0a7 | 151 | EndpointDescriptor* ep_desc; |
va009039 | 12:b91fdea8c0a7 | 152 | |
va009039 | 12:b91fdea8c0a7 | 153 | while (index < len) { |
va009039 | 12:b91fdea8c0a7 | 154 | len_desc = conf_descr[index]; |
va009039 | 12:b91fdea8c0a7 | 155 | id = conf_descr[index+1]; |
va009039 | 12:b91fdea8c0a7 | 156 | USB_DBG_HEX(conf_descr+index, len_desc); |
va009039 | 12:b91fdea8c0a7 | 157 | switch (id) { |
va009039 | 12:b91fdea8c0a7 | 158 | case CONFIGURATION_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 159 | USB_DBG("dev: %p has %d intf", dev, conf_descr[4]); |
va009039 | 12:b91fdea8c0a7 | 160 | dev->setNbIntf(conf_descr[4]); |
va009039 | 12:b91fdea8c0a7 | 161 | break; |
va009039 | 12:b91fdea8c0a7 | 162 | case INTERFACE_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 163 | if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) { |
va009039 | 12:b91fdea8c0a7 | 164 | intf_nb++; |
va009039 | 12:b91fdea8c0a7 | 165 | current_intf = conf_descr[index + 2]; |
va009039 | 12:b91fdea8c0a7 | 166 | dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]); |
va009039 | 12:b91fdea8c0a7 | 167 | 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]); |
va009039 | 12:b91fdea8c0a7 | 168 | parsing_intf = true; |
va009039 | 12:b91fdea8c0a7 | 169 | } else { |
va009039 | 12:b91fdea8c0a7 | 170 | parsing_intf = false; |
va009039 | 12:b91fdea8c0a7 | 171 | } |
va009039 | 12:b91fdea8c0a7 | 172 | break; |
va009039 | 12:b91fdea8c0a7 | 173 | case ENDPOINT_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 174 | ep_desc = reinterpret_cast<EndpointDescriptor*>(conf_descr+index); |
va009039 | 12:b91fdea8c0a7 | 175 | if (parsing_intf && (intf_nb <= MAX_INTF) ) { |
va009039 | 12:b91fdea8c0a7 | 176 | ENDPOINT_TYPE type = (ENDPOINT_TYPE)(ep_desc->bmAttributes & 0x03); |
va009039 | 12:b91fdea8c0a7 | 177 | ENDPOINT_DIRECTION dir = (ep_desc->bEndpointAddress & 0x80) ? IN : OUT; |
va009039 | 12:b91fdea8c0a7 | 178 | if(pEnumerator->useEndpoint(current_intf, type, dir)) { |
va009039 | 12:b91fdea8c0a7 | 179 | ep = new USBEndpoint(dev); |
va009039 | 12:b91fdea8c0a7 | 180 | ep->init(type, dir, ep_desc->wMaxPacketSize, ep_desc->bEndpointAddress); |
va009039 | 12:b91fdea8c0a7 | 181 | USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev); |
va009039 | 12:b91fdea8c0a7 | 182 | dev->addEndpoint(current_intf, ep); |
va009039 | 12:b91fdea8c0a7 | 183 | } |
va009039 | 12:b91fdea8c0a7 | 184 | } |
va009039 | 12:b91fdea8c0a7 | 185 | break; |
va009039 | 12:b91fdea8c0a7 | 186 | case HID_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 187 | //lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8); |
va009039 | 12:b91fdea8c0a7 | 188 | break; |
va009039 | 12:b91fdea8c0a7 | 189 | default: |
va009039 | 12:b91fdea8c0a7 | 190 | break; |
va009039 | 12:b91fdea8c0a7 | 191 | } |
va009039 | 12:b91fdea8c0a7 | 192 | index += len_desc; |
va009039 | 12:b91fdea8c0a7 | 193 | } |
va009039 | 12:b91fdea8c0a7 | 194 | } |
va009039 | 12:b91fdea8c0a7 | 195 | |
va009039 | 12:b91fdea8c0a7 | 196 | USB_TYPE USBHost::controlRead(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { |
va009039 |
18:61554f238584 | 197 | USBEndpoint* ep = dev->getEpCtl(); |
va009039 |
18:61554f238584 | 198 | SETUP_PACKET setup(requestType, request, value, index, len); |
va009039 |
18:61554f238584 | 199 | |
va009039 |
18:61554f238584 | 200 | int result = token_setup(ep, &setup, len); // setup stage |
va009039 |
18:61554f238584 | 201 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 202 | if (result < 0) { |
va009039 |
18:61554f238584 | 203 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 204 | } |
va009039 |
18:61554f238584 | 205 | |
va009039 |
18:61554f238584 | 206 | int read_len = multi_token_in(ep, buf, len); // data stage |
va009039 |
18:61554f238584 | 207 | USB_TRACE1(read_len); |
va009039 |
18:61554f238584 | 208 | if (read_len < 0) { |
Ownasaurus | 28:98551d9289a8 | 209 | printf("ERROR:failing on data stage\r\n"); |
va009039 |
18:61554f238584 | 210 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 211 | } |
va009039 |
18:61554f238584 | 212 | |
va009039 |
18:61554f238584 | 213 | setToggle(ep, 1); // DATA1 |
va009039 |
18:61554f238584 | 214 | result = multi_token_out(ep); // status stage |
va009039 |
18:61554f238584 | 215 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 216 | if (result < 0) { |
va009039 |
18:61554f238584 | 217 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 218 | } |
va009039 |
18:61554f238584 | 219 | ep->setLengthTransferred(read_len); |
va009039 |
18:61554f238584 | 220 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 221 | } |
va009039 | 12:b91fdea8c0a7 | 222 | |
va009039 | 12:b91fdea8c0a7 | 223 | USB_TYPE USBHost::controlWrite(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { |
va009039 |
18:61554f238584 | 224 | USBEndpoint* ep = dev->getEpCtl(); |
va009039 |
18:61554f238584 | 225 | SETUP_PACKET setup(requestType, request, value, index, len); |
va009039 |
18:61554f238584 | 226 | |
va009039 |
18:61554f238584 | 227 | int result = token_setup(ep, &setup, len); // setup stage |
va009039 |
18:61554f238584 | 228 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 229 | if (result < 0) { |
va009039 |
18:61554f238584 | 230 | return USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 231 | } |
va009039 |
18:61554f238584 | 232 | int write_len = 0; |
va009039 |
18:61554f238584 | 233 | if (buf != NULL) { |
va009039 |
18:61554f238584 | 234 | write_len = multi_token_out(ep, buf, len); // data stage |
va009039 |
18:61554f238584 | 235 | USB_TRACE1(write_len); |
va009039 |
18:61554f238584 | 236 | if (write_len < 0) { |
va009039 |
18:61554f238584 | 237 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 238 | } |
va009039 |
18:61554f238584 | 239 | } |
va009039 |
18:61554f238584 | 240 | |
va009039 |
18:61554f238584 | 241 | setToggle(ep, 1); // DATA1 |
va009039 |
18:61554f238584 | 242 | result = multi_token_in(ep); // status stage |
va009039 |
18:61554f238584 | 243 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 244 | if (result < 0) { |
va009039 |
18:61554f238584 | 245 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 246 | } |
va009039 |
18:61554f238584 | 247 | ep->setLengthTransferred(write_len); |
va009039 |
18:61554f238584 | 248 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 249 | } |
va009039 | 12:b91fdea8c0a7 | 250 | |
va009039 | 12:b91fdea8c0a7 | 251 | USB_TYPE USBHost::bulkRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 252 | if (blocking == false) { |
va009039 | 12:b91fdea8c0a7 | 253 | ep->setBuffer(buf, len); |
va009039 | 12:b91fdea8c0a7 | 254 | ep_queue.push(ep); |
va009039 |
18:61554f238584 | 255 | multi_token_inNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 256 | return USB_TYPE_PROCESSING; |
va009039 | 12:b91fdea8c0a7 | 257 | } |
va009039 |
18:61554f238584 | 258 | int result = multi_token_in(ep, buf, len); |
va009039 |
18:61554f238584 | 259 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 260 | if (result < 0) { |
va009039 |
18:61554f238584 | 261 | return USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 262 | } |
va009039 |
18:61554f238584 | 263 | ep->setLengthTransferred(result); |
va009039 |
18:61554f238584 | 264 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 265 | } |
va009039 | 12:b91fdea8c0a7 | 266 | |
va009039 | 12:b91fdea8c0a7 | 267 | USB_TYPE USBHost::bulkWrite(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 268 | USB_TEST_ASSERT(blocking); |
va009039 |
18:61554f238584 | 269 | int result = multi_token_out(ep, buf, len); |
va009039 |
18:61554f238584 | 270 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 271 | if (result < 0) { |
va009039 |
18:61554f238584 | 272 | return USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 273 | } |
va009039 |
18:61554f238584 | 274 | ep->setLengthTransferred(result); |
va009039 |
18:61554f238584 | 275 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 276 | } |
va009039 | 12:b91fdea8c0a7 | 277 | |
va009039 | 12:b91fdea8c0a7 | 278 | USB_TYPE USBHost::interruptRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 279 | if (blocking == false) { |
va009039 | 12:b91fdea8c0a7 | 280 | ep->setBuffer(buf, len); |
va009039 | 12:b91fdea8c0a7 | 281 | ep_queue.push(ep); |
va009039 |
18:61554f238584 | 282 | multi_token_inNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 283 | return USB_TYPE_PROCESSING; |
va009039 | 12:b91fdea8c0a7 | 284 | } |
va009039 |
18:61554f238584 | 285 | int result = multi_token_in(ep, buf, len); |
va009039 |
18:61554f238584 | 286 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 287 | if (result < 0) { |
va009039 |
18:61554f238584 | 288 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 289 | } |
va009039 |
18:61554f238584 | 290 | ep->setLengthTransferred(result); |
va009039 | 12:b91fdea8c0a7 | 291 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 292 | } |
va009039 | 12:b91fdea8c0a7 | 293 | |
va009039 | 12:b91fdea8c0a7 | 294 | USB_TYPE USBHost::interruptWrite(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 295 | USB_TEST_ASSERT(blocking); |
va009039 |
18:61554f238584 | 296 | int result = multi_token_out(ep, buf, len); |
va009039 |
18:61554f238584 | 297 | USB_TRACE1(result); |
va009039 |
18:61554f238584 | 298 | if (result < 0) { |
va009039 |
18:61554f238584 | 299 | return USB_TYPE_ERROR; |
va009039 |
18:61554f238584 | 300 | } |
va009039 |
18:61554f238584 | 301 | ep->setLengthTransferred(result); |
va009039 | 12:b91fdea8c0a7 | 302 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 303 | } |
va009039 | 12:b91fdea8c0a7 | 304 | |
va009039 | 12:b91fdea8c0a7 | 305 | USB_TYPE USBHost::isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 |
18:61554f238584 | 306 | USB_TEST_ASSERT(blocking); |
va009039 | 12:b91fdea8c0a7 | 307 | isochronousReadNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 308 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 309 | } |
va009039 | 12:b91fdea8c0a7 | 310 | |
va009039 |
18:61554f238584 | 311 | int USBHost::interruptReadNB(USBEndpoint* ep, uint8_t* data, int size) { |
va009039 |
18:61554f238584 | 312 | USB_TRACE1(size); |
va009039 |
18:61554f238584 | 313 | if (ep->getState() != USB_TYPE_PROCESSING) { |
va009039 |
18:61554f238584 | 314 | ep->setState(USB_TYPE_PROCESSING); |
va009039 |
18:61554f238584 | 315 | ep->setBuffer(data, size); |
va009039 |
18:61554f238584 | 316 | multi_token_inNB(ep, data, size); |
va009039 | 12:b91fdea8c0a7 | 317 | } |
va009039 |
18:61554f238584 | 318 | if (multi_token_inNB_result(ep) != USB_TYPE_PROCESSING) { |
va009039 |
18:61554f238584 | 319 | return ep->getLengthTransferred(); |
va009039 | 12:b91fdea8c0a7 | 320 | } |
va009039 |
18:61554f238584 | 321 | return -1; |
va009039 | 12:b91fdea8c0a7 | 322 | } |
va009039 | 12:b91fdea8c0a7 | 323 | |
va009039 |
18:61554f238584 | 324 | int USBHost::bulkReadNB(USBEndpoint* ep, uint8_t* data, int size) { |
va009039 |
18:61554f238584 | 325 | USB_TRACE1(size); |
va009039 |
18:61554f238584 | 326 | return interruptReadNB(ep, data, size); |
va009039 | 12:b91fdea8c0a7 | 327 | } |
va009039 | 12:b91fdea8c0a7 | 328 | |
va009039 | 12:b91fdea8c0a7 | 329 | int USBHost::isochronousReadNB(USBEndpoint* ep, uint8_t* data, int size) { |
va009039 |
18:61554f238584 | 330 | USB_TRACE1(size); |
va009039 | 12:b91fdea8c0a7 | 331 | int result = token_iso_in(ep, data, size); |
va009039 | 12:b91fdea8c0a7 | 332 | if (result >= 0) { |
va009039 | 12:b91fdea8c0a7 | 333 | ep->setLengthTransferred(result); |
va009039 | 12:b91fdea8c0a7 | 334 | } |
va009039 | 12:b91fdea8c0a7 | 335 | return result; |
va009039 | 12:b91fdea8c0a7 | 336 | } |
va009039 | 12:b91fdea8c0a7 | 337 | |
va009039 |
18:61554f238584 | 338 | void USBHost::task() { |
va009039 | 12:b91fdea8c0a7 | 339 | USBEndpoint* ep = ep_queue.pop(); |
va009039 |
18:61554f238584 | 340 | if (ep) { |
va009039 |
18:61554f238584 | 341 | USB_TEST_ASSERT(ep->getDir() == IN); |
va009039 |
18:61554f238584 | 342 | if (multi_token_inNB_result(ep) != USB_TYPE_PROCESSING) { |
va009039 |
18:61554f238584 | 343 | ep->call(); |
va009039 |
18:61554f238584 | 344 | } else { |
va009039 |
18:61554f238584 | 345 | ep_queue.push(ep); |
va009039 |
18:61554f238584 | 346 | } |
va009039 | 12:b91fdea8c0a7 | 347 | } |
va009039 | 12:b91fdea8c0a7 | 348 | } |
va009039 |
18:61554f238584 | 349 | |
va009039 |
18:61554f238584 | 350 | void usb_test_assert_internal(const char *expr, const char *file, int line){ |
va009039 |
18:61554f238584 | 351 | error("\n\n%s@%d %s ASSERT!\n\n", file, line, expr); |
va009039 |
18:61554f238584 | 352 | } |
va009039 |
18:61554f238584 | 353 |