From 0672cb8460dab60a3b668217b0c96bb3626ae191 Mon Sep 17 00:00:00 2001 From: Misaka19465 <19465@misakanet.team> Date: Sun, 15 Sep 2024 15:49:58 +0800 Subject: [PATCH 1/2] add Adafruit_USBD_CDC::ignoreFlowControl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some cases, the target application will not assert the DTR virtual line, thus preventing writing operations to succeed. For this reason, the Adafruit_USBD_CDC::ignoreFlowControl() method disables the connection’s state verification, enabling the program to write on the port, even though the data might be lost. --- src/arduino/Adafruit_USBD_CDC.cpp | 6 +++++- src/arduino/Adafruit_USBD_CDC.h | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/arduino/Adafruit_USBD_CDC.cpp b/src/arduino/Adafruit_USBD_CDC.cpp index 893db83e..6345043a 100644 --- a/src/arduino/Adafruit_USBD_CDC.cpp +++ b/src/arduino/Adafruit_USBD_CDC.cpp @@ -118,6 +118,10 @@ void Adafruit_USBD_CDC::end(void) { _instance = INVALID_INSTANCE; } +void Adafruit_USBD_CDC::ignoreFlowControl(bool ignore) { + _ignoreFlowControl = ignore; +} + uint32_t Adafruit_USBD_CDC::baud(void) { if (!isValid()) { return 0; @@ -243,7 +247,7 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) { } size_t remain = size; - while (remain && tud_cdc_n_connected(_instance)) { + while (remain && (tud_cdc_n_connected(_instance) || _ignoreFlowControl)) { size_t wrcount = tud_cdc_n_write(_instance, buffer, remain); remain -= wrcount; buffer += wrcount; diff --git a/src/arduino/Adafruit_USBD_CDC.h b/src/arduino/Adafruit_USBD_CDC.h index 9c0952f3..8d0dab16 100644 --- a/src/arduino/Adafruit_USBD_CDC.h +++ b/src/arduino/Adafruit_USBD_CDC.h @@ -54,6 +54,14 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { void begin(uint32_t baud, uint8_t config); void end(void); + // In some cases, the target application will not assert + // the DTR virtual line, thus preventing writing operations + // to succeed. For this reason, the + // Adafruit_USBD_CDC::ignoreFlowControl() method disables the + // connection’s state verification, enabling the program to + // write on the port, even though the data might be lost. + void ignoreFlowControl(bool ignore = true); + // return line coding set by host uint32_t baud(void); uint8_t stopbits(void); @@ -90,6 +98,8 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { uint8_t _instance; + bool _ignoreFlowControl = false; + bool isValid(void) { return _instance != INVALID_INSTANCE; } }; From e674b684745504f489d76e4af034e2b35db3b206 Mon Sep 17 00:00:00 2001 From: Misaka19465 <19465@misakanet.team> Date: Wed, 18 Sep 2024 22:56:25 +0800 Subject: [PATCH 2/2] Limit Adafruit_USBD_CDC::ignoreFlowcontrol to rp2040 core Adafruit_USBD_CDC::ignoreFlowcontrol added in 0672cb8 may only work for rp2040 for now. So add #ifdef to limit this method to rp2040 core only. --- src/arduino/Adafruit_USBD_CDC.cpp | 9 ++++++++- src/arduino/Adafruit_USBD_CDC.h | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/arduino/Adafruit_USBD_CDC.cpp b/src/arduino/Adafruit_USBD_CDC.cpp index 6345043a..8be4bfe5 100644 --- a/src/arduino/Adafruit_USBD_CDC.cpp +++ b/src/arduino/Adafruit_USBD_CDC.cpp @@ -118,9 +118,11 @@ void Adafruit_USBD_CDC::end(void) { _instance = INVALID_INSTANCE; } +#ifdef ARDUINO_ARCH_RP2040 void Adafruit_USBD_CDC::ignoreFlowControl(bool ignore) { _ignoreFlowControl = ignore; } +#endif uint32_t Adafruit_USBD_CDC::baud(void) { if (!isValid()) { @@ -247,7 +249,12 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) { } size_t remain = size; - while (remain && (tud_cdc_n_connected(_instance) || _ignoreFlowControl)) { +#ifdef ARDUINO_ARCH_RP2040 + while (remain && (tud_cdc_n_connected(_instance) || _ignoreFlowControl)) +#else + while (remain && tud_cdc_n_connected(_instance)) +#endif + { size_t wrcount = tud_cdc_n_write(_instance, buffer, remain); remain -= wrcount; buffer += wrcount; diff --git a/src/arduino/Adafruit_USBD_CDC.h b/src/arduino/Adafruit_USBD_CDC.h index 8d0dab16..793d482e 100644 --- a/src/arduino/Adafruit_USBD_CDC.h +++ b/src/arduino/Adafruit_USBD_CDC.h @@ -54,6 +54,7 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { void begin(uint32_t baud, uint8_t config); void end(void); +#ifdef ARDUINO_ARCH_RP2040 // In some cases, the target application will not assert // the DTR virtual line, thus preventing writing operations // to succeed. For this reason, the @@ -61,6 +62,7 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { // connection’s state verification, enabling the program to // write on the port, even though the data might be lost. void ignoreFlowControl(bool ignore = true); +#endif // return line coding set by host uint32_t baud(void); @@ -98,7 +100,9 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { uint8_t _instance; +#ifdef ARDUINO_ARCH_RP2040 bool _ignoreFlowControl = false; +#endif bool isValid(void) { return _instance != INVALID_INSTANCE; } };