Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 19:57:37 2014 +0000
Revision:
2:2f9019c5a9fc
Parent:
0:65004368569c
ip switch

Who changed what in which revision?

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