USBHost library. NOTE: This library is only officially supported on the LPC1768 platform. For more information, please see the handbook page.

Dependencies:   FATFileSystem mbed-rtos

Dependents:   BTstack WallbotWii SD to Flash Data Transfer USBHost-MSD_HelloWorld ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Thu Mar 14 14:20:03 2013 +0000
Parent:
8:93da8ea2708b
Child:
10:0c11cf1cc218
Commit message:
use queue to wait end of transfer - use mailbox

Changed in this revision

USBHost/USBEndpoint.h Show annotated file Show diff for this revision Revisions of this file
USBHost/USBHost.cpp Show annotated file Show diff for this revision Revisions of this file
USBHost/USBHost.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBHost/USBEndpoint.h	Thu Mar 14 11:49:53 2013 +0000
+++ b/USBHost/USBEndpoint.h	Thu Mar 14 14:20:03 2013 +0000
@@ -19,10 +19,11 @@
 
 #include "FunctionPointer.h"
 #include "USBHostTypes.h"
+#include "rtos.h"
 
 class USBDeviceConnected;
 
-/** 
+/**
 * USBEndpoint class
 */
 class USBEndpoint
@@ -137,6 +138,8 @@
     inline uint8_t              getIntfNb() { return intf_nb; };
     
     USBDeviceConnected * dev;
+    
+    Queue<uint8_t, 1> ep_queue;
 
 private:
     ENDPOINT_TYPE type;
--- a/USBHost/USBHost.cpp	Thu Mar 14 11:49:53 2013 +0000
+++ b/USBHost/USBHost.cpp	Thu Mar 14 14:20:03 2013 +0000
@@ -68,9 +68,9 @@
 #endif
     
     while(1) {
-        osEvent evt = queue_usb_event.get();
+        osEvent evt = mail_usb_event.get();
         
-        if (evt.status == osEventMessage) {
+        if (evt.status == osEventMail) {
             
             l4 = !l4;
             message_t * usb_msg = (message_t*)evt.value.p;
@@ -96,6 +96,7 @@
                     
                     if (i == MAX_DEVICE_CONNECTED) {
                         USB_ERR("Too many device connected!!\r\n");
+                        deviceInited[i] = false;
                         usb_mutex.unlock();
                         continue;
                     }
@@ -242,7 +243,7 @@
                     break;
             }
             
-            mpool_usb_event.free(usb_msg);
+            mail_usb_event.free(usb_msg);
         }
     }
 }
@@ -317,13 +318,14 @@
             
             if (ep->getType() != CONTROL_ENDPOINT) {
                 // callback on the processed td will be called from the usb_thread (not in ISR)
-                message_t * usb_msg = mpool_usb_event.alloc();
+                message_t * usb_msg = mail_usb_event.alloc();
                 usb_msg->event_id = TD_PROCESSED_EVENT;
                 usb_msg->td_addr = (void *)td;
                 usb_msg->td_state = state;
-                queue_usb_event.put(usb_msg);
+                mail_usb_event.put(usb_msg);
             }
             ep->setState(state);
+            ep->ep_queue.put((uint8_t*)1);
         }
     }
 }
@@ -351,13 +353,13 @@
             return;
     }
     
-    message_t * usb_msg = mpool_usb_event.alloc();
+    message_t * usb_msg = mail_usb_event.alloc();
     usb_msg->event_id = DEVICE_CONNECTED_EVENT;
     usb_msg->hub = hub;
     usb_msg->port = port;
     usb_msg->lowSpeed = lowSpeed;
     usb_msg->hub_parent = hub_parent;
-    queue_usb_event.put(usb_msg);
+    mail_usb_event.put(usb_msg);
 }
 
 /*
@@ -375,12 +377,12 @@
         return;
     }
     
-    message_t * usb_msg = mpool_usb_event.alloc();
+    message_t * usb_msg = mail_usb_event.alloc();
     usb_msg->event_id = DEVICE_DISCONNECTED_EVENT;
     usb_msg->hub = hub;
     usb_msg->port = port;
     usb_msg->hub_parent = hub_parent;
-    queue_usb_event.put(usb_msg);
+    mail_usb_event.put(usb_msg);
 }
 
 void USBHost::freeDevice(USBDeviceConnected * dev)
@@ -528,7 +530,7 @@
     int index = findDevice(dev);
     if (index != -1) {
         USB_DBG("Resetting hub %d, port %d\n", dev->getHub(), dev->getPort());
-        wait_ms(100);
+        Thread::wait(100);
         if (dev->getHub() == 0) {
             resetRootHub();
         }
@@ -537,7 +539,7 @@
             dev->getHubParent()->portReset(dev->getPort());
         }
 #endif
-        wait_ms(100);
+        Thread::wait(100);
         deviceReset[index] = true;
         return USB_TYPE_OK;
     }
@@ -689,6 +691,7 @@
 // add a transfer on the TD linked list
 USB_TYPE USBHost::addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len)
 {
+    td_mutex.lock();
 
     // allocate a TD which will be freed in TDcompletion
     volatile HCTD * td = ed->getNextTD();
@@ -720,6 +723,8 @@
     ed->queueTransfer();
     printList(type);
     enableList(type);
+    
+    td_mutex.unlock();
 
     return USB_TYPE_PROCESSING;
 }
@@ -887,7 +892,6 @@
 
     return USB_TYPE_OK;
 }
-
 // this method fills the USBDeviceConnected object: class,.... . It also add endpoints found in the descriptor.
 void USBHost::parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator)
 {
@@ -985,11 +989,11 @@
     USB_DBG_TRANSFER("----- %s %s [dev: %p - %s - hub: %d - port: %d - addr: %d - ep: %02X]------", type_str, (write) ? "WRITE" : "READ", dev, dev->getName(ep->getIntfNb()), dev->getHub(), dev->getPort(), dev->getAddress(), ep->getAddress());
 #endif
     
+    usb_mutex.lock();
+    
     USB_TYPE res;
     ENDPOINT_DIRECTION dir = (write) ? OUT : IN;
     
-    usb_mutex.lock();
-    
     if (dev == NULL) {
         USB_ERR("dev NULL");
         usb_mutex.unlock();
@@ -1032,7 +1036,8 @@
 
     if (blocking) {
         
-        while((res = ep->getState()) == USB_TYPE_PROCESSING);
+        ep->ep_queue.get();
+        res = ep->getState();
         
         USB_DBG_TRANSFER("%s TRANSFER res: %s on ep: %p\r\n", type_str, ep->getStateString(), ep);
         
@@ -1089,7 +1094,8 @@
     control->setNextToken(TD_SETUP);
     addTransfer(control, (uint8_t*)setupPacket, 8);
 
-    while((res = control->getState()) == USB_TYPE_PROCESSING);
+    control->ep_queue.get();
+    res = control->getState();
 
     USB_DBG_TRANSFER("CONTROL setup stage %s", control->getStateString());
 
@@ -1103,7 +1109,8 @@
         control->setNextToken(token);
         addTransfer(control, (uint8_t *)buf, length_transfer);
 
-        while((res = control->getState()) == USB_TYPE_PROCESSING);
+        control->ep_queue.get();
+        res = control->getState();
 
 #if DEBUG_TRANSFER
         USB_DBG_TRANSFER("CONTROL %s stage %s", (write) ? "WRITE" : "READ", control->getStateString());
@@ -1130,7 +1137,8 @@
     control->setNextToken(token);
     addTransfer(control, NULL, 0);
 
-    while((res = control->getState()) == USB_TYPE_PROCESSING);
+    control->ep_queue.get();
+    res = control->getState();
 
     USB_DBG_TRANSFER("CONTROL ack stage %s", control->getStateString());
     usb_mutex.unlock();
--- a/USBHost/USBHost.h	Thu Mar 14 11:49:53 2013 +0000
+++ b/USBHost/USBHost.h	Thu Mar 14 14:20:03 2013 +0000
@@ -133,7 +133,7 @@
     * @param dev device which will be resetted
     */
     USB_TYPE resetDevice(USBDeviceConnected * dev);
-    
+
     /**
     * Get a device
     *
@@ -197,7 +197,7 @@
     * @param addr list of the TDs which have been completed
     */
     virtual void transferCompleted(volatile uint32_t addr);
-    
+
     /**
     * Virtual method called when a device has been connected
     *
@@ -267,9 +267,9 @@
     Thread usbThread;
     void usb_process();
     static void usb_process_static(void const * arg);
-    MemoryPool<message_t, 10> mpool_usb_event;
-    Queue<message_t, 10> queue_usb_event;
+    Mail<message_t, 10> mail_usb_event;
     Mutex usb_mutex;
+    Mutex td_mutex;
     
     // buffer for conf descriptor
     uint8_t data[300];
@@ -349,7 +349,7 @@
     * @param dev device to be freed
     */
     void freeDevice(USBDeviceConnected * dev);
-    
+
     USB_TYPE controlTransfer(   USBDeviceConnected * dev,
                                 uint8_t requestType,
                                 uint8_t request,