Skip to content

Workaround for when USB CDC is unplugged #7583

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 4 commits into from
Dec 14, 2022
Merged
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion cores/esp32/HWCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

static esp_event_loop_handle_t arduino_hw_cdc_event_loop_handle = NULL;

static esp_err_t arduino_hw_cdc_event_post(esp_event_base_t event_base, int32_t event_id, void *event_data, size_t event_data_size, BaseType_t *task_unblocked){
Expand Down