Skip to content

Commit a7b0f71

Browse files
jfischer-nocfriedt
authored andcommitted
samples: usb: move the legacy code out of the CDC ACM sample
Move the legacy code of the CDC ACM sample to a separate sample in the legacy directory. Signed-off-by: Johann Fischer <[email protected]>
1 parent a466cd8 commit a7b0f71

File tree

11 files changed

+310
-81
lines changed

11 files changed

+310
-81
lines changed

samples/subsys/usb/cdc_acm/README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. zephyr:code-sample:: usb-cdc-acm
2-
:name: USB CDC-ACM
3-
:relevant-api: usbd_api _usb_device_core_api uart_interface
2+
:name: USB CDC ACM UART sample
3+
:relevant-api: usbd_api uart_interface
44

5-
Use USB CDC-ACM driver to implement a serial port echo.
5+
Use USB CDC ACM UART driver to implement a serial port echo.
66

77
Overview
88
********

samples/subsys/usb/cdc_acm/prj.conf

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
CONFIG_USB_DEVICE_STACK_NEXT=y
2+
13
CONFIG_STDOUT_CONSOLE=y
2-
CONFIG_USB_DEVICE_STACK=y
3-
CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC ACM sample"
4-
CONFIG_USB_DEVICE_PID=0x0001
5-
CONFIG_LOG=y
6-
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
7-
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
84
CONFIG_SERIAL=y
9-
CONFIG_UART_INTERRUPT_DRIVEN=y
105
CONFIG_UART_LINE_CTRL=y
11-
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
6+
CONFIG_USBD_CDC_ACM_CLASS=y
7+
8+
CONFIG_LOG=y
9+
CONFIG_USBD_LOG_LEVEL_ERR=y
10+
CONFIG_UDC_DRIVER_LOG_LEVEL_ERR=y
11+
CONFIG_USBD_CDC_ACM_LOG_LEVEL_ERR=y
12+
13+
CONFIG_SAMPLE_USBD_PID=0x0001
14+
CONFIG_SAMPLE_USBD_PRODUCT="USBD CDC ACM sample"
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
sample:
22
name: CDC ACM USB
33
tests:
4-
sample.usb.cdc-acm:
5-
depends_on: usb_device
6-
tags: usb
7-
arch_exclude: posix
8-
harness: console
9-
harness_config:
10-
type: one_line
11-
regex:
12-
- "Wait for DTR"
134
sample.usb_device_next.cdc-acm:
145
depends_on: usbd
156
tags: usb
16-
extra_args: CONF_FILE="usbd_next_prj.conf"
177
integration_platforms:
188
- nrf52840dk/nrf52840
199
- nrf54h20dk/nrf54h20/cpuapp
@@ -34,7 +24,6 @@ tests:
3424
depends_on: usbd
3525
tags: usb
3626
extra_args:
37-
- CONF_FILE="usbd_next_prj.conf"
3827
- DCONFIG_USBD_CDC_ACM_WORKQUEUE=y
3928
integration_platforms:
4029
- frdm_k64f
@@ -43,10 +32,3 @@ tests:
4332
type: one_line
4433
regex:
4534
- "Wait for DTR"
46-
sample.usb.cdc-acm.buildonly:
47-
depends_on: usb_device
48-
tags: usb
49-
arch_allow: posix
50-
build_only: true
51-
integration_platforms:
52-
- native_sim

samples/subsys/usb/cdc_acm/src/main.c

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
/**
8-
* @file
9-
* @brief Sample echo app for CDC ACM class
10-
*
11-
* Sample app for USB CDC ACM class driver. The received data is echoed back
12-
* to the serial port.
13-
*/
14-
157
#include <sample_usbd.h>
168

179
#include <stdio.h>
@@ -21,7 +13,6 @@
2113
#include <zephyr/kernel.h>
2214
#include <zephyr/sys/ring_buffer.h>
2315

24-
#include <zephyr/usb/usb_device.h>
2516
#include <zephyr/usb/usbd.h>
2617
#include <zephyr/logging/log.h>
2718
LOG_MODULE_REGISTER(cdc_acm_echo, LOG_LEVEL_INF);
@@ -48,7 +39,6 @@ static inline void print_baudrate(const struct device *dev)
4839
}
4940
}
5041

51-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
5242
static struct usbd_context *sample_usbd;
5343
K_SEM_DEFINE(dtr_sem, 0, 1);
5444

@@ -106,7 +96,6 @@ static int enable_usb_device_next(void)
10696

10797
return 0;
10898
}
109-
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
11099

111100
static void interrupt_handler(const struct device *dev, void *user_data)
112101
{
@@ -178,37 +167,16 @@ int main(void)
178167
return 0;
179168
}
180169

181-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
182-
ret = enable_usb_device_next();
183-
#else
184-
ret = usb_enable(NULL);
185-
#endif
186-
170+
ret = enable_usb_device_next();
187171
if (ret != 0) {
188-
LOG_ERR("Failed to enable USB");
172+
LOG_ERR("Failed to enable USB device support");
189173
return 0;
190174
}
191175

192176
ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer);
193177

194178
LOG_INF("Wait for DTR");
195-
196-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
197179
k_sem_take(&dtr_sem, K_FOREVER);
198-
#else
199-
while (true) {
200-
uint32_t dtr = 0U;
201-
202-
uart_line_ctrl_get(uart_dev, UART_LINE_CTRL_DTR, &dtr);
203-
if (dtr) {
204-
break;
205-
} else {
206-
/* Give CPU resources to low priority threads. */
207-
k_sleep(K_MSEC(100));
208-
}
209-
}
210-
#endif
211-
212180
LOG_INF("DTR set");
213181

214182
/* They are optional, we use them to test the interrupt endpoint */
@@ -225,11 +193,7 @@ int main(void)
225193
/* Wait 100ms for the host to do all settings */
226194
k_msleep(100);
227195

228-
#ifndef CONFIG_USB_DEVICE_STACK_NEXT
229-
print_baudrate(uart_dev);
230-
#endif
231196
uart_irq_callback_set(uart_dev, interrupt_handler);
232-
233197
/* Enable rx interrupts */
234198
uart_irq_rx_enable(uart_dev);
235199

samples/subsys/usb/cdc_acm/usbd_next_prj.conf

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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(cdc_acm)
6+
7+
include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
.. zephyr:code-sample:: legacy-usb-cdc-acm
2+
:name: Legacy USB CDC ACM UART sample
3+
:relevant-api: _usb_device_core_api uart_interface
4+
5+
Use USB CDC ACM UART driver to implement a serial port echo.
6+
7+
Overview
8+
********
9+
10+
This sample app demonstrates use of a USB Communication Device Class (CDC)
11+
Abstract Control Model (ACM) driver provided by the Zephyr project.
12+
Received data from the serial port is echoed back to the same port
13+
provided by this driver.
14+
15+
.. note::
16+
This samples demonstrate deprecated :ref:`usb_device_stack`.
17+
18+
This sample can be found under :zephyr_file:`samples/subsys/usb/legacy/cdc_acm` in the
19+
Zephyr project tree.
20+
21+
Requirements
22+
************
23+
24+
This project requires an USB device driver, which is available for multiple
25+
boards supported in Zephyr.
26+
27+
Building and Running
28+
********************
29+
30+
Reel Board
31+
===========
32+
33+
To see the console output of the app, open a serial port emulator and
34+
attach it to the USB to TTL Serial cable. Build and flash the project:
35+
36+
.. zephyr-app-commands::
37+
:zephyr-app: samples/subsys/usb/legacy/cdc_acm
38+
:board: reel_board
39+
:goals: flash
40+
:compact:
41+
42+
Running
43+
=======
44+
45+
Plug the board into a host device, for example, a PC running Linux.
46+
The board will be detected as shown by the Linux dmesg command:
47+
48+
.. code-block:: console
49+
50+
usb 9-1: new full-speed USB device number 112 using uhci_hcd
51+
usb 9-1: New USB device found, idVendor=8086, idProduct=f8a1
52+
usb 9-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
53+
usb 9-1: Product: CDC-ACM
54+
usb 9-1: Manufacturer: Intel
55+
usb 9-1: SerialNumber: 00.01
56+
cdc_acm 9-1:1.0: ttyACM1: USB ACM device
57+
58+
The app prints on serial output (UART1), used for the console:
59+
60+
.. code-block:: console
61+
62+
Wait for DTR
63+
64+
Open a serial port emulator, for example minicom
65+
and attach it to detected CDC ACM device:
66+
67+
.. code-block:: console
68+
69+
minicom --device /dev/ttyACM1
70+
71+
The app should respond on serial output with:
72+
73+
.. code-block:: console
74+
75+
DTR set, start test
76+
Baudrate detected: 115200
77+
78+
And on ttyACM device, provided by zephyr USB device stack:
79+
80+
.. code-block:: console
81+
82+
Send characters to the UART device
83+
Characters read:
84+
85+
The characters entered in serial port emulator will be echoed back.
86+
87+
Troubleshooting
88+
===============
89+
90+
If the ModemManager runs on your operating system, it will try
91+
to access the CDC ACM device and maybe you can see several characters
92+
including "AT" on the terminal attached to the CDC ACM device.
93+
You can add or extend the udev rule for your board to inform
94+
ModemManager to skip the CDC ACM device.
95+
For this example, it would look like this:
96+
97+
.. code-block:: none
98+
99+
ATTRS{idVendor}=="8086" ATTRS{idProduct}=="f8a1", ENV{ID_MM_DEVICE_IGNORE}="1"
100+
101+
You can use
102+
``/lib/udev/rules.d/77-mm-usb-device-blacklist.rules`` as reference.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2021 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&zephyr_udc0 {
8+
cdc_acm_uart0 {
9+
compatible = "zephyr,cdc-acm-uart";
10+
};
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_USB_DEVICE_STACK=y
3+
CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC ACM sample"
4+
CONFIG_USB_DEVICE_PID=0x0001
5+
CONFIG_LOG=y
6+
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
7+
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
8+
CONFIG_SERIAL=y
9+
CONFIG_UART_INTERRUPT_DRIVEN=y
10+
CONFIG_UART_LINE_CTRL=y
11+
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
name: CDC ACM USB
3+
tests:
4+
sample.usb.legacy.cdc-acm:
5+
depends_on: usb_device
6+
tags: usb
7+
build_only: true

0 commit comments

Comments
 (0)