Skip to content

Commit 1354027

Browse files
jfischer-notmon-nordic
authored andcommitted
[nrf fromtree] samples: usb: add console sample for the new USB device stack
Add sample to shows how to use the CDC ACM UART provided by the new experimental USB device stack as a serial backend for the console. Signed-off-by: Johann Fischer <[email protected]> (cherry picked from commit be5e666) Signed-off-by: Tomasz Moń <[email protected]>
1 parent 7de7359 commit 1354027

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(console-next)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.. zephyr:code-sample:: usbd-cdc-acm-console
2+
:name: Console over USB CDC ACM
3+
:relevant-api: usbd_api uart_interface
4+
5+
Output "Hello World!" to the console over USB CDC ACM.
6+
7+
Overview
8+
********
9+
10+
This example application shows how to use the CDC ACM UART provided by the new
11+
experimental USB device stack as a serial backend for the console.
12+
13+
Requirements
14+
************
15+
16+
This project requires an experimental USB device driver (UDC API).
17+
18+
Building and Running
19+
********************
20+
21+
This sample can be built for multiple boards, in this example we will build it
22+
for the reel_board board:
23+
24+
.. zephyr-app-commands::
25+
:zephyr-app: samples/subsys/usb/console-next
26+
:board: reel_board
27+
:goals: flash
28+
:compact:
29+
30+
Plug the board into a host device, for sample, a PC running Linux OS.
31+
The board will be detected as a CDC ACM serial device. To see the console output
32+
from the sample, use a command similar to :command:`minicom -D /dev/ttyACM1`.
33+
34+
.. code-block:: console
35+
36+
Hello World! arm
37+
Hello World! arm
38+
Hello World! arm
39+
Hello World! arm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2021 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
chosen {
9+
zephyr,console = &cdc_acm_uart0;
10+
};
11+
};
12+
13+
&zephyr_udc0 {
14+
cdc_acm_uart0: cdc_acm_uart0 {
15+
compatible = "zephyr,cdc-acm-uart";
16+
};
17+
};
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG_SERIAL=y
2+
CONFIG_CONSOLE=y
3+
CONFIG_UART_CONSOLE=y
4+
CONFIG_STDOUT_CONSOLE=y
5+
CONFIG_UART_LINE_CTRL=y
6+
7+
CONFIG_USB_DEVICE_STACK_NEXT=y
8+
CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=y
9+
CONFIG_CDC_ACM_SERIAL_PID=0x0004
10+
CONFIG_CDC_ACM_SERIAL_PRODUCT_STRING="USBD console sample"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sample:
2+
name: Console over CDC ACM serial
3+
tests:
4+
sample.usbd.console:
5+
depends_on:
6+
- usbd
7+
tags: usb
8+
harness: console
9+
harness_config:
10+
fixture: fixture_usb_cdc
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/printk.h>
9+
#include <zephyr/drivers/uart.h>
10+
11+
BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
12+
"Console device is not ACM CDC UART device");
13+
14+
int main(void)
15+
{
16+
const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
17+
uint32_t dtr = 0;
18+
19+
/* Poll if the DTR flag was set */
20+
while (!dtr) {
21+
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
22+
/* Give CPU resources to low priority threads. */
23+
k_sleep(K_MSEC(100));
24+
}
25+
26+
while (1) {
27+
printk("Hello World! %s\n", CONFIG_ARCH);
28+
k_sleep(K_SECONDS(1));
29+
}
30+
}

0 commit comments

Comments
 (0)