Skip to content

Commit 0a26a8c

Browse files
authored
Fixes HW Serial attaching pins, PHY initialization, crash after end() (#9365)
* fix: HWCDC pin number Fixes HW Serial pin setup. The pins were set up to the wrong value and it could not be correctly configured and used. * fix: PHY initialization Fixes the PHY initialization. After detaching the pin and ending the HW Serial, a new begin() wouldn't start the CDC because it lacked the proper PHY initialization. * fix: crashing end() Fixes a crash when calling end() end() treminanates the `tx_ring_buf` but it was not tested for NULL in the ISR and in the cdc0_write_char() causing a crash if those are used. This depends on events and happens eventually. * reduces number of debug messages * fix git stash/commit added lines * fixes usb_phy_ll include and call * roll back * solves HWSerial initialization * fixes C6|H2 issue issue with `if(Serial)` not working always * github commit problem * fixes
1 parent 2479efb commit 0a26a8c

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

cores/esp32/HWCDC.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "esp_intr_alloc.h"
2525
#include "soc/periph_defs.h"
2626
#include "soc/io_mux_reg.h"
27+
#include "soc/usb_serial_jtag_struct.h"
2728
#pragma GCC diagnostic ignored "-Wvolatile"
2829
#include "hal/usb_serial_jtag_ll.h"
2930
#pragma GCC diagnostic warning "-Wvolatile"
@@ -86,7 +87,7 @@ static void hw_cdc_isr_handler(void *arg) {
8687
} else {
8788
connected = true;
8889
}
89-
if (usb_serial_jtag_ll_txfifo_writable() == 1) {
90+
if (tx_ring_buf != NULL && usb_serial_jtag_ll_txfifo_writable() == 1) {
9091
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
9192
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
9293
size_t queued_size;
@@ -164,6 +165,9 @@ bool HWCDC::isCDC_Connected()
164165
}
165166

166167
static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
168+
if(tx_ring_buf == NULL) {
169+
return;
170+
}
167171
uint32_t tx_timeout_ms = 0;
168172
if(HWCDC::isConnected()) {
169173
tx_timeout_ms = requested_tx_timeout_ms;
@@ -238,32 +242,33 @@ void HWCDC::begin(unsigned long baud)
238242
log_e("HW CDC TX Buffer error");
239243
}
240244
}
245+
246+
// the HW Serial pins needs to be first deinited in order to allow `if(Serial)` to work :-(
247+
deinit(NULL);
248+
delay(10); // USB Host has to enumerate it again
249+
250+
// Peripheral Manager setting for USB D+ D- pins
251+
uint8_t pin = USB_DM_GPIO_NUM;
252+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err;
253+
pin = USB_DP_GPIO_NUM;
254+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err;
255+
256+
// Configure PHY
257+
// USB_Serial_JTAG use internal PHY
258+
USB_SERIAL_JTAG.conf0.phy_sel = 0;
259+
// Disable software control USB D+ D- pullup pulldown (Device FS: dp_pullup = 1)
260+
USB_SERIAL_JTAG.conf0.pad_pull_override = 0;
261+
// Enable USB D+ pullup
262+
USB_SERIAL_JTAG.conf0.dp_pullup = 1;
263+
// Enable USB pad function
264+
USB_SERIAL_JTAG.conf0.usb_pad_enable = 1;
241265
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
242266
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);
243267
if(!intr_handle && esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, hw_cdc_isr_handler, NULL, &intr_handle) != ESP_OK){
244268
isr_log_e("HW USB CDC failed to init interrupts");
245269
end();
246270
return;
247271
}
248-
// Setting USB D+ D- pins
249-
uint8_t pin = ESP32_BUS_TYPE_USB_DM;
250-
if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){
251-
if(!perimanClearPinBus(pin)){
252-
goto err;
253-
}
254-
}
255-
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)){
256-
goto err;
257-
}
258-
pin = ESP32_BUS_TYPE_USB_DP;
259-
if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){
260-
if(!perimanClearPinBus(pin)){
261-
goto err;
262-
}
263-
}
264-
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)){
265-
goto err;
266-
}
267272
return;
268273

269274
err:
@@ -289,6 +294,7 @@ void HWCDC::end()
289294
arduino_hw_cdc_event_loop_handle = NULL;
290295
}
291296
HWCDC::deinit(this);
297+
setDebugOutput(false);
292298
connected = false;
293299
}
294300

0 commit comments

Comments
 (0)