Using USB instead of UART0 for debugging/communciation on the nRF52840DK

Hello :) The default setting for the console on the nRF52840 is over UART0. I can easily print logging messages to the console and I have also implemented a function to parse incoming serial messages. See the code below:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include "stdio.h"
#include "my_gpio.h"
#include "zephyr/drivers/uart.h"
#include <zephyr/sys/ring_buffer.h>
#define LOG_LEVEL 4
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(nrf52_learning);
#define UART_BUF_SIZE 24
#define UART_RX_TIMEOUT_MS 1000
K_SEM_DEFINE(rx_disabled, 0, 1);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I want to be able to send any messages via the serial terminal to the device and I want the device to be able to parse incomming messages and take any required action. For example. I may want so end a serial command "Turn on LED1" and the device can parse the message and turn on the LED1...

For my application, I want to reconfigure UART0 to communicate with external device instead of logging and console and I want to use USB for this instead.

So the plan is:

UART0 - Communication with external device 1

UART1 - Communication with external device 2

USB - Logging and parsing commands

I have looked at the Console over USB example (zephyr/samples/subsys/usb/console) to check how to setup the console over the USB and I came up with the project below:

https://github.com/krupis/nrf52_learning/tree/USB_debug

The full code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/poweroff.h>
#include "stdio.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");
#define LOG_LEVEL 4
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(nrf52_learning);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

nrf52840dk_nrf52840.overlay:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
arduino_serial: &uart1 {
status = "okay";
};
/ {
chosen {
zephyr,console = &cdc_acm_uart0;
};
};
&zephyr_udc0 {
cdc_acm_uart0: cdc_acm_uart0 {
compatible = "zephyr,cdc-acm-uart";
label = "CDC_ACM_0";
};
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

prj.conf:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Zephyr test"
CONFIG_USB_DEVICE_PID=0x0004
CONFIG_USB_DEVICE_MANUFACTURER="Nordic Semiconductor"
CONFIG_USB_DEVICE_VID=0x1915
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The result:

I can see that the logging via the USB is working as expected. If I connect the USB to the nRF USB port instead of the nRF debug USB, I can see that the logs are coming through:

My questions:

Question 1:

I have read through the USB documentation (https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.2-dev1/zephyr/reference/usb/uds.html) but it is still not fully clear to me how can I implement serial command parsing via the USB. I want to be able to send the commands via the serial terminal and I expect some callback to be triggered that receives the data (Simillarly to how the UART_RX_RDY event is triggered when the data is received using UART ASYNC method) and I can then parse the data accordingly. Could someone point me in the right direction on how to get this implemented as I could not find any relevant example projects to get started with.

Question 2:

I would like to understand how can I change the COM port name that my device appears as. Currently it appears as a USB Serial Device (COM41).  Can I change this to any name I want? 

Question 3:

What is the purpose of 

CONFIG_USB_DEVICE_PRODUCT="Zephyr test"
and 
CONFIG_USB_DEVICE_MANUFACTURER="Nordic Semiconductor"

As they do not seem to show up anywhere?