From 0a4d92a71e8b25daedc8cf4a388dd9abd2539419 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 28 Nov 2023 18:27:40 +0700 Subject: [PATCH 1/4] update hil for pi4 to use new hil_test.py --- .github/workflows/cmake_arm.yml | 43 ++++----------------------------- hw/bsp/family_support.cmake | 16 ++++++++++++ hw/bsp/rp2040/family.cmake | 2 ++ test/hil/hil_pi4.json | 11 +++++++++ test/hil/hil_test.py | 40 ++++++++++++++++++++++++------ 5 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 test/hil/hil_pi4.json diff --git a/.github/workflows/cmake_arm.yml b/.github/workflows/cmake_arm.yml index e57c297d7..d2a551699 100644 --- a/.github/workflows/cmake_arm.yml +++ b/.github/workflows/cmake_arm.yml @@ -105,15 +105,14 @@ jobs: # --------------------------------------- # Hardware in the loop (HIL) - # Current self-hosted instance is running on an RPI4 with - # - pico + pico-probe connected via USB - # - pico-probe is /dev/ttyACM0 + # Current self-hosted instance is running on an RPI4. + # For attached hardware checkout hil_pi4.json # --------------------------------------- hw-rp2040-test: # run only with hathach's commit due to limited resource on RPI4 if: github.repository_owner == 'hathach' needs: build-arm - runs-on: [self-hosted, Linux, ARM64, rp2040] + runs-on: [self-hosted, rp2040, hardware-in-the-loop] steps: - name: Clean workspace @@ -127,38 +126,6 @@ jobs: with: name: rp2040 - - name: Create flash.sh + - name: Test on actual hardware (hardware in the loop) run: | - echo > flash.sh 'cmdout=$(openocd -f "interface/cmsis-dap.cfg" -f "target/rp2040.cfg" -c "adapter speed 5000" -c "program $1 reset exit")' - echo >> flash.sh 'if (( $? )) ; then echo $cmdout ; fi' - chmod +x flash.sh - - - name: Test cdc_dual_ports - run: | - ./flash.sh cdc_dual_ports.elf - while (! ([ -e /dev/ttyACM1 ] && [ -e /dev/ttyACM2 ])) && [ $SECONDS -le 10 ]; do :; done - test -e /dev/ttyACM1 && echo "ttyACM1 exists" - test -e /dev/ttyACM2 && echo "ttyACM2 exists" - - - name: Test cdc_msc - run: | - ./flash.sh cdc_msc.elf - readme='/media/pi/TinyUSB MSC/README.TXT' - while (! ([ -e /dev/ttyACM1 ] && [ -f "$readme" ])) && [ $SECONDS -le 10 ]; do :; done - test -e /dev/ttyACM1 && echo "ttyACM1 exists" - test -f "$readme" && echo "$readme exists" - cat "$readme" - - - name: Test dfu - run: | - ./flash.sh dfu.elf - while (! (dfu-util -l | grep "Found DFU")) && [ $SECONDS -le 10 ]; do :; done - dfu-util -d cafe -a 0 -U dfu0 - dfu-util -d cafe -a 1 -U dfu1 - grep "TinyUSB DFU! - Partition 0" dfu0 - grep "TinyUSB DFU! - Partition 1" dfu1 - - - name: Test dfu_runtime - run: | - ./flash.sh dfu_runtime.elf - while (! (dfu-util -l | grep "Found Runtime")) && [ $SECONDS -le 10 ]; do :; done + python3 test/hil/hil_test.py hil_pi4.json diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake index 930237fea..bd072e89d 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -395,6 +395,22 @@ function(family_flash_stlink TARGET) endfunction() +# Add flash openocd target +function(family_flash_openocd TARGET CLI_OPTIONS) + if (NOT DEFINED OPENOCD) + set(OPENOCD openocd) + endif () + + separate_arguments(CLI_OPTIONS_LIST UNIX_COMMAND ${CLI_OPTIONS}) + + # note skip verify since it has issue with rp2040 + add_custom_target(${TARGET}-openocd + DEPENDS ${TARGET} + COMMAND ${OPENOCD} ${CLI_OPTIONS_LIST} -c "program $ reset exit" + VERBATIM + ) +endfunction() + # Add flash pycod target function(family_flash_pyocd TARGET) if (NOT DEFINED PYOC) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 53e2add56..77d8029d9 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -12,6 +12,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_import.cmake) # include basic family CMake functionality set(FAMILY_MCUS RP2040) set(JLINK_DEVICE rp2040_m0_0) +set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"") include(${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}/board.cmake) @@ -158,6 +159,7 @@ function(family_configure_target TARGET RTOS) pico_enable_stdio_uart(${TARGET} 1) target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_bootsel_via_double_reset tinyusb_board${RTOS_SUFFIX} tinyusb_additions) + family_flash_openocd(${TARGET} ${OPENOCD_OPTION}) family_flash_jlink(${TARGET}) endfunction() diff --git a/test/hil/hil_pi4.json b/test/hil/hil_pi4.json new file mode 100644 index 000000000..86bae3154 --- /dev/null +++ b/test/hil/hil_pi4.json @@ -0,0 +1,11 @@ +{ + "boards": [ + { + "name": "raspberry_pi_pico", + "uid": "E6614864D35DAE36", + "debugger": "openocd", + "debugger_sn": "E6614103E78E8324", + "debugger_args": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"" + } + ] +} diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index 6bcde36c6..06640a470 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -38,6 +38,7 @@ def get_serial_dev(id, product, ifnum): return f'/dev/serial/by-id/usb-TinyUSB_{product}_{id}-if{ifnum:02d}' +# Currently not used, left as reference def get_disk_dev(id, lun): # get usb disk by id return f'/dev/disk/by-id/usb-TinyUSB_Mass_Storage_{id}-0:{lun}' @@ -59,10 +60,22 @@ def flash_jlink(sn, dev, firmware): assert ret.returncode == 0, 'Flash failed\n' + stdout +def flash_openocd(sn, args, firmware): + + ret = subprocess.run(f'openocd {args} -c "program {firmware} reset exit"', + shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + stdout = ret.stdout.decode() + assert ret.returncode == 0, 'Flash failed\n' + stdout + + +# ------------------------------------------------------------- +# Tests +# ------------------------------------------------------------- def test_board_test(id): # Dummy test pass + def test_cdc_dual_ports(id): port1 = get_serial_dev(id, "TinyUSB_Device", 0) port2 = get_serial_dev(id, "TinyUSB_Device", 2) @@ -99,12 +112,22 @@ 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' + file_list = [f'/media/blkUSB_{id[-8:]}.02/README.TXT', f'/media/{os.getenv("USER")}/TinyUSB MSC/README.TXT'] # Wait device enum timeout = 10 while timeout: - if os.path.exists(port) and os.path.isfile(file): + f_found = False + if os.path.exists(port): + for file in file_list: + if os.path.isfile(file): + f_found = True + f = open(file, 'rb') + data = f.read() + break + + if f_found: break + time.sleep(1) timeout = timeout - 1 @@ -112,7 +135,6 @@ def test_cdc_msc(id): # Echo test ser1 = serial.Serial(port) - ser1.timeout = 1 str = b"test_str" @@ -121,9 +143,6 @@ def test_cdc_msc(id): assert ser1.read(100) == str, 'CDC wrong data' # Block test - f = open(file, 'rb') - data = f.read() - 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,6 +150,7 @@ issue at github.com/hathach/tinyusb" assert data == readme, 'MSC wrong data' + def test_dfu(id): # Wait device enum timeout = 10 @@ -211,11 +231,13 @@ if __name__ == '__main__': # all possible tests, board_test is last to disable board's usb 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', + 'board_test' ] for board in config['boards']: print(f'Testing board:{board["name"]}') + debugger = board['debugger'].lower() # default to all tests if 'tests' in board: @@ -240,8 +262,10 @@ if __name__ == '__main__': print(f'Cannot find firmware file for {test}') sys.exit(-1) - if board['debugger'].lower() == 'jlink': + if debugger == 'jlink': flash_jlink(board['debugger_sn'], board['cpu'], elf) + elif debugger == 'openocd': + flash_openocd(board['debugger_sn'], board['debugger_args'], elf) else: # ToDo pass From 1a6756c254a9f9a349bc691cc6aa8cff4ac2c920 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 28 Nov 2023 19:10:19 +0700 Subject: [PATCH 2/4] fixed pi4 hil test elf location --- test/hil/hil_test.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index 06640a470..a6542fe2d 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -252,13 +252,20 @@ if __name__ == '__main__': 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: + # cmake, make, download from artifacts + elf_list = [ + f'cmake-build/cmake-build-{board["name"]}/device/{test}/{test}.elf', + f'examples/device/{test}/_build/{board["name"]}/{test}.elf', + f'{test}.elf' + ] + + elf = None + for e in elf_list: + if os.path.isfile(e): + elf = e + break + + if elf is None: print(f'Cannot find firmware file for {test}') sys.exit(-1) From b45ad57c50d13af1d2ee9e1cc2342feac4ea676d Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 28 Nov 2023 23:02:26 +0700 Subject: [PATCH 3/4] spare checkout test/hil and correct hil_pi4 uuid --- .github/workflows/cmake_arm.yml | 5 +++++ test/hil/hil_pi4.json | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake_arm.yml b/.github/workflows/cmake_arm.yml index d2a551699..f4b93cec7 100644 --- a/.github/workflows/cmake_arm.yml +++ b/.github/workflows/cmake_arm.yml @@ -121,6 +121,11 @@ jobs: rm -rf "${{ github.workspace }}" mkdir -p "${{ github.workspace }}" + - name: Checkout test/hil + uses: actions/checkout@v3 + with: + sparse-checkout: test/hil + - name: Download rp2040 Artifacts uses: actions/download-artifact@v3 with: diff --git a/test/hil/hil_pi4.json b/test/hil/hil_pi4.json index 86bae3154..18ac12bc4 100644 --- a/test/hil/hil_pi4.json +++ b/test/hil/hil_pi4.json @@ -2,9 +2,9 @@ "boards": [ { "name": "raspberry_pi_pico", - "uid": "E6614864D35DAE36", + "uid": "E6614103E72C1D2F", "debugger": "openocd", - "debugger_sn": "E6614103E78E8324", + "debugger_sn": "E6614C311B764A37", "debugger_args": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"" } ] From b8d1acd1049d0535c9c2ef8f63a7aea82abd9cbd Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 29 Nov 2023 12:00:52 +0700 Subject: [PATCH 4/4] improve hil test, correct pi4 uuid --- test/hil/hil_pi4.json | 4 +- test/hil/hil_test.py | 123 +++++++++++++++++-------------- tools/codespell/ignore-words.txt | 1 + 3 files changed, 69 insertions(+), 59 deletions(-) diff --git a/test/hil/hil_pi4.json b/test/hil/hil_pi4.json index 18ac12bc4..d613f8da8 100644 --- a/test/hil/hil_pi4.json +++ b/test/hil/hil_pi4.json @@ -2,9 +2,9 @@ "boards": [ { "name": "raspberry_pi_pico", - "uid": "E6614103E72C1D2F", + "uid": "E6614C311B764A37", "debugger": "openocd", - "debugger_sn": "E6614C311B764A37", + "debugger_sn": "E6614103E72C1D2F", "debugger_args": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \"adapter speed 5000\"" } ] diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index a6542fe2d..5f8425549 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -32,22 +32,66 @@ import serial import subprocess import json +ENUM_TIMEOUT = 10 -def get_serial_dev(id, product, ifnum): + +def get_serial_dev(id, vendor_str, product_str, ifnum): # get usb serial by id - return f'/dev/serial/by-id/usb-TinyUSB_{product}_{id}-if{ifnum:02d}' + return f'/dev/serial/by-id/usb-{vendor_str}_{product_str}_{id}-if{ifnum:02d}' # Currently not used, left as reference -def get_disk_dev(id, lun): +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, product, event): - return f'/dev/input/by-id/usb-TinyUSB_{product}_{id}-{event}' +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.2) + ser = serial.Serial(port, timeout=1) + break + except serial.SerialException: + pass + time.sleep(0.8) + timeout = timeout - 1 + assert timeout, 'Device not available or Cannot open port' + return ser + + +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 + + assert timeout, 'Device not available' + return None + + +# ------------------------------------------------------------- +# Flash with debugger +# ------------------------------------------------------------- def flash_jlink(sn, dev, firmware): script = ['halt', 'r', f'loadfile {firmware}', 'r', 'go', 'exit'] f = open('flash.jlink', 'w') @@ -61,7 +105,6 @@ def flash_jlink(sn, dev, firmware): def flash_openocd(sn, args, firmware): - ret = subprocess.run(f'openocd {args} -c "program {firmware} reset exit"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout = ret.stdout.decode() @@ -77,26 +120,13 @@ def test_board_test(id): def test_cdc_dual_ports(id): - port1 = get_serial_dev(id, "TinyUSB_Device", 0) - port2 = get_serial_dev(id, "TinyUSB_Device", 2) + port1 = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 0) + port2 = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 2) - # Wait device enum - timeout = 10 - while timeout: - if os.path.exists(port1) and os.path.exists(port2): - break - time.sleep(1) - timeout = timeout - 1 - - assert timeout, 'Device not available' + ser1 = open_serial_dev(port1) + ser2 = open_serial_dev(port2) # Echo test - ser1 = serial.Serial(port1) - ser2 = serial.Serial(port2) - - ser1.timeout = 1 - ser2.timeout = 1 - str1 = b"test_no1" ser1.write(str1) ser1.flush() @@ -111,38 +141,17 @@ def test_cdc_dual_ports(id): def test_cdc_msc(id): - port = get_serial_dev(id, "TinyUSB_Device", 0) - file_list = [f'/media/blkUSB_{id[-8:]}.02/README.TXT', f'/media/{os.getenv("USER")}/TinyUSB MSC/README.TXT'] - # Wait device enum - timeout = 10 - while timeout: - f_found = False - if os.path.exists(port): - for file in file_list: - if os.path.isfile(file): - f_found = True - f = open(file, 'rb') - data = f.read() - break - - if f_found: - 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 + 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\ @@ -153,7 +162,7 @@ issue at github.com/hathach/tinyusb" 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) @@ -192,7 +201,7 @@ 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) @@ -206,11 +215,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 diff --git a/tools/codespell/ignore-words.txt b/tools/codespell/ignore-words.txt index 2513691cb..957cbd86b 100644 --- a/tools/codespell/ignore-words.txt +++ b/tools/codespell/ignore-words.txt @@ -11,3 +11,4 @@ attch endianess pris busses +ser