Skip to content

Fixed slowdown with repeated CDC bool operator test #3624

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions hardware/arduino/avr/cores/arduino/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ typedef struct
u8 bParityType;
u8 bDataBits;
u8 lineState;
u8 prevLineState;
} LineInfo;

static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00, 0x00 };

#define WEAK __attribute__ ((weak))

Expand Down Expand Up @@ -177,7 +178,7 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
// TODO - ZE - check behavior on different OSes and test what happens if an
// open connection isn't broken cleanly (cable is yanked out, host dies
// or locks up, or host virtual serial port hangs)
if (_usbLineInfo.lineState > 0) {
if (this) {
int r = USB_Send(CDC_TX,buffer,size);
if (r > 0) {
return r;
Expand All @@ -196,13 +197,19 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
// setup() before printing to ensure that an application on the host is
// actually ready to receive and display the data.
// We add a short delay before returning to fix a bug observed by Federico
// where the port is configured (lineState != 0) but not quite opened.
// where the port is configured (lineState != 0) but not quite opened. This
// only happens when the line state transitions from ==0 to >0 otherwise you
// get a nasty delay when one isn't needed.
Serial_::operator bool() {
bool result = false;
if (_usbLineInfo.lineState > 0)
result = true;
delay(10);
return result;
bool result = false;
if (_usbLineInfo.lineState > 0) {
result = true;
if (_usbLineInfo.prevLineState == 0) {
delay(10);
}
}
_usbLineInfo.prevLineState = _usbLineInfo.lineState;
return result;
}

Serial_ Serial;
Expand Down