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 <emil.obalski@nordicsemi.no>
This commit is contained in:
Emil Obalski 2020-07-30 14:52:15 +02:00 committed by Carles Cufí
parent 6ecfe3eba1
commit b552e60765
3 changed files with 31 additions and 26 deletions

View file

@ -34,9 +34,6 @@
#ifdef CONFIG_UART_CONSOLE_MCUMGR #ifdef CONFIG_UART_CONSOLE_MCUMGR
#include "mgmt/serial.h" #include "mgmt/serial.h"
#endif #endif
#ifdef CONFIG_USB_UART_CONSOLE
#include <usb/usb_device.h>
#endif
static struct device *uart_console_dev; static struct device *uart_console_dev;
@ -598,31 +595,9 @@ static int uart_console_init(struct device *arg)
ARG_UNUSED(arg); ARG_UNUSED(arg);
/* Claim console device */
uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); 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(); uart_console_hook_install();
return 0; return 0;

View file

@ -12,6 +12,8 @@
#include <drivers/i2c.h> #include <drivers/i2c.h>
#include <drivers/spi.h> #include <drivers/spi.h>
#include <drivers/sensor.h> #include <drivers/sensor.h>
#include <usb/usb_device.h>
#include <drivers/uart.h>
#include <stdio.h> #include <stdio.h>
@ -248,8 +250,21 @@ static void iis3dhhc_config(struct device *iis3dhhc)
void main(void) void main(void)
{ {
static struct device *led0, *led1; static struct device *led0, *led1;
struct device *dev = device_get_binding(
CONFIG_UART_CONSOLE_ON_DEV_NAME);
int i, on = 1; int i, on = 1;
int cnt = 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)); led0 = device_get_binding(DT_GPIO_LABEL(DT_ALIAS(led0), gpios));
gpio_pin_configure(led0, DT_GPIO_PIN(DT_ALIAS(led0), gpios), gpio_pin_configure(led0, DT_GPIO_PIN(DT_ALIAS(led0), gpios),

View file

@ -8,9 +8,24 @@
#include <sys/printk.h> #include <sys/printk.h>
#include <sys/util.h> #include <sys/util.h>
#include <string.h> #include <string.h>
#include <usb/usb_device.h>
#include <drivers/uart.h>
void main(void) 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) != if (strlen(CONFIG_UART_CONSOLE_ON_DEV_NAME) !=
strlen("CDC_ACM_0") || strlen("CDC_ACM_0") ||
strncmp(CONFIG_UART_CONSOLE_ON_DEV_NAME, "CDC_ACM_0", strncmp(CONFIG_UART_CONSOLE_ON_DEV_NAME, "CDC_ACM_0",