Usb read
Dependencies: FATFileSystem
Fork of F401RE-USBHost by
USBHost/USBHostHub.cpp@9:7f9f64cf5ded, 2014-02-03 (annotated)
- Committer:
- va009039
- Date:
- Mon Feb 03 13:00:16 2014 +0000
- Revision:
- 9:7f9f64cf5ded
- Parent:
- 8:6463cd1964c0
- Child:
- 10:40c7f6788902
add interrupt write transfer.
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 | 8:6463cd1964c0 | 18 | HubDescriptor hubdesc; |
va009039 | 8:6463cd1964c0 | 19 | // get HUB descriptor |
va009039 | 8:6463cd1964c0 | 20 | int rc = controlRead(dev, |
va009039 | 8:6463cd1964c0 | 21 | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, |
va009039 | 8:6463cd1964c0 | 22 | GET_DESCRIPTOR, |
va009039 | 8:6463cd1964c0 | 23 | 0x29 << 8, 0, reinterpret_cast<uint8_t*>(&hubdesc), |
va009039 | 8:6463cd1964c0 | 24 | sizeof(HubDescriptor)); |
va009039 | 8:6463cd1964c0 | 25 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 8:6463cd1964c0 | 26 | if (rc != USB_TYPE_OK) { |
va009039 | 8:6463cd1964c0 | 27 | return false; |
va009039 | 8:6463cd1964c0 | 28 | } |
va009039 | 8:6463cd1964c0 | 29 | USB_DBG_HEX((uint8_t*)&hubdesc, sizeof(hubdesc)); |
va009039 | 8:6463cd1964c0 | 30 | |
va009039 | 8:6463cd1964c0 | 31 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 32 | rc = controlRead( dev, |
va009039 | 8:6463cd1964c0 | 33 | 0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4); |
va009039 | 8:6463cd1964c0 | 34 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
va009039 | 8:6463cd1964c0 | 35 | if (rc != USB_TYPE_OK) { |
va009039 | 8:6463cd1964c0 | 36 | return false; |
va009039 | 8:6463cd1964c0 | 37 | } |
va009039 | 8:6463cd1964c0 | 38 | USB_DBG("HUB STATUS: %08X\n", status); |
va009039 | 8:6463cd1964c0 | 39 | |
va009039 | 8:6463cd1964c0 | 40 | for(int i = 1; i <= hubdesc.bNbrPorts; i++) { |
va009039 | 8:6463cd1964c0 | 41 | SetPortPower(dev, i); // power on |
va009039 | 8:6463cd1964c0 | 42 | wait_ms(hubdesc.bPwrOn2PwrGood*2); |
va009039 | 8:6463cd1964c0 | 43 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 44 | GetPortStatus(dev, i, &status); |
va009039 | 8:6463cd1964c0 | 45 | USB_DBG("port: %d status: %08X\n", i, status); |
va009039 | 8:6463cd1964c0 | 46 | if (status & 0x010000) { // Connect Status Change, has changed |
va009039 | 8:6463cd1964c0 | 47 | USB_TEST_ASSERT(status & 0x000001); |
va009039 | 8:6463cd1964c0 | 48 | ClearPortFeature(dev, C_PORT_CONNECTION, i); |
va009039 | 8:6463cd1964c0 | 49 | int lowSpeed = 0; |
va009039 | 8:6463cd1964c0 | 50 | if (status & 0x0200) { |
va009039 | 8:6463cd1964c0 | 51 | lowSpeed = 1; |
va009039 | 8:6463cd1964c0 | 52 | } |
va009039 | 8:6463cd1964c0 | 53 | PortReset(dev, i); |
va009039 | 8:6463cd1964c0 | 54 | if (!addDevice(1, i, lowSpeed)) { |
va009039 | 8:6463cd1964c0 | 55 | ClearPortPower(dev, i); // power off |
va009039 | 8:6463cd1964c0 | 56 | } |
va009039 | 8:6463cd1964c0 | 57 | } else { |
va009039 | 8:6463cd1964c0 | 58 | ClearPortPower(dev, i); // power off |
va009039 | 8:6463cd1964c0 | 59 | } |
va009039 | 8:6463cd1964c0 | 60 | } |
va009039 | 8:6463cd1964c0 | 61 | return false; |
va009039 | 8:6463cd1964c0 | 62 | } |
va009039 | 8:6463cd1964c0 | 63 | |
va009039 | 8:6463cd1964c0 | 64 | |
va009039 | 8:6463cd1964c0 | 65 | int USBHost::SetPortPower(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 66 | { |
va009039 | 8:6463cd1964c0 | 67 | return SetPortFeature(dev, PORT_POWER, port); |
va009039 | 8:6463cd1964c0 | 68 | } |
va009039 | 8:6463cd1964c0 | 69 | |
va009039 | 8:6463cd1964c0 | 70 | int USBHost::ClearPortPower(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 71 | { |
va009039 | 8:6463cd1964c0 | 72 | return ClearPortFeature(dev, PORT_POWER, port); |
va009039 | 8:6463cd1964c0 | 73 | } |
va009039 | 8:6463cd1964c0 | 74 | |
va009039 | 8:6463cd1964c0 | 75 | int USBHost::SetPortFeature(USBDeviceConnected* dev, int feature, int index) |
va009039 | 8:6463cd1964c0 | 76 | { |
va009039 | 8:6463cd1964c0 | 77 | return controlWrite(dev, 0x23, SET_FEATURE,feature,index,0,0); |
va009039 | 8:6463cd1964c0 | 78 | } |
va009039 | 8:6463cd1964c0 | 79 | |
va009039 | 8:6463cd1964c0 | 80 | int USBHost::ClearPortFeature(USBDeviceConnected* dev, int feature, int index) |
va009039 | 8:6463cd1964c0 | 81 | { |
va009039 | 8:6463cd1964c0 | 82 | return controlWrite(dev, 0x23, CLEAR_FEATURE,feature,index,0,0); |
va009039 | 8:6463cd1964c0 | 83 | } |
va009039 | 8:6463cd1964c0 | 84 | |
va009039 | 8:6463cd1964c0 | 85 | int USBHost::SetPortReset(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 86 | { |
va009039 | 8:6463cd1964c0 | 87 | return SetPortFeature(dev, PORT_RESET, port); |
va009039 | 8:6463cd1964c0 | 88 | } |
va009039 | 8:6463cd1964c0 | 89 | |
va009039 | 8:6463cd1964c0 | 90 | int USBHost::GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status) |
va009039 | 8:6463cd1964c0 | 91 | { |
va009039 | 8:6463cd1964c0 | 92 | return controlRead(dev, 0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4); |
va009039 | 8:6463cd1964c0 | 93 | } |
va009039 | 8:6463cd1964c0 | 94 | |
va009039 | 8:6463cd1964c0 | 95 | int USBHost::PortReset(USBDeviceConnected* dev, int port) |
va009039 | 8:6463cd1964c0 | 96 | { |
va009039 | 8:6463cd1964c0 | 97 | USB_DBG("%p port=%d\n", this, port); |
va009039 | 8:6463cd1964c0 | 98 | USB_TEST_ASSERT(port >= 1); |
va009039 | 8:6463cd1964c0 | 99 | SetPortReset(dev, port); |
va009039 | 8:6463cd1964c0 | 100 | // wait reset |
va009039 | 8:6463cd1964c0 | 101 | for(int i = 0; i < 100; i++) { |
va009039 | 8:6463cd1964c0 | 102 | uint32_t status; |
va009039 | 8:6463cd1964c0 | 103 | GetPortStatus(dev, port, &status); |
va009039 | 8:6463cd1964c0 | 104 | USB_DBG("RESET port: %d status: %08X\n", port, status); |
va009039 | 8:6463cd1964c0 | 105 | if (status & 0x100000) { // Reset change , Reset complete |
va009039 | 8:6463cd1964c0 | 106 | return USB_TYPE_OK; |
va009039 | 8:6463cd1964c0 | 107 | } |
va009039 | 8:6463cd1964c0 | 108 | wait_ms(5); |
va009039 | 8:6463cd1964c0 | 109 | } |
va009039 | 8:6463cd1964c0 | 110 | return USB_TYPE_ERROR; |
va009039 | 8:6463cd1964c0 | 111 | } |