Skip to content

Commit 6adeca4

Browse files
me-no-devSuGliderpre-commit-ci-lite[bot]
authored
fix(cdc): Disable SOF interrupt and CDC reset on begin() (#9628)
* fix(cdc): Disable SOF interrupt and CDC reset on begin() * feat(jtag/hwcdc): uses SOF detection from IDF Restores back IDF 5.1 SOF detection method in order to fix the HW CDC uploading process. Enabling SOF mask in the ISR routine causes a problem with esptool uploading when using CDC/JTAG port. * feat(jtag/hwcdc): uses SOF detection from IDF Restores back IDF 5.1 SOF detection method in order to fix the HW CDC uploading process. Enabling SOF mask in the ISR routine causes a problem with esptool uploading when using CDC/JTAG port. * feat: revert include This include is not necessary here. Moving it back to the HWCDC.cpp file. * feat: adding a necessary include Adding the IDF 5.1 SOF check include file. Necessary to make it compile. Moved from HWCDC.h file to here. * feat: move function call to header file * feat: Moved SOF function * feat: Removed unused header file * fix: Use correct SOF header file * ci(pre-commit): Apply automatic fixes * Small fixes for Debug prints on C3, C6 and H2 * fix(usb): Fix log prints --------- Co-authored-by: Rodrigo Garcia <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7acd875 commit 6adeca4

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

Diff for: cores/esp32/HWCDC.cpp

+34-22
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ static intr_handle_t intr_handle = NULL;
3939
static SemaphoreHandle_t tx_lock = NULL;
4040
static volatile bool connected = false;
4141

42-
static volatile unsigned long lastSOF_ms;
43-
static volatile uint8_t SOF_TIMEOUT;
42+
// SOF in ISR causes problems for uploading firmware
43+
//static volatile unsigned long lastSOF_ms;
44+
//static volatile uint8_t SOF_TIMEOUT;
4445

4546
// timeout has no effect when USB CDC is unplugged
4647
static uint32_t tx_timeout_ms = 100;
@@ -90,7 +91,7 @@ static void hw_cdc_isr_handler(void *arg) {
9091
if (tx_ring_buf != NULL && usb_serial_jtag_ll_txfifo_writable() == 1) {
9192
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
9293
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
93-
size_t queued_size;
94+
size_t queued_size = 0;
9495
uint8_t *queued_buff = (uint8_t *)xRingbufferReceiveUpToFromISR(tx_ring_buf, &queued_size, 64);
9596
// If the hardware fifo is available, write in it. Otherwise, do nothing.
9697
if (queued_buff != NULL) { //Although tx_queued_bytes may be larger than 0. We may have interrupt before xRingbufferSend() was called.
@@ -134,19 +135,23 @@ static void hw_cdc_isr_handler(void *arg) {
134135
connected = false;
135136
}
136137

137-
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_SOF) {
138-
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SOF);
139-
lastSOF_ms = millis();
140-
}
138+
// SOF ISR is causing esptool to be unable to upload firmware to the board
139+
// if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_SOF) {
140+
// usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SOF);
141+
// lastSOF_ms = millis();
142+
// }
141143

142144
if (xTaskWoken == pdTRUE) {
143145
portYIELD_FROM_ISR();
144146
}
145147
}
146148

147-
inline bool HWCDC::isPlugged(void) {
148-
return (lastSOF_ms + SOF_TIMEOUT) >= millis();
149-
}
149+
// Moved to header file as inline function. Kept just as future reference.
150+
//inline bool HWCDC::isPlugged(void) {
151+
// SOF ISR is causing esptool to be unable to upload firmware to the board
152+
// Timer test for SOF seems to work when uploading firmware
153+
// return usb_serial_jtag_is_connected();//(lastSOF_ms + SOF_TIMEOUT) >= millis();
154+
//}
150155

151156
bool HWCDC::isCDC_Connected() {
152157
static bool running = false;
@@ -155,11 +160,13 @@ bool HWCDC::isCDC_Connected() {
155160
if (!isPlugged()) {
156161
connected = false;
157162
running = false;
158-
SOF_TIMEOUT = 5; // SOF timeout when unplugged
163+
// SOF in ISR causes problems for uploading firmware
164+
//SOF_TIMEOUT = 5; // SOF timeout when unplugged
159165
return false;
160-
} else {
161-
SOF_TIMEOUT = 50; // SOF timeout when plugged
162166
}
167+
//else {
168+
// SOF_TIMEOUT = 50; // SOF timeout when plugged
169+
//}
163170

164171
if (connected) {
165172
running = false;
@@ -242,13 +249,15 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
242249
xRingbufferSend(tx_ring_buf, (void *)(&c), 1, tx_timeout_ms / portTICK_PERIOD_MS);
243250
}
244251
usb_serial_jtag_ll_txfifo_flush();
252+
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
245253
}
246254

247255
HWCDC::HWCDC() {
248256
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit);
249257
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit);
250-
lastSOF_ms = 0;
251-
SOF_TIMEOUT = 5;
258+
// SOF in ISR causes problems for uploading firmware
259+
// lastSOF_ms = 0;
260+
// SOF_TIMEOUT = 5;
252261
}
253262

254263
HWCDC::~HWCDC() {
@@ -309,8 +318,9 @@ void HWCDC::begin(unsigned long baud) {
309318
}
310319

311320
// the HW Serial pins needs to be first deinited in order to allow `if(Serial)` to work :-(
312-
deinit(NULL);
313-
delay(10); // USB Host has to enumerate it again
321+
// But this is also causing terminal to hang, so they are disabled
322+
// deinit(NULL);
323+
// delay(10); // USB Host has to enumerate it again
314324

315325
// Peripheral Manager setting for USB D+ D- pins
316326
uint8_t pin = USB_DM_GPIO_NUM;
@@ -332,9 +342,11 @@ void HWCDC::begin(unsigned long baud) {
332342
// Enable USB pad function
333343
USB_SERIAL_JTAG.conf0.usb_pad_enable = 1;
334344
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
335-
usb_serial_jtag_ll_ena_intr_mask(
336-
USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET | USB_SERIAL_JTAG_INTR_SOF
337-
);
345+
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET);
346+
// SOF ISR is causing esptool to be unable to upload firmware to the board
347+
// usb_serial_jtag_ll_ena_intr_mask(
348+
// USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET | USB_SERIAL_JTAG_INTR_SOF
349+
// );
338350
if (!intr_handle && esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, hw_cdc_isr_handler, NULL, &intr_handle) != ESP_OK) {
339351
isr_log_e("HW USB CDC failed to init interrupts");
340352
end();
@@ -587,9 +599,9 @@ size_t HWCDC::read(uint8_t *buffer, size_t size) {
587599
void HWCDC::setDebugOutput(bool en) {
588600
if (en) {
589601
uartSetDebug(NULL);
590-
ets_install_putc1((void (*)(char)) & cdc0_write_char);
602+
ets_install_putc2((void (*)(char)) & cdc0_write_char);
591603
} else {
592-
ets_install_putc1(NULL);
604+
ets_install_putc2(NULL);
593605
}
594606
}
595607

Diff for: cores/esp32/HWCDC.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <inttypes.h>
2222
#include "esp_event.h"
2323
#include "Stream.h"
24+
#include "driver/usb_serial_jtag.h"
2425

2526
ESP_EVENT_DECLARE_BASE(ARDUINO_HW_CDC_EVENTS);
2627

@@ -69,7 +70,12 @@ class HWCDC : public Stream {
6970
size_t write(const uint8_t *buffer, size_t size);
7071
void flush(void);
7172

72-
static bool isPlugged(void);
73+
inline static bool isPlugged(void) {
74+
// SOF ISR is causing esptool to be unable to upload firmware to the board
75+
// Using IDF 5.1 helper function because it is based on Timer check instead of ISR
76+
return usb_serial_jtag_is_connected();
77+
}
78+
7379
inline static bool isConnected(void) {
7480
return isCDC_Connected();
7581
}

Diff for: cores/esp32/USBCDC.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ uint32_t USBCDC::baudRate() {
417417
void USBCDC::setDebugOutput(bool en) {
418418
if (en) {
419419
uartSetDebug(NULL);
420-
ets_install_putc1((void (*)(char)) & cdc0_write_char);
420+
ets_install_putc2((void (*)(char)) & cdc0_write_char);
421421
} else {
422-
ets_install_putc1(NULL);
422+
ets_install_putc2(NULL);
423423
}
424424
}
425425

Diff for: cores/esp32/esp32-hal-uart.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ void uart_install_putc() {
828828
#endif
829829
default: ets_install_putc1(NULL); break;
830830
}
831+
ets_install_putc2(NULL);
831832
}
832833

833834
// Routines that take care of UART mode in the HardwareSerial Class code
@@ -878,7 +879,7 @@ int log_printfv(const char *format, va_list arg) {
878879
}
879880
#endif
880881
*/
881-
#if CONFIG_IDF_TARGET_ESP32C3
882+
#if CONFIG_IDF_TARGET_ESP32C3 || ((CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C6) && ARDUINO_USB_CDC_ON_BOOT)
882883
vsnprintf(temp, len + 1, format, arg);
883884
ets_printf("%s", temp);
884885
#else

0 commit comments

Comments
 (0)