Simple USBHost library for STM32F746NG Discovery board. Only either the Fastspeed or the Highspeed port can be used( not both together)
Dependents: DISCO-F746NG_USB_Host
Fork of KL46Z-USBHost by
USBHost/USBHostHub.cpp@10:40c7f6788902, 2014-02-05 (annotated)
- Committer:
- va009039
- Date:
- Wed Feb 05 13:34:37 2014 +0000
- Revision:
- 10:40c7f6788902
- Parent:
- 9:7f9f64cf5ded
fix endpoint callback.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 8:6463cd1964c0 | 1 | #include "USBHost.h" |
va009039 | 8:6463cd1964c0 | 2 | |
va009039 | 8:6463cd1964c0 | 3 | #define PORT_CONNECTION 0 |
va009039 | 8:6463cd1964c0 | 4 | #define PORT_ENABLE 1 |
va009039 | 8:6463cd1964c0 | 5 | #define PORT_SUSPEND 2 |
va009039 | 8:6463cd1964c0 | 6 | #define PORT_OVER_CURRENT 3 |
va009039 | 8:6463cd1964c0 | 7 | #define PORT_RESET 4 |
va009039 | 8:6463cd1964c0 | 8 | #define PORT_POWER 8 |
va009039 | 8:6463cd1964c0 | 9 | #define PORT_LOW_SPEED 9 |
va009039 | 8:6463cd1964c0 | 10 | |
va009039 | 8:6463cd1964c0 | 11 | #define C_PORT_CONNECTION 16 |
va009039 | 8:6463cd1964c0 | 12 | #define C_PORT_ENABLE 17 |
va009039 | 8:6463cd1964c0 | 13 | #define C_PORT_SUSPEND 18 |
va009039 | 8:6463cd1964c0 | 14 | #define C_PORT_OVER_CURRENT 19 |
va009039 | 8:6463cd1964c0 | 15 | #define C_PORT_RESET 20 |
va009039 | 8:6463cd1964c0 | 16 | |
va009039 | 8:6463cd1964c0 | 17 | bool USBHost::Hub(USBDeviceConnected* dev) { |
va009039 | 10:40c7f6788902 | 18 | USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p]", dev->getVid(), dev->getPid(), dev); |
va009039 | 8:6463cd1964c0 | 19 | HubDescriptor hubdesc; |
va009039 | 8:6463cd1964c0 | 20 | // get HUB descriptor |
va009039 | 8:6463cd1964c0 | 21 | int rc = controlRead(dev, |
va009039 | 8:6463cd1964c0 | 22 | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
va009039 | 8:6463cd1964c0 | 23 | GET_DESCRIPTOR, |
va009039 | 8:6463cd1964c0 | 24 | 0x29 << 8, 0, reinterpret_cast<uint8_t*>(&hubdesc), |
va009039 | 8:6463cd1964c0 | 25 | sizeof(HubDescriptor)); |
va009039 | 8:6463cd1964c0 | 26 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 8:6463cd1964c0 | 27 | if (rc != USB_TYPE_OK) { |
va009039 | 8:6463cd1964c0 | 28 | return false; |
va009039 | 8:6463cd1964c0 | 29 | } |
va009039 | 8:6463cd1964c0 | 30 | USB_DBG_HEX((uint8_t*)&hubdesc, sizeof(hubdesc)); |
va009039 | 8:6463cd1964c0 | 31 | |
va009039 | 8:6463cd1964c0 | 32 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 33 | rc = controlRead( dev, |
va009039 | 8:6463cd1964c0 | 34 | 0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4); |
va009039 | 8:6463cd1964c0 | 35 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 8:6463cd1964c0 | 36 | if (rc != USB_TYPE_OK) { |
va009039 | 8:6463cd1964c0 | 37 | return false; |
va009039 | 8:6463cd1964c0 | 38 | } |
va009039 | 8:6463cd1964c0 | 39 | USB_DBG("HUB STATUS: %08X\n", status); |
va009039 | 8:6463cd1964c0 | 40 | |
va009039 | 8:6463cd1964c0 | 41 | for(int i = 1; i <= hubdesc.bNbrPorts; i++) { |
va009039 | 8:6463cd1964c0 | 42 | SetPortPower(dev, i); // power on |
va009039 | 8:6463cd1964c0 | 43 | wait_ms(hubdesc.bPwrOn2PwrGood*2); |
va009039 | 8:6463cd1964c0 | 44 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 45 | GetPortStatus(dev, i, &status); |
va009039 | 8:6463cd1964c0 | 46 | USB_DBG("port: %d status: %08X\n", i, status); |
va009039 | 8:6463cd1964c0 | 47 | if (status & 0x010000) { // Connect Status Change, has changed |
va009039 | 8:6463cd1964c0 | 48 | USB_TEST_ASSERT(status & 0x000001); |
va009039 | 8:6463cd1964c0 | 49 | ClearPortFeature(dev, C_PORT_CONNECTION, i); |
va009039 | 8:6463cd1964c0 | 50 | int lowSpeed = 0; |
va009039 | 8:6463cd1964c0 | 51 | if (status & 0x0200) { |
va009039 | 8:6463cd1964c0 | 52 | lowSpeed = 1; |
va009039 | 8:6463cd1964c0 | 53 | } |
va009039 | 8:6463cd1964c0 | 54 | PortReset(dev, i); |
va009039 | 10:40c7f6788902 | 55 | if (!addDevice(dev, i, lowSpeed)) { |
va009039 | 8:6463cd1964c0 | 56 | ClearPortPower(dev, i); // power off |
va009039 | 8:6463cd1964c0 | 57 | } |
va009039 | 8:6463cd1964c0 | 58 | } else { |
va009039 | 8:6463cd1964c0 | 59 | ClearPortPower(dev, i); // power off |
va009039 | 8:6463cd1964c0 | 60 | } |
va009039 | 8:6463cd1964c0 | 61 | } |
va009039 | 8:6463cd1964c0 | 62 | return false; |
va009039 | 8:6463cd1964c0 | 63 | } |
va009039 | 8:6463cd1964c0 | 64 | |
va009039 | 8:6463cd1964c0 | 65 | |
va009039 | 8:6463cd1964c0 | 66 | int USBHost::SetPortPower(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 67 | { |
va009039 | 8:6463cd1964c0 | 68 | return SetPortFeature(dev, PORT_POWER, port); |
va009039 | 8:6463cd1964c0 | 69 | } |
va009039 | 8:6463cd1964c0 | 70 | |
va009039 | 8:6463cd1964c0 | 71 | int USBHost::ClearPortPower(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 72 | { |
va009039 | 8:6463cd1964c0 | 73 | return ClearPortFeature(dev, PORT_POWER, port); |
va009039 | 8:6463cd1964c0 | 74 | } |
va009039 | 8:6463cd1964c0 | 75 | |
va009039 | 8:6463cd1964c0 | 76 | int USBHost::SetPortFeature(USBDeviceConnected* dev, int feature, int index) |
va009039 | 8:6463cd1964c0 | 77 | { |
va009039 | 8:6463cd1964c0 | 78 | return controlWrite(dev, 0x23, SET_FEATURE,feature,index,0,0); |
va009039 | 8:6463cd1964c0 | 79 | } |
va009039 | 8:6463cd1964c0 | 80 | |
va009039 | 8:6463cd1964c0 | 81 | int USBHost::ClearPortFeature(USBDeviceConnected* dev, int feature, int index) |
va009039 | 8:6463cd1964c0 | 82 | { |
va009039 | 8:6463cd1964c0 | 83 | return controlWrite(dev, 0x23, CLEAR_FEATURE,feature,index,0,0); |
va009039 | 8:6463cd1964c0 | 84 | } |
va009039 | 8:6463cd1964c0 | 85 | |
va009039 | 8:6463cd1964c0 | 86 | int USBHost::SetPortReset(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 87 | { |
va009039 | 8:6463cd1964c0 | 88 | return SetPortFeature(dev, PORT_RESET, port); |
va009039 | 8:6463cd1964c0 | 89 | } |
va009039 | 8:6463cd1964c0 | 90 | |
va009039 | 8:6463cd1964c0 | 91 | int USBHost::GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status) |
va009039 | 8:6463cd1964c0 | 92 | { |
va009039 | 8:6463cd1964c0 | 93 | return controlRead(dev, 0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4); |
va009039 | 8:6463cd1964c0 | 94 | } |
va009039 | 8:6463cd1964c0 | 95 | |
va009039 | 8:6463cd1964c0 | 96 | int USBHost::PortReset(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 97 | { |
va009039 | 8:6463cd1964c0 | 98 | USB_DBG("%p port=%d\n", this, port); |
va009039 | 8:6463cd1964c0 | 99 | USB_TEST_ASSERT(port >= 1); |
va009039 | 8:6463cd1964c0 | 100 | SetPortReset(dev, port); |
va009039 | 8:6463cd1964c0 | 101 | // wait reset |
va009039 | 8:6463cd1964c0 | 102 | for(int i = 0; i < 100; i++) { |
va009039 | 8:6463cd1964c0 | 103 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 104 | GetPortStatus(dev, port, &status); |
va009039 | 8:6463cd1964c0 | 105 | USB_DBG("RESET port: %d status: %08X\n", port, status); |
va009039 | 8:6463cd1964c0 | 106 | if (status & 0x100000) { // Reset change , Reset complete |
va009039 | 8:6463cd1964c0 | 107 | return USB_TYPE_OK; |
va009039 | 8:6463cd1964c0 | 108 | } |
va009039 | 8:6463cd1964c0 | 109 | wait_ms(5); |
va009039 | 8:6463cd1964c0 | 110 | } |
va009039 | 8:6463cd1964c0 | 111 | return USB_TYPE_ERROR; |
va009039 | 8:6463cd1964c0 | 112 | } |