Usb read
Dependencies: FATFileSystem
Fork of F401RE-USBHost by
USBHost/USBHost.cpp@12:b91fdea8c0a7, 2014-06-09 (annotated)
- Committer:
- va009039
- Date:
- Mon Jun 09 09:01:10 2014 +0000
- Revision:
- 12:b91fdea8c0a7
- Child:
- 16:981c3104f6c0
Simple USBHost for Nucleo F401RE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 12:b91fdea8c0a7 | 1 | // Simple USBHost for FRDM-KL46Z |
va009039 | 12:b91fdea8c0a7 | 2 | #include "USBHost.h" |
va009039 | 12:b91fdea8c0a7 | 3 | #include <algorithm> |
va009039 | 12:b91fdea8c0a7 | 4 | |
va009039 | 12:b91fdea8c0a7 | 5 | USBHost* USBHost::inst = NULL; |
va009039 | 12:b91fdea8c0a7 | 6 | |
va009039 | 12:b91fdea8c0a7 | 7 | USBHost* USBHost::getHostInst() |
va009039 | 12:b91fdea8c0a7 | 8 | { |
va009039 | 12:b91fdea8c0a7 | 9 | if (inst == NULL) { |
va009039 | 12:b91fdea8c0a7 | 10 | inst = new USBHost(); |
va009039 | 12:b91fdea8c0a7 | 11 | inst->init(); |
va009039 | 12:b91fdea8c0a7 | 12 | } |
va009039 | 12:b91fdea8c0a7 | 13 | return inst; |
va009039 | 12:b91fdea8c0a7 | 14 | } |
va009039 | 12:b91fdea8c0a7 | 15 | |
va009039 | 12:b91fdea8c0a7 | 16 | void USBHost::poll() |
va009039 | 12:b91fdea8c0a7 | 17 | { |
va009039 | 12:b91fdea8c0a7 | 18 | if (inst) { |
va009039 | 12:b91fdea8c0a7 | 19 | inst->task(); |
va009039 | 12:b91fdea8c0a7 | 20 | } |
va009039 | 12:b91fdea8c0a7 | 21 | } |
va009039 | 12:b91fdea8c0a7 | 22 | |
va009039 | 12:b91fdea8c0a7 | 23 | USBHost::USBHost() { |
va009039 | 12:b91fdea8c0a7 | 24 | } |
va009039 | 12:b91fdea8c0a7 | 25 | |
va009039 | 12:b91fdea8c0a7 | 26 | /* virtual */ bool USBHost::addDevice(USBDeviceConnected* parent, int port, bool lowSpeed) { |
va009039 | 12:b91fdea8c0a7 | 27 | USBDeviceConnected* dev = new USBDeviceConnected; |
va009039 | 12:b91fdea8c0a7 | 28 | USBEndpoint* ep = new USBEndpoint(dev); |
va009039 | 12:b91fdea8c0a7 | 29 | dev->init(0, port, lowSpeed); |
va009039 | 12:b91fdea8c0a7 | 30 | dev->setAddress(0); |
va009039 | 12:b91fdea8c0a7 | 31 | dev->setEpCtl(ep); |
va009039 | 12:b91fdea8c0a7 | 32 | uint8_t desc[18]; |
va009039 | 12:b91fdea8c0a7 | 33 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 34 | |
va009039 | 12:b91fdea8c0a7 | 35 | int rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, 8); |
va009039 | 12:b91fdea8c0a7 | 36 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 37 | if (rc != USB_TYPE_OK) { |
va009039 | 12:b91fdea8c0a7 | 38 | USB_ERR("ADD DEVICE FAILD"); |
va009039 | 12:b91fdea8c0a7 | 39 | } |
va009039 | 12:b91fdea8c0a7 | 40 | USB_DBG_HEX(desc, 8); |
va009039 | 12:b91fdea8c0a7 | 41 | DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc); |
va009039 | 12:b91fdea8c0a7 | 42 | ep->setSize(dev_desc->bMaxPacketSize); |
va009039 | 12:b91fdea8c0a7 | 43 | |
va009039 | 12:b91fdea8c0a7 | 44 | int new_addr = USBDeviceConnected::getNewAddress(); |
va009039 | 12:b91fdea8c0a7 | 45 | rc = controlWrite(dev, 0x00, SET_ADDRESS, new_addr, 0, NULL, 0); |
va009039 | 12:b91fdea8c0a7 | 46 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 47 | dev->setAddress(new_addr); |
va009039 | 12:b91fdea8c0a7 | 48 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 49 | |
va009039 | 12:b91fdea8c0a7 | 50 | rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 51 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 52 | USB_DBG_HEX(desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 53 | |
va009039 | 12:b91fdea8c0a7 | 54 | dev->setVid(dev_desc->idVendor); |
va009039 | 12:b91fdea8c0a7 | 55 | dev->setPid(dev_desc->idProduct); |
va009039 | 12:b91fdea8c0a7 | 56 | dev->setClass(dev_desc->bDeviceClass); |
va009039 | 12:b91fdea8c0a7 | 57 | USB_INFO("parent:%p port:%d speed:%s VID:%04x PID:%04x class:%02x addr:%d", |
va009039 | 12:b91fdea8c0a7 | 58 | parent, port, (lowSpeed ? "low " : "full"), dev->getVid(), dev->getPid(), dev->getClass(), |
va009039 | 12:b91fdea8c0a7 | 59 | dev->getAddress()); |
va009039 | 12:b91fdea8c0a7 | 60 | |
va009039 | 12:b91fdea8c0a7 | 61 | DeviceLists.push_back(dev); |
va009039 | 12:b91fdea8c0a7 | 62 | |
va009039 | 12:b91fdea8c0a7 | 63 | if (dev->getClass() == HUB_CLASS) { |
va009039 | 12:b91fdea8c0a7 | 64 | const int config = 1; |
va009039 | 12:b91fdea8c0a7 | 65 | int rc = controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); |
va009039 | 12:b91fdea8c0a7 | 66 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 67 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 68 | Hub(dev); |
va009039 | 12:b91fdea8c0a7 | 69 | } |
va009039 | 12:b91fdea8c0a7 | 70 | return true; |
va009039 | 12:b91fdea8c0a7 | 71 | } |
va009039 | 12:b91fdea8c0a7 | 72 | |
va009039 | 12:b91fdea8c0a7 | 73 | // enumerate a device with the control USBEndpoint |
va009039 | 12:b91fdea8c0a7 | 74 | USB_TYPE USBHost::enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator) |
va009039 | 12:b91fdea8c0a7 | 75 | { |
va009039 | 12:b91fdea8c0a7 | 76 | if (dev->getClass() == HUB_CLASS) { // skip hub class |
va009039 | 12:b91fdea8c0a7 | 77 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 78 | } |
va009039 | 12:b91fdea8c0a7 | 79 | uint8_t desc[18]; |
va009039 | 12:b91fdea8c0a7 | 80 | USB_TYPE rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 1<<8, 0, desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 81 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 82 | USB_DBG_HEX(desc, sizeof(desc)); |
va009039 | 12:b91fdea8c0a7 | 83 | if (rc != USB_TYPE_OK) { |
va009039 | 12:b91fdea8c0a7 | 84 | return rc; |
va009039 | 12:b91fdea8c0a7 | 85 | } |
va009039 | 12:b91fdea8c0a7 | 86 | DeviceDescriptor* dev_desc = reinterpret_cast<DeviceDescriptor*>(desc); |
va009039 | 12:b91fdea8c0a7 | 87 | dev->setClass(dev_desc->bDeviceClass); |
va009039 | 12:b91fdea8c0a7 | 88 | pEnumerator->setVidPid(dev->getVid(), dev->getPid()); |
va009039 | 12:b91fdea8c0a7 | 89 | |
va009039 | 12:b91fdea8c0a7 | 90 | rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, desc, 4); |
va009039 | 12:b91fdea8c0a7 | 91 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 92 | USB_DBG_HEX(desc, 4); |
va009039 | 12:b91fdea8c0a7 | 93 | |
va009039 | 12:b91fdea8c0a7 | 94 | int TotalLength = desc[2]|desc[3]<<8; |
va009039 | 12:b91fdea8c0a7 | 95 | uint8_t* buf = new uint8_t[TotalLength]; |
va009039 | 12:b91fdea8c0a7 | 96 | rc = controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, buf, TotalLength); |
va009039 | 12:b91fdea8c0a7 | 97 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 12:b91fdea8c0a7 | 98 | //USB_DBG_HEX(buf, TotalLength); |
va009039 | 12:b91fdea8c0a7 | 99 | |
va009039 | 12:b91fdea8c0a7 | 100 | // Parse the configuration descriptor |
va009039 | 12:b91fdea8c0a7 | 101 | parseConfDescr(dev, buf, TotalLength, pEnumerator); |
va009039 | 12:b91fdea8c0a7 | 102 | delete[] buf; |
va009039 | 12:b91fdea8c0a7 | 103 | // only set configuration if not enumerated before |
va009039 | 12:b91fdea8c0a7 | 104 | if (!dev->isEnumerated()) { |
va009039 | 12:b91fdea8c0a7 | 105 | USB_DBG("Set configuration 1 on dev: %p", dev); |
va009039 | 12:b91fdea8c0a7 | 106 | // sixth step: set configuration (only 1 supported) |
va009039 | 12:b91fdea8c0a7 | 107 | int config = 1; |
va009039 | 12:b91fdea8c0a7 | 108 | USB_TYPE res = controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); |
va009039 | 12:b91fdea8c0a7 | 109 | if (res != USB_TYPE_OK) { |
va009039 | 12:b91fdea8c0a7 | 110 | USB_ERR("SET CONF FAILED"); |
va009039 | 12:b91fdea8c0a7 | 111 | return res; |
va009039 | 12:b91fdea8c0a7 | 112 | } |
va009039 | 12:b91fdea8c0a7 | 113 | // Some devices may require this delay |
va009039 | 12:b91fdea8c0a7 | 114 | wait_ms(100); |
va009039 | 12:b91fdea8c0a7 | 115 | dev->setEnumerated(); |
va009039 | 12:b91fdea8c0a7 | 116 | // Now the device is enumerated! |
va009039 | 12:b91fdea8c0a7 | 117 | USB_DBG("dev %p is enumerated", dev); |
va009039 | 12:b91fdea8c0a7 | 118 | } |
va009039 | 12:b91fdea8c0a7 | 119 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 120 | } |
va009039 | 12:b91fdea8c0a7 | 121 | |
va009039 | 12:b91fdea8c0a7 | 122 | // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor. |
va009039 | 12:b91fdea8c0a7 | 123 | void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) |
va009039 | 12:b91fdea8c0a7 | 124 | { |
va009039 | 12:b91fdea8c0a7 | 125 | uint32_t index = 0; |
va009039 | 12:b91fdea8c0a7 | 126 | uint32_t len_desc = 0; |
va009039 | 12:b91fdea8c0a7 | 127 | uint8_t id = 0; |
va009039 | 12:b91fdea8c0a7 | 128 | USBEndpoint * ep = NULL; |
va009039 | 12:b91fdea8c0a7 | 129 | uint8_t intf_nb = 0; |
va009039 | 12:b91fdea8c0a7 | 130 | bool parsing_intf = false; |
va009039 | 12:b91fdea8c0a7 | 131 | uint8_t current_intf = 0; |
va009039 | 12:b91fdea8c0a7 | 132 | EndpointDescriptor* ep_desc; |
va009039 | 12:b91fdea8c0a7 | 133 | |
va009039 | 12:b91fdea8c0a7 | 134 | while (index < len) { |
va009039 | 12:b91fdea8c0a7 | 135 | len_desc = conf_descr[index]; |
va009039 | 12:b91fdea8c0a7 | 136 | id = conf_descr[index+1]; |
va009039 | 12:b91fdea8c0a7 | 137 | USB_DBG_HEX(conf_descr+index, len_desc); |
va009039 | 12:b91fdea8c0a7 | 138 | switch (id) { |
va009039 | 12:b91fdea8c0a7 | 139 | case CONFIGURATION_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 140 | USB_DBG("dev: %p has %d intf", dev, conf_descr[4]); |
va009039 | 12:b91fdea8c0a7 | 141 | dev->setNbIntf(conf_descr[4]); |
va009039 | 12:b91fdea8c0a7 | 142 | break; |
va009039 | 12:b91fdea8c0a7 | 143 | case INTERFACE_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 144 | if(pEnumerator->parseInterface(conf_descr[index + 2], conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7])) { |
va009039 | 12:b91fdea8c0a7 | 145 | intf_nb++; |
va009039 | 12:b91fdea8c0a7 | 146 | current_intf = conf_descr[index + 2]; |
va009039 | 12:b91fdea8c0a7 | 147 | dev->addInterface(current_intf, conf_descr[index + 5], conf_descr[index + 6], conf_descr[index + 7]); |
va009039 | 12:b91fdea8c0a7 | 148 | 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 | 149 | parsing_intf = true; |
va009039 | 12:b91fdea8c0a7 | 150 | } else { |
va009039 | 12:b91fdea8c0a7 | 151 | parsing_intf = false; |
va009039 | 12:b91fdea8c0a7 | 152 | } |
va009039 | 12:b91fdea8c0a7 | 153 | break; |
va009039 | 12:b91fdea8c0a7 | 154 | case ENDPOINT_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 155 | ep_desc = reinterpret_cast<EndpointDescriptor*>(conf_descr+index); |
va009039 | 12:b91fdea8c0a7 | 156 | if (parsing_intf && (intf_nb <= MAX_INTF) ) { |
va009039 | 12:b91fdea8c0a7 | 157 | ENDPOINT_TYPE type = (ENDPOINT_TYPE)(ep_desc->bmAttributes & 0x03); |
va009039 | 12:b91fdea8c0a7 | 158 | ENDPOINT_DIRECTION dir = (ep_desc->bEndpointAddress & 0x80) ? IN : OUT; |
va009039 | 12:b91fdea8c0a7 | 159 | if(pEnumerator->useEndpoint(current_intf, type, dir)) { |
va009039 | 12:b91fdea8c0a7 | 160 | ep = new USBEndpoint(dev); |
va009039 | 12:b91fdea8c0a7 | 161 | ep->init(type, dir, ep_desc->wMaxPacketSize, ep_desc->bEndpointAddress); |
va009039 | 12:b91fdea8c0a7 | 162 | USB_DBG("ADD USBEndpoint %p, on interf %d on device %p", ep, current_intf, dev); |
va009039 | 12:b91fdea8c0a7 | 163 | dev->addEndpoint(current_intf, ep); |
va009039 | 12:b91fdea8c0a7 | 164 | } |
va009039 | 12:b91fdea8c0a7 | 165 | } |
va009039 | 12:b91fdea8c0a7 | 166 | break; |
va009039 | 12:b91fdea8c0a7 | 167 | case HID_DESCRIPTOR: |
va009039 | 12:b91fdea8c0a7 | 168 | //lenReportDescr = conf_descr[index + 7] | (conf_descr[index + 8] << 8); |
va009039 | 12:b91fdea8c0a7 | 169 | break; |
va009039 | 12:b91fdea8c0a7 | 170 | default: |
va009039 | 12:b91fdea8c0a7 | 171 | break; |
va009039 | 12:b91fdea8c0a7 | 172 | } |
va009039 | 12:b91fdea8c0a7 | 173 | index += len_desc; |
va009039 | 12:b91fdea8c0a7 | 174 | } |
va009039 | 12:b91fdea8c0a7 | 175 | } |
va009039 | 12:b91fdea8c0a7 | 176 | |
va009039 | 12:b91fdea8c0a7 | 177 | 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 | 12:b91fdea8c0a7 | 178 | SETUP_PACKET setup = {requestType, request, value, index}; |
va009039 | 12:b91fdea8c0a7 | 179 | int result = ControlRead(dev, &setup, buf, len); |
va009039 | 12:b91fdea8c0a7 | 180 | //USB_DBG2("result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 181 | return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 182 | } |
va009039 | 12:b91fdea8c0a7 | 183 | |
va009039 | 12:b91fdea8c0a7 | 184 | 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 | 12:b91fdea8c0a7 | 185 | SETUP_PACKET setup = {requestType, request, value, index}; |
va009039 | 12:b91fdea8c0a7 | 186 | int result = ControlWrite(dev, &setup, buf, len); |
va009039 | 12:b91fdea8c0a7 | 187 | if (result >= 0) { |
va009039 | 12:b91fdea8c0a7 | 188 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 189 | } |
va009039 | 12:b91fdea8c0a7 | 190 | USB_DBG("result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 191 | USB_DBG_HEX(buf, len); |
va009039 | 12:b91fdea8c0a7 | 192 | return USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 193 | } |
va009039 | 12:b91fdea8c0a7 | 194 | |
va009039 | 12:b91fdea8c0a7 | 195 | USB_TYPE USBHost::bulkRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 196 | if (blocking == false) { |
va009039 | 12:b91fdea8c0a7 | 197 | ep->setBuffer(buf, len); |
va009039 | 12:b91fdea8c0a7 | 198 | ep_queue.push(ep); |
va009039 | 12:b91fdea8c0a7 | 199 | return USB_TYPE_PROCESSING; |
va009039 | 12:b91fdea8c0a7 | 200 | } |
va009039 | 12:b91fdea8c0a7 | 201 | int result = bulkReadBLOCK(ep, buf, len, -1); |
va009039 | 12:b91fdea8c0a7 | 202 | if (result >= 0) { |
va009039 | 12:b91fdea8c0a7 | 203 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 204 | } |
va009039 | 12:b91fdea8c0a7 | 205 | //USB_DBG2("result=%d %02x", result, host->LastStatus); |
va009039 | 12:b91fdea8c0a7 | 206 | return USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 207 | } |
va009039 | 12:b91fdea8c0a7 | 208 | |
va009039 | 12:b91fdea8c0a7 | 209 | USB_TYPE USBHost::bulkWrite(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 210 | USB_TEST_ASSERT(blocking); |
va009039 | 12:b91fdea8c0a7 | 211 | int result = bulkWriteNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 212 | if (result >= 0) { |
va009039 | 12:b91fdea8c0a7 | 213 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 214 | } |
va009039 | 12:b91fdea8c0a7 | 215 | USB_DBG2("result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 216 | return USB_TYPE_ERROR; |
va009039 | 12:b91fdea8c0a7 | 217 | } |
va009039 | 12:b91fdea8c0a7 | 218 | |
va009039 | 12:b91fdea8c0a7 | 219 | USB_TYPE USBHost::interruptRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 220 | if (blocking == false) { |
va009039 | 12:b91fdea8c0a7 | 221 | ep->setBuffer(buf, len); |
va009039 | 12:b91fdea8c0a7 | 222 | ep_queue.push(ep); |
va009039 | 12:b91fdea8c0a7 | 223 | return USB_TYPE_PROCESSING; |
va009039 | 12:b91fdea8c0a7 | 224 | } |
va009039 | 12:b91fdea8c0a7 | 225 | interruptReadNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 226 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 227 | } |
va009039 | 12:b91fdea8c0a7 | 228 | |
va009039 | 12:b91fdea8c0a7 | 229 | USB_TYPE USBHost::interruptWrite(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 230 | USB_TEST_ASSERT(blocking); |
va009039 | 12:b91fdea8c0a7 | 231 | interruptWriteNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 232 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 233 | } |
va009039 | 12:b91fdea8c0a7 | 234 | |
va009039 | 12:b91fdea8c0a7 | 235 | USB_TYPE USBHost::isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking) { |
va009039 | 12:b91fdea8c0a7 | 236 | if (blocking == false) { |
va009039 | 12:b91fdea8c0a7 | 237 | ep->setBuffer(buf, len); |
va009039 | 12:b91fdea8c0a7 | 238 | ep_queue.push(ep); |
va009039 | 12:b91fdea8c0a7 | 239 | return USB_TYPE_PROCESSING; |
va009039 | 12:b91fdea8c0a7 | 240 | } |
va009039 | 12:b91fdea8c0a7 | 241 | isochronousReadNB(ep, buf, len); |
va009039 | 12:b91fdea8c0a7 | 242 | return USB_TYPE_OK; |
va009039 | 12:b91fdea8c0a7 | 243 | } |
va009039 | 12:b91fdea8c0a7 | 244 | |
va009039 | 12:b91fdea8c0a7 | 245 | int USBHost::ControlRead(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data, int size) { |
va009039 | 12:b91fdea8c0a7 | 246 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 247 | USBEndpoint* ep = dev->getEpCtl(); |
va009039 | 12:b91fdea8c0a7 | 248 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 249 | setAddr(dev->getAddress(), dev->getSpeed()); |
va009039 | 12:b91fdea8c0a7 | 250 | token_setup(ep, setup, size); // setup stage |
va009039 | 12:b91fdea8c0a7 | 251 | if (LastStatus != ACK) { |
va009039 | 12:b91fdea8c0a7 | 252 | USB_DBG("setup %02x", LastStatus); |
va009039 | 12:b91fdea8c0a7 | 253 | return -1; |
va009039 | 12:b91fdea8c0a7 | 254 | } |
va009039 | 12:b91fdea8c0a7 | 255 | int read_len = 0; |
va009039 | 12:b91fdea8c0a7 | 256 | while(read_len < size) { |
va009039 | 12:b91fdea8c0a7 | 257 | int size2 = std::min(size-read_len, ep->getSize()); |
va009039 | 12:b91fdea8c0a7 | 258 | int result = token_in(ep, data+read_len, size2); |
va009039 | 12:b91fdea8c0a7 | 259 | //USB_DBG("token_in result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 260 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 261 | USB_DBG("token_in %d/%d %02x", read_len, size, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 262 | return result; |
va009039 | 12:b91fdea8c0a7 | 263 | } |
va009039 | 12:b91fdea8c0a7 | 264 | read_len += result; |
va009039 | 12:b91fdea8c0a7 | 265 | if (result < ep->getSize()) { |
va009039 | 12:b91fdea8c0a7 | 266 | break; |
va009039 | 12:b91fdea8c0a7 | 267 | } |
va009039 | 12:b91fdea8c0a7 | 268 | } |
va009039 | 12:b91fdea8c0a7 | 269 | ep->setData01(DATA1); |
va009039 | 12:b91fdea8c0a7 | 270 | int result = token_out(ep); // status stage |
va009039 | 12:b91fdea8c0a7 | 271 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 272 | USB_DBG("status token_out %02x", LastStatus); |
va009039 | 12:b91fdea8c0a7 | 273 | if (LastStatus == STALL) { |
va009039 | 12:b91fdea8c0a7 | 274 | ep->setLengthTransferred(read_len); |
va009039 | 12:b91fdea8c0a7 | 275 | return read_len; |
va009039 | 12:b91fdea8c0a7 | 276 | } |
va009039 | 12:b91fdea8c0a7 | 277 | return result; |
va009039 | 12:b91fdea8c0a7 | 278 | } |
va009039 | 12:b91fdea8c0a7 | 279 | ep->setLengthTransferred(read_len); |
va009039 | 12:b91fdea8c0a7 | 280 | return read_len; |
va009039 | 12:b91fdea8c0a7 | 281 | } |
va009039 | 12:b91fdea8c0a7 | 282 | |
va009039 | 12:b91fdea8c0a7 | 283 | int USBHost::ControlWrite(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data, int size) { |
va009039 | 12:b91fdea8c0a7 | 284 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 285 | USBEndpoint* ep = dev->getEpCtl(); |
va009039 | 12:b91fdea8c0a7 | 286 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 287 | setAddr(dev->getAddress(), dev->getSpeed()); |
va009039 | 12:b91fdea8c0a7 | 288 | token_setup(ep, setup, size); // setup stage |
va009039 | 12:b91fdea8c0a7 | 289 | if (LastStatus != ACK) { |
va009039 | 12:b91fdea8c0a7 | 290 | USB_DBG("setup %02x", LastStatus); |
va009039 | 12:b91fdea8c0a7 | 291 | return -1; |
va009039 | 12:b91fdea8c0a7 | 292 | } |
va009039 | 12:b91fdea8c0a7 | 293 | int write_len = 0; |
va009039 | 12:b91fdea8c0a7 | 294 | if (data != NULL) { |
va009039 | 12:b91fdea8c0a7 | 295 | write_len = token_out(ep, data, size); |
va009039 | 12:b91fdea8c0a7 | 296 | if (write_len < 0) { |
va009039 | 12:b91fdea8c0a7 | 297 | return -1; |
va009039 | 12:b91fdea8c0a7 | 298 | } |
va009039 | 12:b91fdea8c0a7 | 299 | } |
va009039 | 12:b91fdea8c0a7 | 300 | ep->setData01(DATA1); |
va009039 | 12:b91fdea8c0a7 | 301 | int result = token_in(ep); // status stage |
va009039 | 12:b91fdea8c0a7 | 302 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 303 | USB_DBG("result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 304 | //return result; |
va009039 | 12:b91fdea8c0a7 | 305 | } |
va009039 | 12:b91fdea8c0a7 | 306 | ep->setLengthTransferred(write_len); |
va009039 | 12:b91fdea8c0a7 | 307 | return write_len; |
va009039 | 12:b91fdea8c0a7 | 308 | } |
va009039 | 12:b91fdea8c0a7 | 309 | |
va009039 | 12:b91fdea8c0a7 | 310 | int USBHost::interruptReadNB(USBEndpoint* ep, uint8_t* data, int size) |
va009039 | 12:b91fdea8c0a7 | 311 | { |
va009039 | 12:b91fdea8c0a7 | 312 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 313 | USBDeviceConnected* dev = ep->getDevice(); |
va009039 | 12:b91fdea8c0a7 | 314 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 315 | setAddr(dev->getAddress(), dev->getSpeed()); |
va009039 | 12:b91fdea8c0a7 | 316 | setEndpoint(); |
va009039 | 12:b91fdea8c0a7 | 317 | const int retryLimit = 0; |
va009039 | 12:b91fdea8c0a7 | 318 | int read_len = 0; |
va009039 | 12:b91fdea8c0a7 | 319 | for(int n = 0; read_len < size; n++) { |
va009039 | 12:b91fdea8c0a7 | 320 | int size2 = std::min(size-read_len, ep->getSize()); |
va009039 | 12:b91fdea8c0a7 | 321 | int result = token_in(ep, data+read_len, size2, retryLimit); |
va009039 | 12:b91fdea8c0a7 | 322 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 323 | if (LastStatus == NAK) { |
va009039 | 12:b91fdea8c0a7 | 324 | if (n == 0) { |
va009039 | 12:b91fdea8c0a7 | 325 | return -1; |
va009039 | 12:b91fdea8c0a7 | 326 | } |
va009039 | 12:b91fdea8c0a7 | 327 | break; |
va009039 | 12:b91fdea8c0a7 | 328 | } |
va009039 | 12:b91fdea8c0a7 | 329 | //USB_DBG("token_in result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 330 | return result; |
va009039 | 12:b91fdea8c0a7 | 331 | } |
va009039 | 12:b91fdea8c0a7 | 332 | read_len += result; |
va009039 | 12:b91fdea8c0a7 | 333 | if (result < ep->getSize()) { |
va009039 | 12:b91fdea8c0a7 | 334 | break; |
va009039 | 12:b91fdea8c0a7 | 335 | } |
va009039 | 12:b91fdea8c0a7 | 336 | } |
va009039 | 12:b91fdea8c0a7 | 337 | ep->setLengthTransferred(read_len); |
va009039 | 12:b91fdea8c0a7 | 338 | return read_len; |
va009039 | 12:b91fdea8c0a7 | 339 | } |
va009039 | 12:b91fdea8c0a7 | 340 | |
va009039 | 12:b91fdea8c0a7 | 341 | int USBHost::interruptWriteNB(USBEndpoint* ep, const uint8_t* data, int size) |
va009039 | 12:b91fdea8c0a7 | 342 | { |
va009039 | 12:b91fdea8c0a7 | 343 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 344 | USBDeviceConnected* dev = ep->getDevice(); |
va009039 | 12:b91fdea8c0a7 | 345 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 346 | setAddr(dev->getAddress(), dev->getSpeed()); |
va009039 | 12:b91fdea8c0a7 | 347 | setEndpoint(); |
va009039 | 12:b91fdea8c0a7 | 348 | const int retryLimit = 0; |
va009039 | 12:b91fdea8c0a7 | 349 | int transferred_len = 0; |
va009039 | 12:b91fdea8c0a7 | 350 | for(int n = 0; transferred_len < size; n++) { |
va009039 | 12:b91fdea8c0a7 | 351 | int size2 = std::min(size-transferred_len, ep->getSize()); |
va009039 | 12:b91fdea8c0a7 | 352 | int result = token_out(ep, data+transferred_len, size2, retryLimit); |
va009039 | 12:b91fdea8c0a7 | 353 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 354 | if (LastStatus == NAK) { |
va009039 | 12:b91fdea8c0a7 | 355 | if (n == 0) { |
va009039 | 12:b91fdea8c0a7 | 356 | return -1; |
va009039 | 12:b91fdea8c0a7 | 357 | } |
va009039 | 12:b91fdea8c0a7 | 358 | break; |
va009039 | 12:b91fdea8c0a7 | 359 | } |
va009039 | 12:b91fdea8c0a7 | 360 | //USB_DBG("token_in result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 361 | return result; |
va009039 | 12:b91fdea8c0a7 | 362 | } |
va009039 | 12:b91fdea8c0a7 | 363 | transferred_len += result; |
va009039 | 12:b91fdea8c0a7 | 364 | if (result < ep->getSize()) { |
va009039 | 12:b91fdea8c0a7 | 365 | break; |
va009039 | 12:b91fdea8c0a7 | 366 | } |
va009039 | 12:b91fdea8c0a7 | 367 | } |
va009039 | 12:b91fdea8c0a7 | 368 | ep->setLengthTransferred(transferred_len); |
va009039 | 12:b91fdea8c0a7 | 369 | return transferred_len; |
va009039 | 12:b91fdea8c0a7 | 370 | } |
va009039 | 12:b91fdea8c0a7 | 371 | |
va009039 | 12:b91fdea8c0a7 | 372 | int USBHost::bulkReadNB(USBEndpoint* ep, uint8_t* data, int size) |
va009039 | 12:b91fdea8c0a7 | 373 | { |
va009039 | 12:b91fdea8c0a7 | 374 | return bulkReadBLOCK(ep, data, size, 0); |
va009039 | 12:b91fdea8c0a7 | 375 | } |
va009039 | 12:b91fdea8c0a7 | 376 | |
va009039 | 12:b91fdea8c0a7 | 377 | int USBHost::bulkReadBLOCK(USBEndpoint* ep, uint8_t* data, int size, int timeout_ms) { |
va009039 | 12:b91fdea8c0a7 | 378 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 379 | USBDeviceConnected* dev = ep->getDevice(); |
va009039 | 12:b91fdea8c0a7 | 380 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 381 | setAddr(dev->getAddress()); |
va009039 | 12:b91fdea8c0a7 | 382 | setEndpoint(); |
va009039 | 12:b91fdea8c0a7 | 383 | int retryLimit = (timeout_ms == 0) ? 0 : 10; |
va009039 | 12:b91fdea8c0a7 | 384 | int read_len = 0; |
va009039 | 12:b91fdea8c0a7 | 385 | Timer t; |
va009039 | 12:b91fdea8c0a7 | 386 | for(int n = 0; read_len < size; n++) { |
va009039 | 12:b91fdea8c0a7 | 387 | int size2 = std::min(size-read_len, ep->getSize()); |
va009039 | 12:b91fdea8c0a7 | 388 | int result = token_in(ep, data+read_len, size2, retryLimit); |
va009039 | 12:b91fdea8c0a7 | 389 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 390 | if (LastStatus == NAK) { |
va009039 | 12:b91fdea8c0a7 | 391 | if (n == 0) { |
va009039 | 12:b91fdea8c0a7 | 392 | return -1; |
va009039 | 12:b91fdea8c0a7 | 393 | } |
va009039 | 12:b91fdea8c0a7 | 394 | break; |
va009039 | 12:b91fdea8c0a7 | 395 | } |
va009039 | 12:b91fdea8c0a7 | 396 | //USB_DBG("token_in result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 397 | return result; |
va009039 | 12:b91fdea8c0a7 | 398 | } |
va009039 | 12:b91fdea8c0a7 | 399 | read_len += result; |
va009039 | 12:b91fdea8c0a7 | 400 | if (result < ep->getSize()) { |
va009039 | 12:b91fdea8c0a7 | 401 | break; |
va009039 | 12:b91fdea8c0a7 | 402 | } |
va009039 | 12:b91fdea8c0a7 | 403 | if (timeout_ms > 0 && t.read_ms() > timeout_ms) { |
va009039 | 12:b91fdea8c0a7 | 404 | USB_DBG("timeout_ms: %d", timeout_ms); |
va009039 | 12:b91fdea8c0a7 | 405 | break; |
va009039 | 12:b91fdea8c0a7 | 406 | } |
va009039 | 12:b91fdea8c0a7 | 407 | } |
va009039 | 12:b91fdea8c0a7 | 408 | ep->setLengthTransferred(read_len); |
va009039 | 12:b91fdea8c0a7 | 409 | return read_len; |
va009039 | 12:b91fdea8c0a7 | 410 | } |
va009039 | 12:b91fdea8c0a7 | 411 | |
va009039 | 12:b91fdea8c0a7 | 412 | int USBHost::bulkWriteNB(USBEndpoint* ep, const uint8_t* data, int size) { |
va009039 | 12:b91fdea8c0a7 | 413 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 414 | USBDeviceConnected* dev = ep->getDevice(); |
va009039 | 12:b91fdea8c0a7 | 415 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 416 | setAddr(dev->getAddress()); |
va009039 | 12:b91fdea8c0a7 | 417 | setEndpoint(); |
va009039 | 12:b91fdea8c0a7 | 418 | int write_len = 0; |
va009039 | 12:b91fdea8c0a7 | 419 | for(int n = 0; write_len < size; n++) { |
va009039 | 12:b91fdea8c0a7 | 420 | int size2 = std::min(size-write_len, ep->getSize()); |
va009039 | 12:b91fdea8c0a7 | 421 | int result = token_out(ep, data+write_len, size2); |
va009039 | 12:b91fdea8c0a7 | 422 | if (result < 0) { |
va009039 | 12:b91fdea8c0a7 | 423 | if (LastStatus == NAK) { |
va009039 | 12:b91fdea8c0a7 | 424 | if (n == 0) { |
va009039 | 12:b91fdea8c0a7 | 425 | return -1; |
va009039 | 12:b91fdea8c0a7 | 426 | } |
va009039 | 12:b91fdea8c0a7 | 427 | break; |
va009039 | 12:b91fdea8c0a7 | 428 | } |
va009039 | 12:b91fdea8c0a7 | 429 | USB_DBG("token_out result=%d %02x", result, LastStatus); |
va009039 | 12:b91fdea8c0a7 | 430 | return result; |
va009039 | 12:b91fdea8c0a7 | 431 | } |
va009039 | 12:b91fdea8c0a7 | 432 | write_len += result; |
va009039 | 12:b91fdea8c0a7 | 433 | if (result < ep->getSize()) { |
va009039 | 12:b91fdea8c0a7 | 434 | break; |
va009039 | 12:b91fdea8c0a7 | 435 | } |
va009039 | 12:b91fdea8c0a7 | 436 | } |
va009039 | 12:b91fdea8c0a7 | 437 | ep->setLengthTransferred(write_len); |
va009039 | 12:b91fdea8c0a7 | 438 | return write_len; |
va009039 | 12:b91fdea8c0a7 | 439 | } |
va009039 | 12:b91fdea8c0a7 | 440 | |
va009039 | 12:b91fdea8c0a7 | 441 | int USBHost::isochronousReadNB(USBEndpoint* ep, uint8_t* data, int size) { |
va009039 | 12:b91fdea8c0a7 | 442 | USBDeviceConnected* dev = ep->getDevice(); |
va009039 | 12:b91fdea8c0a7 | 443 | USB_TEST_ASSERT(dev); |
va009039 | 12:b91fdea8c0a7 | 444 | setAddr(dev->getAddress()); |
va009039 | 12:b91fdea8c0a7 | 445 | int result = token_iso_in(ep, data, size); |
va009039 | 12:b91fdea8c0a7 | 446 | if (result >= 0) { |
va009039 | 12:b91fdea8c0a7 | 447 | ep->setLengthTransferred(result); |
va009039 | 12:b91fdea8c0a7 | 448 | } |
va009039 | 12:b91fdea8c0a7 | 449 | return result; |
va009039 | 12:b91fdea8c0a7 | 450 | } |
va009039 | 12:b91fdea8c0a7 | 451 | |
va009039 | 12:b91fdea8c0a7 | 452 | void USBHost::task() |
va009039 | 12:b91fdea8c0a7 | 453 | { |
va009039 | 12:b91fdea8c0a7 | 454 | if (ep_queue.empty()) { |
va009039 | 12:b91fdea8c0a7 | 455 | return; |
va009039 | 12:b91fdea8c0a7 | 456 | } |
va009039 | 12:b91fdea8c0a7 | 457 | USBEndpoint* ep = ep_queue.pop(); |
va009039 | 12:b91fdea8c0a7 | 458 | USB_TEST_ASSERT(ep); |
va009039 | 12:b91fdea8c0a7 | 459 | ep->setLengthTransferred(0); |
va009039 | 12:b91fdea8c0a7 | 460 | switch(ep->getType()) { |
va009039 | 12:b91fdea8c0a7 | 461 | case INTERRUPT_ENDPOINT: |
va009039 | 12:b91fdea8c0a7 | 462 | if (ep->getDir() == IN) { |
va009039 | 12:b91fdea8c0a7 | 463 | interruptReadNB(ep, ep->getBufStart(), ep->getBufSize()); |
va009039 | 12:b91fdea8c0a7 | 464 | } |
va009039 | 12:b91fdea8c0a7 | 465 | break; |
va009039 | 12:b91fdea8c0a7 | 466 | case BULK_ENDPOINT: |
va009039 | 12:b91fdea8c0a7 | 467 | if (ep->getDir() == IN) { |
va009039 | 12:b91fdea8c0a7 | 468 | bulkReadNB(ep, ep->getBufStart(), ep->getBufSize()); |
va009039 | 12:b91fdea8c0a7 | 469 | } |
va009039 | 12:b91fdea8c0a7 | 470 | break; |
va009039 | 12:b91fdea8c0a7 | 471 | case ISOCHRONOUS_ENDPOINT: |
va009039 | 12:b91fdea8c0a7 | 472 | if (ep->getDir() == IN) { |
va009039 | 12:b91fdea8c0a7 | 473 | isochronousReadNB(ep, ep->getBufStart(), ep->getBufSize()); |
va009039 | 12:b91fdea8c0a7 | 474 | } |
va009039 | 12:b91fdea8c0a7 | 475 | break; |
va009039 | 12:b91fdea8c0a7 | 476 | } |
va009039 | 12:b91fdea8c0a7 | 477 | ep->call(); |
va009039 | 12:b91fdea8c0a7 | 478 | } |