Skip to content

Improve USB wrapper #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions cores/arduino/USB/PluggableUSBSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
*
* @returns the number of bytes available
*/
uint8_t _available();
uint32_t _available();

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

private:
RingBufferN<256> rx_buffer;
rtos::Thread t;
rtos::Thread* t;
int _baud, _bits, _parity, _stop;

void onInterrupt() {
while (rx_buffer.availableForStore() && _available()) {
rx_buffer.store_char(_getc());
uint8_t buf[256];
int howMany = _available();
uint32_t toRead;
if (howMany > rx_buffer.availableForStore()) {
howMany = rx_buffer.availableForStore();
}
receive_nb(buf, howMany, &toRead);
while (rx_buffer.availableForStore() && toRead > 0) {
rx_buffer.store_char(buf[howMany-toRead]);
toRead --;
}
}

Expand Down
18 changes: 11 additions & 7 deletions cores/arduino/USB/USBSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@

using namespace arduino;

static rtos::EventFlags event;

static void waitForPortClose() {

event.wait_any(0xFF);
// wait for DTR be 0 (port closed) and timeout to be over
long start = millis();
static const int WAIT_TIMEOUT = 200;
Expand All @@ -37,11 +41,9 @@ static void waitForPortClose() {
_ontouch1200bps_();
}

static events::EventQueue queue(2 * EVENTS_EVENT_SIZE);

void usbPortChanged(int baud, int bits, int parity, int stop) {
if (baud == 1200) {
queue.call(waitForPortClose);
event.set(1);
}
}

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

int USBSerial::_putc(int c)
Expand Down Expand Up @@ -104,13 +108,13 @@ void USBSerial::data_rx()
}
}

uint8_t USBSerial::_available()
uint32_t USBSerial::_available()
{
USBCDC::lock();

uint8_t size = 0;
uint32_t size = 0;
if (!_rx_in_progress) {
size = _rx_size > 0xFF ? 0xFF : _rx_size;
size = _rx_size > CDC_MAX_PACKET_SIZE ? CDC_MAX_PACKET_SIZE : _rx_size;
}

USBCDC::unlock();
Expand Down