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

Dependents:   BaseUsbHost_example BaseJpegDecode_example SimpleJpegDecode_example

Import programBaseUsbHost_example

BaseUsbHost example program

BaseUsbHostHub.cpp

Committer:
va009039
Date:
2012-12-11
Revision:
2:fe1e62051d88
Parent:
0:b7d6879637a8
Child:
3:ae77d63a1eda

File content as of revision 2:fe1e62051d88:

// BaseUsbHostHub.cpp 2012/12/11
#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

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++) {
        SetPortPower(i); // power on
        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);
        } else {
            ClearPortPower(i); // power on
        }
    }
}

ControlEp* UsbHub::GetPortEp(int port)
{
    if (port >= 1 && port <= MAX_HUB_PORT) {
        return PortEp[port-1];
    }
    return NULL;
}

int UsbHub::SetPortPower(int port)
{
    return SetPortFeature(PORT_POWER, port);
}

int UsbHub::ClearPortPower(int port)
{
    return ClearPortFeature(PORT_POWER, port);
}

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::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);
}

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;
}