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 Axeda Corp

Committer:
AxedaCorp
Date:
Tue Jul 01 21:31:54 2014 +0000
Revision:
0:65004368569c
Made initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 #include "USBHostMouse.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 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 #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 13 #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A))
AxedaCorp 0:65004368569c 14
AxedaCorp 0:65004368569c 15 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0);
AxedaCorp 0:65004368569c 16
AxedaCorp 0:65004368569c 17 USBHostMouse::USBHostMouse() {
AxedaCorp 0:65004368569c 18 host = USBHost::getHostInst();
AxedaCorp 0:65004368569c 19 init();
AxedaCorp 0:65004368569c 20 }
AxedaCorp 0:65004368569c 21
AxedaCorp 0:65004368569c 22 void USBHostMouse::init() {
AxedaCorp 0:65004368569c 23 dev = NULL;
AxedaCorp 0:65004368569c 24 int_in = NULL;
AxedaCorp 0:65004368569c 25 onUpdate = NULL;
AxedaCorp 0:65004368569c 26 onButtonUpdate = NULL;
AxedaCorp 0:65004368569c 27 onXUpdate = NULL;
AxedaCorp 0:65004368569c 28 onYUpdate = NULL;
AxedaCorp 0:65004368569c 29 onZUpdate = NULL;
AxedaCorp 0:65004368569c 30 report_id = 0;
AxedaCorp 0:65004368569c 31 dev_connected = false;
AxedaCorp 0:65004368569c 32 mouse_device_found = false;
AxedaCorp 0:65004368569c 33 mouse_intf = -1;
AxedaCorp 0:65004368569c 34
AxedaCorp 0:65004368569c 35 buttons = 0;
AxedaCorp 0:65004368569c 36 x = 0;
AxedaCorp 0:65004368569c 37 y = 0;
AxedaCorp 0:65004368569c 38 z = 0;
AxedaCorp 0:65004368569c 39 }
AxedaCorp 0:65004368569c 40
AxedaCorp 0:65004368569c 41 bool USBHostMouse::connected() {
AxedaCorp 0:65004368569c 42 return dev_connected;
AxedaCorp 0:65004368569c 43 }
AxedaCorp 0:65004368569c 44
AxedaCorp 0:65004368569c 45 bool USBHostMouse::connect() {
AxedaCorp 0:65004368569c 46
AxedaCorp 0:65004368569c 47 if (dev_connected) {
AxedaCorp 0:65004368569c 48 return true;
AxedaCorp 0:65004368569c 49 }
AxedaCorp 0:65004368569c 50
AxedaCorp 0:65004368569c 51 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
AxedaCorp 0:65004368569c 52 if ((dev = host->getDevice(i)) != NULL) {
AxedaCorp 0:65004368569c 53
AxedaCorp 0:65004368569c 54 if(host->enumerate(dev, this))
AxedaCorp 0:65004368569c 55 break;
AxedaCorp 0:65004368569c 56
AxedaCorp 0:65004368569c 57 if (mouse_device_found) {
AxedaCorp 0:65004368569c 58
AxedaCorp 0:65004368569c 59 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
AxedaCorp 0:65004368569c 60 USB_DBG("int_in=%p", int_in);
AxedaCorp 0:65004368569c 61 if (!int_in)
AxedaCorp 0:65004368569c 62 break;
AxedaCorp 0:65004368569c 63
AxedaCorp 0:65004368569c 64 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]\n", dev->getVid(), dev->getPid(), dev, mouse_intf);
AxedaCorp 0:65004368569c 65 dev->setName("Mouse", mouse_intf);
AxedaCorp 0:65004368569c 66 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
AxedaCorp 0:65004368569c 67
AxedaCorp 0:65004368569c 68 //int_in->attach(this, &USBHostMouse::rxHandler);
AxedaCorp 0:65004368569c 69 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
AxedaCorp 0:65004368569c 70
AxedaCorp 0:65004368569c 71 dev_connected = true;
AxedaCorp 0:65004368569c 72 return true;
AxedaCorp 0:65004368569c 73 }
AxedaCorp 0:65004368569c 74 }
AxedaCorp 0:65004368569c 75 }
AxedaCorp 0:65004368569c 76 init();
AxedaCorp 0:65004368569c 77 return false;
AxedaCorp 0:65004368569c 78 }
AxedaCorp 0:65004368569c 79
AxedaCorp 0:65004368569c 80 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
AxedaCorp 0:65004368569c 81 {
AxedaCorp 0:65004368569c 82 USB_DBG("vid:%04x pid:%04x", vid, pid);
AxedaCorp 0:65004368569c 83 // we don't check VID/PID for mouse driver
AxedaCorp 0:65004368569c 84 }
AxedaCorp 0:65004368569c 85
AxedaCorp 0:65004368569c 86 /*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
AxedaCorp 0:65004368569c 87 {
AxedaCorp 0:65004368569c 88 USB_DBG("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol);
AxedaCorp 0:65004368569c 89 if ((mouse_intf == -1) &&
AxedaCorp 0:65004368569c 90 (intf_class == HID_CLASS) &&
AxedaCorp 0:65004368569c 91 (intf_subclass == 0x01) &&
AxedaCorp 0:65004368569c 92 (intf_protocol == 0x02)) {
AxedaCorp 0:65004368569c 93 mouse_intf = intf_nb;
AxedaCorp 0:65004368569c 94 return true;
AxedaCorp 0:65004368569c 95 }
AxedaCorp 0:65004368569c 96 return false;
AxedaCorp 0:65004368569c 97 }
AxedaCorp 0:65004368569c 98
AxedaCorp 0:65004368569c 99 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
AxedaCorp 0:65004368569c 100 {
AxedaCorp 0:65004368569c 101 USB_DBG("intf_nb=%d type=%d dir=%d", intf_nb, type, dir);
AxedaCorp 0:65004368569c 102
AxedaCorp 0:65004368569c 103 if (intf_nb == mouse_intf) {
AxedaCorp 0:65004368569c 104 if (type == INTERRUPT_ENDPOINT && dir == IN) {
AxedaCorp 0:65004368569c 105 mouse_device_found = true;
AxedaCorp 0:65004368569c 106 return true;
AxedaCorp 0:65004368569c 107 }
AxedaCorp 0:65004368569c 108 }
AxedaCorp 0:65004368569c 109 return false;
AxedaCorp 0:65004368569c 110 }
AxedaCorp 0:65004368569c 111
AxedaCorp 0:65004368569c 112 #if 0
AxedaCorp 0:65004368569c 113 void USBHostMouse::setup() {
AxedaCorp 0:65004368569c 114 dev = host->getDevice(0);
AxedaCorp 0:65004368569c 115 if (dev->getClass() == HUB_CLASS) {
AxedaCorp 0:65004368569c 116 for(int i = 1; ; i++) {
AxedaCorp 0:65004368569c 117 dev = host->getDevice(i);
AxedaCorp 0:65004368569c 118 if (dev == NULL) {
AxedaCorp 0:65004368569c 119 break;
AxedaCorp 0:65004368569c 120 }
AxedaCorp 0:65004368569c 121 if (enumeration(dev)) {
AxedaCorp 0:65004368569c 122 break;
AxedaCorp 0:65004368569c 123 }
AxedaCorp 0:65004368569c 124 }
AxedaCorp 0:65004368569c 125 USB_TEST_ASSERT(ep_int_in);
AxedaCorp 0:65004368569c 126 } else {
AxedaCorp 0:65004368569c 127 ep_int_in = NULL;
AxedaCorp 0:65004368569c 128 }
AxedaCorp 0:65004368569c 129 }
AxedaCorp 0:65004368569c 130
AxedaCorp 0:65004368569c 131 bool USBHostMouse::enumeration(USBDeviceConnected* dev) {
AxedaCorp 0:65004368569c 132 // config descriptor
AxedaCorp 0:65004368569c 133 uint8_t desc[4];
AxedaCorp 0:65004368569c 134 int rc = host->controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, desc, 4);
AxedaCorp 0:65004368569c 135 USB_TEST_ASSERT(rc == USB_TYPE_OK);
AxedaCorp 0:65004368569c 136 USB_DBG_HEX(desc, 4);
AxedaCorp 0:65004368569c 137
AxedaCorp 0:65004368569c 138 int TotalLength = desc[2]|desc[3]<<8;
AxedaCorp 0:65004368569c 139 uint8_t* buf = new uint8_t[TotalLength];
AxedaCorp 0:65004368569c 140 rc = host->controlRead(dev, 0x80, GET_DESCRIPTOR, 2<<8, 0, buf, TotalLength);
AxedaCorp 0:65004368569c 141 USB_TEST_ASSERT(rc == USB_TYPE_OK);
AxedaCorp 0:65004368569c 142 //USB_DBG_HEX(buf, TotalLength);
AxedaCorp 0:65004368569c 143 bool found = false;
AxedaCorp 0:65004368569c 144 for(int i = 0; i < TotalLength; ) {
AxedaCorp 0:65004368569c 145 int Length = buf[i];
AxedaCorp 0:65004368569c 146 uint8_t DescriptorType = buf[i+1];
AxedaCorp 0:65004368569c 147 if (DescriptorType == 0x04) { // interface
AxedaCorp 0:65004368569c 148 InterfaceDescriptor* desc = reinterpret_cast<InterfaceDescriptor*>(buf+i);
AxedaCorp 0:65004368569c 149 if (desc->bInterfaceClass == 0x03) {
AxedaCorp 0:65004368569c 150 found = true;
AxedaCorp 0:65004368569c 151 }
AxedaCorp 0:65004368569c 152 } else if (DescriptorType == 0x05) { // endpoint
AxedaCorp 0:65004368569c 153 EndpointDescriptor* desc = reinterpret_cast<EndpointDescriptor*>(buf+i);
AxedaCorp 0:65004368569c 154 if (desc->bmAttributes == 0x03 &&
AxedaCorp 0:65004368569c 155 (desc->bEndpointAddress & 0x80)) { // interrupt in
AxedaCorp 0:65004368569c 156 ep_int_in = new USBEndpoint;
AxedaCorp 0:65004368569c 157 ep_int_in->setDevice(dev);
AxedaCorp 0:65004368569c 158 ep_int_in->setAddress(desc->bEndpointAddress);
AxedaCorp 0:65004368569c 159 ep_int_in->setSize(desc->wMaxPacketSize);
AxedaCorp 0:65004368569c 160 }
AxedaCorp 0:65004368569c 161 }
AxedaCorp 0:65004368569c 162 USB_DBG_HEX(buf+i, Length);
AxedaCorp 0:65004368569c 163 i += Length;
AxedaCorp 0:65004368569c 164 }
AxedaCorp 0:65004368569c 165 delete[] buf;
AxedaCorp 0:65004368569c 166 if (!found) {
AxedaCorp 0:65004368569c 167 return false;
AxedaCorp 0:65004368569c 168 }
AxedaCorp 0:65004368569c 169 int config = 1;
AxedaCorp 0:65004368569c 170 host->controlWrite(dev, 0x00, SET_CONFIGURATION, config, 0, NULL, 0);
AxedaCorp 0:65004368569c 171 wait_ms(100);
AxedaCorp 0:65004368569c 172 return true;
AxedaCorp 0:65004368569c 173 }
AxedaCorp 0:65004368569c 174 #endif
AxedaCorp 0:65004368569c 175