This example demonstrates the reading of the USB Gamepad in the Nucleo.

Dependencies:   mbed

Intro

This example demonstrates the reading of the USB Gamepad in the Nucleo.

Parts

STM32 Nucleo F446RE
USB Connector
LED 2pcs
Register 470 ohm 2pcs
Breadboard
Wires

Wiring diagram

/media/uploads/beaglescout007/nucleo_ex04_usbpad.png This circuit diagram was created by fritzing.

/media/uploads/beaglescout007/usbcon.jpg

USB con.Nucleo
GNDGND
+PA_12
-PA_11
5V5V

https://youtu.be/EYIukjwJSew

Original Library

Committer:
beaglescout007
Date:
Tue Mar 15 11:39:04 2016 +0000
Revision:
0:b5f79b4f741d
Release

Who changed what in which revision?

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