Skip to content

Commit 3190bd5

Browse files
authored
feat(hwcdc): Refactor HWCDC constructor for bus deinitialization
Refactor HWCDC constructor to conditionally set bus deinitialization for USB DM and DP.
1 parent 5954b23 commit 3190bd5

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

cores/esp32/HWCDC.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
253253
}
254254

255255
HWCDC::HWCDC() {
256-
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit);
257-
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit);
258256
// SOF in ISR causes problems for uploading firmware
259257
// lastSOF_ms = 0;
260258
// SOF_TIMEOUT = 5;
@@ -323,15 +321,19 @@ void HWCDC::begin(unsigned long baud) {
323321
// delay(10); // USB Host has to enumerate it again
324322

325323
// Peripheral Manager setting for USB D+ D- pins
326-
uint8_t pin = USB_INT_PHY0_DM_GPIO_NUM;
327-
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) {
324+
// Peripheral Manager setting for USB D+ D- pins
325+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DM) == NULL) {
326+
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit);
327+
}
328+
if (!perimanSetPinBus(USB_INT_PHY0_DM_GPIO_NUM, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) {
328329
goto err;
329330
}
330-
pin = USB_INT_PHY0_DP_GPIO_NUM;
331-
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) {
331+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DP) == NULL) {
332+
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit);
333+
}
334+
if (!perimanSetPinBus(USB_INT_PHY0_DP_GPIO_NUM, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) {
332335
goto err;
333336
}
334-
335337
// Configure PHY
336338
// USB_Serial_JTAG use internal PHY
337339
USB_SERIAL_JTAG.conf0.phy_sel = 0;

0 commit comments

Comments
 (0)