|
7 | 7 | #include <zephyr/kernel.h>
|
8 | 8 | #include <zephyr/sys/printk.h>
|
9 | 9 | #include <zephyr/usb/usb_device.h>
|
| 10 | +#include <zephyr/usb/usbd.h> |
10 | 11 | #include <zephyr/drivers/uart.h>
|
11 | 12 |
|
12 | 13 | BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
|
13 | 14 | "Console device is not ACM CDC UART device");
|
14 | 15 |
|
| 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 | + |
15 | 78 | void main(void)
|
16 | 79 | {
|
17 | 80 | const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
|
18 | 81 | uint32_t dtr = 0;
|
19 | 82 |
|
| 83 | +#if IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT) |
| 84 | + if (enable_usb_device_next()) { |
| 85 | + return; |
| 86 | + } |
| 87 | +#else |
20 | 88 | if (usb_enable(NULL)) {
|
21 | 89 | return;
|
22 | 90 | }
|
| 91 | +#endif |
23 | 92 |
|
24 | 93 | /* Poll if the DTR flag was set */
|
25 | 94 | while (!dtr) {
|
|
0 commit comments