Skip to content

Commit 7d50114

Browse files
committed
Add files to cmakelists and disconnect CDC if unplugged
1 parent be074ee commit 7d50114

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(CORE_SRCS
1717
cores/esp32/esp32-hal-timer.c
1818
cores/esp32/esp32-hal-touch.c
1919
cores/esp32/esp32-hal-uart.c
20+
cores/esp32/esp32-hal-usb.c
2021
cores/esp32/esp32-hal-rmt.c
2122
cores/esp32/Esp.cpp
2223
cores/esp32/FunctionalInterrupt.cpp
@@ -60,6 +61,8 @@ set(LIBRARY_SRCS
6061
libraries/SPI/src/SPI.cpp
6162
libraries/Ticker/src/Ticker.cpp
6263
libraries/Update/src/Updater.cpp
64+
libraries/USB/src/USB.cpp
65+
libraries/USB/src/USBCDC.cpp
6366
libraries/WebServer/src/WebServer.cpp
6467
libraries/WebServer/src/Parsing.cpp
6568
libraries/WebServer/src/detail/mimetable.cpp

libraries/USB/src/USBCDC.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414
#include "esp32-hal.h"
1515
#include "esp32-hal-tinyusb.h"
16+
#include "USB.h"
1617
#include "USBCDC.h"
1718
#if CONFIG_USB_ENABLED
1819

@@ -99,11 +100,16 @@ void tud_cdc_rx_cb(uint8_t itf)
99100

100101
//void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
101102

103+
static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
104+
((USBCDC*)arg)->_onUnplugged();
105+
}
106+
102107
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(false), reboot_enable(true), rx_queue(NULL) {
103108
tinyusb_enable_interface(USB_INTERFACE_CDC);
104109
tinyusb_enable_interface(USB_INTERFACE_DFU);
105110
if(itf < MAX_USB_CDC_DEVICES){
106111
devices[itf] = this;
112+
arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
107113
}
108114
}
109115

@@ -131,6 +137,16 @@ void USBCDC::end()
131137
tinyusb_persist_set_mode(REBOOT_NO_PERSIST);
132138
}
133139

140+
void USBCDC::_onUnplugged(void){
141+
if(connected){
142+
connected = false;
143+
dtr = false;
144+
rts = false;
145+
arduino_usb_cdc_event_data_t p = {0};
146+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_DISCONNECTED_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
147+
}
148+
}
149+
134150
void USBCDC::_onDFU(void){
135151
if(reboot_enable){
136152
tinyusb_persist_set_mode(REBOOT_BOOTLOADER_DFU);

libraries/USB/src/USBCDC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class USBCDC: public Stream
108108
void _onLineState(bool _dtr, bool _rts);
109109
void _onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _parity, uint8_t _data_bits);
110110
void _onRX(void);
111+
void _onUnplugged(void);
111112

112113
protected:
113114
uint8_t itf;

0 commit comments

Comments
 (0)