24
24
#include " esp_intr_alloc.h"
25
25
#include " soc/periph_defs.h"
26
26
#include " soc/io_mux_reg.h"
27
+ #include " soc/usb_serial_jtag_struct.h"
27
28
#pragma GCC diagnostic ignored "-Wvolatile"
28
29
#include " hal/usb_serial_jtag_ll.h"
29
30
#pragma GCC diagnostic warning "-Wvolatile"
@@ -86,7 +87,7 @@ static void hw_cdc_isr_handler(void *arg) {
86
87
} else {
87
88
connected = true ;
88
89
}
89
- if (usb_serial_jtag_ll_txfifo_writable () == 1 ) {
90
+ if (tx_ring_buf != NULL && usb_serial_jtag_ll_txfifo_writable () == 1 ) {
90
91
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
91
92
usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
92
93
size_t queued_size;
@@ -164,6 +165,9 @@ bool HWCDC::isCDC_Connected()
164
165
}
165
166
166
167
static void ARDUINO_ISR_ATTR cdc0_write_char (char c) {
168
+ if (tx_ring_buf == NULL ) {
169
+ return ;
170
+ }
167
171
uint32_t tx_timeout_ms = 0 ;
168
172
if (HWCDC::isConnected ()) {
169
173
tx_timeout_ms = requested_tx_timeout_ms;
@@ -238,32 +242,33 @@ void HWCDC::begin(unsigned long baud)
238
242
log_e (" HW CDC TX Buffer error" );
239
243
}
240
244
}
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 ;
241
265
usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_LL_INTR_MASK);
242
266
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);
243
267
if (!intr_handle && esp_intr_alloc (ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0 , hw_cdc_isr_handler, NULL , &intr_handle) != ESP_OK){
244
268
isr_log_e (" HW USB CDC failed to init interrupts" );
245
269
end ();
246
270
return ;
247
271
}
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
- }
267
272
return ;
268
273
269
274
err:
@@ -289,6 +294,7 @@ void HWCDC::end()
289
294
arduino_hw_cdc_event_loop_handle = NULL ;
290
295
}
291
296
HWCDC::deinit (this );
297
+ setDebugOutput (false );
292
298
connected = false ;
293
299
}
294
300
0 commit comments