15
15
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
16
16
17
17
#include " esp32-hal.h"
18
+ #include " esp32-hal-periman.h"
18
19
#include " HWCDC.h"
19
20
#include " freertos/FreeRTOS.h"
20
21
#include " freertos/semphr.h"
21
22
#include " freertos/queue.h"
22
23
#include " freertos/ringbuf.h"
23
24
#include " esp_intr_alloc.h"
24
25
#include " soc/periph_defs.h"
26
+ #include " soc/io_mux_reg.h"
25
27
#pragma GCC diagnostic ignored "-Wvolatile"
26
28
#include " hal/usb_serial_jtag_ll.h"
27
29
#pragma GCC diagnostic warning "-Wvolatile"
@@ -171,6 +173,28 @@ void HWCDC::onEvent(arduino_hw_cdc_event_t event, esp_event_handler_t callback){
171
173
arduino_hw_cdc_event_handler_register_with (ARDUINO_HW_CDC_EVENTS, event, callback, this );
172
174
}
173
175
176
+ bool HWCDC::deinit (void * busptr)
177
+ {
178
+ // avoid any recursion issue with Peripheral Manager perimanSetPinBus() call
179
+ static bool running = false ;
180
+ if (running) return true ;
181
+ running = true ;
182
+ // Setting USB D+ D- pins
183
+ bool retCode = true ;
184
+ retCode &= perimanSetPinBus (USB_DM_GPIO_NUM, ESP32_BUS_TYPE_INIT, NULL );
185
+ retCode &= perimanSetPinBus (USB_DP_GPIO_NUM, ESP32_BUS_TYPE_INIT, NULL );
186
+ if (retCode) {
187
+ // Force the host to re-enumerate (BUS_RESET)
188
+ pinMode (USB_DM_GPIO_NUM, OUTPUT_OPEN_DRAIN);
189
+ pinMode (USB_DP_GPIO_NUM, OUTPUT_OPEN_DRAIN);
190
+ digitalWrite (USB_DM_GPIO_NUM, LOW);
191
+ digitalWrite (USB_DP_GPIO_NUM, LOW);
192
+ }
193
+ // release the flag
194
+ running = false ;
195
+ return retCode;
196
+ }
197
+
174
198
void HWCDC::begin (unsigned long baud)
175
199
{
176
200
if (tx_lock == NULL ) {
@@ -187,6 +211,14 @@ void HWCDC::begin(unsigned long baud)
187
211
end ();
188
212
return ;
189
213
}
214
+ if (perimanSetBusDeinit (ESP32_BUS_TYPE_USB, HWCDC::deinit)) {
215
+ // Setting USB D+ D- pins
216
+ perimanSetPinBus (USB_DM_GPIO_NUM, ESP32_BUS_TYPE_USB, (void *) this );
217
+ perimanSetPinBus (USB_DP_GPIO_NUM, ESP32_BUS_TYPE_USB, (void *) this );
218
+ } else {
219
+ log_e (" Serial JTAG Pins can't be set into Peripheral Manager." );
220
+ }
221
+
190
222
usb_serial_jtag_ll_txfifo_flush ();
191
223
}
192
224
@@ -206,6 +238,7 @@ void HWCDC::end()
206
238
esp_event_loop_delete (arduino_hw_cdc_event_loop_handle);
207
239
arduino_hw_cdc_event_loop_handle = NULL ;
208
240
}
241
+ HWCDC::deinit (this );
209
242
}
210
243
211
244
void HWCDC::setTxTimeoutMs (uint32_t timeout){
0 commit comments