Skip to content

Commit 9e611cf

Browse files
authored
fix: HW CDC write()
When writing a stream of data to the HW CDC of S3/C3/C6/H2, the SoC looses some bytes and don't send them all. This fix makes it work fine.
1 parent 29ede48 commit 9e611cf

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

cores/esp32/HWCDC.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,14 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
367367
if(xSemaphoreTake(tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
368368
return 0;
369369
}
370-
size_t max_size = xRingbufferGetMaxItemSize(tx_ring_buf);
371370
size_t space = xRingbufferGetCurFreeSize(tx_ring_buf);
372371
size_t to_send = size, so_far = 0;
373372

374373
if(space > size){
375374
space = size;
376375
}
377376
// Non-Blocking method, Sending data to ringbuffer, and handle the data in ISR.
378-
if(xRingbufferSend(tx_ring_buf, (void*) (buffer), space, 0) != pdTRUE){
377+
if(space > 0 && xRingbufferSend(tx_ring_buf, (void*) (buffer), space, 0) != pdTRUE){
379378
size = 0;
380379
} else {
381380
to_send -= space;
@@ -385,16 +384,17 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
385384
if(connected) usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
386385

387386
while(to_send){
388-
if(max_size > to_send){
389-
max_size = to_send;
387+
space = xRingbufferGetCurFreeSize(tx_ring_buf);
388+
if(space > to_send){
389+
space = to_send;
390390
}
391391
// Blocking method, Sending data to ringbuffer, and handle the data in ISR.
392-
if(xRingbufferSend(tx_ring_buf, (void*) (buffer+so_far), max_size, tx_timeout_ms / portTICK_PERIOD_MS) != pdTRUE){
392+
if(xRingbufferSend(tx_ring_buf, (void*) (buffer+so_far), space, tx_timeout_ms / portTICK_PERIOD_MS) != pdTRUE){
393393
size = so_far;
394394
break;
395395
}
396-
so_far += max_size;
397-
to_send -= max_size;
396+
so_far += space;
397+
to_send -= space;
398398
// Now trigger the ISR to read data from the ring buffer.
399399
usb_serial_jtag_ll_txfifo_flush();
400400
if(connected) usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);

0 commit comments

Comments
 (0)