Skip to content

Commit 1f76ea9

Browse files
committed
fix(usb): fix ths bug that ALT escape input for USB HID keyboard
1 parent 8f9eeed commit 1f76ea9

File tree

11 files changed

+359
-263
lines changed

11 files changed

+359
-263
lines changed

examples/peripherals/usb/device/cherryusb_serial_device/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The following lines of boilerplate have to be in your project's
22
# CMakeLists in this exact order for cmake to work correctly
3-
cmake_minimum_required(VERSION 3.5)
3+
cmake_minimum_required(VERSION 3.22)
44

55
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
66
idf_build_set_property(MINIMAL_BUILD ON)

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

Lines changed: 22 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ static TaskHandle_t s_task_handle = NULL;
3333
#define CDC_IN_EP 0x81
3434
#define CDC_OUT_EP 0x01
3535
#define CDC_INT_EP 0x83
36-
36+
#if CDC_ACM_CHANNEL_NUM >= 2
3737
#define CDC_IN_EP1 0x82
3838
#define CDC_OUT_EP1 0x02
3939
#define CDC_INT_EP1 0x84
40+
#endif
4041

4142
#define USBD_VID 0xFFFF
4243
#define USBD_PID 0xFFFF
4344
#define USBD_MAX_POWER 100
4445
#define USBD_LANGID_STRING 1033
4546

4647
/*!< config descriptor size */
47-
#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN * 4)
48+
#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN * CDC_ACM_CHANNEL_NUM)
4849

4950
#define CDC_FS_MAX_MPS 64
5051
#define CDC_HS_MAX_MPS 512
@@ -85,7 +86,6 @@ static uint32_t s_mps = CDC_MAX_MPS;
8586
static DRAM_DMA_ALIGNED_ATTR uint8_t read_buffer[CDC_ACM_CHANNEL_NUM][READ_BUFFER_SIZE];
8687
static DRAM_DMA_ALIGNED_ATTR uint8_t write_buffer[CDC_ACM_CHANNEL_NUM][WRITE_BUFFER_SIZE];
8788

88-
#ifdef CONFIG_USBDEV_ADVANCE_DESC
8989
static const uint8_t device_descriptor[] = {
9090
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
9191
};
@@ -170,90 +170,8 @@ const struct usb_descriptor cdc_descriptor = {
170170
.device_quality_descriptor_callback = device_quality_descriptor_callback,
171171
.string_descriptor_callback = string_descriptor_callback
172172
};
173-
#else
174-
/*!< global descriptor */
175-
static const uint8_t cdc_descriptor[] = {
176-
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
177-
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTERFACES_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
178-
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
179-
#ifdef CONFIG_EXAMPLE_CHERRYUSB_CDC_ACM_TWO_CHANNEL
180-
CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP1, CDC_OUT_EP1, CDC_IN_EP1, CDC_MAX_MPS, 0x02),
181-
#endif
182-
///////////////////////////////////////
183-
/// string0 descriptor
184-
///////////////////////////////////////
185-
USB_LANGID_INIT(USBD_LANGID_STRING),
186-
///////////////////////////////////////
187-
/// string1 descriptor
188-
///////////////////////////////////////
189-
0x14, /* bLength */
190-
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
191-
'C', 0x00, /* wcChar0 */
192-
'h', 0x00, /* wcChar1 */
193-
'e', 0x00, /* wcChar2 */
194-
'r', 0x00, /* wcChar3 */
195-
'r', 0x00, /* wcChar4 */
196-
'y', 0x00, /* wcChar5 */
197-
'U', 0x00, /* wcChar6 */
198-
'S', 0x00, /* wcChar7 */
199-
'B', 0x00, /* wcChar8 */
200-
///////////////////////////////////////
201-
/// string2 descriptor
202-
///////////////////////////////////////
203-
0x26, /* bLength */
204-
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
205-
'C', 0x00, /* wcChar0 */
206-
'h', 0x00, /* wcChar1 */
207-
'e', 0x00, /* wcChar2 */
208-
'r', 0x00, /* wcChar3 */
209-
'r', 0x00, /* wcChar4 */
210-
'y', 0x00, /* wcChar5 */
211-
'U', 0x00, /* wcChar6 */
212-
'S', 0x00, /* wcChar7 */
213-
'B', 0x00, /* wcChar8 */
214-
' ', 0x00, /* wcChar9 */
215-
'C', 0x00, /* wcChar10 */
216-
'D', 0x00, /* wcChar11 */
217-
'C', 0x00, /* wcChar12 */
218-
' ', 0x00, /* wcChar13 */
219-
'D', 0x00, /* wcChar14 */
220-
'E', 0x00, /* wcChar15 */
221-
'M', 0x00, /* wcChar16 */
222-
'O', 0x00, /* wcChar17 */
223-
///////////////////////////////////////
224-
/// string3 descriptor
225-
///////////////////////////////////////
226-
0x16, /* bLength */
227-
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
228-
'2', 0x00, /* wcChar0 */
229-
'0', 0x00, /* wcChar1 */
230-
'2', 0x00, /* wcChar2 */
231-
'5', 0x00, /* wcChar3 */
232-
'0', 0x00, /* wcChar4 */
233-
'6', 0x00, /* wcChar5 */
234-
'0', 0x00, /* wcChar6 */
235-
'5', 0x00, /* wcChar7 */
236-
'1', 0x00, /* wcChar8 */
237-
'8', 0x00, /* wcChar9 */
238-
#ifdef CONFIG_USB_HS
239-
///////////////////////////////////////
240-
/// device qualifier descriptor
241-
///////////////////////////////////////
242-
0x0a,
243-
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
244-
0x00,
245-
0x02,
246-
0x00,
247-
0x00,
248-
0x00,
249-
0x40,
250-
0x00,
251-
0x00,
252-
#endif
253-
0x00
254-
};
255-
#endif
256173

174+
//Note: This callback is in the interrupt context
257175
void usbd_event_handler(uint8_t busid, uint8_t event)
258176
{
259177
switch (event) {
@@ -285,6 +203,7 @@ void usbd_event_handler(uint8_t busid, uint8_t event)
285203
}
286204
}
287205

206+
//Note: This callback is in the interrupt context
288207
static void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
289208
{
290209
ESP_EARLY_LOGI(TAG, "ep 0x%02X actual out len:%d", ep, nbytes);
@@ -321,6 +240,7 @@ static void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
321240
}
322241
}
323242

243+
//Note: This callback is in the interrupt context
324244
static void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
325245
{
326246
BaseType_t HPTaskAwoken = pdFALSE;
@@ -373,11 +293,8 @@ struct usbd_interface intf3;
373293

374294
static void cdc_acm_init(void)
375295
{
376-
#ifdef CONFIG_USBDEV_ADVANCE_DESC
377296
usbd_desc_register(0, &cdc_descriptor);
378-
#else
379-
usbd_desc_register(0, cdc_descriptor);
380-
#endif
297+
381298
usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf0));
382299
usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf1));
383300
usbd_add_endpoint(0, &cdc_out_ep);
@@ -396,11 +313,13 @@ static void cdc_acm_init(void)
396313
#endif
397314
}
398315

316+
//Note: This callback is in the interrupt context
399317
void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr)
400318
{
401319
ESP_EARLY_LOGI(TAG, "intf:%u, dtr:%d", intf, dtr);
402320
}
403321

322+
//Note: This callback is in the interrupt context
404323
void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts)
405324
{
406325
ESP_EARLY_LOGI(TAG, "intf:%u, rts:%d", intf, rts);
@@ -470,22 +389,22 @@ void cdc_acm_task(void *arg)
470389
void app_main(void)
471390
{
472391
#if CONFIG_EXAMPLE_CHERRYUSB_INIT_DEINIT_LOOP
473-
reinit :
392+
while (1)
474393
#endif
475-
476-
xTaskCreatePinnedToCore(&cdc_acm_task, "cdc_acm_task", 1024 * 3, NULL, 10, &s_task_handle, 0);
394+
{
395+
xTaskCreatePinnedToCore(&cdc_acm_task, "cdc_acm_task", 1024 * 3, NULL, 10, &s_task_handle, 0);
477396

478397
#if CONFIG_EXAMPLE_CHERRYUSB_INIT_DEINIT_LOOP
479-
for (int i = 10; i >= 0; i--) {
480-
ESP_LOGW(TAG, "Deinit usb after %d seconds...", i);
481-
vTaskDelay(1000 / portTICK_PERIOD_MS);
482-
}
483-
ESP_LOGW(TAG, "Notify deinit usb");
484-
xTaskNotify(s_task_handle, NOTIFY_EXIT_BIT, eSetBits);
485-
while (s_task_handle) {
486-
vTaskDelay(100 / portTICK_PERIOD_MS);
487-
}
488-
goto reinit;
398+
for (int i = 10; i >= 0; i--) {
399+
ESP_LOGW(TAG, "Deinit usb after %d seconds...", i);
400+
vTaskDelay(1000 / portTICK_PERIOD_MS);
401+
}
402+
ESP_LOGW(TAG, "Notify deinit usb");
403+
xTaskNotify(s_task_handle, NOTIFY_EXIT_BIT, eSetBits);
404+
while (s_task_handle) {
405+
vTaskDelay(100 / portTICK_PERIOD_MS);
406+
}
489407
#endif
408+
}
490409

491410
}
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.1~8
3+
cherry-embedded/cherryusb: 1.5.2~1

examples/peripherals/usb/host/cherryusb_host/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The following lines of boilerplate have to be in your project's
22
# CMakeLists in this exact order for cmake to work correctly
3-
cmake_minimum_required(VERSION 3.5)
3+
cmake_minimum_required(VERSION 3.22)
44

55
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
66
idf_build_set_property(MINIMAL_BUILD ON)

examples/peripherals/usb/host/cherryusb_host/main/CMakeLists.txt

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ endif()
77
if(CONFIG_CHERRYUSB_HOST_CDC_ACM OR CONFIG_CHERRYUSB_HOST_FTDI OR CONFIG_CHERRYUSB_HOST_CH34X OR
88
CONFIG_CHERRYUSB_HOST_CP210X OR CONFIG_CHERRYUSB_HOST_PL2303)
99
list(APPEND srcs "cdc_acm.c")
10+
set(CHERRYUSB_HOST_CDC_ACM_INCLUDE TRUE)
1011
endif()
1112

1213
if(CONFIG_CHERRYUSB_HOST_MSC)
@@ -15,40 +16,20 @@ endif()
1516

1617
idf_component_register(SRCS ${srcs}
1718
INCLUDE_DIRS "."
18-
PRIV_REQUIRES esp_timer fatfs
19+
PRIV_REQUIRES esp_timer fatfs esp_ringbuf
1920
)
2021

2122
if(CONFIG_CHERRYUSB_HOST_HID)
2223
# Make sure the definitions in hid.c are linked correctly
23-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_hid_run_real")
24+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u ld_include_hid")
2425
endif()
2526

26-
if(CONFIG_CHERRYUSB_HOST_CDC_ACM)
27-
# Make sure the definitions in hid.c are linked correctly
28-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_cdc_acm_run_real")
29-
endif()
30-
31-
if(CONFIG_CHERRYUSB_HOST_FTDI)
32-
# Make sure the definitions in hid.c are linked correctly
33-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_ftdi_run_real")
34-
endif()
35-
36-
if(CONFIG_CHERRYUSB_HOST_CH34X)
37-
# Make sure the definitions in hid.c are linked correctly
38-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_ch34x_run_real")
39-
endif()
40-
41-
if(CONFIG_CHERRYUSB_HOST_CP210X)
42-
# Make sure the definitions in hid.c are linked correctly
43-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_cp210x_run_real")
44-
endif()
45-
46-
if(CONFIG_CHERRYUSB_HOST_PL2303)
47-
# Make sure the definitions in hid.c are linked correctly
48-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_pl2303_run_real")
27+
if(CHERRYUSB_HOST_CDC_ACM_INCLUDE)
28+
# Make sure the definitions in cdc_acm.c are linked correctly
29+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u ld_include_cdc_acm")
4930
endif()
5031

5132
if(CONFIG_CHERRYUSB_HOST_MSC)
52-
# Make sure the definitions in hid.c are linked correctly
53-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usbh_msc_run_real")
33+
# Make sure the definitions in msc.c are linked correctly
34+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u ld_include_msc")
5435
endif()

0 commit comments

Comments
 (0)