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