From b552e607656092b3dcca126bdc240017ba7f0f15 Mon Sep 17 00:00:00 2001 From: Emil Obalski Date: Thu, 30 Jul 2020 14:52:15 +0200 Subject: [PATCH] samples: usb: Make USB console samples call usb_enable() usb_enable() must be called once by the application. The application may want to register usb_dc_status_callback and trace usb status codes (usb_dc_status_code). After this patch all pre APPLICATION messages will be dropped as USB console device is enabled in the application. Application waits for console device until its ready by checking DTR flag - uart_line_ctrl_get(). This function could be dropped but then some log messages that were generated before USB device is ready could also be dropped. Signed-off-by: Emil Obalski --- drivers/console/uart_console.c | 27 +----------------------- samples/boards/sensortile_box/src/main.c | 15 +++++++++++++ samples/subsys/usb/console/src/main.c | 15 +++++++++++++ 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/drivers/console/uart_console.c b/drivers/console/uart_console.c index 752b81ed52..5d84660f02 100644 --- a/drivers/console/uart_console.c +++ b/drivers/console/uart_console.c @@ -34,9 +34,6 @@ #ifdef CONFIG_UART_CONSOLE_MCUMGR #include "mgmt/serial.h" #endif -#ifdef CONFIG_USB_UART_CONSOLE -#include -#endif static struct device *uart_console_dev; @@ -598,31 +595,9 @@ static int uart_console_init(struct device *arg) ARG_UNUSED(arg); + /* Claim console device */ uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); - __ASSERT_NO_MSG(uart_console_dev); - -#if defined(CONFIG_USB_UART_CONSOLE) - int ret; - - ret = usb_enable(NULL); - if (ret != 0) { - return ret; - } - -#if defined(CONFIG_USB_UART_DTR_WAIT) - while (1) { - uint32_t dtr = 0U; - - uart_line_ctrl_get(uart_console_dev, UART_LINE_CTRL_DTR, &dtr); - if (dtr) { - break; - } - } - k_busy_wait(1000000); -#endif -#endif - uart_console_hook_install(); return 0; diff --git a/samples/boards/sensortile_box/src/main.c b/samples/boards/sensortile_box/src/main.c index 9db2d1f115..0f3da04eae 100644 --- a/samples/boards/sensortile_box/src/main.c +++ b/samples/boards/sensortile_box/src/main.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include @@ -248,8 +250,21 @@ static void iis3dhhc_config(struct device *iis3dhhc) void main(void) { static struct device *led0, *led1; + struct device *dev = device_get_binding( + CONFIG_UART_CONSOLE_ON_DEV_NAME); int i, on = 1; int cnt = 1; + uint32_t dtr = 0; + + /* Application must enable USB by itself */ + if (usb_enable(NULL)) { + return; + } + + /* Poll if the DTR flag was set, optional */ + while (!dtr) { + uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr); + } led0 = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(led0), gpios)); gpio_pin_configure(led0, DT_GPIO_PIN(DT_ALIAS(led0), gpios), diff --git a/samples/subsys/usb/console/src/main.c b/samples/subsys/usb/console/src/main.c index db23e528cd..9698a38f66 100644 --- a/samples/subsys/usb/console/src/main.c +++ b/samples/subsys/usb/console/src/main.c @@ -8,9 +8,24 @@ #include #include #include +#include +#include void main(void) { + struct device *dev = device_get_binding( + CONFIG_UART_CONSOLE_ON_DEV_NAME); + uint32_t dtr = 0; + + if (usb_enable(NULL)) { + return; + } + + /* Poll if the DTR flag was set, optional */ + while (!dtr) { + uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr); + } + if (strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME) != strlen("CDC_ACM_0") || strncmp(CONFIG_UART_CONSOLE_ON_DEV_NAME, "CDC_ACM_0",