Skip to content

Commit 911337d

Browse files
2 parents 5761cfe + b49f56b commit 911337d

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
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

variants/heltec_capsule_sensor_v3/pins_arduino.h

-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#define USB_VID 0x303a
1010
#define USB_PID 0x1001
1111

12-
#define EXTERNAL_NUM_INTERRUPTS 46
13-
#define NUM_DIGITAL_PINS 48
14-
#define NUM_ANALOG_INPUTS 20
1512

1613
// Some boards have too low voltage on this pin (board design bug)
1714
// Use different pin with 3V and connect with 48
@@ -22,9 +19,6 @@ static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+48;
2219
#define RGB_BUILTIN LED_BUILTIN
2320
#define RGB_BRIGHTNESS 64
2421

25-
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)
26-
#define digitalPinToInterrupt(p) (((p)<48)?(p):-1)
27-
#define digitalPinHasPWM(p) (p < 46)
2822

2923
static const uint8_t TX = 43;
3024
static const uint8_t RX = 44;

variants/heltec_ht_de01/pins_arduino.h

-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66
#define HT_DE01 true
77

8-
#define EXTERNAL_NUM_INTERRUPTS 16
9-
#define NUM_DIGITAL_PINS 40
10-
#define NUM_ANALOG_INPUTS 16
11-
12-
#define analogInputToDigitalPin(p) (((p)<20)?(analogChannelToDigitalPin(p)):-1)
13-
#define digitalPinToInterrupt(p) (((p)<40)?(p):-1)
14-
#define digitalPinHasPWM(p) (p < 34)
158

169
static const uint8_t LED_BUILTIN = 35;
1710
#define BUILTIN_LED LED_BUILTIN // backward compatibility

variants/heltec_wireless_mini_shell/pins_arduino.h

-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55
#include "soc/soc_caps.h"
66

77
#define WIRELESS_MINI_SHELL true
8-
#define EXTERNAL_NUM_INTERRUPTS 22
9-
#define NUM_DIGITAL_PINS 22
10-
#define NUM_ANALOG_INPUTS 6
118

129
static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT+8;
1310
#define BUILTIN_LED LED_BUILTIN // backward compatibility
1411
#define LED_BUILTIN LED_BUILTIN
1512
#define RGB_BUILTIN LED_BUILTIN
1613
#define RGB_BRIGHTNESS 64
1714

18-
#define analogInputToDigitalPin(p) (((p)<NUM_ANALOG_INPUTS)?(analogChannelToDigitalPin(p)):-1)
19-
#define digitalPinToInterrupt(p) (((p)<NUM_DIGITAL_PINS)?(p):-1)
20-
#define digitalPinHasPWM(p) (p < EXTERNAL_NUM_INTERRUPTS)
2115

2216
static const uint8_t TX = 21;
2317
static const uint8_t RX = 20;

0 commit comments

Comments
 (0)