Skip to content

Commit

Permalink
Merge pull request #17 from espressif/refactor/restructure_test_app
Browse files Browse the repository at this point in the history
Chore: Restructure test_app
  • Loading branch information
tore-espressif authored Mar 1, 2024
2 parents a0eae4d + 6c054f0 commit 76a723a
Show file tree
Hide file tree
Showing 57 changed files with 654 additions and 133 deletions.
8 changes: 7 additions & 1 deletion .build-test-rules.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
test_app:
device/esp_tinyusb:
enable:
- if: IDF_TARGET in ["esp32s2", "esp32s3"]
disable:
- if: IDF_VERSION < 5.0

host/class:
enable:
- if: IDF_TARGET in ["esp32s2", "esp32s3"]
11 changes: 6 additions & 5 deletions .github/workflows/build_and_run_test_app_usb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ jobs:
with:
name: usb_test_app_bin_${{ matrix.idf_ver }}
path: |
test_app/build_esp*/bootloader/bootloader.bin
test_app/build_esp*/partition_table/partition-table.bin
test_app/build_esp*/usb_test_app.bin
test_app/build_esp*/usb_test_app.elf
test_app/build_esp*/flasher_args.json
**/test_app/build_esp*/bootloader/bootloader.bin
**/test_app/build_esp*/partition_table/partition-table.bin
**/test_app/build_esp*/test_app_usb_*.bin
**/test_app/build_esp*/test_app_usb_*.elf
**/test_app/build_esp*/flasher_args.json
if-no-files-found: error

run-target:
name: Run USB Test App on target
Expand Down
10 changes: 8 additions & 2 deletions .idf_build_apps.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
target = "all"
paths = "test_app"
paths = [
"device/esp_tinyusb/test_app",
"host/class/cdc/usb_host_cdc_acm/test_app",
"host/class/hid/usb_host_hid/test_app",
"host/class/msc/usb_host_msc/test_app",
"host/class/uvc/usb_host_uvc/test_app",
]
recursive = true
manifest_file = ".build-test-rules.yml"
check_warnings = true

# build related options
build_dir = "build_@t"
build_dir = "build_@t_@w"
11 changes: 11 additions & 0 deletions device/esp_tinyusb/test_app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

# Set the components to include the tests for.
set(EXTRA_COMPONENT_DIRS
../
)

project(test_app_usb_device_esp_tinyusb)
4 changes: 4 additions & 0 deletions device/esp_tinyusb/test_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Supported Targets | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | -------- |

# USB: esp_tinyusb test application
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
idf_component_register(SRCS "test_esp_tinyusb.c" "test_bvalid_sig.c"
include($ENV{IDF_PATH}/tools/cmake/version.cmake)

idf_component_register(SRCS "test_esp_tinyusb.c" "test_bvalid_sig.c" "test_app_main.c"
INCLUDE_DIRS "."
REQUIRES unity esp_tinyusb
)
WHOLE_ARCHIVE)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
dependencies:
espressif/esp_tinyusb:
version: "*"
override_path: "../../device/esp_tinyusb"
override_path: "../../"
rules:
- if: "idf_version >= 5.0"
- if: "idf_version >= 5.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -22,6 +22,19 @@ static void check_leak(size_t before_free, size_t after_free, const char *type)

void app_main(void)
{
// ____ ___ ___________________ __ __
// | | \/ _____/\______ \ _/ |_ ____ _______/ |_
// | | /\_____ \ | | _/ \ __\/ __ \ / ___/\ __\.
// | | / / \ | | \ | | \ ___/ \___ \ | |
// |______/ /_______ / |______ / |__| \___ >____ > |__|
// \/ \/ \/ \/
printf(" ____ ___ ___________________ __ __ \r\n");
printf("| | \\/ _____/\\______ \\ _/ |_ ____ _______/ |_ \r\n");
printf("| | /\\_____ \\ | | _/ \\ __\\/ __ \\ / ___/\\ __\\\r\n");
printf("| | / / \\ | | \\ | | \\ ___/ \\___ \\ | | \r\n");
printf("|______/ /_______ / |______ / |__| \\___ >____ > |__| \r\n");
printf(" \\/ \\/ \\/ \\/ \r\n");

UNITY_BEGIN();
unity_run_menu();
UNITY_END();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "soc/gpio_sig_map.h"
#include "unity.h"
#include "tinyusb.h"
#include "class/msc/msc_device.h"

#define DEVICE_DETACH_TEST_ROUNDS 10
#define DEVICE_DETACH_ROUND_DELAY_MS 1000
Expand Down Expand Up @@ -66,7 +65,7 @@ void tud_umount_cb(void)
dev_umounted++;
}

TEST_CASE("bvalid_signal", "[esp_tinyusb]")
TEST_CASE("bvalid_signal", "[esp_tinyusb][usb_device]")
{
unsigned int rounds = DEVICE_DETACH_TEST_ROUNDS;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -22,6 +22,9 @@

#define VFS_PATH "/dev/usb-cdc1"

// idf_component_register(WHOLE_ARCHIVE) backward compatibility to IDF_v4.4
void linker_hook(void) {};

static const tusb_desc_device_t cdc_device_descriptor = {
.bLength = sizeof(cdc_device_descriptor),
.bDescriptorType = TUSB_DESC_DEVICE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

from typing import Tuple

import pytest
from pytest_embedded_idf.dut import IdfDut
from time import sleep
Expand All @@ -13,7 +11,7 @@
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.usb_device
def test_usb_device(dut) -> None:
def test_usb_device_cdc(dut) -> None:
'''
Running the test locally:
1. Build the testa app for your DUT (ESP32-S2 or S3)
Expand Down
12 changes: 12 additions & 0 deletions device/esp_tinyusb/test_app/pytest_usb_device_esp_tinyusb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

import pytest
from pytest_embedded_idf.dut import IdfDut


@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.usb_device
def test_usb_device_esp_tinyusb(dut: IdfDut) -> None:
dut.run_all_single_board_cases(group='usb_device')
19 changes: 19 additions & 0 deletions device/esp_tinyusb/test_app/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Configure TinyUSB, it will be used to mock USB devices
CONFIG_TINYUSB=y
CONFIG_TINYUSB_MSC_ENABLED=n
CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_TINYUSB_CDC_COUNT=2
CONFIG_TINYUSB_HID_COUNT=0

# Disable watchdogs, they'd get triggered during unity interactive menu
CONFIG_ESP_INT_WDT=n
CONFIG_ESP_TASK_WDT=n

# Run-time checks of Heap and Stack
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
CONFIG_COMPILER_STACK_CHECK=y

CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL=y

CONFIG_COMPILER_CXX_EXCEPTIONS=y
10 changes: 0 additions & 10 deletions host/class/cdc/usb_host_cdc_acm/test/CMakeLists.txt

This file was deleted.

20 changes: 20 additions & 0 deletions host/class/cdc/usb_host_cdc_acm/test_app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

set(EXTRA_COMPONENT_DIRS
../../esp_modem_usb_dte
../../usb_host_cdc_acm
../../usb_host_ch34x_vcp
../../usb_host_cp210x_vcp
../../usb_host_ftdi_vcp
../../usb_host_vcp
)

# Set the components to include the tests for.
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.0")
list(APPEND EXTRA_COMPONENT_DIRS ../../../../../device/esp_tinyusb)
endif()

project(test_app_usb_host_cdc)
15 changes: 15 additions & 0 deletions host/class/cdc/usb_host_cdc_acm/test_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
| Supported Targets | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | -------- |

# USB: CDC Class test application

## CDC-ACM driver

It tests basic functionality of the driver like open/close/read/write operations,
advanced features like CDC control request, multi-threaded or multi-device access,
as well as reaction to sudden disconnection and other error states.

### Hardware Required

This test expects that TinyUSB dual CDC device with VID = 0x303A and PID = 0x4002
is connected to the USB host.
23 changes: 23 additions & 0 deletions host/class/cdc/usb_host_cdc_acm/test_app/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include($ENV{IDF_PATH}/tools/cmake/version.cmake)
set (TINYUSB_LIB)
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.0")
set(TINYUSB_LIB "esp_tinyusb")
else()
set(TINYUSB_LIB "tinyusb")
endif()

# TODO: once IDF_v4.4 is at the EOL support, use WHOLE_ARCHIVE
idf_component_register(SRCS "test_cdc_acm_host.c" "usb_device.c" "test_app_main.c"
INCLUDE_DIRS "."
REQUIRES usb_host_cdc_acm unity ${TINYUSB_LIB})

# In order for the cases defined by `TEST_CASE` to be linked into the final elf,
# the component can be registered as WHOLE_ARCHIVE

# Due to the backward compatibility to IDFv4.4 (in which WHOLE_ARCHIVE is not implemented) we use following approach:
# Any non-static function test_app/main/*.c (apart from test_app_main.c) file is added as an undefined symbol
# because otherwise the linker will ignore test_app/main/*.c as it has no other files depending on any
# symbols in it.

# force-link test_cdc_acm_host.c
set_property(TARGET ${COMPONENT_LIB} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "-u usb_lib_task")
57 changes: 57 additions & 0 deletions host/class/cdc/usb_host_cdc_acm/test_app/main/test_app_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "esp_heap_caps.h"

static size_t before_free_8bit;
static size_t before_free_32bit;

#define TEST_MEMORY_LEAK_THRESHOLD (-530)
static void check_leak(size_t before_free, size_t after_free, const char *type)
{
ssize_t delta = after_free - before_free;
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
}

void app_main(void)
{
// ____ ___ ___________________ __ __
// | | \/ _____/\______ \ _/ |_ ____ _______/ |_
// | | /\_____ \ | | _/ \ __\/ __ \ / ___/\ __\.
// | | / / \ | | \ | | \ ___/ \___ \ | |
// |______/ /_______ / |______ / |__| \___ >____ > |__|
// \/ \/ \/ \/
printf(" ____ ___ ___________________ __ __ \r\n");
printf("| | \\/ _____/\\______ \\ _/ |_ ____ _______/ |_ \r\n");
printf("| | /\\_____ \\ | | _/ \\ __\\/ __ \\ / ___/\\ __\\\r\n");
printf("| | / / \\ | | \\ | | \\ ___/ \\___ \\ | | \r\n");
printf("|______/ /_______ / |______ / |__| \\___ >____ > |__| \r\n");
printf(" \\/ \\/ \\/ \\/ \r\n");

UNITY_BEGIN();
unity_run_menu();
UNITY_END();
}

/* setUp runs before every test */
void setUp(void)
{
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
}

/* tearDown runs after every test */
void tearDown(void)
{
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
check_leak(before_free_8bit, after_free_8bit, "8BIT");
check_leak(before_free_32bit, after_free_32bit, "32BIT");
}
26 changes: 26 additions & 0 deletions host/class/cdc/usb_host_cdc_acm/test_app/pytest_usb_host_cdc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

from typing import Tuple

import pytest
from pytest_embedded_idf.dut import IdfDut


@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.usb_host
@pytest.mark.parametrize('count', [
2,
], indirect=True)
def test_usb_host(dut: Tuple[IdfDut, IdfDut]) -> None:
device = dut[0]
host = dut[1]

# 1.1 Prepare USB device for CDC test
device.expect_exact('Press ENTER to see the list of tests.')
device.write('[cdc_acm_device]')
device.expect_exact('USB initialization DONE')

# 1.2 Run CDC test
host.run_all_single_board_cases(group='cdc_acm')
19 changes: 19 additions & 0 deletions host/class/cdc/usb_host_cdc_acm/test_app/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Configure TinyUSB, it will be used to mock USB devices
CONFIG_TINYUSB=y
CONFIG_TINYUSB_MSC_ENABLED=n
CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_TINYUSB_CDC_COUNT=2
CONFIG_TINYUSB_HID_COUNT=0

# Disable watchdogs, they'd get triggered during unity interactive menu
CONFIG_ESP_INT_WDT=n
CONFIG_ESP_TASK_WDT=n

# Run-time checks of Heap and Stack
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
CONFIG_COMPILER_STACK_CHECK=y

CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL=y

CONFIG_COMPILER_CXX_EXCEPTIONS=y
10 changes: 0 additions & 10 deletions host/class/hid/usb_host_hid/test/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 76a723a

Please sign in to comment.