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
USBHostMouse.cpp
00001 #include "USBHostMouse.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 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 #define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; 00013 #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) 00014 00015 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0); 00016 00017 USBHostMouse::USBHostMouse() { 00018 host = USBHost::getHostInst(); 00019 init(); 00020 } 00021 00022 void USBHostMouse::init() { 00023 dev = NULL; 00024 int_in = NULL; 00025 onUpdate = NULL; 00026 onButtonUpdate = NULL; 00027 onXUpdate = NULL; 00028 onYUpdate = NULL; 00029 onZUpdate = NULL; 00030 report_id = 0; 00031 dev_connected = false; 00032 mouse_device_found = false; 00033 mouse_intf = -1; 00034 00035 buttons = 0; 00036 x = 0; 00037 y = 0; 00038 z = 0; 00039 } 00040 00041 bool USBHostMouse::connected() { 00042 return dev_connected; 00043 } 00044 00045 bool USBHostMouse::connect() { 00046 00047 if (dev_connected) { 00048 return true; 00049 } 00050 00051 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00052 if ((dev = host->getDevice(i)) != NULL) { 00053 00054 if(host->enumerate(dev, this)) 00055 break; 00056 00057 if (mouse_device_found) { 00058 00059 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN); 00060 USB_DBG("int_in=%p", int_in); 00061 if (!int_in) 00062 break; 00063 00064 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]\n", dev->getVid(), dev->getPid(), dev, mouse_intf); 00065 dev->setName("Mouse", mouse_intf); 00066 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init); 00067 00068 //int_in->attach(this, &USBHostMouse::rxHandler); 00069 host->interruptRead(dev, int_in, report, int_in->getSize(), false); 00070 00071 dev_connected = true; 00072 return true; 00073 } 00074 } 00075 } 00076 init(); 00077 return false; 00078 } 00079 00080 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid) 00081 { 00082 USB_DBG("vid:%04x pid:%04x", vid, pid); 00083 // we don't check VID/PID for mouse driver 00084 } 00085 00086 /*virtual*/ bool USBHostMouse::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed 00087 { 00088 USB_DBG("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol); 00089 if ((mouse_intf == -1) && 00090 (intf_class == HID_CLASS) && 00091 (intf_subclass == 0x01) && 00092 (intf_protocol == 0x02)) { 00093 mouse_intf = intf_nb; 00094 return true; 00095 } 00096 return false; 00097 } 00098 00099 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00100 { 00101 USB_DBG("intf_nb=%d type=%d dir=%d", intf_nb, type, dir); 00102 00103 if (intf_nb == mouse_intf) { 00104 if (type == INTERRUPT_ENDPOINT && dir == IN) { 00105 mouse_device_found = true; 00106 return true; 00107 } 00108 } 00109 return false; 00110 } 00111 00112 #if 0 00113 void USBHostMouse::setup() { 00114 dev = host->getDevice(0); 00115 if (dev->getClass() == HUB_CLASS) { 00116 for(int i = 1; ; i++) { 00117 dev = host->getDevice(i); 00118 if (dev == NULL) { 00119 break; 00120 } 00121 if (enumeration(dev)) { 00122 break; 00123 } 00124 } 00125 USB_TEST_ASSERT(ep_int_in); 00126 } else { 00127 ep_int_in = NULL; 00128 } 00129 } 00130 00131 bool USBHostMouse::enumeration(USBDeviceConnected* dev) { 00132 // config descriptor 00133 uint8_t desc[4]; 00134 int rc = host->controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, desc, 4); 00135 USB_TEST_ASSERT(rc == USB_TYPE_OK); 00136 USB_DBG_HEX(desc, 4); 00137 00138 int TotalLength = desc[2]|desc[3]<<8; 00139 uint8_t* buf = new uint8_t[TotalLength]; 00140 rc = host->controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, buf, TotalLength); 00141 USB_TEST_ASSERT(rc == USB_TYPE_OK); 00142 //USB_DBG_HEX(buf, TotalLength); 00143 bool found = false; 00144 for(int i = 0; i < TotalLength; ) { 00145 int Length = buf[i]; 00146 uint8_t DescriptorType = buf[i+1]; 00147 if (DescriptorType == 0x04) { // interface 00148 InterfaceDescriptor* desc = reinterpret_cast<InterfaceDescriptor*>(buf+i); 00149 if (desc->bInterfaceClass == 0x03) { 00150 found = true; 00151 } 00152 } else if (DescriptorType == 0x05) { // endpoint 00153 EndpointDescriptor* desc = reinterpret_cast<EndpointDescriptor*>(buf+i); 00154 if (desc->bmAttributes == 0x03 && 00155 (desc->bEndpointAddress & 0x80)) { // interrupt in 00156 ep_int_in = new USBEndpoint; 00157 ep_int_in->setDevice(dev); 00158 ep_int_in->setAddress(desc->bEndpointAddress); 00159 ep_int_in->setSize(desc->wMaxPacketSize); 00160 } 00161 } 00162 USB_DBG_HEX(buf+i, Length); 00163 i += Length; 00164 } 00165 delete[] buf; 00166 if (!found) { 00167 return false; 00168 } 00169 int config = 1; 00170 host->controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0); 00171 wait_ms(100); 00172 return true; 00173 } 00174 #endif 00175
Generated on Wed Jul 13 2022 02:45:01 by 1.7.2