From 2a24cc206efb4c92b5a2e40fb71db7650d9cf112 Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Mon, 17 Jun 2024 10:46:34 +0200 Subject: [PATCH] fix(console): USB Serial JTAG freezes when input received before init When data was sent through USB Serial JTAG before the driver was installed, the bus was malfunctioning. This was because the interrupt bit for data reception was cleared regardless of whether data was received or not. Consequently, usb_serial_jtag_isr_handler_default was not triggered and the data was never read causing the bus to malfunction. This commit is modifying usb_serial_jtag_driver_install to prevent clearing USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT and USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY thus allowing the callback usb_serial_jtag_isr_handler_default to trigger for possible data exchanged prior to the call to usb_serial_jtag_driver_install. This commit also modified the while logic in linenoiseProbe to discard any data that doesn't match the expected chaaracter sequences to prevent random input from interfering with evaluating whether the terminal supports escape sequences or not. See https://github.com/espressif/esp-idf/issues/13940 --- components/console/linenoise/linenoise.c | 6 +++--- .../esp_driver_usb_serial_jtag/src/usb_serial_jtag.c | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/components/console/linenoise/linenoise.c b/components/console/linenoise/linenoise.c index b4e8409af90..e585ab7e89c 100644 --- a/components/console/linenoise/linenoise.c +++ b/components/console/linenoise/linenoise.c @@ -1091,9 +1091,9 @@ int linenoiseProbe(void) { if (cb < 0) { continue; } - if (read_bytes == 0 && c != '\x1b') { - /* invalid response */ - break; + if (read_bytes == 0 && c != ESC) { + /* invalid response, try again until the timeout triggers */ + continue; } read_bytes += cb; } diff --git a/components/esp_driver_usb_serial_jtag/src/usb_serial_jtag.c b/components/esp_driver_usb_serial_jtag/src/usb_serial_jtag.c index e03b4efc764..790aed174cf 100644 --- a/components/esp_driver_usb_serial_jtag/src/usb_serial_jtag.c +++ b/components/esp_driver_usb_serial_jtag/src/usb_serial_jtag.c @@ -193,10 +193,14 @@ esp_err_t usb_serial_jtag_driver_install(usb_serial_jtag_driver_config_t *usb_se usb_serial_jtag_ll_phy_set_defaults(); // External PHY not supported. Set default values. #endif // USB_WRAP_LL_EXT_PHY_SUPPORTED - usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | - USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT); - usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | - USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT); + // Note: DO NOT clear the interrupt status bits here. The output routine needs + // USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY set because it needs the ISR to trigger + // as soon as data is sent; the input routine needs the status to retrieve any + // data that is still in the FIFOs. + + // We only enable the RX interrupt; we'll enable the TX one when we actually + // have anything to send. + usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT); err = esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, usb_serial_jtag_isr_handler_default, NULL, &p_usb_serial_jtag_obj->intr_handle); if (err != ESP_OK) {