Skip to content

Commit 6921da9

Browse files
committed
samples: usb: testusb: use new USB device stack
Rework sample to use new USB device stack. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
1 parent fb02eea commit 6921da9

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

samples/subsys/usb/testusb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ cmake_minimum_required(VERSION 3.20.0)
44
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
55
project(testusb)
66

7+
include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
78
FILE(GLOB app_sources src/*.c)
89
target_sources(app PRIVATE ${app_sources})

samples/subsys/usb/testusb/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2023 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Source common USB sample options used to initialize new experimental USB
5+
# device stack. The scope of these options is limited to USB samples in project
6+
# tree, you cannot use them in your own application.
7+
source "samples/subsys/usb/common/Kconfig.sample_usbd"
8+
9+
source "Kconfig.zephyr"

samples/subsys/usb/testusb/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. zephyr:code-sample:: testusb-app
2-
:name: USB testing application
3-
:relevant-api: _usb_device_core_api
2+
:name: USB device testing application
3+
:relevant-api: usbd_api
44

55
Test USB device drivers using a loopback function.
66

samples/subsys/usb/testusb/prj.conf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
CONFIG_STDOUT_CONSOLE=y
1+
CONFIG_USB_DEVICE_STACK_NEXT=y
2+
CONFIG_USBD_LOOPBACK_CLASS=y
3+
CONFIG_UDC_BUF_POOL_SIZE=4096
24

3-
#USB related configs
4-
CONFIG_USB_DEVICE_STACK=y
5-
CONFIG_USB_DEVICE_PRODUCT="Zephyr testusb sample"
6-
CONFIG_USB_DEVICE_PID=0x0009
75
CONFIG_LOG=y
8-
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
9-
CONFIG_USB_DEVICE_LOOPBACK=y
10-
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
11-
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
6+
CONFIG_USBD_LOG_LEVEL_ERR=y
7+
CONFIG_UDC_DRIVER_LOG_LEVEL_ERR=y
8+
CONFIG_USBD_LOOPBACK_LOG_LEVEL_ERR=y
9+
10+
CONFIG_SAMPLE_USBD_PID=0x0009
11+
CONFIG_SAMPLE_USBD_PRODUCT="Zephyr testusb sample"
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sample:
2-
name: USB loopback sample
2+
name: USB device loopback sample
33
tests:
4-
sample.usb.loopback:
5-
depends_on: usb_device
4+
sample.usbd.loopback:
5+
depends_on: usbd
66
tags: usb
7-
harness: button
7+
harness: TBD

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
/*
2-
* Copyright (c) 2018 Phytec Messtechnik GmbH
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <zephyr/kernel.h>
7+
#include <sample_usbd.h>
8+
#include <zephyr/usb/usbd.h>
9+
810
#include <zephyr/logging/log.h>
9-
#include <zephyr/usb/usb_device.h>
10-
LOG_MODULE_REGISTER(main);
11+
LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
1112

1213
int main(void)
1314
{
15+
struct usbd_context *sample_usbd;
1416
int ret;
1517

16-
ret = usb_enable(NULL);
17-
if (ret != 0) {
18-
LOG_ERR("Failed to enable USB");
19-
return 0;
18+
sample_usbd = sample_usbd_setup_device(NULL);
19+
if (sample_usbd == NULL) {
20+
LOG_ERR("Failed to setup USB device");
21+
return -ENODEV;
22+
}
23+
24+
ret = usbd_init(sample_usbd);
25+
if (ret) {
26+
LOG_ERR("Failed to initialize device support");
27+
return ret;
28+
}
29+
30+
ret = usbd_enable(sample_usbd);
31+
if (ret) {
32+
LOG_ERR("Failed to enable device support");
33+
return ret;
2034
}
2135

22-
LOG_INF("entered main.");
2336
return 0;
2437
}

0 commit comments

Comments
 (0)