Skip to content

Commit e96e5fd

Browse files
committed
feat(platform/rtthread): support cdc acm chardev
Signed-off-by: sakumisu <[email protected]>
1 parent 9d4faca commit e96e5fd

File tree

5 files changed

+500
-3
lines changed

5 files changed

+500
-3
lines changed

Kconfig.rtt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ if RT_USING_CHERRYUSB
135135
prompt "Enable usb dfu device"
136136
default n
137137

138+
config RT_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV
139+
bool
140+
prompt "Enable chardev for cdc acm device"
141+
default n
142+
138143
choice
139144
prompt "Select usb device template"
140145
default RT_CHERRYUSB_DEVICE_TEMPLATE_NONE
@@ -176,6 +181,8 @@ if RT_USING_CHERRYUSB
176181
bool "winusbv2_hid"
177182
config RT_CHERRYUSB_DEVICE_TEMPLATE_ADB
178183
bool "adb"
184+
config RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_CHARDEV
185+
bool "cdc_acm_chardev"
179186
endchoice
180187

181188
config CONFIG_USBDEV_MSC_BLOCK_DEV_NAME

Kconfig.rttpkg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ if PKG_USING_CHERRYUSB
134134
prompt "Enable usb dfu device"
135135
default n
136136

137+
config PKG_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV
138+
bool
139+
prompt "Enable chardev for cdc acm device"
140+
default n
141+
137142
choice
138143
prompt "Select usb device template"
139144
default PKG_CHERRYUSB_DEVICE_TEMPLATE_NONE
@@ -175,6 +180,8 @@ if PKG_USING_CHERRYUSB
175180
bool "winusbv2_hid"
176181
config PKG_CHERRYUSB_DEVICE_TEMPLATE_ADB
177182
bool "adb"
183+
config PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_CHARDEV
184+
bool "cdc_acm_chardev"
178185
endchoice
179186

180187
config CONFIG_USBDEV_MSC_BLOCK_DEV_NAME

SConscript

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']):
131131
src += Glob('class/adb/usbd_adb.c')
132132
src += Glob('platform/rtthread/usbd_adb_shell.c')
133133

134+
if GetDepend(['PKG_CHERRYUSB_DEVICE_CDC_ACM_CHARDEV']):
135+
src += Glob('platform/rtthread/usbd_serial.c')
136+
134137
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM']):
135138
src += Glob('demo/cdc_acm_template.c')
136-
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC']):
139+
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC']) or GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV']):
137140
src += Glob('demo/msc_ram_template.c')
138-
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV']):
139-
src += Glob('platform/rtthread/usbd_msc_blkdev.c')
140141
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_HID_MOUSE']):
141142
src += Glob('demo/hid_mouse_template.c')
142143
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD']):
@@ -167,6 +168,8 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']):
167168
src += Glob('demo/winusb2.0_hid_template.c')
168169
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_ADB']):
169170
src += Glob('demo/adb/usbd_adb_template.c')
171+
if GetDepend(['PKG_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_CHARDEV']):
172+
src += Glob('demo/cdc_acm_rttchardev_template.c')
170173

171174
# USB HOST
172175
if GetDepend(['PKG_CHERRYUSB_HOST']):

demo/cdc_acm_rttchardev_template.c

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Copyright (c) 2025, sakumisu
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "usbd_core.h"
7+
#include "usbd_cdc_acm.h"
8+
9+
/*!< endpoint address */
10+
#define CDC_IN_EP 0x81
11+
#define CDC_OUT_EP 0x02
12+
#define CDC_INT_EP 0x83
13+
14+
#define USBD_VID 0xFFFF
15+
#define USBD_PID 0xFFFF
16+
#define USBD_MAX_POWER 100
17+
#define USBD_LANGID_STRING 1033
18+
19+
/*!< config descriptor size */
20+
#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN)
21+
22+
#ifdef CONFIG_USB_HS
23+
#define CDC_MAX_MPS 512
24+
#else
25+
#define CDC_MAX_MPS 64
26+
#endif
27+
28+
#ifdef CONFIG_USBDEV_ADVANCE_DESC
29+
static const uint8_t device_descriptor[] = {
30+
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
31+
};
32+
33+
static const uint8_t config_descriptor[] = {
34+
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
35+
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02)
36+
};
37+
38+
static const uint8_t device_quality_descriptor[] = {
39+
///////////////////////////////////////
40+
/// device qualifier descriptor
41+
///////////////////////////////////////
42+
0x0a,
43+
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
44+
0x00,
45+
0x02,
46+
0x00,
47+
0x00,
48+
0x00,
49+
0x40,
50+
0x00,
51+
0x00,
52+
};
53+
54+
static const char *string_descriptors[] = {
55+
(const char[]){ 0x09, 0x04 }, /* Langid */
56+
"CherryUSB", /* Manufacturer */
57+
"CherryUSB CDC DEMO", /* Product */
58+
"2022123456", /* Serial Number */
59+
};
60+
61+
static const uint8_t *device_descriptor_callback(uint8_t speed)
62+
{
63+
return device_descriptor;
64+
}
65+
66+
static const uint8_t *config_descriptor_callback(uint8_t speed)
67+
{
68+
return config_descriptor;
69+
}
70+
71+
static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
72+
{
73+
return device_quality_descriptor;
74+
}
75+
76+
static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
77+
{
78+
if (index > 3) {
79+
return NULL;
80+
}
81+
return string_descriptors[index];
82+
}
83+
84+
const struct usb_descriptor cdc_descriptor = {
85+
.device_descriptor_callback = device_descriptor_callback,
86+
.config_descriptor_callback = config_descriptor_callback,
87+
.device_quality_descriptor_callback = device_quality_descriptor_callback,
88+
.string_descriptor_callback = string_descriptor_callback
89+
};
90+
#else
91+
/*!< global descriptor */
92+
static const uint8_t cdc_descriptor[] = {
93+
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
94+
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
95+
CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
96+
///////////////////////////////////////
97+
/// string0 descriptor
98+
///////////////////////////////////////
99+
USB_LANGID_INIT(USBD_LANGID_STRING),
100+
///////////////////////////////////////
101+
/// string1 descriptor
102+
///////////////////////////////////////
103+
0x14, /* bLength */
104+
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
105+
'C', 0x00, /* wcChar0 */
106+
'h', 0x00, /* wcChar1 */
107+
'e', 0x00, /* wcChar2 */
108+
'r', 0x00, /* wcChar3 */
109+
'r', 0x00, /* wcChar4 */
110+
'y', 0x00, /* wcChar5 */
111+
'U', 0x00, /* wcChar6 */
112+
'S', 0x00, /* wcChar7 */
113+
'B', 0x00, /* wcChar8 */
114+
///////////////////////////////////////
115+
/// string2 descriptor
116+
///////////////////////////////////////
117+
0x26, /* bLength */
118+
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
119+
'C', 0x00, /* wcChar0 */
120+
'h', 0x00, /* wcChar1 */
121+
'e', 0x00, /* wcChar2 */
122+
'r', 0x00, /* wcChar3 */
123+
'r', 0x00, /* wcChar4 */
124+
'y', 0x00, /* wcChar5 */
125+
'U', 0x00, /* wcChar6 */
126+
'S', 0x00, /* wcChar7 */
127+
'B', 0x00, /* wcChar8 */
128+
' ', 0x00, /* wcChar9 */
129+
'C', 0x00, /* wcChar10 */
130+
'D', 0x00, /* wcChar11 */
131+
'C', 0x00, /* wcChar12 */
132+
' ', 0x00, /* wcChar13 */
133+
'D', 0x00, /* wcChar14 */
134+
'E', 0x00, /* wcChar15 */
135+
'M', 0x00, /* wcChar16 */
136+
'O', 0x00, /* wcChar17 */
137+
///////////////////////////////////////
138+
/// string3 descriptor
139+
///////////////////////////////////////
140+
0x16, /* bLength */
141+
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
142+
'2', 0x00, /* wcChar0 */
143+
'0', 0x00, /* wcChar1 */
144+
'2', 0x00, /* wcChar2 */
145+
'2', 0x00, /* wcChar3 */
146+
'1', 0x00, /* wcChar4 */
147+
'2', 0x00, /* wcChar5 */
148+
'3', 0x00, /* wcChar6 */
149+
'4', 0x00, /* wcChar7 */
150+
'5', 0x00, /* wcChar8 */
151+
'6', 0x00, /* wcChar9 */
152+
#ifdef CONFIG_USB_HS
153+
///////////////////////////////////////
154+
/// device qualifier descriptor
155+
///////////////////////////////////////
156+
0x0a,
157+
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
158+
0x00,
159+
0x02,
160+
0x00,
161+
0x00,
162+
0x00,
163+
0x40,
164+
0x00,
165+
0x00,
166+
#endif
167+
0x00
168+
};
169+
#endif
170+
171+
static void usbd_event_handler(uint8_t busid, uint8_t event)
172+
{
173+
switch (event) {
174+
case USBD_EVENT_RESET:
175+
break;
176+
case USBD_EVENT_CONNECTED:
177+
break;
178+
case USBD_EVENT_DISCONNECTED:
179+
break;
180+
case USBD_EVENT_RESUME:
181+
break;
182+
case USBD_EVENT_SUSPEND:
183+
break;
184+
case USBD_EVENT_CONFIGURED:
185+
186+
break;
187+
case USBD_EVENT_SET_REMOTE_WAKEUP:
188+
break;
189+
case USBD_EVENT_CLR_REMOTE_WAKEUP:
190+
break;
191+
192+
default:
193+
break;
194+
}
195+
}
196+
197+
extern void usbd_cdc_acm_serial_init(uint8_t busid, uint8_t in_ep, uint8_t out_ep);
198+
199+
void cdc_acm_chardev_init(uint8_t busid, uintptr_t reg_base)
200+
{
201+
#ifdef CONFIG_USBDEV_ADVANCE_DESC
202+
usbd_desc_register(busid, &cdc_descriptor);
203+
#else
204+
usbd_desc_register(busid, cdc_descriptor);
205+
#endif
206+
usbd_cdc_acm_serial_init(busid, CDC_IN_EP, CDC_OUT_EP);
207+
usbd_initialize(busid, reg_base, usbd_event_handler);
208+
}

0 commit comments

Comments
 (0)