Skip to content

Commit

Permalink
Fixed bug: routine descriptor configuration error caused crash
Browse files Browse the repository at this point in the history
  • Loading branch information
udoudou committed May 7, 2024
1 parent cfa885d commit d167b88
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 62 deletions.
31 changes: 31 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@ menu "ESP CherryUSB"
config CHERRYUSB_SUPPORTED
bool
default y if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3

choice USB_DBG_LEVEL
bool "Default log verbosity"
default USB_DBG_LEVEL_INFO
help
Specify how much output to see in logs by default.
You can set lower verbosity level at runtime using
esp_log_level_set function.

By default, this setting limits which log statements
are compiled into the program. For example, selecting
"Warning" would mean that changing log level to "Debug"
at runtime will not be possible. To allow increasing log
level above the default at runtime, see the next option.

config USB_DBG_LEVEL_ERROR
bool "Error"
config USB_DBG_LEVEL_WARN
bool "Warning"
config USB_DBG_LEVEL_INFO
bool "Info"
config USB_DBG_LEVEL_DEBUG
bool "Debug"
endchoice

config USB_DBG_LEVEL
int
default 0 if USB_DBG_LEVEL_ERROR
default 1 if USB_DBG_LEVEL_WARN
default 2 if USB_DBG_LEVEL_INFO
default 3 if USB_DBG_LEVEL_DEBUG

menuconfig CHERRYUSBD_ENABLED
bool "Enable CherryUSB Device"
Expand Down
2 changes: 1 addition & 1 deletion additions/esp_cherryusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void usb_dc_low_level_init(void)
}

// TODO: Check when to enable interrupt
ret = esp_intr_alloc(ETS_USB_INTR_SOURCE, ESP_INTR_FLAG_LEVEL2, usb_interrupt_cb, 0, &s_interrupt_handle);
ret = esp_intr_alloc(ETS_USB_INTR_SOURCE, ESP_INTR_FLAG_LOWMED, usb_interrupt_cb, 0, &s_interrupt_handle);
if (ret != ESP_OK) {
USB_LOG_ERR("USB Interrupt Init Failed!\r\n");
return;
Expand Down
63 changes: 2 additions & 61 deletions examples/device/cherryusb_device_mtp/main/device_mtp_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,7 @@
#include "usbd_core.h"
#include "usb_mtp.h"

static char *TAG = "device_cdc_main";

#define WCID_VENDOR_CODE 0x01

__ALIGN_BEGIN const uint8_t WCID_StringDescriptor_MSOS[18] __ALIGN_END = {
///////////////////////////////////////
/// MS OS string descriptor
///////////////////////////////////////
0x12, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
/* MSFT100 */
'M', 0x00, 'S', 0x00, 'F', 0x00, 'T', 0x00, /* wcChar_7 */
'1', 0x00, '0', 0x00, '0', 0x00, /* wcChar_7 */
WCID_VENDOR_CODE, /* bVendorCode */
0x00, /* bReserved */
};

__ALIGN_BEGIN const uint8_t WINUSB_WCIDDescriptor[40] __ALIGN_END = {
///////////////////////////////////////
/// WCID descriptor
///////////////////////////////////////
0x28, 0x00, 0x00, 0x00, /* dwLength */
0x00, 0x01, /* bcdVersion */
0x04, 0x00, /* wIndex */
0x01, /* bCount */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_7 */

///////////////////////////////////////
/// WCID function descriptor
///////////////////////////////////////
0x00, /* bFirstInterfaceNumber */
0x01, /* bReserved */
/* MTP */
'M', 'T', 'P', 0x00, 0x00, 0x00, 0x00, 0x00, /* cCID_8 */
/* */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* cSubCID_8 */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */
};

struct usb_msosv1_descriptor msosv1_desc = {
.string = (uint8_t *)WCID_StringDescriptor_MSOS,
.vendor_code = WCID_VENDOR_CODE,
.compat_id = (uint8_t *)WINUSB_WCIDDescriptor,
.comp_id_property = NULL,
};
static char *TAG = "device_mtp_main";

/*!< endpoint address */
#define CDC_IN_EP 0x81
Expand All @@ -79,7 +35,7 @@ struct usb_msosv1_descriptor msosv1_desc = {
#endif

const uint8_t mtp_descriptor[] = {
USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0201, 0x01),
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0201, 0x01),
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
MTP_DESCRIPTOR_INIT(0x00, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, MTP_MAX_MPS, 0x02),
///////////////////////////////////////
Expand Down Expand Up @@ -156,12 +112,6 @@ const uint8_t mtp_descriptor[] = {
0x00
};

const uint8_t bos_descriptor[] = {
0x05, 0x0f, 0x16, 0x00, 0x02,
0x07, 0x10, 0x02, 0x06, 0x00, 0x00, 0x00,
0x0a, 0x10, 0x03, 0x00, 0x0f, 0x00, 0x01, 0x01, 0xf4, 0x01
};

void usbd_event_handler(uint8_t event)
{
switch (event) {
Expand Down Expand Up @@ -189,19 +139,12 @@ void usbd_event_handler(uint8_t event)

struct usbd_interface intf0;

struct usb_bos_descriptor bos_desc = {
.string = bos_descriptor,
.string_len = sizeof(bos_descriptor)
};

void app_main(void)
{
void sd_main(void);
sd_main();
uint32_t before = heap_caps_get_free_size(MALLOC_CAP_DEFAULT);
usbd_desc_register(mtp_descriptor);
usbd_msosv1_desc_register(&msosv1_desc);
usbd_bos_desc_register(&bos_desc);
usbd_add_interface(usbd_mtp_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP));
usbd_initialize();
while (1){
Expand All @@ -213,8 +156,6 @@ void app_main(void)
now = heap_caps_get_free_size(MALLOC_CAP_DEFAULT);
ESP_LOGW(TAG, "use %"PRIu32, before - now);
usbd_desc_register(mtp_descriptor);
usbd_msosv1_desc_register(&msosv1_desc);
usbd_bos_desc_register(&bos_desc);
usbd_add_interface(usbd_mtp_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP));
usbd_initialize();
}
Expand Down

0 comments on commit d167b88

Please sign in to comment.