diff options
| author | Michael Fischer <[email protected]> | 2024-02-04 10:55:34 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-04 10:55:34 +0100 |
| commit | ea30041168d19a9239b700b745e7d7f17c760a8d (patch) | |
| tree | ff30e202d98c29ef0df73e30ea75ed136f827f80 /test | |
| parent | 9063ede25f4a013dae8edccc2d953ba2de7ed684 (diff) | |
| parent | f918406a12cabbc66a6c297b2fd27f8eda357fcd (diff) | |
Merge branch 'hathach:master' into nxp_k64
Diffstat (limited to 'test')
| -rw-r--r-- | test/hil/hil_hfp.json | 18 | ||||
| -rw-r--r-- | test/hil/hil_pi4.json | 37 | ||||
| -rw-r--r-- | test/hil/hil_test.py | 317 |
3 files changed, 272 insertions, 100 deletions
diff --git a/test/hil/hil_hfp.json b/test/hil/hil_hfp.json index 58f550901..e79a3cfc9 100644 --- a/test/hil/hil_hfp.json +++ b/test/hil/hil_hfp.json @@ -3,23 +3,23 @@ { "name": "stm32l412nucleo", "uid": "41003B000E504E5457323020", - "debugger": "jlink", - "debugger_sn": "774470029", - "cpu": "STM32L412KB" + "flasher": "jlink", + "flasher_sn": "774470029", + "flasher_args": "-device STM32L412KB" }, { "name": "stm32f746disco", "uid": "210041000C51343237303334", - "debugger": "jlink", - "debugger_sn": "770935966", - "cpu": "STM32F746NG" + "flasher": "jlink", + "flasher_sn": "770935966", + "flasher_args": "-device STM32F746NG" }, { "name": "lpcxpresso43s67", "uid": "08F000044528BAAA8D858F58C50700F5", - "debugger": "jlink", - "debugger_sn": "728973776", - "cpu": "LPC43S67_M4" + "flasher": "jlink", + "flasher_sn": "728973776", + "flasher_args": "-device LPC43S67_M4" } ] } diff --git a/test/hil/hil_pi4.json b/test/hil/hil_pi4.json new file mode 100644 index 000000000..17d9ff1fa --- /dev/null +++ b/test/hil/hil_pi4.json @@ -0,0 +1,37 @@ +{ + "boards": [ + { + "name": "raspberry_pi_pico", + "uid": "E6614C311B764A37", + "flasher": "openocd", + "flasher_sn": "E6614103E72C1D2F", + "flasher_args": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"" + }, + { + "name": "espressif_s3_devkitc", + "uid": "7CDFA1E073CC", + "tests": [ + "cdc_msc_freertos", "hid_composite_freertos" + ], + "flasher": "esptool", + "flasher_sn": "461cb8d7decdeb119be9b506e93fd3f1", + "flasher_args": "-b 1500000" + }, + { + "name": "feather_nrf52840_express", + "uid": "1F0479CD0F764471", + "flasher": "jlink", + "flasher_sn": "000682804350", + "flasher_args": "-device nrf52840_xxaa" + }, + { + "name": "itsybitsy_m4", + "uid": "D784B28C5338533335202020FF044726", + "flasher": "bossac", + "flashser_vendor": "Adafruit Industries", + "flasher_product": "ItsyBitsy M4 Express", + "flasher_reset_pin": "2", + "flasher_args": "--offset 0x4000" + } + ] +} diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index 6bcde36c6..7f03ce43f 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -28,62 +28,167 @@ import os import sys import time +import click import serial import subprocess import json +import glob +# for RPI double reset +try: + import gpiozero +except ImportError: + pass + + +ENUM_TIMEOUT = 10 -def get_serial_dev(id, product, ifnum): - # get usb serial by id - return f'/dev/serial/by-id/usb-TinyUSB_{product}_{id}-if{ifnum:02d}' +# get usb serial by id +def get_serial_dev(id, vendor_str, product_str, ifnum): + if vendor_str and product_str: + # known vendor and product + vendor_str = vendor_str.replace(' ', '_') + product_str = product_str.replace(' ', '_') + return f'/dev/serial/by-id/usb-{vendor_str}_{product_str}_{id}-if{ifnum:02d}' + else: + # just use id: mostly for cp210x/ftdi flasher + pattern = f'/dev/serial/by-id/usb-*_{id}-if{ifnum:02d}*' + port_list = glob.glob(pattern) + return port_list[0] -def get_disk_dev(id, lun): + +# Currently not used, left as reference +def get_disk_dev(id, vendor_str, lun): # get usb disk by id - return f'/dev/disk/by-id/usb-TinyUSB_Mass_Storage_{id}-0:{lun}' + return f'/dev/disk/by-id/usb-{vendor_str}_Mass_Storage_{id}-0:{lun}' + + +def get_hid_dev(id, vendor_str, product_str, event): + return f'/dev/input/by-id/usb-{vendor_str}_{product_str}_{id}-{event}' + + +def open_serial_dev(port): + timeout = ENUM_TIMEOUT + ser = None + while timeout: + if os.path.exists(port): + try: + # slight delay since kernel may occupy the port briefly + time.sleep(0.5) + timeout = timeout - 0.5 + ser = serial.Serial(port, timeout=1) + break + except serial.SerialException: + pass + time.sleep(0.5) + timeout = timeout - 0.5 + assert timeout, 'Device not available or Cannot open port' + return ser -def get_hid_dev(id, product, event): - return f'/dev/input/by-id/usb-TinyUSB_{product}_{id}-{event}' +def read_disk_file(id, fname): + # on different self-hosted, the mount point is different + file_list = [ + f'/media/blkUSB_{id[-8:]}.02/{fname}', + f'/media/{os.getenv("USER")}/TinyUSB MSC/{fname}' + ] + timeout = ENUM_TIMEOUT + while timeout: + for file in file_list: + if os.path.isfile(file): + with open(file, 'rb') as f: + data = f.read() + return data + time.sleep(1) + timeout = timeout - 1 -def flash_jlink(sn, dev, firmware): + assert timeout, 'Device not available' + return None + + +# ------------------------------------------------------------- +# Flashing firmware +# ------------------------------------------------------------- +def flash_jlink(board, firmware): script = ['halt', 'r', f'loadfile {firmware}', 'r', 'go', 'exit'] - f = open('flash.jlink', 'w') - f.writelines(f'{s}\n' for s in script) - f.close() - ret = subprocess.run(f'JLinkExe -USB {sn} -device {dev} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile flash.jlink', - shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - stdout = ret.stdout.decode() + with open('flash.jlink', 'w') as f: + f.writelines(f'{s}\n' for s in script) + ret = subprocess.run( + f'JLinkExe -USB {board["flasher_sn"]} {board["flasher_args"]} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile flash.jlink', + shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) os.remove('flash.jlink') - assert ret.returncode == 0, 'Flash failed\n' + stdout + return ret -def test_board_test(id): - # Dummy test - pass +def flash_openocd(board, firmware): + ret = subprocess.run( + f'openocd -c "adapter serial {board["flasher_sn"]}" {board["flasher_args"]} -c "program {firmware} reset exit"', + shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + return ret -def test_cdc_dual_ports(id): - port1 = get_serial_dev(id, "TinyUSB_Device", 0) - port2 = get_serial_dev(id, "TinyUSB_Device", 2) - # Wait device enum - timeout = 10 +def flash_esptool(board, firmware): + port = get_serial_dev(board["flasher_sn"], None, None, 0) + dir = os.path.dirname(firmware) + with open(f'{dir}/config.env') as f: + IDF_TARGET = json.load(f)['IDF_TARGET'] + with open(f'{dir}/flash_args') as f: + flash_args = f.read().strip().replace('\n', ' ') + command = (f'esptool.py --chip {IDF_TARGET} -p {port} {board["flasher_args"]} ' + f'--before=default_reset --after=hard_reset write_flash {flash_args}') + ret = subprocess.run(command, shell=True, cwd=dir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + return ret + + +def doublereset_with_rpi_gpio(board): + # Off = 0 = Reset + led = gpiozero.LED(board["flasher_reset_pin"]) + + led.off() + time.sleep(0.1) + led.on() + time.sleep(0.1) + led.off() + time.sleep(0.1) + led.on() + +def flash_bossac(board, firmware): + # double reset to enter bootloader + doublereset_with_rpi_gpio(board) + + port = get_serial_dev(board["uid"], board["flashser_vendor"], board["flasher_product"], 0) + timeout = ENUM_TIMEOUT while timeout: - if os.path.exists(port1) and os.path.exists(port2): + if os.path.exists(port): break - time.sleep(1) - timeout = timeout - 1 + else: + time.sleep(0.5) + timeout = timeout - 0.5 + assert timeout, 'bossac bootloader is not available' + # sleep a bit more for bootloader to be ready + time.sleep(0.5) + ret = subprocess.run(f'bossac --port {port} {board["flasher_args"]} -U -i -R -e -w {firmware}', shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + return ret - assert timeout, 'Device not available' +# ------------------------------------------------------------- +# Tests +# ------------------------------------------------------------- +def test_board_test(id): + # Dummy test + pass - # Echo test - ser1 = serial.Serial(port1) - ser2 = serial.Serial(port2) - ser1.timeout = 1 - ser2.timeout = 1 +def test_cdc_dual_ports(id): + port1 = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 0) + port2 = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 2) + + ser1 = open_serial_dev(port1) + ser2 = open_serial_dev(port2) + # Echo test str1 = b"test_no1" ser1.write(str1) ser1.flush() @@ -98,32 +203,17 @@ def test_cdc_dual_ports(id): def test_cdc_msc(id): - port = get_serial_dev(id, "TinyUSB_Device", 0) - file = f'/media/blkUSB_{id[-8:]}.02/README.TXT' - # Wait device enum - timeout = 10 - while timeout: - if os.path.exists(port) and os.path.isfile(file): - break - time.sleep(1) - timeout = timeout - 1 - - assert timeout, 'Device not available' - # Echo test - ser1 = serial.Serial(port) - - ser1.timeout = 1 + port = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 0) + ser = open_serial_dev(port) str = b"test_str" - ser1.write(str) - ser1.flush() - assert ser1.read(100) == str, 'CDC wrong data' + ser.write(str) + ser.flush() + assert ser.read(100) == str, 'CDC wrong data' # Block test - f = open(file, 'rb') - data = f.read() - + data = read_disk_file(id, 'README.TXT') readme = \ b"This is tinyusb's MassStorage Class demo.\r\n\r\n\ If you find any bugs or get any questions, feel free to file an\r\n\ @@ -131,12 +221,17 @@ issue at github.com/hathach/tinyusb" assert data == readme, 'MSC wrong data' + +def test_cdc_msc_freertos(id): + test_cdc_msc(id) + + def test_dfu(id): # Wait device enum - timeout = 10 + timeout = ENUM_TIMEOUT while timeout: ret = subprocess.run(f'dfu-util -l', - shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = ret.stdout.decode() if f'serial="{id}"' in stdout and 'Found DFU: [cafe:4000]' in stdout: break @@ -161,10 +256,10 @@ def test_dfu(id): assert ret.returncode == 0, 'Upload failed' with open('dfu0') as f: - assert 'Hello world from TinyUSB DFU! - Partition 0' in f.read(), 'Wrong uploaded data' + assert 'Hello world from TinyUSB DFU! - Partition 0' in f.read(), 'Wrong uploaded data' with open('dfu1') as f: - assert 'Hello world from TinyUSB DFU! - Partition 1' in f.read(), 'Wrong uploaded data' + assert 'Hello world from TinyUSB DFU! - Partition 1' in f.read(), 'Wrong uploaded data' os.remove('dfu0') os.remove('dfu1') @@ -172,10 +267,10 @@ def test_dfu(id): def test_dfu_runtime(id): # Wait device enum - timeout = 10 + timeout = ENUM_TIMEOUT while timeout: ret = subprocess.run(f'dfu-util -l', - shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = ret.stdout.decode() if f'serial="{id}"' in stdout and 'Found Runtime: [cafe:4000]' in stdout: break @@ -186,11 +281,11 @@ def test_dfu_runtime(id): def test_hid_boot_interface(id): - kbd = get_hid_dev(id, 'TinyUSB_Device', 'event-kbd') - mouse1 = get_hid_dev(id, 'TinyUSB_Device', 'if01-event-mouse') - mouse2 = get_hid_dev(id, 'TinyUSB_Device', 'if01-mouse') + kbd = get_hid_dev(id, 'TinyUSB', 'TinyUSB_Device', 'event-kbd') + mouse1 = get_hid_dev(id, 'TinyUSB', 'TinyUSB_Device', 'if01-event-mouse') + mouse2 = get_hid_dev(id, 'TinyUSB', 'TinyUSB_Device', 'if01-mouse') # Wait device enum - timeout = 10 + timeout = ENUM_TIMEOUT while timeout: if os.path.exists(kbd) and os.path.exists(mouse1) and os.path.exists(mouse2): break @@ -200,51 +295,91 @@ def test_hid_boot_interface(id): assert timeout, 'Device not available' -if __name__ == '__main__': - if len(sys.argv) != 2: - print('Usage:') - print('python hitl_test.py config.json') - sys.exit(-1) +def test_hid_composite_freertos(id): + # TODO implement later + pass + - with open(f'{os.path.dirname(__file__)}/{sys.argv[1]}') as f: +# ------------------------------------------------------------- +# Main +# ------------------------------------------------------------- [email protected]('config_file') [email protected]('-b', '--board', multiple=True, default=None, help='Boards to test, all if not specified') +def main(config_file, board): + """ + Hardware test on specified boards + """ + config_file = os.path.join(os.path.dirname(__file__), config_file) + with open(config_file) as f: config = json.load(f) - # all possible tests, board_test is last to disable board's usb + # all possible tests all_tests = [ - 'cdc_dual_ports', 'cdc_msc', 'dfu', 'dfu_runtime', 'hid_boot_interface', 'board_test' + 'cdc_dual_ports', 'cdc_msc', 'dfu', 'dfu_runtime', 'hid_boot_interface', ] - for board in config['boards']: - print(f'Testing board:{board["name"]}') + if len(board) == 0: + config_boards = config['boards'] + else: + config_boards = [e for e in config['boards'] if e['name'] in board] + + for item in config_boards: + print(f'Testing board:{item["name"]}') + flasher = item['flasher'].lower() # default to all tests - if 'tests' in board: - test_list = board['tests'] + if 'tests' in item: + test_list = item['tests'] else: test_list = all_tests + # board_test is added last to disable board's usb + test_list.append('board_test') + # remove skip_tests - if 'tests_skip' in board: - for skip in board['tests_skip']: + if 'tests_skip' in item: + for skip in item['tests_skip']: if skip in test_list: test_list.remove(skip) for test in test_list: - mk_elf = f'examples/device/{test}/_build/{board["name"]}/{test}.elf' - cmake_elf = f'cmake-build/cmake-build-{board["name"]}/device/{test}/{test}.elf' - if os.path.isfile(cmake_elf): - elf = cmake_elf - elif os.path.isfile(mk_elf): - elf = mk_elf - else: - print(f'Cannot find firmware file for {test}') + fw_list = [ + # cmake: esp32 & samd51 use .bin file + f'cmake-build/cmake-build-{item["name"]}/device/{test}/{test}.elf', + f'cmake-build/cmake-build-{item["name"]}/device/{test}/{test}.bin', + # make + f'examples/device/{test}/_build/{item["name"]}/{test}.elf' + ] + + fw = None + for f in fw_list: + if os.path.isfile(f): + fw = f + break + + if fw is None: + print(f'Cannot find binary file for {test}') sys.exit(-1) - if board['debugger'].lower() == 'jlink': - flash_jlink(board['debugger_sn'], board['cpu'], elf) - else: - # ToDo - pass print(f' {test} ...', end='') - locals()[f'test_{test}'](board['uid']) + + # flash firmware. It may fail randomly, retry a few times + for i in range(3): + ret = globals()[f'flash_{flasher}'](item, fw) + if ret.returncode == 0: + break + else: + print(f'Flashing failed, retry {i+1}') + time.sleep(1) + + assert ret.returncode == 0, 'Flash failed\n' + ret.stdout.decode() + + # run test + globals()[f'test_{test}'](item['uid']) + print('OK') + + +if __name__ == '__main__': + main() |
