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:
Tue Jun 10 13:38:41 2014 +0900
Revision:
13:8774c07f12a5
add USBHostC270

Who changed what in which revision?

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