Skip to content

Commit 8f9eeed

Browse files
committed
feat(usb): add CherryUSB support for multiple USB controllers selection
1 parent 0ac445c commit 8f9eeed

File tree

9 files changed

+111
-22
lines changed

9 files changed

+111
-22
lines changed

examples/peripherals/usb/device/cherryusb_serial_device/main/Kconfig.projbuild

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
menu "Example Configuration"
22

3+
choice EXAMPLE_USB_DEVICE_RHPORT
4+
prompt "USB Device Peripheral"
5+
default EXAMPLE_USB_DEVICE_RHPORT_HS if IDF_TARGET_ESP32P4
6+
default EXAMPLE_USB_DEVICE_RHPORT_FS
7+
help
8+
Allows set the USB Peripheral Controller for USB device.
9+
10+
- High-speed (USB OTG2.0 Peripheral for High-, Full- and Low-speed)
11+
- Full-speed (USB OTG1.1 Peripheral for Full- and Low-speed)
12+
13+
config EXAMPLE_USB_DEVICE_RHPORT_HS
14+
bool "OTG2.0"
15+
depends on IDF_TARGET_ESP32P4
16+
config EXAMPLE_USB_DEVICE_RHPORT_FS
17+
bool "OTG1.1"
18+
19+
endchoice
20+
321
config EXAMPLE_CHERRYUSB_CDC_ACM_TWO_CHANNEL
422
bool "Enable usb cdc acm two channel"
523
default n

examples/peripherals/usb/device/cherryusb_serial_device/main/device_cdc_main.c

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ static TaskHandle_t s_task_handle = NULL;
4646
/*!< config descriptor size */
4747
#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN * 4)
4848

49+
#define CDC_FS_MAX_MPS 64
50+
#define CDC_HS_MAX_MPS 512
51+
4952
#ifdef CONFIG_USB_HS
50-
#define CDC_MAX_MPS 512
53+
#define CDC_MAX_MPS CDC_HS_MAX_MPS
5154
#else
52-
#define CDC_MAX_MPS 64
55+
#define CDC_MAX_MPS CDC_FS_MAX_MPS
5356
#endif
5457

5558
typedef struct {
@@ -68,26 +71,42 @@ static ep_status_t s_ep_status[CDC_ACM_CHANNEL_NUM] = {
6871
#endif
6972
};
7073

74+
#define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
75+
76+
#define WRITE_BUFFER_SIZE ALIGN_UP(ALIGN_UP(2048, CDC_MAX_MPS), CONFIG_USB_ALIGN_SIZE)
77+
7178
#if CONFIG_EXAMPLE_CHERRYUSB_SET_READ_BUFFER_SIZE_MPS
72-
#define READ_BUFFER_SIZE CDC_MAX_MPS
79+
#define READ_BUFFER_SIZE ALIGN_UP(CDC_MAX_MPS, CONFIG_USB_ALIGN_SIZE)
7380
#else
74-
#define READ_BUFFER_SIZE 2048
81+
#define READ_BUFFER_SIZE WRITE_BUFFER_SIZE
7582
#endif
83+
84+
static uint32_t s_mps = CDC_MAX_MPS;
7685
static DRAM_DMA_ALIGNED_ATTR uint8_t read_buffer[CDC_ACM_CHANNEL_NUM][READ_BUFFER_SIZE];
77-
static DRAM_DMA_ALIGNED_ATTR uint8_t write_buffer[CDC_ACM_CHANNEL_NUM][2048];
86+
static DRAM_DMA_ALIGNED_ATTR uint8_t write_buffer[CDC_ACM_CHANNEL_NUM][WRITE_BUFFER_SIZE];
7887

7988
#ifdef CONFIG_USBDEV_ADVANCE_DESC
8089
static const uint8_t device_descriptor[] = {
8190
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
8291
};
8392

84-
static const uint8_t config_descriptor[] = {
93+
static const uint8_t config_descriptor_fs[] = {
8594
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTERFACES_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
86-
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
95+
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_FS_MAX_MPS, 0x02),
8796
#ifdef CONFIG_EXAMPLE_CHERRYUSB_CDC_ACM_TWO_CHANNEL
88-
CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP1, CDC_OUT_EP1, CDC_IN_EP1, CDC_MAX_MPS, 0x02),
97+
CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP1, CDC_OUT_EP1, CDC_IN_EP1, CDC_FS_MAX_MPS, 0x02),
98+
#endif
99+
};
100+
101+
#ifdef CONFIG_USB_HS
102+
static const uint8_t config_descriptor_hs[] = {
103+
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTERFACES_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
104+
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_HS_MAX_MPS, 0x02),
105+
#ifdef CONFIG_EXAMPLE_CHERRYUSB_CDC_ACM_TWO_CHANNEL
106+
CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP1, CDC_OUT_EP1, CDC_IN_EP1, CDC_HS_MAX_MPS, 0x02),
89107
#endif
90108
};
109+
#endif
91110

92111
static const uint8_t device_quality_descriptor[] = {
93112
///////////////////////////////////////
@@ -122,7 +141,14 @@ static const uint8_t *device_descriptor_callback(uint8_t speed)
122141

123142
static const uint8_t *config_descriptor_callback(uint8_t speed)
124143
{
125-
return config_descriptor;
144+
#ifdef CONFIG_USB_HS
145+
if (speed >= USB_SPEED_HIGH) {
146+
s_mps = CDC_HS_MAX_MPS;
147+
return config_descriptor_hs;
148+
}
149+
s_mps = CDC_FS_MAX_MPS;
150+
#endif
151+
return config_descriptor_fs;
126152
}
127153

128154
static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
@@ -270,14 +296,22 @@ static void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
270296
if (ep_status->write_len == 0) {
271297
memcpy(write_buffer[i], read_buffer[i], nbytes);
272298
ep_status->write_len = nbytes;
299+
#if CONFIG_EXAMPLE_CHERRYUSB_SET_READ_BUFFER_SIZE_MPS
300+
usbd_ep_start_read(0, ep, read_buffer[i], s_mps);
301+
#else
273302
usbd_ep_start_read(0, ep, read_buffer[i], sizeof(read_buffer[0]));
303+
#endif
274304
} else {
275305
ep_status->read_len = nbytes;
276306
ep_status->rx_busy_flag = false;
277307
}
278308
xTaskNotifyFromISR(s_task_handle, NOTIFY_EP_BIT, eSetBits, &HPTaskAwoken);
279309
} else {
280-
usbd_ep_start_read(0, ep_status->rx_addr, read_buffer[i], sizeof(read_buffer[i]));
310+
#if CONFIG_EXAMPLE_CHERRYUSB_SET_READ_BUFFER_SIZE_MPS
311+
usbd_ep_start_read(0, ep, read_buffer[i], s_mps);
312+
#else
313+
usbd_ep_start_read(0, ep, read_buffer[i], sizeof(read_buffer[i]));
314+
#endif
281315
}
282316
break;
283317
}
@@ -291,7 +325,7 @@ static void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
291325
{
292326
BaseType_t HPTaskAwoken = pdFALSE;
293327
ESP_EARLY_LOGI(TAG, "ep 0x%02X actual in len:%d", ep, nbytes);
294-
if ((nbytes % CDC_MAX_MPS) == 0 && nbytes) {
328+
if ((nbytes % s_mps) == 0 && nbytes) {
295329
usbd_ep_start_write(0, ep, NULL, 0);
296330
} else {
297331
for (size_t i = 0; i < CDC_ACM_CHANNEL_NUM; i++) {
@@ -354,7 +388,12 @@ static void cdc_acm_init(void)
354388
usbd_add_endpoint(0, &cdc_out_ep1);
355389
usbd_add_endpoint(0, &cdc_in_ep1);
356390
#endif
357-
usbd_initialize(0, ESP_USBD_BASE, usbd_event_handler);
391+
392+
#if CONFIG_EXAMPLE_USB_DEVICE_RHPORT_HS
393+
usbd_initialize(0, ESP_USB_HS0_BASE, usbd_event_handler);
394+
#else
395+
usbd_initialize(0, ESP_USB_FS0_BASE, usbd_event_handler);
396+
#endif
358397
}
359398

360399
void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr)
@@ -389,7 +428,11 @@ void cdc_acm_task(void *arg)
389428

390429
ep_status->read_len = 0;
391430
ep_status->rx_busy_flag = true;
431+
#if CONFIG_EXAMPLE_CHERRYUSB_SET_READ_BUFFER_SIZE_MPS
432+
usbd_ep_start_read(0, ep_status->rx_addr, read_buffer[i], s_mps);
433+
#else
392434
usbd_ep_start_read(0, ep_status->rx_addr, read_buffer[i], sizeof(read_buffer[0]));
435+
#endif
393436
}
394437
}
395438
for (size_t i = 0; i < sizeof(s_ep_status) / sizeof(s_ep_status[0]); i++) {
@@ -408,7 +451,11 @@ void cdc_acm_task(void *arg)
408451
if (ep_status->rx_busy_flag == false) {
409452
if (ep_status->read_len == 0) {
410453
ep_status->rx_busy_flag = true;
454+
#if CONFIG_EXAMPLE_CHERRYUSB_SET_READ_BUFFER_SIZE_MPS
455+
usbd_ep_start_read(0, ep_status->rx_addr, read_buffer[i], s_mps);
456+
#else
411457
usbd_ep_start_read(0, ep_status->rx_addr, read_buffer[i], sizeof(read_buffer[i]));
458+
#endif
412459
}
413460
}
414461
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
## IDF Component Manager Manifest File
22
dependencies:
3-
cherry-embedded/cherryusb: ~=1.5.0~4
3+
cherry-embedded/cherryusb: ~=1.5.1~8

examples/peripherals/usb/host/cherryusb_host/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ This example demonstrates how to use the CherryUSB host driver. Currently, this
99

1010
## How to use example
1111

12-
If you need to enable support for FTDI, please enable the following configuration
13-
Run `idf.py menuconfig` and in `Component config → CherryUSB Configuration → Enable usb host mode → Enable usb ftdi driver` enable.
12+
By default, all drivers supported by the routine are enabled. If you need to trim or disable unnecessary drivers, please disable the corresponding driver configuration.
13+
Run `idf.py menuconfig` and in `Component config → CherryUSB Configuration → Enable usb host mode`, Uncheck the drivers you don’t need.
1414

1515
### Hardware Required
1616
* Development board with USB-OTG support

examples/peripherals/usb/host/cherryusb_host/main/Kconfig.projbuild

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
menu "Example Configuration"
22

3+
choice EXAMPLE_USB_HOST_RHPORT
4+
prompt "USB Host Peripheral"
5+
default EXAMPLE_USB_HOST_RHPORT_HS if IDF_TARGET_ESP32P4
6+
default EXAMPLE_USB_HOST_RHPORT_FS
7+
help
8+
Allows set the USB Peripheral Controller for USB host.
9+
10+
- High-speed (USB OTG2.0 Peripheral for High-, Full- and Low-speed)
11+
- Full-speed (USB OTG1.1 Peripheral for Full- and Low-speed)
12+
13+
config EXAMPLE_USB_HOST_RHPORT_HS
14+
bool "OTG2.0"
15+
depends on IDF_TARGET_ESP32P4
16+
config EXAMPLE_USB_HOST_RHPORT_FS
17+
bool "OTG1.1"
18+
19+
endchoice
20+
321
config EXAMPLE_HAL_USE_ESP32_S3_USB_OTG
422
bool "Use dev kit ESP32-S3-USB-OTG"
523
depends on IDF_TARGET_ESP32S3

examples/peripherals/usb/host/cherryusb_host/main/hid.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ static void usbh_hid_callback(void *arg, int nbytes)
346346
{
347347
struct usbh_hid *hid_class = (struct usbh_hid *)arg;
348348
hid_int_in_t *hid_intin = (hid_int_in_t *)hid_class->user_data;
349-
hid_intin->is_active = false;
349+
350350
if (nbytes <= 0) {
351+
hid_intin->is_active = false;
351352
return;
352353
}
353354
uint8_t sub_class = hid_class->hport->config.intf[hid_class->intf].altsetting[0].intf_desc.bInterfaceSubClass;
@@ -362,6 +363,7 @@ static void usbh_hid_callback(void *arg, int nbytes)
362363
}
363364
}
364365
complete_cb(hid_intin->buffer, nbytes);
366+
hid_intin->is_active = false;
365367
}
366368

367369
static void intin_timer_cb(void *arg)
@@ -437,11 +439,8 @@ void usbh_hid_run(struct usbh_hid *hid_class)
437439

438440
hid_class->user_data = hid_intin;
439441

440-
//周期间隔,低速、全速单位为1ms,高速和超高速是125us
441-
ret = USBH_GET_URB_INTERVAL(hid_class->intin->bInterval, hid_class->hport->speed);
442-
ret = (hid_class->hport->speed < USB_SPEED_HIGH) ? (ret * 1000) : (ret * 125);
442+
esp_timer_start_periodic(hid_intin->timer, USBH_GET_URB_INTERVAL(hid_class->intin->bInterval, hid_class->hport->speed));
443443

444-
esp_timer_start_periodic(hid_intin->timer, ret);
445444
return;
446445
error:
447446
if (hid_intin->buffer) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
## IDF Component Manager Manifest File
22
dependencies:
3-
cherry-embedded/cherryusb: ~=1.5.0~4
3+
cherry-embedded/cherryusb: ~=1.5.1~8

examples/peripherals/usb/host/cherryusb_host/main/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ void app_main(void)
4343
#if CONFIG_EXAMPLE_CHERRYUSB_INIT_DEINIT_LOOP
4444
reinit:
4545
#endif
46-
usbh_initialize(0, ESP_USBH_BASE);
46+
47+
#if CONFIG_EXAMPLE_USB_HOST_RHPORT_HS
48+
usbh_initialize(0, ESP_USB_HS0_BASE);
49+
#else
50+
usbh_initialize(0, ESP_USB_FS0_BASE);
51+
#endif
52+
4753
ESP_LOGI(TAG, "Init usb");
4854

4955
#if CONFIG_EXAMPLE_CHERRYUSB_INIT_DEINIT_LOOP

examples/peripherals/usb/host/cherryusb_host/sdkconfig.defaults

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG_CHERRYUSB_HOST_DWC2_ESP=y
1010
CONFIG_CHERRYUSB_HOST_CDC_ACM=y
1111
CONFIG_CHERRYUSB_HOST_HID=y
1212
CONFIG_CHERRYUSB_HOST_MSC=y
13+
CONFIG_CHERRYUSB_HOST_FTDI=y
1314
CONFIG_CHERRYUSB_HOST_CH34X=y
1415
CONFIG_CHERRYUSB_HOST_CP210X=y
1516
CONFIG_CHERRYUSB_HOST_PL2303=y

0 commit comments

Comments
 (0)