Axeda Corp / Mbed 2 deprecated AxedaGo-Freescal_FRDM-KL46Z

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostGPS.cpp Source File

USBHostGPS.cpp

00001 #include "USBHostGPS.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_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0);
00006 #define USB_DBG_HEX(A,B) debug_hex(A,B)
00007 void debug_hex(uint8_t* buf, int size);
00008 #else
00009 #define USB_DBG(...) while(0)
00010 #define USB_DBG2(...) while(0)
00011 #define USB_DBG_HEX(A,B) while(0)
00012 #endif
00013 
00014 #define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
00015 #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A))
00016 
00017 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0);
00018 
00019 
00020 USBHostGPS::USBHostGPS(int baud_)
00021 {
00022     host = USBHost::getHostInst();
00023     init();
00024     baud = baud_;
00025 }
00026 
00027 void USBHostGPS::init() {
00028     dev = NULL;
00029     bulk_in = NULL;
00030     onUpdate = NULL;
00031     dev_connected = false;
00032     gps_device_found = false;
00033     gps_intf = -1;
00034 }
00035 
00036 bool USBHostGPS::connected() {
00037     return dev_connected;
00038 }
00039 
00040 bool USBHostGPS::connect() {
00041 
00042     if (dev_connected) {
00043         return true;
00044     }
00045     
00046     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00047         if ((dev = host->getDevice(i)) != NULL) {
00048             if(host->enumerate(dev, this)) {
00049                 break;
00050             }
00051             if (gps_device_found) {
00052                 bulk_in = dev->getEndpoint(gps_intf, BULK_ENDPOINT, IN);
00053                 USB_TEST_ASSERT(bulk_in);
00054                 // stop bit = 1, parity = none, 8bit
00055                 uint8_t data[] = {baud&0xff, baud>>8, baud>>16, baud>>24, 0x00, 0x00, 0x08};
00056                 USB_TYPE rc = host->controlWrite(dev, 0x21, PL2303_SET_LINE_CODING, 0, 0, data, sizeof(data));
00057                 USB_TEST_ASSERT(rc == USB_TYPE_OK);
00058                 USB_INFO("New GPS device: VID:%04x PID:%04x [dev: %p - intf: %d]\n", dev->getVid(), dev->getPid(), dev, gps_intf);
00059                 dev_connected = true;
00060                 return true;
00061             }
00062         }
00063     }
00064     init();
00065     return false;
00066 }
00067 
00068 /*virtual*/ void USBHostGPS::setVidPid(uint16_t vid, uint16_t pid)
00069 {
00070     USB_DBG("vid:%04x pid:%04x", vid, pid);
00071     if (pid == 0x2303) {
00072         gps_device_found = true;
00073     }
00074 }
00075 
00076 /*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
00077 {
00078     USB_DBG("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol);
00079     if (gps_device_found) {
00080         if (gps_intf == -1) {
00081             gps_intf = intf_nb;
00082             return true;
00083         }
00084     }    
00085     return false;
00086 }
00087 
00088 /*virtual*/ bool USBHostGPS::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00089 {
00090     USB_DBG("intf_nb=%d type=%d dir=%d", intf_nb, type, dir);
00091     if (gps_device_found) {
00092         if (intf_nb == gps_intf) {
00093             if (type == BULK_ENDPOINT && dir == IN) {
00094                 return true;
00095             }
00096         }
00097     }
00098     return false;
00099 }
00100