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

Dependents:   BaseUsbHost_example BaseJpegDecode_example SimpleJpegDecode_example

Import programBaseUsbHost_example

BaseUsbHost example program

Revision:
0:b7d6879637a8
Child:
2:fe1e62051d88
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BaseUsbHostHub.cpp	Tue Dec 04 13:29:41 2012 +0000
@@ -0,0 +1,135 @@
+// BaseUsbHostHub.cpp 2012/12/4
+#include "mbed.h"
+#include "rtos.h"
+#include "BaseUsbHost.h"
+#define DEBUG
+#include "BaseUsbHostDebug.h"
+#define TEST
+#include "BaseUsbHostTest.h"
+
+#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 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
+
+int UsbHub::SetPortFeature(int feature, int index)
+{
+    return m_ctlEp->controlSend(0x23, SET_FEATURE,feature,index,0,0);
+}
+
+int UsbHub::ClearPortFeature(int feature, int index)
+{
+    return m_ctlEp->controlSend(0x23, CLEAR_FEATURE,feature,index,0,0);
+}
+
+int UsbHub::SetPortPower(int port)
+{
+    return SetPortFeature(PORT_POWER, port);
+}
+
+int UsbHub::SetPortReset(int port)
+{
+    return SetPortFeature(PORT_RESET, port);
+}
+
+int UsbHub::GetPortStatus(int port, uint32_t* status)
+{
+    return m_ctlEp->controlReceive(0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4);
+}
+
+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;
+        if (LPC_USB->HcRhPortStatus1 & 0x200) {
+            lowSpeed = 1;
+        }
+        m_ctlEp = new ControlEp(lowSpeed);
+        TEST_ASSERT_TRUE(m_ctlEp);
+    } else {
+        m_ctlEp = ctlEp;
+    }
+    uint8_t desc[9];
+    m_ctlEp->GetDescriptor(1, 0, desc, sizeof(desc));
+    DBG_HEX(desc, sizeof(desc));
+    TEST_ASSERT_TRUE(desc[0] == 0x12);
+    TEST_ASSERT_TRUE(desc[1] == 0x01);
+    TEST_ASSERT_TRUE(desc[4] == 0x09); // hub
+    if (desc[4] != 0x09) { // hub ?
+        return;
+    }
+
+    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);
+    DBG("HUB STATUS: %08X\n", status);
+
+    for(int i = 1; i <= bNbrPorts; i++) {
+        SetPortFeature(PORT_POWER, i); // power on
+    }
+
+    for(int i = 1; i <= bNbrPorts; i++) {
+        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;
+            if (status & 0x0200) {
+                low_speed = true;
+            }
+            DeviceConnected(i, low_speed);
+        }
+    }
+}
+
+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);
+    TEST_ASSERT(port >= 1);
+    SetPortReset(port);
+    // wait reset
+    for(int i = 0; i < 100; i++) {
+        uint32_t status;    
+        GetPortStatus(port, &status);
+        DBG("RESET port: %d status: %08X\n", port, status);
+        if (status & 0x100000) { // Reset change , Reset complete
+            return USB_OK;
+        }
+        wait_ms(5);
+     }
+     return USB_ERROR;
+}