USBHost custom library.
Dependents: GR-PEACH_HVC-P2_sample_client mbed-os-storage-access GR-PEACH_Digital_Signage GR-PEACH_Audio_Playback_Sample ... more
Fork of USBHost by
Diff: USBHostSerial/USBHostSerial.cpp
- Revision:
- 36:f8c0979c2814
- Parent:
- 27:4206883f4cb7
- Child:
- 38:dd168a3e4194
--- a/USBHostSerial/USBHostSerial.cpp Fri Apr 29 01:16:38 2016 +0100 +++ b/USBHostSerial/USBHostSerial.cpp Mon Jan 23 06:05:50 2017 +0000 @@ -30,12 +30,6 @@ { host = USBHost::getHostInst(); ports_found = 0; - dev_connected = false; -} - -bool USBHostSerial::connected() -{ - return dev_connected; } void USBHostSerial::disconnect(void) @@ -46,18 +40,15 @@ bool USBHostSerial::connect() { - if (dev) - { - for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) - { + if (dev) { + for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { USBDeviceConnected* d = host->getDevice(i); if (dev == d) return true; } disconnect(); } - for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) - { + for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { USBDeviceConnected* d = host->getDevice(i); if (d != NULL) { @@ -71,7 +62,6 @@ { USBHostSerialPort::connect(host,d,port_intf,bulk_in, bulk_out); dev = d; - dev_connected = true; } } } @@ -113,7 +103,6 @@ dev = NULL; memset(ports, NULL, sizeof(ports)); ports_found = 0; - dev_connected = false; } USBHostMultiSerial::~USBHostMultiSerial() @@ -121,11 +110,6 @@ disconnect(); } -bool USBHostMultiSerial::connected() -{ - return dev_connected; -} - void USBHostMultiSerial::disconnect(void) { for (int port = 0; port < USBHOST_SERIAL; port ++) @@ -172,7 +156,6 @@ { ports[port]->connect(host,d,port_intf[port],bulk_in, bulk_out); dev = d; - dev_connected = true; } } } @@ -218,9 +201,10 @@ void USBHostSerialPort::init(void) { + dev_connected = false; host = NULL; dev = NULL; - serial_intf = NULL; + serial_intf = 0; size_bulk_in = 0; size_bulk_out = 0; bulk_in = NULL; @@ -250,6 +234,11 @@ bulk_in->attach(this, &USBHostSerialPort::rxHandler); bulk_out->attach(this, &USBHostSerialPort::txHandler); host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); + dev_connected = true; +} + +bool USBHostSerialPort::connected() { + return dev_connected; } void USBHostSerialPort::rxHandler() { @@ -257,6 +246,9 @@ int len = bulk_in->getLengthTransferred(); if (bulk_in->getState() == USB_TYPE_IDLE) { for (int i = 0; i < len; i++) { + while (circ_buf.isFull()) { + Thread::wait(1); + } circ_buf.queue(buf[i]); } rx.call(); @@ -305,41 +297,59 @@ init(); return -1; } - while (circ_buf.isEmpty()); + while (circ_buf.isEmpty()) { + if (dev_connected == false) { + return -1; + } + Thread::wait(1); + } circ_buf.dequeue(&c); return c; } -int USBHostSerialPort::writeBuf(const char* b, int s) -{ +int USBHostSerialPort::writeBuf(const char* b, int s) { + int i; int c = 0; - if (bulk_out) - { - while (c < s) - { - int i = (s < size_bulk_out) ? s : size_bulk_out; - if (host->bulkWrite(dev, bulk_out, (uint8_t *)(b+c), i) == USB_TYPE_OK) + if (bulk_out) { + while (c < s) { + if (dev_connected == false) { + break; + } + i = (s < size_bulk_out) ? s : size_bulk_out; + if (host->bulkWrite(dev, bulk_out, (uint8_t *)(b+c), i) == USB_TYPE_OK) { c += i; + } } } - return s; + return c; } -int USBHostSerialPort::readBuf(char* b, int s) -{ +int USBHostSerialPort::readBuf(char* b, int s, int timeout) { int i = 0; - if (bulk_in) - { - for (i = 0; i < s; ) - b[i++] = getc(); + + if (bulk_out) { + for (i = 0; i < s; i++) { + while ((circ_buf.isEmpty()) && (dev_connected)) { + if (timeout == 0) { + break; + } else { + if (timeout > 0) { + timeout--; + } + Thread::wait(1); + } + } + if (!circ_buf.dequeue((uint8_t *)&b[i])) { + break; + } + } } return i; } -uint8_t USBHostSerialPort::available() { +uint32_t USBHostSerialPort::available() { return circ_buf.available(); } - +#endif -#endif