Skip to content

Commit f095c02

Browse files
author
Josuah Demangeon
committed
tests: usb: host: test basic API functionality
Add tests making sure the USB Host class APIs introduced build and run as expected. Signed-off-by: Josuah Demangeon <[email protected]>
1 parent 44a8b71 commit f095c02

File tree

9 files changed

+140
-0
lines changed

9 files changed

+140
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(test_usb_host)
8+
9+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/usb/host)
10+
11+
target_sources(app PRIVATE src/main.c)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000000
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/delete-node/ &zephyr_udc0;
8+
9+
/ {
10+
zephyr_uhc0: uhc_vrt0 {
11+
compatible = "zephyr,uhc-virtual";
12+
13+
zephyr_udc0: udc_vrt0 {
14+
compatible = "zephyr,udc-virtual";
15+
num-bidir-endpoints = <8>;
16+
maximum-speed = "high-speed";
17+
};
18+
};
19+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000000
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "native_sim.overlay"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
zephyr_uhc0: uhc_vrt0 {
9+
compatible = "zephyr,uhc-virtual";
10+
11+
zephyr_udc0: udc_vrt0 {
12+
compatible = "zephyr,udc-virtual";
13+
num-bidir-endpoints = <8>;
14+
maximum-speed = "high-speed";
15+
};
16+
};
17+
};

tests/subsys/usb/host/prj.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_LOG=y
5+
CONFIG_ZTEST=y
6+
7+
CONFIG_USB_HOST_STACK=y
8+
CONFIG_UHC_DRIVER=y

tests/subsys/usb/host/src/main.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/usb/usbh.h>
9+
#include <zephyr/usb/usbh.h>
10+
11+
#include "usbh_ch9.h"
12+
#include "usbh_host.h"
13+
14+
#include <zephyr/logging/log.h>
15+
LOG_MODULE_REGISTER(usb_test, LOG_LEVEL_INF);
16+
17+
USBH_CONTROLLER_DEFINE(uhs_ctx, DEVICE_DT_GET(DT_NODELABEL(zephyr_uhc0)));
18+
19+
static void *usb_test_enable(void)
20+
{
21+
int ret;
22+
23+
ret = usbh_init(&uhs_ctx);
24+
zassert_ok(ret, "Failed to initialize USB host (%d)", ret);
25+
26+
ret = usbh_enable(&uhs_ctx);
27+
zassert_ok(ret, "Failed to enable USB host (%d)", ret);
28+
29+
ret = uhc_bus_reset(uhs_ctx.dev);
30+
zassert_ok(ret, "Failed to signal bus reset (%d)", ret);
31+
32+
ret = uhc_bus_resume(uhs_ctx.dev);
33+
zassert_ok(ret, "Failed to signal bus resume (%d)", ret);
34+
35+
ret = uhc_sof_enable(uhs_ctx.dev);
36+
zassert_ok(ret, "Failed to enable SoF generator (%d)", ret);
37+
38+
LOG_INF("Host controller enabled");
39+
40+
/* Allow the host time to reset the host. */
41+
k_msleep(200);
42+
43+
return NULL;
44+
}
45+
46+
static void usb_test_shutdown(void *f)
47+
{
48+
int ret;
49+
50+
ret = usbh_disable(&uhs_ctx);
51+
zassert_ok(ret, "Failed to enable host support (%d)", ret);
52+
53+
ret = usbh_shutdown(&uhs_ctx);
54+
zassert_ok(ret, "Failed to shutdown host support (%d)", ret);
55+
56+
LOG_INF("Host controller disabled");
57+
}
58+
59+
ZTEST_SUITE(host, NULL, usb_test_enable, NULL, NULL, usb_test_shutdown);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests:
2+
usb.host:
3+
platform_allow:
4+
- native_sim
5+
- native_sim/native/64
6+
- qemu_cortex_m3
7+
integration_platforms:
8+
- native_sim
9+
tags: usb
10+
usb.host.build_all:
11+
platform_allow:
12+
- native_sim
13+
- native_sim/native/64
14+
integration_platforms:
15+
- native_sim
16+
tags: usb
17+
build_only: true

0 commit comments

Comments
 (0)