From 2f9d2f0fffc958bbaff7dfcf9e0412dbd254f6f4 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 10 May 2021 17:17:31 +0200 Subject: [PATCH 1/3] USBSerial: fix available() return size --- cores/arduino/USB/PluggableUSBSerial.h | 2 +- cores/arduino/USB/USBSerial.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/arduino/USB/PluggableUSBSerial.h b/cores/arduino/USB/PluggableUSBSerial.h index 31d2e8bf7..a44e63fb7 100644 --- a/cores/arduino/USB/PluggableUSBSerial.h +++ b/cores/arduino/USB/PluggableUSBSerial.h @@ -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. diff --git a/cores/arduino/USB/USBSerial.cpp b/cores/arduino/USB/USBSerial.cpp index 60ebdda4a..108697ef9 100644 --- a/cores/arduino/USB/USBSerial.cpp +++ b/cores/arduino/USB/USBSerial.cpp @@ -104,13 +104,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(); From 5f9d84f7b7560420053ac68fe0227749e64ef8be Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 10 May 2021 17:18:12 +0200 Subject: [PATCH 2/3] USBSerial: improve 1200bps reset routine --- cores/arduino/USB/PluggableUSBSerial.h | 2 +- cores/arduino/USB/USBSerial.cpp | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cores/arduino/USB/PluggableUSBSerial.h b/cores/arduino/USB/PluggableUSBSerial.h index a44e63fb7..c7aa7ade4 100644 --- a/cores/arduino/USB/PluggableUSBSerial.h +++ b/cores/arduino/USB/PluggableUSBSerial.h @@ -294,7 +294,7 @@ 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() { diff --git a/cores/arduino/USB/USBSerial.cpp b/cores/arduino/USB/USBSerial.cpp index 108697ef9..d46e8b24d 100644 --- a/cores/arduino/USB/USBSerial.cpp +++ b/cores/arduino/USB/USBSerial.cpp @@ -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; @@ -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); } } @@ -68,7 +70,8 @@ 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); } int USBSerial::_putc(int c) From 91b5fb6d31ace2891a6f545cdf1debf4439100fd Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 10 May 2021 17:18:03 +0200 Subject: [PATCH 3/3] USBSerial: speedup by using non blocking read --- cores/arduino/USB/PluggableUSBSerial.h | 12 ++++++++++-- cores/arduino/USB/USBSerial.cpp | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cores/arduino/USB/PluggableUSBSerial.h b/cores/arduino/USB/PluggableUSBSerial.h index c7aa7ade4..505999c99 100644 --- a/cores/arduino/USB/PluggableUSBSerial.h +++ b/cores/arduino/USB/PluggableUSBSerial.h @@ -298,8 +298,16 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial { 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 --; } } diff --git a/cores/arduino/USB/USBSerial.cpp b/cores/arduino/USB/USBSerial.cpp index d46e8b24d..4a756ba45 100644 --- a/cores/arduino/USB/USBSerial.cpp +++ b/cores/arduino/USB/USBSerial.cpp @@ -72,6 +72,7 @@ void USBSerial::begin(unsigned long) { this->attach(::mbed::callback(this, &USBSerial::onInterrupt)); t = new rtos::Thread(osPriorityNormal, 256, nullptr, "USBevt"); t->start(waitForPortClose); + onInterrupt(); } int USBSerial::_putc(int c)