Skip to content
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

Workaround for when USB CDC is unplugged #7583

Merged
merged 4 commits into from
Dec 14, 2022
Merged

Conversation

SuGlider
Copy link
Collaborator

Description of Change

By default Tx Timeout is 200 and it causes a delay every time that HW USB CDC is written when the USB is unplugged.
Setting it to 0 by default solves this issue and it has no side effect.
As stated in the issue #6983, it affects boards that use Batteries and are not connected to a USB port.

Tests scenarios

This issue affects ESP32-C2 and ESP32-S3 with HW Serial.

Related links

Fix #6983

@SuGlider SuGlider added Chip: ESP32-C3 Issue is related to support of ESP32-C3 Chip Chip: ESP32-S3 Issue is related to support of ESP32-S3 Chip Area: Peripherals API Relates to peripheral's APIs. labels Dec 12, 2022
@SuGlider SuGlider added this to the 2.0.6 milestone Dec 12, 2022
@SuGlider SuGlider self-assigned this Dec 12, 2022
@@ -32,7 +32,7 @@ static uint8_t rx_data_buf[64];
static intr_handle_t intr_handle = NULL;
static volatile bool initial_empty = false;
static xSemaphoreHandle tx_lock = NULL;
static uint32_t tx_timeout_ms = 200;
static uint32_t tx_timeout_ms = 0; // workaround for when USB CDC is not connected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this being 0, it means that is the ring buffer is full, the code will not give it time to empty some space and will just discard the input. Maybe wait if the is not enough space? Fixing it for when USB is disconnected, but breaking it for when connected is not all that nice :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting when there is no space (timeout) is exacty the issue... when the board is turned on with the USB unplugged. That is what causes the delay from the point when the RingBuffer gets full and it never is flushed because there is no USB connection.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no event that tells the user that USB is or not plugged. Even the ARDUINO_HW_CDC_CONNECTED_EVENT works only the first time, but if we unplug the board, it doesn't tell anything.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@me-no-dev - Added code to test if USB is plugged and the application has never changed Tx Timeout. In that case, it will set Tx Timeout to default 100ms.
Otherwise (USB has never been plugged or the Application has explicitly changed Tx Timeout Value), it will keep it zero or the new value set by the application.

Sets back default TX timeout whenever USB is plugged, otherwise it is kept as zero.
@me-no-dev me-no-dev merged commit 1d595fd into master Dec 14, 2022
@me-no-dev me-no-dev deleted the USB-CDC-Unplugged branch December 14, 2022 10:47
Human pushed a commit to Human/arduino-esp32 that referenced this pull request Dec 17, 2022
* Workaround for when USB CDC is unplugged

* Considers default TX timeout

Sets back default TX timeout whenever USB is plugged, otherwise it is kept as zero.

* fixed left over code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Peripherals API Relates to peripheral's APIs. Chip: ESP32-C3 Issue is related to support of ESP32-C3 Chip Chip: ESP32-S3 Issue is related to support of ESP32-S3 Chip
Projects
Development

Successfully merging this pull request may close these issues.

ESP32C3M1: Serial.print() calls when not connected to USB cause delays.
2 participants