Simple USBHost GPS Dongle Receiver for FRDM-KL46Z test program
Dependencies: KL46Z-USBHost mbed SLCD
USBHostGPS.cpp
00001 #include "USBHostGPS.h" 00002 00003 USBHostGPS::USBHostGPS(int baud_) 00004 { 00005 host = USBHost::getHostInst(); 00006 init(); 00007 baud = baud_; 00008 } 00009 00010 void USBHostGPS::init() { 00011 dev = NULL; 00012 bulk_in = NULL; 00013 onUpdateRaw = NULL; 00014 dev_connected = false; 00015 gps_device_found = false; 00016 gps_intf = -1; 00017 } 00018 00019 bool USBHostGPS::connected() { 00020 return dev_connected; 00021 } 00022 00023 bool USBHostGPS::connect() { 00024 00025 if (dev_connected) { 00026 return true; 00027 } 00028 00029 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00030 if ((dev = host->getDevice(i)) != NULL) { 00031 if(host->enumerate(dev, this)) { 00032 break; 00033 } 00034 if (gps_device_found) { 00035 bulk_in = dev->getEndpoint(gps_intf, BULK_ENDPOINT, IN); 00036 USB_TEST_ASSERT(bulk_in); 00037 // stop bit = 1, parity = none, 8bit 00038 uint8_t data[] = {baud&0xff, baud>>8, baud>>16, baud>>24, 0x00, 0x00, 0x08}; 00039 USB_TYPE rc = host->controlWrite(dev, 0x21, PL2303_SET_LINE_CODING, 0, 0, data, sizeof(data)); 00040 USB_TEST_ASSERT(rc == USB_TYPE_OK); 00041 USB_INFO("New GPS device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, gps_intf); 00042 bulk_in->attach(this, &USBHostGPS::rxHandler); 00043 host->bulkRead(dev, bulk_in, bulk_buf, bulk_in->getSize(), false); 00044 00045 dev_connected = true; 00046 return true; 00047 } 00048 } 00049 } 00050 init(); 00051 return false; 00052 } 00053 00054 void USBHostGPS::rxHandler() { 00055 int len = bulk_in->getLengthTransferred(); 00056 if (onUpdateRaw) { 00057 (*onUpdateRaw)((char*)bulk_buf, len); 00058 } 00059 nmea.inputNMEA((char*)bulk_buf, len); 00060 00061 if (dev) { 00062 host->bulkRead(dev, bulk_in, bulk_buf, bulk_in->getSize(), false); 00063 } 00064 } 00065 00066 /*virtual*/ void USBHostGPS::setVidPid(uint16_t vid, uint16_t pid) 00067 { 00068 USB_DBG("vid:%04x pid:%04x", vid, pid); 00069 if (pid == 0x2303) { 00070 gps_device_found = true; 00071 } 00072 } 00073 00074 /*virtual*/ bool USBHostGPS::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 00075 { 00076 USB_DBG("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol); 00077 if (gps_device_found) { 00078 if (gps_intf == -1) { 00079 gps_intf = intf_nb; 00080 return true; 00081 } 00082 } 00083 return false; 00084 } 00085 00086 /*virtual*/ bool USBHostGPS::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00087 { 00088 USB_DBG("intf_nb=%d type=%d dir=%d", intf_nb, type, dir); 00089 if (gps_device_found) { 00090 if (intf_nb == gps_intf) { 00091 if (type == BULK_ENDPOINT && dir == IN) { 00092 return true; 00093 } 00094 } 00095 } 00096 return false; 00097 } 00098
Generated on Fri Jul 15 2022 21:30:00 by 1.7.2