From f565e0251f4f5b7820884517a9b87aa59b83d16b Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:24:00 -0300 Subject: [PATCH 01/15] fix: perimgr + begin Fixes Perimgr setup in begin() Fixes issue with reset INTR mask preventing previous sent data to be read. Adds a INTR mask to end() to clear all pending interrupts. --- cores/esp32/HWCDC.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 7410e257827..771209017ac 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -237,27 +237,33 @@ void HWCDC::begin(unsigned long baud) log_e("HW CDC TX Buffer error"); } } - usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); - usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK); 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); if(!intr_handle && esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, hw_cdc_isr_handler, NULL, &intr_handle) != ESP_OK){ isr_log_e("HW USB CDC failed to init interrupts"); end(); return; } - if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit) && perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit)) { - // Setting USB D+ D- pins - perimanSetPinBus(USB_DM_GPIO_NUM, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1); - perimanSetPinBus(USB_DP_GPIO_NUM, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1); - } else { - log_e("Serial JTAG Pins can't be set into Peripheral Manager."); - } + // Setting USB D+ D- pins + uint8_t pin = ESP32_BUS_TYPE_USB_DM; + perimanSetBusDeinit(pin, HWCDC::deinit); + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT && !perimanClearPinBus(pin)) goto err; + if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; + pin = ESP32_BUS_TYPE_USB_DP; + perimanSetBusDeinit(pin, HWCDC::deinit); + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT && !perimanClearPinBus(pin)) goto err; + if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err; + return; + + err: + log_e("Serial JTAG Pin %u can't be set into Peripheral Manager.", pin); + end(); } void HWCDC::end() { //Disable tx/rx interrupt. - usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); + usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); // disable all + usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK); // clear pending esp_intr_free(intr_handle); intr_handle = NULL; if(tx_lock != NULL) { From eff01f597eaec4b6452269496aa3cf2584ab9eb6 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:37:38 -0300 Subject: [PATCH 02/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 771209017ac..5a2626942ea 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -246,7 +246,7 @@ void HWCDC::begin(unsigned long baud) // Setting USB D+ D- pins uint8_t pin = ESP32_BUS_TYPE_USB_DM; perimanSetBusDeinit(pin, HWCDC::deinit); - if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT && !perimanClearPinBus(pin)) goto err; + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) if(!perimanClearPinBus(pin)) goto err; if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; perimanSetBusDeinit(pin, HWCDC::deinit); From d82143b94e935fa33576409fbc01031a2247c313 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:37:45 -0300 Subject: [PATCH 03/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 5a2626942ea..ef6e119a39f 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -250,7 +250,7 @@ void HWCDC::begin(unsigned long baud) if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; perimanSetBusDeinit(pin, HWCDC::deinit); - if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT && !perimanClearPinBus(pin)) goto err; + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) if(!perimanClearPinBus(pin)) goto err; if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err; return; From d333705d7ce490124f3a00ca55adb720efdc7b8f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:39:50 -0300 Subject: [PATCH 04/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index ef6e119a39f..e6e8385773f 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -262,7 +262,7 @@ void HWCDC::begin(unsigned long baud) void HWCDC::end() { //Disable tx/rx interrupt. - usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); // disable all + usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET); usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK); // clear pending esp_intr_free(intr_handle); intr_handle = NULL; From 1462951878b33f46ec8dc48d43ea72c9548957ea Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:39:57 -0300 Subject: [PATCH 05/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index e6e8385773f..a023f66e338 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -263,7 +263,7 @@ void HWCDC::end() { //Disable tx/rx interrupt. usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET); - usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK); // clear pending + 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_INTR_BUS_RESET); esp_intr_free(intr_handle); intr_handle = NULL; if(tx_lock != NULL) { From e17999cd77e58cff5e0f95b74bf1219bd9e12016 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:47:27 -0300 Subject: [PATCH 06/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index a023f66e338..8ef496bb226 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -237,6 +237,7 @@ void HWCDC::begin(unsigned long baud) log_e("HW CDC TX Buffer error"); } } + usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); 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); if(!intr_handle && esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, hw_cdc_isr_handler, NULL, &intr_handle) != ESP_OK){ isr_log_e("HW USB CDC failed to init interrupts"); From a511d729008f955528097b6792bbf1f8133798a9 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:49:18 -0300 Subject: [PATCH 07/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 8ef496bb226..64f4e243ff9 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -263,8 +263,8 @@ void HWCDC::begin(unsigned long baud) void HWCDC::end() { //Disable tx/rx interrupt. - usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET); - 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_INTR_BUS_RESET); + usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); + usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK); esp_intr_free(intr_handle); intr_handle = NULL; if(tx_lock != NULL) { From 2708729673d0e5d3e08de2042ddbf70d77719cbc Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:50:53 -0300 Subject: [PATCH 08/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 64f4e243ff9..e5dc5f5362d 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -247,7 +247,8 @@ void HWCDC::begin(unsigned long baud) // Setting USB D+ D- pins uint8_t pin = ESP32_BUS_TYPE_USB_DM; perimanSetBusDeinit(pin, HWCDC::deinit); - if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) if(!perimanClearPinBus(pin)) goto err; + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) + if(!perimanClearPinBus(pin)) goto err; if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; perimanSetBusDeinit(pin, HWCDC::deinit); From 4969f13610b0f1e0472759aace779d4f69cfe1c1 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 08:51:14 -0300 Subject: [PATCH 09/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index e5dc5f5362d..0ef90093dda 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -252,7 +252,8 @@ void HWCDC::begin(unsigned long baud) if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; perimanSetBusDeinit(pin, HWCDC::deinit); - if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) if(!perimanClearPinBus(pin)) goto err; + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) + if(!perimanClearPinBus(pin)) goto err; if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err; return; From 9333624a5fc65870224208cc82498c714f9265c7 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 09:00:47 -0300 Subject: [PATCH 10/15] Update HWCDC.cpp --- cores/esp32/HWCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 0ef90093dda..8c2d301152a 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -264,7 +264,7 @@ void HWCDC::begin(unsigned long baud) void HWCDC::end() { - //Disable tx/rx interrupt. + //Disable/clear/free tx/rx interrupt. usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK); usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_LL_INTR_MASK); esp_intr_free(intr_handle); From 9a8ca7b3f421f846ec772c54b623e304a7655c52 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 4 Mar 2024 09:34:12 -0300 Subject: [PATCH 11/15] Update HWCDC.cpp --- cores/esp32/HWCDC.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 8c2d301152a..0e55db30f47 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -177,7 +177,8 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) { } HWCDC::HWCDC() { - + perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit); + perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit); } HWCDC::~HWCDC(){ @@ -246,12 +247,10 @@ void HWCDC::begin(unsigned long baud) } // Setting USB D+ D- pins uint8_t pin = ESP32_BUS_TYPE_USB_DM; - perimanSetBusDeinit(pin, HWCDC::deinit); if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) if(!perimanClearPinBus(pin)) goto err; if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; - perimanSetBusDeinit(pin, HWCDC::deinit); if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) if(!perimanClearPinBus(pin)) goto err; if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err; From 631b7b76a5f4b9c92ad2eb37334af5e0e4ee0e0f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 5 Mar 2024 08:09:49 -0300 Subject: [PATCH 12/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 0e55db30f47..1deeee9cf6f 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -247,8 +247,11 @@ void HWCDC::begin(unsigned long baud) } // Setting USB D+ D- pins uint8_t pin = ESP32_BUS_TYPE_USB_DM; - if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) - if(!perimanClearPinBus(pin)) goto err; + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){ + if(!perimanClearPinBus(pin)){ + goto err; + } + } if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) From 05a35cdc02bb8f72fe4709a3f931e5fa77725d69 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 5 Mar 2024 08:11:01 -0300 Subject: [PATCH 13/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 1deeee9cf6f..f1d3810287c 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -254,8 +254,11 @@ void HWCDC::begin(unsigned long baud) } if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; pin = ESP32_BUS_TYPE_USB_DP; - if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT) - if(!perimanClearPinBus(pin)) goto err; + if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){ + if(!perimanClearPinBus(pin)){ + goto err; + } + } if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err; return; From 5f6373c80ae4eeb0628c5b903f63ff5e4bd7a270 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 5 Mar 2024 08:12:49 -0300 Subject: [PATCH 14/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index f1d3810287c..6a1de62e6d6 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -252,7 +252,9 @@ void HWCDC::begin(unsigned long baud) goto err; } } - if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err; + if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)){ + goto err; + } pin = ESP32_BUS_TYPE_USB_DP; if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){ if(!perimanClearPinBus(pin)){ From bd79884623d4f88f0326774abcf0cc58ec6661c0 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 5 Mar 2024 08:13:31 -0300 Subject: [PATCH 15/15] Update cores/esp32/HWCDC.cpp --- cores/esp32/HWCDC.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 6a1de62e6d6..6657af79fd4 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -261,7 +261,9 @@ void HWCDC::begin(unsigned long baud) goto err; } } - if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err; + if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)){ + goto err; + } return; err: