Skip to content

Commit

Permalink
samples: usb: console: enable optional new USB device support
Browse files Browse the repository at this point in the history
Add code and configuration to enable new USB device support.

Signed-off-by: Johann Fischer <[email protected]>
  • Loading branch information
jfischer-no committed Dec 2, 2022
1 parent eb40cef commit a5cb32c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
69 changes: 69 additions & 0 deletions samples/subsys/usb/console/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,88 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/usb/usbd.h>
#include <zephyr/drivers/uart.h>

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

#if IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT)
USBD_CONFIGURATION_DEFINE(config_1,
USB_SCD_SELF_POWERED,
200);

USBD_DESC_LANG_DEFINE(sample_lang);
USBD_DESC_STRING_DEFINE(sample_mfr, "ZEPHYR", 1);
USBD_DESC_STRING_DEFINE(sample_product, "Zephyr USBD ACM console", 2);
USBD_DESC_STRING_DEFINE(sample_sn, "0123456789ABCDEF", 3);

USBD_DEVICE_DEFINE(sample_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
0x2fe3, 0x0001);

static int enable_usb_device_next(void)
{
int err;

err = usbd_add_descriptor(&sample_usbd, &sample_lang);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_mfr);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_product);
if (err) {
return err;
}

err = usbd_add_descriptor(&sample_usbd, &sample_sn);
if (err) {
return err;
}

err = usbd_add_configuration(&sample_usbd, &config_1);
if (err) {
return err;
}

err = usbd_register_class(&sample_usbd, "cdc_acm_0", 1);
if (err) {
return err;
}

err = usbd_init(&sample_usbd);
if (err) {
return err;
}

err = usbd_enable(&sample_usbd);
if (err) {
return err;
}

return 0;
}
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT) */

void main(void)
{
const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
uint32_t dtr = 0;

#if IS_ENABLED(CONFIG_USB_DEVICE_STACK_NEXT)
if (enable_usb_device_next()) {
return;
}
#else
if (usb_enable(NULL)) {
return;
}
#endif

/* Poll if the DTR flag was set */
while (!dtr) {
Expand Down
10 changes: 10 additions & 0 deletions samples/subsys/usb/console/usbd_next_prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CONFIG_USB_DEVICE_STACK_NEXT=y

CONFIG_STDOUT_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_LINE_CTRL=y
CONFIG_USBD_CDC_ACM_CLASS=y

CONFIG_LOG=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

0 comments on commit a5cb32c

Please sign in to comment.