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
KL46Z-USBHost/USBHostGPS/USBHostGPS.cpp@1:5ad12c581db4, 2014-07-02 (annotated)
- Committer:
- AxedaCorp
- Date:
- Wed Jul 02 15:59:38 2014 +0000
- Revision:
- 1:5ad12c581db4
- Parent:
- 0:65004368569c
url ip switch
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:65004368569c | 1 | #include "USBHostGPS.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_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0); |
AxedaCorp | 0:65004368569c | 6 | #define USB_DBG_HEX(A,B) debug_hex(A,B) |
AxedaCorp | 0:65004368569c | 7 | void debug_hex(uint8_t* buf, int size); |
AxedaCorp | 0:65004368569c | 8 | #else |
AxedaCorp | 0:65004368569c | 9 | #define USB_DBG(...) while(0) |
AxedaCorp | 0:65004368569c | 10 | #define USB_DBG2(...) while(0) |
AxedaCorp | 0:65004368569c | 11 | #define USB_DBG_HEX(A,B) while(0) |
AxedaCorp | 0:65004368569c | 12 | #endif |
AxedaCorp | 0:65004368569c | 13 | |
AxedaCorp | 0:65004368569c | 14 | #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 | 15 | #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) |
AxedaCorp | 0:65004368569c | 16 | |
AxedaCorp | 0:65004368569c | 17 | #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0); |
AxedaCorp | 0:65004368569c | 18 | |
AxedaCorp | 0:65004368569c | 19 | |
AxedaCorp | 0:65004368569c | 20 | USBHostGPS::USBHostGPS(int baud_) |
AxedaCorp | 0:65004368569c | 21 | { |
AxedaCorp | 0:65004368569c | 22 | host = USBHost::getHostInst(); |
AxedaCorp | 0:65004368569c | 23 | init(); |
AxedaCorp | 0:65004368569c | 24 | baud = baud_; |
AxedaCorp | 0:65004368569c | 25 | } |
AxedaCorp | 0:65004368569c | 26 | |
AxedaCorp | 0:65004368569c | 27 | void USBHostGPS::init() { |
AxedaCorp | 0:65004368569c | 28 | dev = NULL; |
AxedaCorp | 0:65004368569c | 29 | bulk_in = NULL; |
AxedaCorp | 0:65004368569c | 30 | onUpdate = NULL; |
AxedaCorp | 0:65004368569c | 31 | dev_connected = false; |
AxedaCorp | 0:65004368569c | 32 | gps_device_found = false; |
AxedaCorp | 0:65004368569c | 33 | gps_intf = -1; |
AxedaCorp | 0:65004368569c | 34 | } |
AxedaCorp | 0:65004368569c | 35 | |
AxedaCorp | 0:65004368569c | 36 | bool USBHostGPS::connected() { |
AxedaCorp | 0:65004368569c | 37 | return dev_connected; |
AxedaCorp | 0:65004368569c | 38 | } |
AxedaCorp | 0:65004368569c | 39 | |
AxedaCorp | 0:65004368569c | 40 | bool USBHostGPS::connect() { |
AxedaCorp | 0:65004368569c | 41 | |
AxedaCorp | 0:65004368569c | 42 | if (dev_connected) { |
AxedaCorp | 0:65004368569c | 43 | return true; |
AxedaCorp | 0:65004368569c | 44 | } |
AxedaCorp | 0:65004368569c | 45 | |
AxedaCorp | 0:65004368569c | 46 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
AxedaCorp | 0:65004368569c | 47 | if ((dev = host->getDevice(i)) != NULL) { |
AxedaCorp | 0:65004368569c | 48 | if(host->enumerate(dev, this)) { |
AxedaCorp | 0:65004368569c | 49 | break; |
AxedaCorp | 0:65004368569c | 50 | } |
AxedaCorp | 0:65004368569c | 51 | if (gps_device_found) { |
AxedaCorp | 0:65004368569c | 52 | bulk_in = dev->getEndpoint(gps_intf, BULK_ENDPOINT, IN); |
AxedaCorp | 0:65004368569c | 53 | USB_TEST_ASSERT(bulk_in); |
AxedaCorp | 0:65004368569c | 54 | // stop bit = 1, parity = none, 8bit |
AxedaCorp | 0:65004368569c | 55 | uint8_t data[] = {baud&0xff, baud>>8, baud>>16, baud>>24, 0x00, 0x00, 0x08}; |
AxedaCorp | 0:65004368569c | 56 | USB_TYPE rc = host->controlWrite(dev, 0x21, PL2303_SET_LINE_CODING, 0, 0, data, sizeof(data)); |
AxedaCorp | 0:65004368569c | 57 | USB_TEST_ASSERT(rc == USB_TYPE_OK); |
AxedaCorp | 0:65004368569c | 58 | USB_INFO("New GPS device: VID:%04x PID:%04x [dev: %p - intf: %d]\n", dev->getVid(), dev->getPid(), dev, gps_intf); |
AxedaCorp | 0:65004368569c | 59 | dev_connected = true; |
AxedaCorp | 0:65004368569c | 60 | return true; |
AxedaCorp | 0:65004368569c | 61 | } |
AxedaCorp | 0:65004368569c | 62 | } |
AxedaCorp | 0:65004368569c | 63 | } |
AxedaCorp | 0:65004368569c | 64 | init(); |
AxedaCorp | 0:65004368569c | 65 | return false; |
AxedaCorp | 0:65004368569c | 66 | } |
AxedaCorp | 0:65004368569c | 67 | |
AxedaCorp | 0:65004368569c | 68 | /*virtual*/ void USBHostGPS::setVidPid(uint16_t vid, uint16_t pid) |
AxedaCorp | 0:65004368569c | 69 | { |
AxedaCorp | 0:65004368569c | 70 | USB_DBG("vid:%04x pid:%04x", vid, pid); |
AxedaCorp | 0:65004368569c | 71 | if (pid == 0x2303) { |
AxedaCorp | 0:65004368569c | 72 | gps_device_found = true; |
AxedaCorp | 0:65004368569c | 73 | } |
AxedaCorp | 0:65004368569c | 74 | } |
AxedaCorp | 0:65004368569c | 75 | |
AxedaCorp | 0:65004368569c | 76 | /*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 |
AxedaCorp | 0:65004368569c | 77 | { |
AxedaCorp | 0:65004368569c | 78 | USB_DBG("intf: %d class: %02x %02x %02x", intf_nb, intf_class, intf_subclass, intf_protocol); |
AxedaCorp | 0:65004368569c | 79 | if (gps_device_found) { |
AxedaCorp | 0:65004368569c | 80 | if (gps_intf == -1) { |
AxedaCorp | 0:65004368569c | 81 | gps_intf = intf_nb; |
AxedaCorp | 0:65004368569c | 82 | return true; |
AxedaCorp | 0:65004368569c | 83 | } |
AxedaCorp | 0:65004368569c | 84 | } |
AxedaCorp | 0:65004368569c | 85 | return false; |
AxedaCorp | 0:65004368569c | 86 | } |
AxedaCorp | 0:65004368569c | 87 | |
AxedaCorp | 0:65004368569c | 88 | /*virtual*/ bool USBHostGPS::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
AxedaCorp | 0:65004368569c | 89 | { |
AxedaCorp | 0:65004368569c | 90 | USB_DBG("intf_nb=%d type=%d dir=%d", intf_nb, type, dir); |
AxedaCorp | 0:65004368569c | 91 | if (gps_device_found) { |
AxedaCorp | 0:65004368569c | 92 | if (intf_nb == gps_intf) { |
AxedaCorp | 0:65004368569c | 93 | if (type == BULK_ENDPOINT && dir == IN) { |
AxedaCorp | 0:65004368569c | 94 | return true; |
AxedaCorp | 0:65004368569c | 95 | } |
AxedaCorp | 0:65004368569c | 96 | } |
AxedaCorp | 0:65004368569c | 97 | } |
AxedaCorp | 0:65004368569c | 98 | return false; |
AxedaCorp | 0:65004368569c | 99 | } |
AxedaCorp | 0:65004368569c | 100 |