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