Skip to content

Commit 2e19d27

Browse files
committed
Change driver attach api and remove DFU from CDC
1 parent b8fcea9 commit 2e19d27

File tree

6 files changed

+66
-133
lines changed

6 files changed

+66
-133
lines changed

cores/esp32/esp32-hal-tinyusb.c

Lines changed: 24 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ static tinyusb_desc_webusb_url_t tinyusb_url_descriptor = {
168168
/*
169169
* Configuration Descriptor
170170
* */
171+
172+
static tinyusb_descriptor_cb_t tinyusb_loaded_interfaces_callbacks[USB_INTERFACE_MAX];
171173
static uint32_t tinyusb_loaded_interfaces_mask = 0;
172174
static uint8_t tinyusb_loaded_interfaces_num = 0;
173175
static uint16_t tinyusb_config_descriptor_len = 0;
@@ -308,47 +310,6 @@ __attribute__ ((weak)) int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_
308310
* Private API
309311
* */
310312

311-
__attribute__ ((weak)) uint16_t tusb_cdc_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
312-
__attribute__ ((weak)) uint16_t tusb_dfu_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
313-
__attribute__ ((weak)) uint16_t tusb_hid_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
314-
__attribute__ ((weak)) uint16_t tusb_msc_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
315-
__attribute__ ((weak)) uint16_t tusb_midi_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
316-
__attribute__ ((weak)) uint16_t tusb_vendor_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
317-
318-
__attribute__ ((weak)) uint16_t tusb_custom_get_descriptor_len() { return 0; }
319-
__attribute__ ((weak)) uint16_t tusb_custom_load_descriptor(uint8_t * dst, uint8_t * itf) { return 0; }
320-
321-
static uint16_t tinyusb_load_descriptor(tinyusb_interface_t interface, uint8_t * dst, uint8_t * itf)
322-
{
323-
switch (interface) {
324-
case USB_INTERFACE_CDC:
325-
return tusb_cdc_load_descriptor(dst, itf);
326-
break;
327-
case USB_INTERFACE_DFU:
328-
return tusb_dfu_load_descriptor(dst, itf);
329-
break;
330-
case USB_INTERFACE_HID:
331-
return tusb_hid_load_descriptor(dst, itf);
332-
break;
333-
case USB_INTERFACE_VENDOR:
334-
return tusb_vendor_load_descriptor(dst, itf);
335-
break;
336-
case USB_INTERFACE_MSC:
337-
return tusb_msc_load_descriptor(dst, itf);
338-
break;
339-
case USB_INTERFACE_MIDI:
340-
return tusb_midi_load_descriptor(dst, itf);
341-
break;
342-
case USB_INTERFACE_CUSTOM:
343-
return tusb_custom_load_descriptor(dst, itf);
344-
break;
345-
default:
346-
347-
break;
348-
}
349-
return 0;
350-
}
351-
352313
static bool tinyusb_reserve_in_endpoint(uint8_t endpoint){
353314
if(endpoint > 6 || (tinyusb_endpoints.in & BIT(endpoint)) != 0){
354315
return false;
@@ -379,6 +340,14 @@ static bool tinyusb_has_available_fifos(void){
379340
return active_endpoints < max_endpoints;
380341
}
381342

343+
static uint16_t tinyusb_load_descriptor(tinyusb_interface_t interface, uint8_t * dst, uint8_t * itf)
344+
{
345+
if(tinyusb_loaded_interfaces_callbacks[interface]){
346+
return tinyusb_loaded_interfaces_callbacks[interface](dst, itf);
347+
}
348+
return 0;
349+
}
350+
382351
static bool tinyusb_load_enabled_interfaces(){
383352
tinyusb_config_descriptor_len += TUD_CONFIG_DESC_LEN;
384353
tinyusb_config_descriptor = (uint8_t *)malloc(tinyusb_config_descriptor_len);
@@ -411,7 +380,7 @@ static bool tinyusb_load_enabled_interfaces(){
411380
TUD_CONFIG_DESCRIPTOR(1, tinyusb_loaded_interfaces_num, str_index, tinyusb_config_descriptor_len, USB_DEVICE_ATTRIBUTES, USB_DEVICE_POWER)
412381
};
413382
memcpy(tinyusb_config_descriptor, descriptor, TUD_CONFIG_DESC_LEN);
414-
if ((tinyusb_loaded_interfaces_mask & ~(BIT(USB_INTERFACE_CDC) | BIT(USB_INTERFACE_DFU))) == 0) {
383+
if ((tinyusb_loaded_interfaces_mask == (BIT(USB_INTERFACE_CDC) | BIT(USB_INTERFACE_DFU))) || (tinyusb_loaded_interfaces_mask == BIT(USB_INTERFACE_CDC))) {
415384
tinyusb_persist_set_enable(true);
416385
log_d("USB Persist enabled");
417386
}
@@ -493,6 +462,19 @@ static void usb_device_task(void *param) {
493462
* PUBLIC API
494463
* */
495464

465+
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb)
466+
{
467+
if((interface >= USB_INTERFACE_MAX) || (tinyusb_loaded_interfaces_mask & (1U << interface))){
468+
log_e("Interface %u not enabled", interface);
469+
return ESP_FAIL;
470+
}
471+
tinyusb_loaded_interfaces_mask |= (1U << interface);
472+
tinyusb_config_descriptor_len += descriptor_len;
473+
tinyusb_loaded_interfaces_callbacks[interface] = cb;
474+
log_d("Interface %u enabled", interface);
475+
return ESP_OK;
476+
}
477+
496478
esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
497479
static bool initialized = false;
498480
if(initialized){
@@ -518,45 +500,6 @@ esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
518500
return err;
519501
}
520502

521-
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface)
522-
{
523-
uint16_t descriptor_len = 0;
524-
switch (interface) {
525-
case USB_INTERFACE_CDC:
526-
descriptor_len = CFG_TUD_CDC * TUD_CDC_DESC_LEN;
527-
break;
528-
case USB_INTERFACE_DFU:
529-
descriptor_len = CFG_TUD_DFU_RT * TUD_DFU_RT_DESC_LEN;
530-
break;
531-
case USB_INTERFACE_HID:
532-
descriptor_len = CFG_TUD_HID * TUD_HID_INOUT_DESC_LEN;
533-
break;
534-
case USB_INTERFACE_MSC:
535-
descriptor_len = CFG_TUD_MSC * TUD_MSC_DESC_LEN;
536-
break;
537-
case USB_INTERFACE_MIDI:
538-
descriptor_len = CFG_TUD_MIDI * TUD_MIDI_DESC_LEN;
539-
break;
540-
case USB_INTERFACE_VENDOR:
541-
descriptor_len = CFG_TUD_VENDOR * TUD_VENDOR_DESC_LEN;
542-
break;
543-
case USB_INTERFACE_CUSTOM:
544-
descriptor_len = tusb_custom_get_descriptor_len();
545-
break;
546-
default:
547-
548-
break;
549-
}
550-
if (descriptor_len) {
551-
tinyusb_config_descriptor_len += descriptor_len;
552-
tinyusb_loaded_interfaces_mask |= (1U << interface);
553-
log_d("Driver %u enabled", interface);
554-
return ESP_OK;
555-
}
556-
log_e("Driver %u not enabled", interface);
557-
return ESP_FAIL;
558-
}
559-
560503
uint8_t tinyusb_add_string_descriptor(const char * str){
561504
if(str == NULL || tinyusb_string_descriptor_len >= MAX_STRING_DESCRIPTORS){
562505
return 0;

cores/esp32/esp32-hal-tinyusb.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ typedef enum {
7373
USB_INTERFACE_MAX
7474
} tinyusb_interface_t;
7575

76-
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface);
76+
typedef uint16_t (*tinyusb_descriptor_cb_t)(uint8_t * dst, uint8_t * itf);
77+
78+
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descriptor_len, tinyusb_descriptor_cb_t cb);
7779
uint8_t tinyusb_add_string_descriptor(const char * str);
7880
uint8_t tinyusb_get_free_duplex_endpoint(void);
7981
uint8_t tinyusb_get_free_in_endpoint(void);

libraries/USB/src/USB.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,38 @@
1515
#include "esp32-hal-tinyusb.h"
1616
#include "USB.h"
1717
#if CONFIG_USB_ENABLED
18+
#include "usb_persist.h"
1819

1920
extern "C" {
2021
#include "tinyusb.h"
2122
}
2223

24+
#if CFG_TUD_DFU_RT
25+
static uint16_t load_dfu_descriptor(uint8_t * dst, uint8_t * itf)
26+
{
27+
#define DFU_ATTR_CAN_DOWNLOAD 1
28+
#define DFU_ATTR_CAN_UPLOAD 2
29+
#define DFU_ATTR_MANIFESTATION_TOLERANT 4
30+
#define DFU_ATTR_WILL_DETACH 8
31+
#define DFU_ATTRS (DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_CAN_UPLOAD | DFU_ATTR_MANIFESTATION_TOLERANT)
32+
33+
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB DFU_RT");
34+
uint8_t descriptor[TUD_DFU_RT_DESC_LEN] = {
35+
// Interface number, string index, attributes, detach timeout, transfer size */
36+
TUD_DFU_RT_DESCRIPTOR(*itf, str_index, DFU_ATTRS, 700, 64)
37+
};
38+
*itf+=1;
39+
memcpy(dst, descriptor, TUD_DFU_RT_DESC_LEN);
40+
return TUD_DFU_RT_DESC_LEN;
41+
}
42+
// Invoked on DFU_DETACH request to reboot to the bootloader
43+
void tud_dfu_rt_reboot_to_dfu(void)
44+
{
45+
tinyusb_persist_set_mode(REBOOT_BOOTLOADER_DFU);
46+
esp_restart();
47+
}
48+
#endif /* CFG_TUD_DFU_RT */
49+
2350
ESP_EVENT_DEFINE_BASE(ARDUINO_USB_EVENTS);
2451

2552
static esp_event_loop_handle_t arduino_usb_event_loop_handle = NULL;
@@ -141,6 +168,12 @@ ESPUSB::operator bool() const
141168
return _started && tinyusb_device_mounted;
142169
}
143170

171+
bool ESPUSB::enableDFU(){
172+
#if CFG_TUD_DFU_RT
173+
return tinyusb_enable_interface(USB_INTERFACE_DFU, TUD_DFU_RT_DESC_LEN, load_dfu_descriptor) == ESP_OK;
174+
#endif /* CFG_TUD_DFU_RT */
175+
return false;
176+
}
144177

145178
bool ESPUSB::VID(uint16_t v){
146179
if(!_started){

libraries/USB/src/USB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class ESPUSB {
8888
bool webUSBURL(const char * name);
8989
const char * webUSBURL(void);
9090

91+
bool enableDFU();
9192
bool begin();
9293
operator bool() const;
9394

libraries/USB/src/USBCDC.cpp

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,13 @@ esp_err_t arduino_usb_event_handler_register_with(esp_event_base_t event_base, i
2424

2525
extern "C" {
2626
#include "tinyusb.h"
27-
28-
#if CFG_TUD_DFU_RT
29-
uint16_t tusb_dfu_load_descriptor(uint8_t * dst, uint8_t * itf)
30-
{
31-
#define DFU_ATTR_CAN_DOWNLOAD 1
32-
#define DFU_ATTR_CAN_UPLOAD 2
33-
#define DFU_ATTR_MANIFESTATION_TOLERANT 4
34-
#define DFU_ATTR_WILL_DETACH 8
35-
#define DFU_ATTRS (DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_CAN_UPLOAD | DFU_ATTR_MANIFESTATION_TOLERANT)
36-
37-
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB DFU_RT");
38-
uint8_t descriptor[TUD_DFU_RT_DESC_LEN] = {
39-
// Interface number, string index, attributes, detach timeout, transfer size */
40-
TUD_DFU_RT_DESCRIPTOR(*itf, str_index, DFU_ATTRS, 700, 64)
41-
};
42-
*itf+=1;
43-
memcpy(dst, descriptor, TUD_DFU_RT_DESC_LEN);
44-
return TUD_DFU_RT_DESC_LEN;
4527
}
46-
#endif /* CFG_TUD_DFU_RT */
4728

4829
#if CFG_TUD_CDC
49-
uint16_t tusb_cdc_load_descriptor(uint8_t * dst, uint8_t * itf)
30+
#define MAX_USB_CDC_DEVICES 2
31+
USBCDC * devices[MAX_USB_CDC_DEVICES] = {NULL, NULL};
32+
33+
static uint16_t load_cdc_descriptor(uint8_t * dst, uint8_t * itf)
5034
{
5135
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB CDC");
5236
// Interface number, string index, attributes, detach timeout, transfer size */
@@ -58,22 +42,6 @@ uint16_t tusb_cdc_load_descriptor(uint8_t * dst, uint8_t * itf)
5842
memcpy(dst, descriptor, TUD_CDC_DESC_LEN);
5943
return TUD_CDC_DESC_LEN;
6044
}
61-
#endif /* CFG_TUD_CDC */
62-
}
63-
64-
#if CFG_TUD_CDC
65-
#define MAX_USB_CDC_DEVICES 2
66-
USBCDC * devices[MAX_USB_CDC_DEVICES] = {NULL, NULL};
67-
68-
#if CFG_TUD_DFU_RT
69-
// Invoked on DFU_DETACH request to reboot to the bootloader
70-
void tud_dfu_rt_reboot_to_dfu(void)
71-
{
72-
if(devices[0] != NULL){
73-
devices[0]->_onDFU();
74-
}
75-
}
76-
#endif /* CFG_TUD_DFU_RT */
7745

7846
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
7947
{
@@ -103,8 +71,7 @@ static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t eve
10371
}
10472

10573
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) {
106-
tinyusb_enable_interface(USB_INTERFACE_CDC);
107-
tinyusb_enable_interface(USB_INTERFACE_DFU);
74+
tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
10875
if(itf < MAX_USB_CDC_DEVICES){
10976
devices[itf] = this;
11077
arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
@@ -127,12 +94,10 @@ void USBCDC::begin(size_t rx_queue_len)
12794
if(!rx_queue){
12895
return;
12996
}
130-
tinyusb_persist_set_mode(REBOOT_PERSIST);
13197
}
13298

13399
void USBCDC::end()
134100
{
135-
tinyusb_persist_set_mode(REBOOT_NO_PERSIST);
136101
}
137102

138103
void USBCDC::_onUnplugged(void){
@@ -145,13 +110,6 @@ void USBCDC::_onUnplugged(void){
145110
}
146111
}
147112

148-
void USBCDC::_onDFU(void){
149-
if(reboot_enable){
150-
tinyusb_persist_set_mode(REBOOT_BOOTLOADER_DFU);
151-
esp_restart();
152-
}
153-
}
154-
155113
enum { CDC_LINE_IDLE, CDC_LINE_1, CDC_LINE_2, CDC_LINE_3 };
156114
void USBCDC::_onLineState(bool _dtr, bool _rts){
157115
static uint8_t lineState = CDC_LINE_IDLE;
@@ -202,10 +160,6 @@ void USBCDC::_onLineState(bool _dtr, bool _rts){
202160
l.line_state.rts = rts;
203161
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
204162
} else {
205-
//[I][USBSerial.cpp:76] _onLineState(): dtr: 0, rts: 1
206-
//[I][USBSerial.cpp:76] _onLineState(): dtr: 1, rts: 1
207-
//[I][USBSerial.cpp:76] _onLineState(): dtr: 1, rts: 0
208-
//[I][USBSerial.cpp:76] _onLineState(): dtr: 0, rts: 0
209163
log_d("CDC RESET: itf: %u, dtr: %u, rts: %u, state: %u", itf, dtr, rts, lineState);
210164
}
211165

tools/sdk/esp32s2/lib/libtinyusb.a

-456 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)