1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/usb/usb_device.h>
10 #include <zephyr/usb/usbd.h>
11 #include <zephyr/drivers/uart.h>
12
13 BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
14 "Console device is not ACM CDC UART device");
15
16 #if defined(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_MANUFACTURER_DEFINE(sample_mfr, "ZEPHYR");
23 USBD_DESC_PRODUCT_DEFINE(sample_product, "Zephyr USBD ACM console");
24 USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn, "0123456789ABCDEF");
25
26 USBD_DEVICE_DEFINE(sample_usbd,
27 DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
28 0x2fe3, 0x0001);
29
enable_usb_device_next(void)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
main(void)78 int main(void)
79 {
80 const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
81 uint32_t dtr = 0;
82
83 #if defined(CONFIG_USB_DEVICE_STACK_NEXT)
84 if (enable_usb_device_next()) {
85 return 0;
86 }
87 #else
88 if (usb_enable(NULL)) {
89 return 0;
90 }
91 #endif
92
93 /* Poll if the DTR flag was set */
94 while (!dtr) {
95 uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
96 /* Give CPU resources to low priority threads. */
97 k_sleep(K_MSEC(100));
98 }
99
100 while (1) {
101 printk("Hello World! %s\n", CONFIG_ARCH);
102 k_sleep(K_SECONDS(1));
103 }
104 }
105