Skip to content

Commit

Permalink
tests: add gs_usb test suite
Browse files Browse the repository at this point in the history
Add initial test suite covering the control transfers of the gs_usb USB
device class implementation.

Signed-off-by: Henrik Brix Andersen <[email protected]>
  • Loading branch information
henrikbrixandersen committed Sep 27, 2024
1 parent 5d4a557 commit c5f1cdf
Show file tree
Hide file tree
Showing 17 changed files with 1,297 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 99-cannectivity.rules
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ ACTION!="add", SUBSYSTEM!="usb_device", GOTO="cannectivity_rules_end"
ATTR{idVendor}=="1209", ATTR{idProduct}=="ca01", RUN+="/sbin/modprobe -b gs_usb" MODE="660", GROUP="plugdev", TAG+="uaccess"
SUBSYSTEM=="drivers", ENV{DEVPATH}=="/bus/usb/drivers/gs_usb", ATTR{new_id}="1209 ca01"

# Used for pytest suites
ATTR{idVendor}=="1209", ATTR{idProduct}=="0001", MODE="660", GROUP="plugdev", TAG+="uaccess"

LABEL="cannectivity_rules_end"
1 change: 1 addition & 0 deletions scripts/requirements-run-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyusb
1 change: 1 addition & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r requirements-run-test.txt
9 changes: 9 additions & 0 deletions tests/subsys/usb/gs_usb/host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024 Henrik Brix Andersen <[email protected]>
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(host)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
59 changes: 59 additions & 0 deletions tests/subsys/usb/gs_usb/host/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) 2024 Henrik Brix Andersen <[email protected]>
# SPDX-License-Identifier: Apache-2.0

menu "Test suite for gs_usb"

config TEST_USB_MANUFACTURER
string "USB device manufacturer string"
default "CANnectivity"
help
USB device manufacturer string.

config TEST_USB_PRODUCT
string "USB device product string"
default "CANnectivity gs_usb Test Suite"
help
USB product string.

config TEST_USB_VID
hex "USB device Vendor ID (VID)"
default 0x1209
help
USB device Vendor ID (VID).

config TEST_USB_PID
hex "USB device Product ID (PID)"
default 0x0001
help
USB device Product ID (PID).

config TEST_USB_MAX_POWER
int "USB device maximum power"
default 125
range 0 250
help
USB maximum current draw in milliampere (mA) divided by 2.
A value of 125 results in a maximum current draw value of 250 mA.

if USB_DEVICE_STACK

configdefault USB_DEVICE_MANUFACTURER
default TEST_USB_MANUFACTURER

configdefault USB_DEVICE_PRODUCT
default TEST_USB_PRODUCT

configdefault USB_DEVICE_VID
default TEST_USB_VID

configdefault USB_DEVICE_PID
default TEST_USB_PID

configdefault USB_MAX_POWER
default TEST_USB_MAX_POWER

endif # USB_DEVICE_STACK

endmenu

source "Kconfig.zephyr"
27 changes: 27 additions & 0 deletions tests/subsys/usb/gs_usb/host/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Geschwister Schneider USB/CAN Protocol Tests
############################################

Overview
********

This test suite uses `PyUSB`_ for testing the Geschwister Schneider USB/CAN protocol implementation.

Prerequisites
*************

The test suite has the following prerequisites:

* The PyUSB library must be installed on the host PC.
* The DUT must be connected to the host PC via USB.

Building and Running
********************

Below is an example for running the test suite on the ``frdm_k64f`` board:

.. code-block:: shell
west twister -v -p frdm_k64f/mk64f12 --device-testing --device-serial /dev/ttyACM0 -X usb -T tests/subsys/usb/gs_usb/host/
.. _PyUSB:
https://pyusb.github.io/pyusb/
33 changes: 33 additions & 0 deletions tests/subsys/usb/gs_usb/host/app.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2022-2024 Henrik Brix Andersen <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
fake_can0: fake_can0 {
status = "okay";
compatible = "zephyr,fake-can";
};

fake_can1: fake_can1 {
status = "okay";
compatible = "zephyr,fake-can";
};

can_loopback0: can_loopback0 {
status = "okay";
compatible = "zephyr,can-loopback";
};

can_loopback1: can_loopback1 {
status = "okay";
compatible = "zephyr,can-loopback";
};
};

&zephyr_udc0 {
gs_usb0: gs_usb0 {
compatible = "gs_usb";
};
};
21 changes: 21 additions & 0 deletions tests/subsys/usb/gs_usb/host/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CONFIG_TEST=y

CONFIG_LOG=y
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_GS_USB_LOG_LEVEL_DBG=y

CONFIG_SHELL=y

CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_BOS=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
CONFIG_USB_COMPOSITE_DEVICE=y

CONFIG_CAN=y
CONFIG_CAN_FD_MODE=y

CONFIG_USB_DEVICE_GS_USB_MAX_CHANNELS=4
CONFIG_USB_DEVICE_GS_USB_IDENTIFICATION=y
CONFIG_USB_DEVICE_GS_USB_TIMESTAMP=y
CONFIG_USB_DEVICE_GS_USB_TERMINATION=y
21 changes: 21 additions & 0 deletions tests/subsys/usb/gs_usb/host/prj_usbd_next.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CONFIG_TEST=y
# Some usbd_next drivers suffer from calling mutexes in ISR context. Disable asserts until fixed
# upstream.
#CONFIG_ASSERT=n

CONFIG_LOG=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_USBD_GS_USB_LOG_LEVEL_DBG=y

CONFIG_SHELL=y

CONFIG_USB_DEVICE_STACK_NEXT=y

CONFIG_CAN=y
CONFIG_CAN_FD_MODE=y

CONFIG_USBD_GS_USB_MAX_CHANNELS=4
CONFIG_USBD_GS_USB_IDENTIFICATION=y
CONFIG_USBD_GS_USB_TIMESTAMP=y
CONFIG_USBD_GS_USB_TERMINATION=y
80 changes: 80 additions & 0 deletions tests/subsys/usb/gs_usb/host/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2024 Henrik Brix Andersen <[email protected]>
# SPDX-License-Identifier: Apache-2.0

"""
Configuration of gs_usb test suite.
"""

import re
import logging
import time
import pytest

from twister_harness import DeviceAdapter, Shell
from gs_usb import GsUSB

logger = logging.getLogger(__name__)

def pytest_addoption(parser) -> None:
"""Add local parser options to pytest."""
parser.addoption('--usb-delay', default=5,
help='Delay to wait for USB enumeration after flashing (default: 5 seconds)')
parser.addoption('--usb-sn', default=None,
help='USB serial number of the DUT (default: None)')

@pytest.fixture(name='usb_vid', scope='module')
def fixture_usb_vid(shell: Shell) -> int:
"""Return the USB VID used by the DUT."""
regex = re.compile(r'USB VID:\s+(\S+)')
lines = shell.get_filtered_output(shell.exec_command('gs_usb vid'))

for line in lines:
m = regex.match(line)
if m:
vid = int(m.groups()[0], 16)
return vid

pytest.fail('USB VID not found')
return 0x0000

@pytest.fixture(name='usb_pid', scope='module')
def fixture_usb_pid(shell: Shell) -> int:
"""Return the USB PID used by the DUT."""
regex = re.compile(r'USB PID:\s+(\S+)')
lines = shell.get_filtered_output(shell.exec_command('gs_usb pid'))

for line in lines:
m = regex.match(line)
if m:
pid = int(m.groups()[0], 16)
return pid

pytest.fail('USB PID not found')
return 0x0000

@pytest.fixture(name='usb_sn', scope='module')
def fixture_usb_sn(request, dut: DeviceAdapter) -> str:
"""Return the USB serial number used by the DUT."""

sn = request.config.getoption('--usb-sn')

if sn is None:
for fixture in dut.device_config.fixtures:
if fixture.startswith('usb:'):
sn = fixture.split(sep=':', maxsplit=1)[1]
break

if sn is not None:
logger.info('using USB S/N: "%s"', sn)

return sn

@pytest.fixture(name='dev', scope='module')
def fixture_gs_usb(request, usb_vid: int, usb_pid: int, usb_sn: str) -> GsUSB:
"""Return gs_usb instance for testing"""

delay = request.config.getoption('--usb-delay')
logger.info('Waiting %d seconds for USB enumeration...', delay)
time.sleep(delay)

return GsUSB(usb_vid, usb_pid, usb_sn)
Loading

0 comments on commit c5f1cdf

Please sign in to comment.