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@8:6463cd1964c0, 2014-01-31 (annotated)
- Committer:
- va009039
- Date:
- Fri Jan 31 13:45:07 2014 +0000
- Revision:
- 8:6463cd1964c0
- Child:
- 9:7f9f64cf5ded
USB hub support.
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 | #ifdef _USB_DBG |
va009039 | 8:6463cd1964c0 | 4 | #define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0); |
va009039 | 8:6463cd1964c0 | 5 | #define USB_DBG_HEX(A,B) debug_hex(A,B) |
va009039 | 8:6463cd1964c0 | 6 | extern void debug_hex(uint8_t* buf, int size); |
va009039 | 8:6463cd1964c0 | 7 | #else |
va009039 | 8:6463cd1964c0 | 8 | #define USB_DBG(...) while(0) |
va009039 | 8:6463cd1964c0 | 9 | #define USB_DBG_HEX(A,B) while(0) |
va009039 | 8:6463cd1964c0 | 10 | #endif |
va009039 | 8:6463cd1964c0 | 11 | |
va009039 | 8:6463cd1964c0 | 12 | #ifdef _USB_TEST |
va009039 | 8:6463cd1964c0 | 13 | #define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; |
va009039 | 8:6463cd1964c0 | 14 | #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) |
va009039 | 8:6463cd1964c0 | 15 | #else |
va009039 | 8:6463cd1964c0 | 16 | #define USB_TEST_ASSERT(A) while(0) |
va009039 | 8:6463cd1964c0 | 17 | #define USB_TEST_ASSERT_FALSE(A) while(0) |
va009039 | 8:6463cd1964c0 | 18 | #endif |
va009039 | 8:6463cd1964c0 | 19 | |
va009039 | 8:6463cd1964c0 | 20 | #define PORT_CONNECTION 0 |
va009039 | 8:6463cd1964c0 | 21 | #define PORT_ENABLE 1 |
va009039 | 8:6463cd1964c0 | 22 | #define PORT_SUSPEND 2 |
va009039 | 8:6463cd1964c0 | 23 | #define PORT_OVER_CURRENT 3 |
va009039 | 8:6463cd1964c0 | 24 | #define PORT_RESET 4 |
va009039 | 8:6463cd1964c0 | 25 | #define PORT_POWER 8 |
va009039 | 8:6463cd1964c0 | 26 | #define PORT_LOW_SPEED 9 |
va009039 | 8:6463cd1964c0 | 27 | |
va009039 | 8:6463cd1964c0 | 28 | #define C_PORT_CONNECTION 16 |
va009039 | 8:6463cd1964c0 | 29 | #define C_PORT_ENABLE 17 |
va009039 | 8:6463cd1964c0 | 30 | #define C_PORT_SUSPEND 18 |
va009039 | 8:6463cd1964c0 | 31 | #define C_PORT_OVER_CURRENT 19 |
va009039 | 8:6463cd1964c0 | 32 | #define C_PORT_RESET 20 |
va009039 | 8:6463cd1964c0 | 33 | |
va009039 | 8:6463cd1964c0 | 34 | bool USBHost::Hub(USBDeviceConnected* dev) { |
va009039 | 8:6463cd1964c0 | 35 | HubDescriptor hubdesc; |
va009039 | 8:6463cd1964c0 | 36 | // get HUB descriptor |
va009039 | 8:6463cd1964c0 | 37 | int rc = controlRead(dev, |
va009039 | 8:6463cd1964c0 | 38 | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
va009039 | 8:6463cd1964c0 | 39 | GET_DESCRIPTOR, |
va009039 | 8:6463cd1964c0 | 40 | 0x29 << 8, 0, reinterpret_cast<uint8_t*>(&hubdesc), |
va009039 | 8:6463cd1964c0 | 41 | sizeof(HubDescriptor)); |
va009039 | 8:6463cd1964c0 | 42 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 8:6463cd1964c0 | 43 | if (rc != USB_TYPE_OK) { |
va009039 | 8:6463cd1964c0 | 44 | return false; |
va009039 | 8:6463cd1964c0 | 45 | } |
va009039 | 8:6463cd1964c0 | 46 | USB_DBG_HEX((uint8_t*)&hubdesc, sizeof(hubdesc)); |
va009039 | 8:6463cd1964c0 | 47 | |
va009039 | 8:6463cd1964c0 | 48 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 49 | rc = controlRead( dev, |
va009039 | 8:6463cd1964c0 | 50 | 0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4); |
va009039 | 8:6463cd1964c0 | 51 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 8:6463cd1964c0 | 52 | if (rc != USB_TYPE_OK) { |
va009039 | 8:6463cd1964c0 | 53 | return false; |
va009039 | 8:6463cd1964c0 | 54 | } |
va009039 | 8:6463cd1964c0 | 55 | USB_DBG("HUB STATUS: %08X\n", status); |
va009039 | 8:6463cd1964c0 | 56 | |
va009039 | 8:6463cd1964c0 | 57 | for(int i = 1; i <= hubdesc.bNbrPorts; i++) { |
va009039 | 8:6463cd1964c0 | 58 | SetPortPower(dev, i); // power on |
va009039 | 8:6463cd1964c0 | 59 | wait_ms(hubdesc.bPwrOn2PwrGood*2); |
va009039 | 8:6463cd1964c0 | 60 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 61 | GetPortStatus(dev, i, &status); |
va009039 | 8:6463cd1964c0 | 62 | USB_DBG("port: %d status: %08X\n", i, status); |
va009039 | 8:6463cd1964c0 | 63 | if (status & 0x010000) { // Connect Status Change, has changed |
va009039 | 8:6463cd1964c0 | 64 | USB_TEST_ASSERT(status & 0x000001); |
va009039 | 8:6463cd1964c0 | 65 | ClearPortFeature(dev, C_PORT_CONNECTION, i); |
va009039 | 8:6463cd1964c0 | 66 | int lowSpeed = 0; |
va009039 | 8:6463cd1964c0 | 67 | if (status & 0x0200) { |
va009039 | 8:6463cd1964c0 | 68 | lowSpeed = 1; |
va009039 | 8:6463cd1964c0 | 69 | } |
va009039 | 8:6463cd1964c0 | 70 | PortReset(dev, i); |
va009039 | 8:6463cd1964c0 | 71 | if (!addDevice(1, i, lowSpeed)) { |
va009039 | 8:6463cd1964c0 | 72 | ClearPortPower(dev, i); // power off |
va009039 | 8:6463cd1964c0 | 73 | } |
va009039 | 8:6463cd1964c0 | 74 | } else { |
va009039 | 8:6463cd1964c0 | 75 | ClearPortPower(dev, i); // power off |
va009039 | 8:6463cd1964c0 | 76 | } |
va009039 | 8:6463cd1964c0 | 77 | } |
va009039 | 8:6463cd1964c0 | 78 | return false; |
va009039 | 8:6463cd1964c0 | 79 | } |
va009039 | 8:6463cd1964c0 | 80 | |
va009039 | 8:6463cd1964c0 | 81 | |
va009039 | 8:6463cd1964c0 | 82 | int USBHost::SetPortPower(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 83 | { |
va009039 | 8:6463cd1964c0 | 84 | return SetPortFeature(dev, PORT_POWER, port); |
va009039 | 8:6463cd1964c0 | 85 | } |
va009039 | 8:6463cd1964c0 | 86 | |
va009039 | 8:6463cd1964c0 | 87 | int USBHost::ClearPortPower(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 88 | { |
va009039 | 8:6463cd1964c0 | 89 | return ClearPortFeature(dev, PORT_POWER, port); |
va009039 | 8:6463cd1964c0 | 90 | } |
va009039 | 8:6463cd1964c0 | 91 | |
va009039 | 8:6463cd1964c0 | 92 | int USBHost::SetPortFeature(USBDeviceConnected* dev, int feature, int index) |
va009039 | 8:6463cd1964c0 | 93 | { |
va009039 | 8:6463cd1964c0 | 94 | return controlWrite(dev, 0x23, SET_FEATURE,feature,index,0,0); |
va009039 | 8:6463cd1964c0 | 95 | } |
va009039 | 8:6463cd1964c0 | 96 | |
va009039 | 8:6463cd1964c0 | 97 | int USBHost::ClearPortFeature(USBDeviceConnected* dev, int feature, int index) |
va009039 | 8:6463cd1964c0 | 98 | { |
va009039 | 8:6463cd1964c0 | 99 | return controlWrite(dev, 0x23, CLEAR_FEATURE,feature,index,0,0); |
va009039 | 8:6463cd1964c0 | 100 | } |
va009039 | 8:6463cd1964c0 | 101 | |
va009039 | 8:6463cd1964c0 | 102 | int USBHost::SetPortReset(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 103 | { |
va009039 | 8:6463cd1964c0 | 104 | return SetPortFeature(dev, PORT_RESET, port); |
va009039 | 8:6463cd1964c0 | 105 | } |
va009039 | 8:6463cd1964c0 | 106 | |
va009039 | 8:6463cd1964c0 | 107 | int USBHost::GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status) |
va009039 | 8:6463cd1964c0 | 108 | { |
va009039 | 8:6463cd1964c0 | 109 | return controlRead(dev, 0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4); |
va009039 | 8:6463cd1964c0 | 110 | } |
va009039 | 8:6463cd1964c0 | 111 | |
va009039 | 8:6463cd1964c0 | 112 | int USBHost::PortReset(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 113 | { |
va009039 | 8:6463cd1964c0 | 114 | USB_DBG("%p port=%d\n", this, port); |
va009039 | 8:6463cd1964c0 | 115 | USB_TEST_ASSERT(port >= 1); |
va009039 | 8:6463cd1964c0 | 116 | SetPortReset(dev, port); |
va009039 | 8:6463cd1964c0 | 117 | // wait reset |
va009039 | 8:6463cd1964c0 | 118 | for(int i = 0; i < 100; i++) { |
va009039 | 8:6463cd1964c0 | 119 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 120 | GetPortStatus(dev, port, &status); |
va009039 | 8:6463cd1964c0 | 121 | USB_DBG("RESET port: %d status: %08X\n", port, status); |
va009039 | 8:6463cd1964c0 | 122 | if (status & 0x100000) { // Reset change , Reset complete |
va009039 | 8:6463cd1964c0 | 123 | return USB_TYPE_OK; |
va009039 | 8:6463cd1964c0 | 124 | } |
va009039 | 8:6463cd1964c0 | 125 | wait_ms(5); |
va009039 | 8:6463cd1964c0 | 126 | } |
va009039 | 8:6463cd1964c0 | 127 | return USB_TYPE_ERROR; |
va009039 | 8:6463cd1964c0 | 128 | } |