USB Host Driver with Socket Modem support. Works with revision 323 of mbed-src but broken with any later version.

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
va009039
Date:
Fri Jan 31 13:45:07 2014 +0000
Revision:
8:6463cd1964c0
USB hub support.

Who changed what in which revision?

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