Added TARGET_DISCO_F469NI in USBHOST\USBHost\TARGET_STM\USBHALHost_STM_TARGET.h
Dependents: DISCO-F469NI_USB_Disk STM32F4xx_USB_Memory
Fork of USBHOST by
Diff: USBHost/USBHost.cpp
- Revision:
- 8:3e7a33f81048
- Parent:
- 5:fc157e6bd5a5
diff -r 7ebd364a6be5 -r 3e7a33f81048 USBHost/USBHost.cpp --- a/USBHost/USBHost.cpp Mon Apr 30 05:37:24 2018 +0000 +++ b/USBHost/USBHost.cpp Sat Jan 04 23:30:59 2020 +0000 @@ -88,14 +88,21 @@ /* check that hub is connected to root port */ if (usb_msg->hub_parent) { /* a hub device must be present */ +#if MAX_HUB_NB + for (k = 0; k < MAX_HUB_NB; k++) { - if ((&hubs[k] == usb_msg->hub_parent) && (hub_in_use[k])) + if ((&hubs[k] == usb_msg->hub_parent) && (hub_in_use[k])) { hub_unplugged=false; + } } - } else hub_unplugged = false; +#endif + } else { + hub_unplugged = false; + } - if (((idx!=-1) && deviceInUse[idx] ) || ((idx == -1) && hub_unplugged)) + if (((idx!=-1) && deviceInUse[idx] ) || ((idx == -1) && hub_unplugged)) { break; + } for (i =0 ; i < MAX_DEVICE_CONNECTED; i++) { if (!deviceInUse[i]) { @@ -119,8 +126,9 @@ } #if MAX_HUB_NB - if (usb_msg->hub_parent) + if (usb_msg->hub_parent) { devices[i].setHubParent((USBHostHub *)(usb_msg->hub_parent)); + } #endif for (j = 0; j < timeout_set_addr; j++) { @@ -155,6 +163,13 @@ devices[i].activeAddress(true); USB_DBG("Address of %p: %d", &devices[i], devices[i].getAddress()); + // Wait for the device to actually set the address. The Status stage + // of SET ADDRESS happens before the device implements the request. + // According to Universal Serial Bus Specification Revision 2.0 chapter + // 9.2.6.3 Set Address Processing, the device is allowed SetAddress() + // recovery interval of 2 ms. + ThisThread::sleep_for(2); + // try to read again the device descriptor to check if the device // answers to its new address res = getDeviceDescriptor(&devices[i], buf, 8); @@ -163,7 +178,7 @@ break; } - Thread::wait(100); + ThisThread::sleep_for(100); } USB_INFO("New device connected: %p [hub: %d - port: %d]", &devices[i], usb_msg->hub, usb_msg->port); @@ -179,8 +194,9 @@ break; } } - if (hub_in_use[k] == true) + if (hub_in_use[k] == true) { break; + } } } @@ -190,8 +206,9 @@ } } - if (usb_msg->hub_parent) + if (usb_msg->hub_parent) { ((USBHostHub *)(usb_msg->hub_parent))->deviceConnected(&devices[i]); + } #endif if ((i < MAX_DEVICE_CONNECTED) && !too_many_hub) { @@ -218,9 +235,15 @@ deviceInited[idx]=false; } - if (controlListState) enableList(CONTROL_ENDPOINT); - if (bulkListState) enableList(BULK_ENDPOINT); - if (interruptListState) enableList(INTERRUPT_ENDPOINT); + if (controlListState) { + enableList(CONTROL_ENDPOINT); + } + if (bulkListState) { + enableList(BULK_ENDPOINT); + } + if (interruptListState) { + enableList(INTERRUPT_ENDPOINT); + } } while(0); @@ -238,8 +261,9 @@ if (ep->getDir() == IN) { buf_transfer = ep->getBufStart(); printf("READ SUCCESS [%d bytes transferred - td: 0x%08X] on ep: [%p - addr: %02X]: ", ep->getLengthTransferred(), usb_msg->td_addr, ep, ep->getAddress()); - for (int i = 0; i < ep->getLengthTransferred(); i++) + for (int i = 0; i < ep->getLengthTransferred(); i++) { printf("%02X ", buf_transfer[i]); + } printf("\r\n\r\n"); } #endif @@ -255,7 +279,7 @@ * if device still in use, this call back * shall ask again an interrupt request. */ - ep->call(); + ep->call(); } } } @@ -286,8 +310,9 @@ devices[i].setAddress(i + 1); deviceReset[i] = false; deviceInited[i] = false; - for (uint8_t j = 0; j < MAX_INTF; j++) + for (uint8_t j = 0; j < MAX_INTF; j++) { deviceAttachedDriver[i][j] = false; + } } #if MAX_HUB_NB @@ -297,7 +322,7 @@ } #endif - usbThread.start(this, &USBHost::usb_process); + usbThread.start(callback(this, &USBHost::usb_process)); } USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost) @@ -314,8 +339,9 @@ { uint8_t state; - if(addr == 0) + if(addr == 0) { return; + } volatile HCTD* tdList = NULL; @@ -335,20 +361,23 @@ #ifdef USBHOST_OTHER state = ((HCTD *)td)->state; - if (state == USB_TYPE_IDLE) + if (state == USB_TYPE_IDLE) { ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); + } #else if (((HCTD *)td)->control >> 28) { state = ((HCTD *)td)->control >> 28; } else { - if (td->currBufPtr) + if (td->currBufPtr) { ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); + } state = 16 /*USB_TYPE_IDLE*/; } #endif - if (state == USB_TYPE_IDLE) + if (state == USB_TYPE_IDLE) { ep->setLengthTransferred((uint32_t)td->currBufPtr - (uint32_t)ep->getBufStart()); + } ep->unqueueTransfer(td); @@ -383,16 +412,13 @@ /* virtual */ void USBHost::deviceConnected(int hub, int port, bool lowSpeed, USBHostHub * hub_parent) { // be sure that the new device connected is not already connected... - disableList(CONTROL_ENDPOINT); - int idx = findDevice(hub, port, hub_parent); if (idx != -1) { if (deviceInited[idx]) { - enableList(CONTROL_ENDPOINT); - return; } } + message_t * usb_msg = mail_usb_event.alloc(); usb_msg->event_id = DEVICE_CONNECTED_EVENT; usb_msg->hub = hub; @@ -400,9 +426,6 @@ usb_msg->lowSpeed = lowSpeed; usb_msg->hub_parent = hub_parent; mail_usb_event.put(usb_msg); - enableList(CONTROL_ENDPOINT); - - } /* @@ -412,17 +435,12 @@ /* virtual */ void USBHost::deviceDisconnected(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr) { // be sure that the device disconnected is connected... - - disableList(CONTROL_ENDPOINT); - int idx = findDevice(hub, port, hub_parent); if (idx != -1) { if (!deviceInUse[idx]) { - enableList(CONTROL_ENDPOINT); return; } } else { - enableList(CONTROL_ENDPOINT); return; } @@ -432,9 +450,6 @@ usb_msg->port = port; usb_msg->hub_parent = hub_parent; mail_usb_event.put(usb_msg); - enableList(CONTROL_ENDPOINT); - - } void USBHost::freeDevice(USBDeviceConnected * dev) @@ -458,8 +473,9 @@ } // notify hub parent that this device has been disconnected - if (dev->getHubParent()) + if (dev->getHubParent()) { dev->getHubParent()->deviceDisconnected(dev); + } #endif @@ -590,7 +606,7 @@ int index = findDevice(dev); if (index != -1) { USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort()); - Thread::wait(100); + ThisThread::sleep_for(100); if (dev->getHub() == 0) { resetRootHub(); } @@ -599,7 +615,7 @@ dev->getHubParent()->portReset(dev->getPort()); } #endif - Thread::wait(100); + ThisThread::sleep_for(100); deviceReset[index] = true; return USB_TYPE_OK; } @@ -703,8 +719,9 @@ for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { if (devices[i].getHub() == hub && devices[i].getPort() == port) { if (hub_parent != NULL) { - if (hub_parent == devices[i].getHubParent()) + if (hub_parent == devices[i].getHubParent()) { return i; + } } else { return i; } @@ -810,8 +827,9 @@ GET_DESCRIPTOR, (DEVICE_DESCRIPTOR << 8) | (0), 0, buf, MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf)); - if (len_dev_descr) + if (len_dev_descr) { *len_dev_descr = MIN(DEVICE_DESCRIPTOR_LENGTH, max_len_buf); + } return t; } @@ -835,8 +853,9 @@ total_conf_descr_length = buf[2] | (buf[3] << 8); total_conf_descr_length = MIN(max_len_buf, total_conf_descr_length); - if (len_conf_descr) + if (len_conf_descr) { *len_conf_descr = total_conf_descr_length; + } USB_DBG("TOTAL_LENGTH: %d \t NUM_INTERF: %d", total_conf_descr_length, buf[4]); @@ -871,11 +890,13 @@ { int index = findDevice(dev); uint8_t cnt = 0; - if (index == -1) + if (index == -1) { return 0; + } for (uint8_t i = 0; i < MAX_INTF; i++) { - if (deviceAttachedDriver[index][i]) + if (deviceAttachedDriver[index][i]) { cnt++; + } } return cnt; } @@ -930,8 +951,9 @@ #if (DEBUG > 3) USB_DBG("CONFIGURATION DESCRIPTOR:\r\n"); - for (int i = 0; i < total_conf_descr_length; i++) + for (int i = 0; i < total_conf_descr_length; i++) { printf("%02X ", data[i]); + } printf("\r\n\r\n"); #endif @@ -959,7 +981,7 @@ } while(0); // Some devices may require this delay - Thread::wait(100); + ThisThread::sleep_for(100); return USB_TYPE_OK; } @@ -1016,7 +1038,11 @@ } nb_endpoints_used++; } else { +#if 0 USB_DBG("ISO USBEndpoint NOT SUPPORTED"); +#else + USB_DBG("???????????????????????????????????????????????????"); +#endif } } } @@ -1094,8 +1120,9 @@ #if DEBUG_TRANSFER if (write) { USB_DBG_TRANSFER("%s WRITE buffer", type_str); - for (int i = 0; i < ep->getLengthTransferred(); i++) + for (int i = 0; i < ep->getLengthTransferred(); i++) { printf("%02X ", buf[i]); + } printf("\r\n\r\n"); } #endif @@ -1104,8 +1131,7 @@ if ((blocking)&& (res == USB_TYPE_PROCESSING)) { #ifdef USBHOST_OTHER osEvent event = ep->ep_queue.get(TD_TIMEOUT); - if (event.status == osEventTimeout) - { + if (event.status == osEventTimeout) { /* control endpoint is confusing for merge on b */ disableList(CONTROL_ENDPOINT); ep->setState(USB_TYPE_ERROR); @@ -1164,8 +1190,9 @@ #if DEBUG_TRANSFER USB_DBG_TRANSFER("SETUP PACKET: "); - for (int i = 0; i < 8; i++) + for (int i = 0; i < 8; i++) { printf("%01X ", setupPacket[i]); + } printf("\r\n"); #endif @@ -1174,7 +1201,8 @@ if (res == USB_TYPE_PROCESSING) #ifdef USBHOST_OTHER - { osEvent event = control->ep_queue.get(TD_TIMEOUT_CTRL); + { + osEvent event = control->ep_queue.get(TD_TIMEOUT_CTRL); if (event.status == osEventTimeout) { disableList(CONTROL_ENDPOINT); control->setState(USB_TYPE_ERROR); @@ -1201,9 +1229,9 @@ if (res == USB_TYPE_PROCESSING) #ifdef USBHOST_OTHER - { osEvent event = control->ep_queue.get(TD_TIMEOUT_CTRL); - if (event.status == osEventTimeout) - { + { + osEvent event = control->ep_queue.get(TD_TIMEOUT_CTRL); + if (event.status == osEventTimeout) { disableList(CONTROL_ENDPOINT); control->setState(USB_TYPE_ERROR); control->ep_queue.get(0); @@ -1212,7 +1240,7 @@ } } #else - control->ep_queue.get(); + control->ep_queue.get(); #endif res = control->getState(); @@ -1220,13 +1248,15 @@ USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString()); if (write) { USB_DBG_TRANSFER("CONTROL WRITE buffer"); - for (int i = 0; i < control->getLengthTransferred(); i++) + for (int i = 0; i < control->getLengthTransferred(); i++) { printf("%02X ", buf[i]); + } printf("\r\n\r\n"); } else { USB_DBG_TRANSFER("CONTROL READ SUCCESS [%d bytes transferred]", control->getLengthTransferred()); - for (int i = 0; i < control->getLengthTransferred(); i++) + for (int i = 0; i < control->getLengthTransferred(); i++) { printf("%02X ", buf[i]); + } printf("\r\n\r\n"); } #endif @@ -1243,8 +1273,7 @@ #ifdef USBHOST_OTHER { osEvent event = control->ep_queue.get(TD_TIMEOUT_CTRL); - if (event.status == osEventTimeout) - { + if (event.status == osEventTimeout) { disableList(CONTROL_ENDPOINT); control->setState(USB_TYPE_ERROR); control->ep_queue.get(0); @@ -1259,8 +1288,9 @@ USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString()); - if (res != USB_TYPE_IDLE) + if (res != USB_TYPE_IDLE) { return res; + } return USB_TYPE_OK; }