Skip to content

Commit dc26a61

Browse files
jfischer-nocarlescufi
authored andcommitted
samples: usb: console: enable optional new USB device support
Add code and configuration to enable new USB device support. Signed-off-by: Johann Fischer <[email protected]>
1 parent 13766dd commit dc26a61

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

samples/subsys/usb/console/src/main.c

+69
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,88 @@
77
#include <zephyr/kernel.h>
88
#include <zephyr/sys/printk.h>
99
#include <zephyr/usb/usb_device.h>
10+
#include <zephyr/usb/usbd.h>
1011
#include <zephyr/drivers/uart.h>
1112

1213
BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
1314
"Console device is not ACM CDC UART device");
1415

16+
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT)
17+
USBD_CONFIGURATION_DEFINE(config_1,
18+
USB_SCD_SELF_POWERED,
19+
200);
20+
21+
USBD_DESC_LANG_DEFINE(sample_lang);
22+
USBD_DESC_STRING_DEFINE(sample_mfr, "ZEPHYR", 1);
23+
USBD_DESC_STRING_DEFINE(sample_product, "Zephyr USBD ACM console", 2);
24+
USBD_DESC_STRING_DEFINE(sample_sn, "0123456789ABCDEF", 3);
25+
26+
USBD_DEVICE_DEFINE(sample_usbd,
27+
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
28+
0x2fe3, 0x0001);
29+
30+
static int enable_usb_device_next(void)
31+
{
32+
int err;
33+
34+
err = usbd_add_descriptor(&sample_usbd, &sample_lang);
35+
if (err) {
36+
return err;
37+
}
38+
39+
err = usbd_add_descriptor(&sample_usbd, &sample_mfr);
40+
if (err) {
41+
return err;
42+
}
43+
44+
err = usbd_add_descriptor(&sample_usbd, &sample_product);
45+
if (err) {
46+
return err;
47+
}
48+
49+
err = usbd_add_descriptor(&sample_usbd, &sample_sn);
50+
if (err) {
51+
return err;
52+
}
53+
54+
err = usbd_add_configuration(&sample_usbd, &config_1);
55+
if (err) {
56+
return err;
57+
}
58+
59+
err = usbd_register_class(&sample_usbd, "cdc_acm_0", 1);
60+
if (err) {
61+
return err;
62+
}
63+
64+
err = usbd_init(&sample_usbd);
65+
if (err) {
66+
return err;
67+
}
68+
69+
err = usbd_enable(&sample_usbd);
70+
if (err) {
71+
return err;
72+
}
73+
74+
return 0;
75+
}
76+
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT) */
77+
1578
void main(void)
1679
{
1780
const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
1881
uint32_t dtr = 0;
1982

83+
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT)
84+
if (enable_usb_device_next()) {
85+
return;
86+
}
87+
#else
2088
if (usb_enable(NULL)) {
2189
return;
2290
}
91+
#endif
2392

2493
/* Poll if the DTR flag was set */
2594
while (!dtr) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG_USB_DEVICE_STACK_NEXT=y
2+
3+
CONFIG_STDOUT_CONSOLE=y
4+
CONFIG_SERIAL=y
5+
CONFIG_UART_LINE_CTRL=y
6+
CONFIG_USBD_CDC_ACM_CLASS=y
7+
8+
CONFIG_LOG=y
9+
CONFIG_USBD_LOG_LEVEL_WRN=y
10+
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

0 commit comments

Comments
 (0)