Skip to content

Commit 4485bff

Browse files
authored
Merge pull request #2771 from hathach/hil-add-host-test
[HIL] add dual host_info_to_device_cdc support
2 parents 858ad66 + 45d06cd commit 4485bff

File tree

7 files changed

+100
-53
lines changed

7 files changed

+100
-53
lines changed

examples/dual/host_info_to_device_cdc/src/main.c

+17-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525

2626
/* Host example will get device descriptors of attached devices and print it out via device cdc as follows:
27-
* Device 1: ID 046d:c52f
27+
* Device 1: ID 046d:c52f SN 11223344
2828
Device Descriptor:
2929
bLength 18
3030
bDescriptorType 1
@@ -147,7 +147,21 @@ void print_device_info(uint8_t daddr) {
147147
return;
148148
}
149149

150-
cdc_printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct);
150+
// Get String descriptor using Sync API
151+
uint16_t serial[64];
152+
uint16_t buf[128];
153+
154+
cdc_printf("Device %u: ID %04x:%04x SN ", daddr, desc_device.idVendor, desc_device.idProduct);
155+
xfer_result = tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, serial, sizeof(serial));
156+
if (XFER_RESULT_SUCCESS != xfer_result) {
157+
serial[0] = 'n';
158+
serial[1] = '/';
159+
serial[2] = 'a';
160+
serial[3] = 0;
161+
}
162+
print_utf16(serial, TU_ARRAY_SIZE(serial));
163+
tud_cdc_write_str("\r\n");
164+
151165
cdc_printf("Device Descriptor:\r\n");
152166
cdc_printf(" bLength %u\r\n" , desc_device.bLength);
153167
cdc_printf(" bDescriptorType %u\r\n" , desc_device.bDescriptorType);
@@ -160,9 +174,6 @@ void print_device_info(uint8_t daddr) {
160174
cdc_printf(" idProduct 0x%04x\r\n" , desc_device.idProduct);
161175
cdc_printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
162176

163-
// Get String descriptor using Sync API
164-
uint16_t buf[128];
165-
166177
cdc_printf(" iManufacturer %u " , desc_device.iManufacturer);
167178
xfer_result = tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, buf, sizeof(buf));
168179
if (XFER_RESULT_SUCCESS == xfer_result ) {
@@ -178,10 +189,7 @@ void print_device_info(uint8_t daddr) {
178189
tud_cdc_write_str("\r\n");
179190

180191
cdc_printf(" iSerialNumber %u " , desc_device.iSerialNumber);
181-
xfer_result = tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, buf, sizeof(buf));
182-
if (XFER_RESULT_SUCCESS == xfer_result) {
183-
print_utf16(buf, TU_ARRAY_SIZE(buf));
184-
}
192+
tud_cdc_write_str((char*)serial); // serial is already to UTF-8
185193
tud_cdc_write_str("\r\n");
186194

187195
cdc_printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations);

hw/bsp/family_support.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ function(family_flash_openocd TARGET)
463463
# note skip verify since it has issue with rp2040
464464
add_custom_target(${TARGET}-openocd
465465
DEPENDS ${TARGET}
466-
COMMAND ${OPENOCD} ${OPTION_LIST} -c init -c halt -c "program $<TARGET_FILE:${TARGET}> reset" ${OPTION_LIST2} -c exit
466+
COMMAND ${OPENOCD} -c "tcl_port disabled" -c "gdb_port disabled" ${OPTION_LIST} -c init -c halt -c "program $<TARGET_FILE:${TARGET}>" -c reset ${OPTION_LIST2} -c exit
467467
VERBATIM
468468
)
469469
endfunction()

hw/bsp/nrf/boards/feather_nrf52840_express/board.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ set(MCU_VARIANT nrf52840)
22
set(LD_FILE_GNU ${CMAKE_CURRENT_LIST_DIR}/../../linker/nrf52840_s140_v6.ld)
33

44
# enable max3421 host driver for this board
5-
set(MAX3421_HOST 1)
5+
# set(MAX3421_HOST 1)
66

77
function(update_board TARGET)
88
endfunction()

hw/bsp/rp2040/family.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
static uart_inst_t *uart_inst;
4343
#endif
4444

45-
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
45+
#if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB)
4646
#include "pio_usb.h"
4747
#endif
4848

@@ -126,7 +126,7 @@ void stdio_rtt_init(void) {
126126

127127
void board_init(void)
128128
{
129-
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
129+
#if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB)
130130
// Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb
131131
set_sys_clock_khz(120000, true);
132132

hw/bsp/samd5x_e5x/boards/metro_m4_express/board.cmake

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ set(SAM_FAMILY samd51)
33
set(JLINK_DEVICE ATSAMD51J19)
44
set(LD_FILE_GNU ${CMAKE_CURRENT_LIST_DIR}/${BOARD}.ld)
55

6+
# force max3421e for testing with hardware-in-the-loop
7+
set(MAX3421_HOST 1)
8+
69
function(update_board TARGET)
710
target_compile_definitions(${TARGET} PUBLIC
811
__SAMD51J19A__

test/hil/hil_test.py

+58-31
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import argparse
2929
import os
30+
import re
3031
import sys
3132
import time
3233
import serial
@@ -212,14 +213,39 @@ def flash_uniflash(board, firmware):
212213

213214

214215
# -------------------------------------------------------------
215-
# Tests
216+
# Tests: dual
216217
# -------------------------------------------------------------
217-
def test_board_test(board):
218+
219+
def test_dual_host_info_to_device_cdc(board):
220+
uid = board['uid']
221+
declared_devs = [f'{d["vid_pid"]}_{d["serial"]}' for d in board['tests']['dual_attached']]
222+
223+
port = get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 0)
224+
ser = open_serial_dev(port)
225+
# read from cdc, first line should contain vid/pid and serial
226+
data = ser.read(1000)
227+
lines = data.decode('utf-8').splitlines()
228+
enum_dev_sn = []
229+
for l in lines:
230+
vid_pid_sn = re.search(r'ID ([0-9a-fA-F]+):([0-9a-fA-F]+) SN (\w+)', l)
231+
if vid_pid_sn:
232+
print(f'\r\n {l} ', end='')
233+
enum_dev_sn.append(f'{vid_pid_sn.group(1)}_{vid_pid_sn.group(2)}_{vid_pid_sn.group(3)}')
234+
235+
assert(set(declared_devs) == set(enum_dev_sn)), \
236+
f'Enumerated devices {enum_dev_sn} not match with declared {declared_devs}'
237+
return 0
238+
239+
240+
# -------------------------------------------------------------
241+
# Tests: device
242+
# -------------------------------------------------------------
243+
def test_device_board_test(board):
218244
# Dummy test
219245
pass
220246

221247

222-
def test_cdc_dual_ports(board):
248+
def test_device_cdc_dual_ports(board):
223249
uid = board['uid']
224250
port1 = get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 0)
225251
port2 = get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 2)
@@ -241,7 +267,7 @@ def test_cdc_dual_ports(board):
241267
assert ser2.read(100) == str2.upper(), 'Port2 wrong data'
242268

243269

244-
def test_cdc_msc(board):
270+
def test_device_cdc_msc(board):
245271
uid = board['uid']
246272
# Echo test
247273
port = get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 0)
@@ -262,11 +288,11 @@ def test_cdc_msc(board):
262288
assert data == readme, 'MSC wrong data'
263289

264290

265-
def test_cdc_msc_freertos(board):
266-
test_cdc_msc(board)
291+
def test_device_cdc_msc_freertos(board):
292+
test_device_cdc_msc(board)
267293

268294

269-
def test_dfu(board):
295+
def test_device_dfu(board):
270296
uid = board['uid']
271297

272298
# Wait device enum
@@ -308,7 +334,7 @@ def test_dfu(board):
308334
os.remove(f_dfu1)
309335

310336

311-
def test_dfu_runtime(board):
337+
def test_device_dfu_runtime(board):
312338
uid = board['uid']
313339

314340
# Wait device enum
@@ -325,7 +351,7 @@ def test_dfu_runtime(board):
325351
assert timeout, 'Device not available'
326352

327353

328-
def test_hid_boot_interface(board):
354+
def test_device_hid_boot_interface(board):
329355
uid = board['uid']
330356
kbd = get_hid_dev(uid, 'TinyUSB', 'TinyUSB_Device', 'event-kbd')
331357
mouse1 = get_hid_dev(uid, 'TinyUSB', 'TinyUSB_Device', 'if01-event-mouse')
@@ -341,7 +367,7 @@ def test_hid_boot_interface(board):
341367
assert timeout, 'HID device not available'
342368

343369

344-
def test_hid_composite_freertos(id):
370+
def test_device_hid_composite_freertos(id):
345371
# TODO implement later
346372
pass
347373

@@ -351,13 +377,15 @@ def test_hid_composite_freertos(id):
351377
# -------------------------------------------------------------
352378
# all possible tests: board_test is added last to disable board's usb
353379
all_tests = [
354-
'cdc_dual_ports',
355-
'cdc_msc',
356-
'dfu',
357-
'cdc_msc_freertos', # dont test 2 cdc_msc next to each other, since they have same vid/pid. Can be confused by host
358-
'dfu_runtime',
359-
'hid_boot_interface',
360-
'board_test'
380+
'device/cdc_dual_ports',
381+
'device/cdc_msc',
382+
'device/dfu',
383+
'device/cdc_msc_freertos', # don't test 2 cdc_msc next to each other
384+
'device/dfu_runtime',
385+
'device/hid_boot_interface',
386+
387+
'dual/host_info_to_device_cdc',
388+
'device/board_test'
361389
]
362390

363391

@@ -366,23 +394,23 @@ def test_board(board):
366394
flasher = board['flasher'].lower()
367395

368396
# default to all tests
369-
if 'tests' in board:
370-
test_list = board['tests'] + ['board_test']
371-
else:
372-
test_list = list(all_tests)
397+
test_list = list(all_tests)
373398

374-
# remove skip_tests
375-
if 'tests_skip' in board:
376-
for skip in board['tests_skip']:
377-
if skip in test_list:
378-
test_list.remove(skip)
399+
if 'tests' in board:
400+
board_tests = board['tests']
401+
if 'only' in board_tests:
402+
test_list = board_tests['only'] + ['device/board_test']
403+
if 'skip' in board_tests:
404+
for skip in board_tests['skip']:
405+
if skip in test_list:
406+
test_list.remove(skip)
379407

380408
err_count = 0
381409
for test in test_list:
382-
fw_dir = f'cmake-build/cmake-build-{name}/device/{test}'
410+
fw_dir = f'cmake-build/cmake-build-{name}/{test}'
383411
if not os.path.exists(fw_dir):
384-
fw_dir = f'examples/cmake-build-{name}/device/{test}'
385-
fw_name = f'{fw_dir}/{test}'
412+
fw_dir = f'examples/cmake-build-{name}/{test}'
413+
fw_name = f'{fw_dir}/{os.path.basename(test)}'
386414
print(f'{name:30} {test:20} ... ', end='')
387415

388416
if not os.path.exists(fw_dir):
@@ -400,7 +428,7 @@ def test_board(board):
400428

401429
if ret.returncode == 0:
402430
try:
403-
ret = globals()[f'test_{test}'](board)
431+
ret = globals()[f'test_{test.replace("/", "_")}'](board)
404432
print('OK')
405433
except Exception as e:
406434
err_count += 1
@@ -409,7 +437,6 @@ def test_board(board):
409437
else:
410438
err_count += 1
411439
print('Flash failed')
412-
413440
return err_count
414441

415442

test/hil/rpi.json

+18-9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
"flasher_args": "-device nrf52840_xxaa"
99
},
1010
{
11-
"name": "itsybitsy_m4",
12-
"uid": "D784B28C5338533335202020FF044726",
11+
"name": "metro_m4_express",
12+
"uid": "9995AD485337433231202020FF100A34",
1313
"flasher": "openocd",
14-
"flasher_sn": "E6614C311B597D32",
15-
"flasher_args": "-f interface/cmsis-dap.cfg -f target/atsame5x.cfg -c \"adapter speed 5000\""
14+
"flasher_sn": "E6633861A3978538",
15+
"flasher_args": "-f interface/cmsis-dap.cfg -f target/atsame5x.cfg",
16+
"tests": {
17+
"dual_attached": [{"vid_pid": "1a86_55d4", "serial": "52D2002130"}]
18+
}
1619
},
1720
{
1821
"name": "max32666fthr",
@@ -31,7 +34,9 @@
3134
{
3235
"name": "ra4m1_ek",
3336
"uid": "152E163038303131393346E46F26574B",
34-
"tests_skip": ["cdc_msc", "cdc_msc_freertos"],
37+
"tests": {
38+
"skip": ["device/cdc_msc", "device/cdc_msc_freertos"]
39+
},
3540
"comment": "MSC is slow to enumerated #2602",
3641
"flasher": "jlink",
3742
"flasher_sn": "000831174392",
@@ -42,7 +47,11 @@
4247
"uid": "E6614C311B764A37",
4348
"flasher": "openocd",
4449
"flasher_sn": "E6614103E72C1D2F",
45-
"flasher_args": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\""
50+
"flasher_args": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"",
51+
"tests": {
52+
"skip": ["dual/host_info_to_device_cdc"],
53+
"dual_attached": [{"vid_pid": "1a86_55d4", "serial": "52D2002470"}]
54+
}
4655
},
4756
{
4857
"name": "stm32f072disco",
@@ -77,9 +86,9 @@
7786
{
7887
"name": "espressif_s3_devkitm",
7988
"uid": "84F703C084E4",
80-
"tests": [
81-
"cdc_msc_freertos", "hid_composite_freertos"
82-
],
89+
"tests": {
90+
"only": ["device/cdc_msc_freertos", "device/hid_composite_freertos"]
91+
},
8392
"flasher": "esptool",
8493
"flasher_sn": "3ea619acd1cdeb11a0a0b806e93fd3f1",
8594
"flasher_args": "-b 921600"

0 commit comments

Comments
 (0)