USB host library, support isochronous,bulk,interrupt and control.

Dependents:   BaseUsbHost_example BaseJpegDecode_example SimpleJpegDecode_example

Import programBaseUsbHost_example

BaseUsbHost example program

Revision:
3:ae77d63a1eda
Parent:
2:fe1e62051d88
Child:
4:d931d24c2f81
--- a/BaseUsbHostHub.cpp	Tue Dec 11 15:26:54 2012 +0000
+++ b/BaseUsbHostHub.cpp	Sun Jan 06 11:45:18 2013 +0000
@@ -1,4 +1,4 @@
-// BaseUsbHostHub.cpp 2012/12/11
+// BaseUsbHostHub.cpp 2013/1/6
 #include "mbed.h"
 #include "rtos.h"
 #include "BaseUsbHost.h"
@@ -7,26 +7,22 @@
 #define TEST
 #include "BaseUsbHostTest.h"
 
-#define PORT_CONNECTION 0
-#define PORT_ENABLE  1
-#define PORT_SUSPEND  2
+#define PORT_CONNECTION   0
+#define PORT_ENABLE       1
+#define PORT_SUSPEND      2
 #define PORT_OVER_CURRENT 3
-#define PORT_RESET 4
-#define PORT_POWER 8
-#define PORT_LOW_SPEED 9
+#define PORT_RESET        4
+#define PORT_POWER        8
+#define PORT_LOW_SPEED    9
 
-#define C_PORT_CONNECTION 16
-#define C_PORT_ENABLE 17
-#define C_PORT_SUSPEND 18
+#define C_PORT_CONNECTION   16
+#define C_PORT_ENABLE       17
+#define C_PORT_SUSPEND      18
 #define C_PORT_OVER_CURRENT 19
-#define C_PORT_RESET 20
+#define C_PORT_RESET        20
 
 UsbHub::UsbHub(ControlEp* ctlEp)
 {
-    for(int i = 0; i < MAX_HUB_PORT; i++) {
-        PortEp[i] = NULL;
-    }
-    
     if (ctlEp == NULL) {
         DBG_OHCI(LPC_USB->HcRhPortStatus1);
         int lowSpeed = 0;
@@ -39,7 +35,11 @@
         m_ctlEp = ctlEp;
     }
     uint8_t desc[9];
-    m_ctlEp->GetDescriptor(1, 0, desc, sizeof(desc));
+    int rc = m_ctlEp->GetDescriptor(1, 0, desc, sizeof(desc));
+    TEST_ASSERT(rc == USB_OK);
+    if (rc != USB_OK) {
+        return;
+    }
     DBG_HEX(desc, sizeof(desc));
     TEST_ASSERT_TRUE(desc[0] == 0x12);
     TEST_ASSERT_TRUE(desc[1] == 0x01);
@@ -47,44 +47,65 @@
     if (desc[4] != 0x09) { // hub ?
         return;
     }
+    CTASSERT(sizeof(HubDescriptor) == 9);
+    TEST_ASSERT(sizeof(HubDescriptor) == 9);
+    HubDescriptor hubdesc;
+    rc = m_ctlEp->controlReceive(0xa0, 6, 0x29<<8, 0, reinterpret_cast<uint8_t*>(&hubdesc), sizeof(HubDescriptor));
+    TEST_ASSERT(rc == USB_OK);
+    if (rc != USB_OK) {
+        return;
+    }
+    TEST_ASSERT(hubdesc.bDescLength == 9); // length
+    TEST_ASSERT(hubdesc.bDescriptorType == 0x29); // hub
+    TEST_ASSERT(hubdesc.bNbrPorts >= 1);
+    TEST_ASSERT(hubdesc.bNbrPorts <= 7);
 
-    m_ctlEp->controlReceive(0xa0, 6, 0x29<<8, 0, desc, sizeof(desc));
-    DBG_HEX(desc, sizeof(desc));
-    TEST_ASSERT_TRUE(desc[0] == 9); // length
-    TEST_ASSERT_TRUE(desc[1] == 0x29); // hub
-    int bNbrPorts = desc[2];
-    TEST_ASSERT_TRUE(bNbrPorts <= MAX_HUB_PORT);
     m_ctlEp->SetConfiguration(1);
 
     uint32_t status;
-    m_ctlEp->controlReceive(0xa0, 0, 0, 0, (uint8_t*)&status, 4);
+    rc = m_ctlEp->controlReceive(0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4);
+    TEST_ASSERT(rc == USB_OK);
+    if (rc != USB_OK) {
+        return;
+    }
     DBG("HUB STATUS: %08X\n", status);
 
-    for(int i = 1; i <= bNbrPorts; i++) {
+    for(int i = 1; i <= hubdesc.bNbrPorts; i++) {
         SetPortPower(i); // power on
+        wait_ms(hubdesc.bPwrOn2PwrGood*2);
         uint32_t status;
         GetPortStatus(i, &status);
         DBG("port: %d status: %08X\n", i, status);
         if (status & 0x010000) { // Connect Status Change, has changed
             TEST_ASSERT(status & 0x000001);
             ClearPortFeature(C_PORT_CONNECTION, i);
-            bool low_speed = false;
+            int lowSpeed = 0;
             if (status & 0x0200) {
-                low_speed = true;
+                lowSpeed = 1;
             }
-            DeviceConnected(i, low_speed);
+            PortReset(i);
+            ControlEp* ctlEp = new ControlEp(lowSpeed);
+            PortEp.push_back(ctlEp);
         } else {
-            ClearPortPower(i); // power on
+            ClearPortPower(i); // power off
         }
     }
 }
 
-ControlEp* UsbHub::GetPortEp(int port)
+bool UsbHub::check(ControlEp* ctlEp)
 {
-    if (port >= 1 && port <= MAX_HUB_PORT) {
-        return PortEp[port-1];
+    if (ctlEp == NULL) {
+        return false;
     }
-    return NULL;
+    StandardDeviceDescriptor desc;
+    int rc = ctlEp->GetDescriptor(USB_DESCRIPTOR_TYPE_DEVICE, 0, reinterpret_cast<uint8_t*>(&desc), sizeof(StandardDeviceDescriptor));
+    if (rc != USB_OK) {
+        return false;
+    }
+    if (desc.bDeviceClass == 9) { // hub?
+        return true;
+    }
+    return false;
 }
 
 int UsbHub::SetPortPower(int port)
@@ -117,17 +138,6 @@
     return m_ctlEp->controlReceive(0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4);
 }
 
-void UsbHub::DeviceConnected(int port, int low_speed)
-{
-    DBG("port=%d low_speed=%d\n", port, low_speed);
-    PortReset(port);
-    ControlEp* ctlEp = new ControlEp(low_speed);
-    TEST_ASSERT_TRUE(ctlEp);
- 
-    PortEp[port-1] = ctlEp;
-    DBG("PortEp[%d]: %p\n", port-1, PortEp[port-1]);
-}
-
 int UsbHub::PortReset(int port)
 {
     DBG("%p port=%d\n", this, port);