Skip to content

Commit d2a9e47

Browse files
authored
Merge pull request #218 from facchinm/improve_usb_wrapper
Improve USB wrapper
2 parents 9209009 + 91b5fb6 commit d2a9e47

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

cores/arduino/USB/PluggableUSBSerial.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
115115
*
116116
* @returns the number of bytes available
117117
*/
118-
uint8_t _available();
118+
uint32_t _available();
119119

120120
/**
121121
* Check if the terminal is connected.
@@ -294,12 +294,20 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
294294

295295
private:
296296
RingBufferN<256> rx_buffer;
297-
rtos::Thread t;
297+
rtos::Thread* t;
298298
int _baud, _bits, _parity, _stop;
299299

300300
void onInterrupt() {
301-
while (rx_buffer.availableForStore() && _available()) {
302-
rx_buffer.store_char(_getc());
301+
uint8_t buf[256];
302+
int howMany = _available();
303+
uint32_t toRead;
304+
if (howMany > rx_buffer.availableForStore()) {
305+
howMany = rx_buffer.availableForStore();
306+
}
307+
receive_nb(buf, howMany, &toRead);
308+
while (rx_buffer.availableForStore() && toRead > 0) {
309+
rx_buffer.store_char(buf[howMany-toRead]);
310+
toRead --;
303311
}
304312
}
305313

cores/arduino/USB/USBSerial.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
using namespace arduino;
2828

29+
static rtos::EventFlags event;
30+
2931
static void waitForPortClose() {
32+
33+
event.wait_any(0xFF);
3034
// wait for DTR be 0 (port closed) and timeout to be over
3135
long start = millis();
3236
static const int WAIT_TIMEOUT = 200;
@@ -37,11 +41,9 @@ static void waitForPortClose() {
3741
_ontouch1200bps_();
3842
}
3943

40-
static events::EventQueue queue(2 * EVENTS_EVENT_SIZE);
41-
4244
void usbPortChanged(int baud, int bits, int parity, int stop) {
4345
if (baud == 1200) {
44-
queue.call(waitForPortClose);
46+
event.set(1);
4547
}
4648
}
4749

@@ -68,7 +70,9 @@ USBSerial::~USBSerial()
6870
void USBSerial::begin(unsigned long) {
6971
this->attach(usbPortChanged);
7072
this->attach(::mbed::callback(this, &USBSerial::onInterrupt));
71-
t.start(callback(&queue, &::events::EventQueue::dispatch_forever));
73+
t = new rtos::Thread(osPriorityNormal, 256, nullptr, "USBevt");
74+
t->start(waitForPortClose);
75+
onInterrupt();
7276
}
7377

7478
int USBSerial::_putc(int c)
@@ -104,13 +108,13 @@ void USBSerial::data_rx()
104108
}
105109
}
106110

107-
uint8_t USBSerial::_available()
111+
uint32_t USBSerial::_available()
108112
{
109113
USBCDC::lock();
110114

111-
uint8_t size = 0;
115+
uint32_t size = 0;
112116
if (!_rx_in_progress) {
113-
size = _rx_size > 0xFF ? 0xFF : _rx_size;
117+
size = _rx_size > CDC_MAX_PACKET_SIZE ? CDC_MAX_PACKET_SIZE : _rx_size;
114118
}
115119

116120
USBCDC::unlock();

0 commit comments

Comments
 (0)