@@ -177,7 +177,8 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
177177}
178178
179179HWCDC::HWCDC () {
180-
180+ perimanSetBusDeinit (ESP32_BUS_TYPE_USB_DM, HWCDC::deinit);
181+ perimanSetBusDeinit (ESP32_BUS_TYPE_USB_DP, HWCDC::deinit);
181182}
182183
183184HWCDC::~HWCDC (){
@@ -238,26 +239,43 @@ void HWCDC::begin(unsigned long baud)
238239 }
239240 }
240241 usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_LL_INTR_MASK);
241- usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_LL_INTR_MASK);
242242 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);
243243 if (!intr_handle && esp_intr_alloc (ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0 , hw_cdc_isr_handler, NULL , &intr_handle) != ESP_OK){
244244 isr_log_e (" HW USB CDC failed to init interrupts" );
245245 end ();
246246 return ;
247247 }
248- if (perimanSetBusDeinit (ESP32_BUS_TYPE_USB_DM, HWCDC::deinit) && perimanSetBusDeinit (ESP32_BUS_TYPE_USB_DP, HWCDC::deinit)) {
249- // Setting USB D+ D- pins
250- perimanSetPinBus (USB_DM_GPIO_NUM, ESP32_BUS_TYPE_USB_DM, (void *) this , -1 , -1 );
251- perimanSetPinBus (USB_DP_GPIO_NUM, ESP32_BUS_TYPE_USB_DP, (void *) this , -1 , -1 );
252- } else {
253- log_e (" Serial JTAG Pins can't be set into Peripheral Manager." );
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;
254257 }
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+ return ;
268+
269+ err:
270+ log_e (" Serial JTAG Pin %u can't be set into Peripheral Manager." , pin);
271+ end ();
255272}
256273
257274void HWCDC::end ()
258275{
259- // Disable tx/rx interrupt.
276+ // Disable/clear/free tx/rx interrupt.
260277 usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_LL_INTR_MASK);
278+ usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_LL_INTR_MASK);
261279 esp_intr_free (intr_handle);
262280 intr_handle = NULL ;
263281 if (tx_lock != NULL ) {
0 commit comments