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

Dependents:   BaseUsbHost_example BaseJpegDecode_example SimpleJpegDecode_example

Import programBaseUsbHost_example

BaseUsbHost example program

Committer:
va009039
Date:
Tue Dec 04 13:29:41 2012 +0000
Revision:
0:b7d6879637a8
Child:
2:fe1e62051d88
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:b7d6879637a8 1 // BaseUsbHostHub.cpp 2012/12/4
va009039 0:b7d6879637a8 2 #include "mbed.h"
va009039 0:b7d6879637a8 3 #include "rtos.h"
va009039 0:b7d6879637a8 4 #include "BaseUsbHost.h"
va009039 0:b7d6879637a8 5 #define DEBUG
va009039 0:b7d6879637a8 6 #include "BaseUsbHostDebug.h"
va009039 0:b7d6879637a8 7 #define TEST
va009039 0:b7d6879637a8 8 #include "BaseUsbHostTest.h"
va009039 0:b7d6879637a8 9
va009039 0:b7d6879637a8 10 #define PORT_CONNECTION 0
va009039 0:b7d6879637a8 11 #define PORT_ENABLE 1
va009039 0:b7d6879637a8 12 #define PORT_SUSPEND 2
va009039 0:b7d6879637a8 13 #define PORT_OVER_CURRENT 3
va009039 0:b7d6879637a8 14 #define PORT_RESET 4
va009039 0:b7d6879637a8 15 #define PORT_POWER 8
va009039 0:b7d6879637a8 16 #define PORT_LOW_SPEED 9
va009039 0:b7d6879637a8 17
va009039 0:b7d6879637a8 18 #define C_PORT_CONNECTION 16
va009039 0:b7d6879637a8 19 #define C_PORT_ENABLE 17
va009039 0:b7d6879637a8 20 #define C_PORT_SUSPEND 18
va009039 0:b7d6879637a8 21 #define C_PORT_OVER_CURRENT 19
va009039 0:b7d6879637a8 22 #define C_PORT_RESET 20
va009039 0:b7d6879637a8 23
va009039 0:b7d6879637a8 24 int UsbHub::SetPortFeature(int feature, int index)
va009039 0:b7d6879637a8 25 {
va009039 0:b7d6879637a8 26 return m_ctlEp->controlSend(0x23, SET_FEATURE,feature,index,0,0);
va009039 0:b7d6879637a8 27 }
va009039 0:b7d6879637a8 28
va009039 0:b7d6879637a8 29 int UsbHub::ClearPortFeature(int feature, int index)
va009039 0:b7d6879637a8 30 {
va009039 0:b7d6879637a8 31 return m_ctlEp->controlSend(0x23, CLEAR_FEATURE,feature,index,0,0);
va009039 0:b7d6879637a8 32 }
va009039 0:b7d6879637a8 33
va009039 0:b7d6879637a8 34 int UsbHub::SetPortPower(int port)
va009039 0:b7d6879637a8 35 {
va009039 0:b7d6879637a8 36 return SetPortFeature(PORT_POWER, port);
va009039 0:b7d6879637a8 37 }
va009039 0:b7d6879637a8 38
va009039 0:b7d6879637a8 39 int UsbHub::SetPortReset(int port)
va009039 0:b7d6879637a8 40 {
va009039 0:b7d6879637a8 41 return SetPortFeature(PORT_RESET, port);
va009039 0:b7d6879637a8 42 }
va009039 0:b7d6879637a8 43
va009039 0:b7d6879637a8 44 int UsbHub::GetPortStatus(int port, uint32_t* status)
va009039 0:b7d6879637a8 45 {
va009039 0:b7d6879637a8 46 return m_ctlEp->controlReceive(0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4);
va009039 0:b7d6879637a8 47 }
va009039 0:b7d6879637a8 48
va009039 0:b7d6879637a8 49 UsbHub::UsbHub(ControlEp* ctlEp)
va009039 0:b7d6879637a8 50 {
va009039 0:b7d6879637a8 51 for(int i = 0; i < MAX_HUB_PORT; i++) {
va009039 0:b7d6879637a8 52 PortEp[i] = NULL;
va009039 0:b7d6879637a8 53 }
va009039 0:b7d6879637a8 54
va009039 0:b7d6879637a8 55 if (ctlEp == NULL) {
va009039 0:b7d6879637a8 56 DBG_OHCI(LPC_USB->HcRhPortStatus1);
va009039 0:b7d6879637a8 57 int lowSpeed = 0;
va009039 0:b7d6879637a8 58 if (LPC_USB->HcRhPortStatus1 & 0x200) {
va009039 0:b7d6879637a8 59 lowSpeed = 1;
va009039 0:b7d6879637a8 60 }
va009039 0:b7d6879637a8 61 m_ctlEp = new ControlEp(lowSpeed);
va009039 0:b7d6879637a8 62 TEST_ASSERT_TRUE(m_ctlEp);
va009039 0:b7d6879637a8 63 } else {
va009039 0:b7d6879637a8 64 m_ctlEp = ctlEp;
va009039 0:b7d6879637a8 65 }
va009039 0:b7d6879637a8 66 uint8_t desc[9];
va009039 0:b7d6879637a8 67 m_ctlEp->GetDescriptor(1, 0, desc, sizeof(desc));
va009039 0:b7d6879637a8 68 DBG_HEX(desc, sizeof(desc));
va009039 0:b7d6879637a8 69 TEST_ASSERT_TRUE(desc[0] == 0x12);
va009039 0:b7d6879637a8 70 TEST_ASSERT_TRUE(desc[1] == 0x01);
va009039 0:b7d6879637a8 71 TEST_ASSERT_TRUE(desc[4] == 0x09); // hub
va009039 0:b7d6879637a8 72 if (desc[4] != 0x09) { // hub ?
va009039 0:b7d6879637a8 73 return;
va009039 0:b7d6879637a8 74 }
va009039 0:b7d6879637a8 75
va009039 0:b7d6879637a8 76 m_ctlEp->controlReceive(0xa0, 6, 0x29<<8, 0, desc, sizeof(desc));
va009039 0:b7d6879637a8 77 DBG_HEX(desc, sizeof(desc));
va009039 0:b7d6879637a8 78 TEST_ASSERT_TRUE(desc[0] == 9); // length
va009039 0:b7d6879637a8 79 TEST_ASSERT_TRUE(desc[1] == 0x29); // hub
va009039 0:b7d6879637a8 80 int bNbrPorts = desc[2];
va009039 0:b7d6879637a8 81 TEST_ASSERT_TRUE(bNbrPorts <= MAX_HUB_PORT);
va009039 0:b7d6879637a8 82 m_ctlEp->SetConfiguration(1);
va009039 0:b7d6879637a8 83
va009039 0:b7d6879637a8 84 uint32_t status;
va009039 0:b7d6879637a8 85 m_ctlEp->controlReceive(0xa0, 0, 0, 0, (uint8_t*)&status, 4);
va009039 0:b7d6879637a8 86 DBG("HUB STATUS: %08X\n", status);
va009039 0:b7d6879637a8 87
va009039 0:b7d6879637a8 88 for(int i = 1; i <= bNbrPorts; i++) {
va009039 0:b7d6879637a8 89 SetPortFeature(PORT_POWER, i); // power on
va009039 0:b7d6879637a8 90 }
va009039 0:b7d6879637a8 91
va009039 0:b7d6879637a8 92 for(int i = 1; i <= bNbrPorts; i++) {
va009039 0:b7d6879637a8 93 uint32_t status;
va009039 0:b7d6879637a8 94 GetPortStatus(i, &status);
va009039 0:b7d6879637a8 95 DBG("port: %d status: %08X\n", i, status);
va009039 0:b7d6879637a8 96 if (status & 0x010000) { // Connect Status Change, has changed
va009039 0:b7d6879637a8 97 TEST_ASSERT(status & 0x000001);
va009039 0:b7d6879637a8 98 ClearPortFeature(C_PORT_CONNECTION, i);
va009039 0:b7d6879637a8 99 bool low_speed = false;
va009039 0:b7d6879637a8 100 if (status & 0x0200) {
va009039 0:b7d6879637a8 101 low_speed = true;
va009039 0:b7d6879637a8 102 }
va009039 0:b7d6879637a8 103 DeviceConnected(i, low_speed);
va009039 0:b7d6879637a8 104 }
va009039 0:b7d6879637a8 105 }
va009039 0:b7d6879637a8 106 }
va009039 0:b7d6879637a8 107
va009039 0:b7d6879637a8 108 void UsbHub::DeviceConnected(int port, int low_speed)
va009039 0:b7d6879637a8 109 {
va009039 0:b7d6879637a8 110 DBG("port=%d low_speed=%d\n", port, low_speed);
va009039 0:b7d6879637a8 111 PortReset(port);
va009039 0:b7d6879637a8 112 ControlEp* ctlEp = new ControlEp(low_speed);
va009039 0:b7d6879637a8 113 TEST_ASSERT_TRUE(ctlEp);
va009039 0:b7d6879637a8 114
va009039 0:b7d6879637a8 115 PortEp[port-1] = ctlEp;
va009039 0:b7d6879637a8 116 DBG("PortEp[%d]: %p\n", port-1, PortEp[port-1]);
va009039 0:b7d6879637a8 117 }
va009039 0:b7d6879637a8 118
va009039 0:b7d6879637a8 119 int UsbHub::PortReset(int port)
va009039 0:b7d6879637a8 120 {
va009039 0:b7d6879637a8 121 DBG("%p port=%d\n", this, port);
va009039 0:b7d6879637a8 122 TEST_ASSERT(port >= 1);
va009039 0:b7d6879637a8 123 SetPortReset(port);
va009039 0:b7d6879637a8 124 // wait reset
va009039 0:b7d6879637a8 125 for(int i = 0; i < 100; i++) {
va009039 0:b7d6879637a8 126 uint32_t status;
va009039 0:b7d6879637a8 127 GetPortStatus(port, &status);
va009039 0:b7d6879637a8 128 DBG("RESET port: %d status: %08X\n", port, status);
va009039 0:b7d6879637a8 129 if (status & 0x100000) { // Reset change , Reset complete
va009039 0:b7d6879637a8 130 return USB_OK;
va009039 0:b7d6879637a8 131 }
va009039 0:b7d6879637a8 132 wait_ms(5);
va009039 0:b7d6879637a8 133 }
va009039 0:b7d6879637a8 134 return USB_ERROR;
va009039 0:b7d6879637a8 135 }