1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <sample_usbd.h>
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/usb/usb_device.h>
12 #include <zephyr/usb/usbd.h>
13 #include <zephyr/drivers/uart.h>
14 
15 BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
16 	     "Console device is not ACM CDC UART device");
17 
18 #if defined(CONFIG_USB_DEVICE_STACK_NEXT)
19 static struct usbd_contex *sample_usbd;
20 
enable_usb_device_next(void)21 static int enable_usb_device_next(void)
22 {
23 	int err;
24 
25 	sample_usbd = sample_usbd_init_device();
26 	if (sample_usbd == NULL) {
27 		return -ENODEV;
28 	}
29 
30 	err = usbd_enable(sample_usbd);
31 	if (err) {
32 		return err;
33 	}
34 
35 	return 0;
36 }
37 #endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT) */
38 
main(void)39 int main(void)
40 {
41 	const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
42 	uint32_t dtr = 0;
43 
44 #if defined(CONFIG_USB_DEVICE_STACK_NEXT)
45 	if (enable_usb_device_next()) {
46 		return 0;
47 	}
48 #else
49 	if (usb_enable(NULL)) {
50 		return 0;
51 	}
52 #endif
53 
54 	/* Poll if the DTR flag was set */
55 	while (!dtr) {
56 		uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
57 		/* Give CPU resources to low priority threads. */
58 		k_sleep(K_MSEC(100));
59 	}
60 
61 	while (1) {
62 		printk("Hello World! %s\n", CONFIG_ARCH);
63 		k_sleep(K_SECONDS(1));
64 	}
65 }
66