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 revert by
USBHostHub.cpp
00001 #include "USBHost.h" 00002 00003 #ifdef _USB_DBG 00004 #define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0); 00005 #define USB_DBG_HEX(A,B) debug_hex(A,B) 00006 extern void debug_hex(uint8_t* buf, int size); 00007 #else 00008 #define USB_DBG(...) while(0) 00009 #define USB_DBG_HEX(A,B) while(0) 00010 #endif 00011 00012 #ifdef _USB_TEST 00013 #define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; 00014 #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) 00015 #else 00016 #define USB_TEST_ASSERT(A) while(0) 00017 #define USB_TEST_ASSERT_FALSE(A) while(0) 00018 #endif 00019 00020 #define PORT_CONNECTION 0 00021 #define PORT_ENABLE 1 00022 #define PORT_SUSPEND 2 00023 #define PORT_OVER_CURRENT 3 00024 #define PORT_RESET 4 00025 #define PORT_POWER 8 00026 #define PORT_LOW_SPEED 9 00027 00028 #define C_PORT_CONNECTION 16 00029 #define C_PORT_ENABLE 17 00030 #define C_PORT_SUSPEND 18 00031 #define C_PORT_OVER_CURRENT 19 00032 #define C_PORT_RESET 20 00033 00034 bool USBHost::Hub(USBDeviceConnected* dev) { 00035 HubDescriptor hubdesc; 00036 // get HUB descriptor 00037 int rc = controlRead(dev, 00038 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, 00039 GET_DESCRIPTOR, 00040 0x29 << 8, 0, reinterpret_cast<uint8_t*>(&hubdesc), 00041 sizeof(HubDescriptor)); 00042 USB_TEST_ASSERT(rc == USB_TYPE_OK); 00043 if (rc != USB_TYPE_OK) { 00044 return false; 00045 } 00046 USB_DBG_HEX((uint8_t*)&hubdesc, sizeof(hubdesc)); 00047 00048 uint32_t status; 00049 rc = controlRead( dev, 00050 0xa0, 0, 0, 0, reinterpret_cast<uint8_t*>(&status), 4); 00051 USB_TEST_ASSERT(rc == USB_TYPE_OK); 00052 if (rc != USB_TYPE_OK) { 00053 return false; 00054 } 00055 USB_DBG("HUB STATUS: %08X\n", status); 00056 00057 for(int i = 1; i <= hubdesc.bNbrPorts; i++) { 00058 SetPortPower(dev, i); // power on 00059 wait_ms(hubdesc.bPwrOn2PwrGood*2); 00060 uint32_t status; 00061 GetPortStatus(dev, i, &status); 00062 USB_DBG("port: %d status: %08X\n", i, status); 00063 if (status & 0x010000) { // Connect Status Change, has changed 00064 USB_TEST_ASSERT(status & 0x000001); 00065 ClearPortFeature(dev, C_PORT_CONNECTION, i); 00066 int lowSpeed = 0; 00067 if (status & 0x0200) { 00068 lowSpeed = 1; 00069 } 00070 PortReset(dev, i); 00071 if (!addDevice(1, i, lowSpeed)) { 00072 ClearPortPower(dev, i); // power off 00073 } 00074 } else { 00075 ClearPortPower(dev, i); // power off 00076 } 00077 } 00078 return false; 00079 } 00080 00081 00082 int USBHost::SetPortPower(USBDeviceConnected* dev, int port) 00083 { 00084 return SetPortFeature(dev, PORT_POWER, port); 00085 } 00086 00087 int USBHost::ClearPortPower(USBDeviceConnected* dev, int port) 00088 { 00089 return ClearPortFeature(dev, PORT_POWER, port); 00090 } 00091 00092 int USBHost::SetPortFeature(USBDeviceConnected* dev, int feature, int index) 00093 { 00094 return controlWrite(dev, 0x23, SET_FEATURE,feature,index,0,0); 00095 } 00096 00097 int USBHost::ClearPortFeature(USBDeviceConnected* dev, int feature, int index) 00098 { 00099 return controlWrite(dev, 0x23, CLEAR_FEATURE,feature,index,0,0); 00100 } 00101 00102 int USBHost::SetPortReset(USBDeviceConnected* dev, int port) 00103 { 00104 return SetPortFeature(dev, PORT_RESET, port); 00105 } 00106 00107 int USBHost::GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status) 00108 { 00109 return controlRead(dev, 0xa3, GET_STATUS, 0, port, (uint8_t*)status, 4); 00110 } 00111 00112 int USBHost::PortReset(USBDeviceConnected* dev, int port) 00113 { 00114 USB_DBG("%p port=%d\n", this, port); 00115 USB_TEST_ASSERT(port >= 1); 00116 SetPortReset(dev, port); 00117 // wait reset 00118 for(int i = 0; i < 100; i++) { 00119 uint32_t status; 00120 GetPortStatus(dev, port, &status); 00121 USB_DBG("RESET port: %d status: %08X\n", port, status); 00122 if (status & 0x100000) { // Reset change , Reset complete 00123 return USB_TYPE_OK; 00124 } 00125 wait_ms(5); 00126 } 00127 return USB_TYPE_ERROR; 00128 }
Generated on Wed Jul 13 2022 02:45:01 by 1.7.2