From 19d7d62e804dbf26f9d193d72710574abcd6fa95 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Dec 2023 18:54:00 +0700 Subject: [PATCH 1/4] try to add samd51 to hil pool using doublereset (rpi gpio) and bossac --- .github/workflows/cmake_arm.yml | 10 ++++++++- hw/bsp/samd21/family.cmake | 3 ++- hw/bsp/samd51/family.c | 14 ++++++++++++ hw/bsp/samd51/family.cmake | 4 +++- src/device/usbd_pvt.h | 5 ----- src/host/usbh_pvt.h | 5 ----- src/tusb_option.h | 10 +++++++++ test/hil/hil_pi4.json | 9 ++++++++ test/hil/hil_test.py | 39 +++++++++++++++++++++++++++++++-- 9 files changed, 84 insertions(+), 15 deletions(-) diff --git a/.github/workflows/cmake_arm.yml b/.github/workflows/cmake_arm.yml index f760ca3ca..46dc7a93b 100644 --- a/.github/workflows/cmake_arm.yml +++ b/.github/workflows/cmake_arm.yml @@ -102,6 +102,13 @@ jobs: path: | cmake-build/cmake-build-feather_nrf52840_express/*/*/*.elf + - name: Upload Artifacts for Hardware Testing (samd51) + if: matrix.family == 'samd51' && github.repository_owner == 'hathach' + uses: actions/upload-artifact@v3 + with: + name: itsybitsy_m4 + path: | + cmake-build/cmake-build-itsybitsy_m4/*/*/*.bin # --------------------------------------- # Hardware in the loop (HIL) @@ -116,8 +123,9 @@ jobs: fail-fast: false matrix: board: - - 'raspberry_pi_pico' - 'feather_nrf52840_express' + - 'itsybitsy_m4' + - 'raspberry_pi_pico' steps: - name: Clean workspace run: | diff --git a/hw/bsp/samd21/family.cmake b/hw/bsp/samd21/family.cmake index 44aad6ecb..a79a25b54 100644 --- a/hw/bsp/samd21/family.cmake +++ b/hw/bsp/samd21/family.cmake @@ -10,7 +10,7 @@ set(CMAKE_SYSTEM_PROCESSOR cortex-m0plus CACHE INTERNAL "System Processor") set(CMAKE_TOOLCHAIN_FILE ${TOP}/examples/build_system/cmake/toolchain/arm_${TOOLCHAIN}.cmake) set(FAMILY_MCUS SAMD21 CACHE INTERNAL "") - +set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -c \"transport select swd\" -f target/at91samdXX.cfg") #------------------------------------ # BOARD_TARGET @@ -102,4 +102,5 @@ function(family_configure_example TARGET RTOS) # Flashing family_flash_jlink(${TARGET}) + #family_flash_openocd(${TARGET} ${OPENOCD_OPTION}) endfunction() diff --git a/hw/bsp/samd51/family.c b/hw/bsp/samd51/family.c index 009a0d290..170c88191 100644 --- a/hw/bsp/samd51/family.c +++ b/hw/bsp/samd51/family.c @@ -157,6 +157,20 @@ uint32_t board_button_read(void) { return gpio_get_pin_level(BUTTON_PIN) ? 0 : 1; } +size_t board_get_unique_id(uint8_t id[], size_t max_len) { + (void) max_len; + + uint32_t did_addr[4] = {0x008061FC, 0x00806010, 0x00806014, 0x00806018}; + + for (int i = 0; i < 4; i++) { + uint32_t did = *((uint32_t const*) did_addr[i]); + did = TU_BSWAP32(did); // swap endian to match samd51 uf2 bootloader + memcpy(id + i * 4, &did, 4); + } + + return 16; +} + int board_uart_read(uint8_t* buf, int len) { (void) buf; (void) len; diff --git a/hw/bsp/samd51/family.cmake b/hw/bsp/samd51/family.cmake index 7591f3b1b..4d91bf1a6 100644 --- a/hw/bsp/samd51/family.cmake +++ b/hw/bsp/samd51/family.cmake @@ -10,7 +10,7 @@ set(CMAKE_SYSTEM_PROCESSOR cortex-m4 CACHE INTERNAL "System Processor") set(CMAKE_TOOLCHAIN_FILE ${TOP}/examples/build_system/cmake/toolchain/arm_${TOOLCHAIN}.cmake) set(FAMILY_MCUS SAMD51 CACHE INTERNAL "") - +set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -c \"transport select swd\" -c \"set CHIPNAME samd51\" -f target/atsame5x.cfg") #------------------------------------ # BOARD_TARGET @@ -100,5 +100,7 @@ function(family_configure_example TARGET RTOS) target_link_libraries(${TARGET} PUBLIC board_${BOARD} ${TARGET}-tinyusb) # Flashing + family_add_bin_hex(${TARGET}) family_flash_jlink(${TARGET}) + #family_flash_openocd(${TARGET} ${OPENOCD_OPTION}) endfunction() diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 153be7cee..9039bc9d6 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -33,11 +33,6 @@ extern "C" { #endif -// Level where CFG_TUSB_DEBUG must be at least for USBD is logged -#ifndef CFG_TUD_LOG_LEVEL -#define CFG_TUD_LOG_LEVEL 2 -#endif - #define TU_LOG_USBD(...) TU_LOG(CFG_TUD_LOG_LEVEL, __VA_ARGS__) //--------------------------------------------------------------------+ diff --git a/src/host/usbh_pvt.h b/src/host/usbh_pvt.h index 3363ab9eb..4a97a1c18 100644 --- a/src/host/usbh_pvt.h +++ b/src/host/usbh_pvt.h @@ -35,11 +35,6 @@ extern "C" { #endif -// Level where CFG_TUSB_DEBUG must be at least for USBH is logged -#ifndef CFG_TUH_LOG_LEVEL - #define CFG_TUH_LOG_LEVEL 2 -#endif - #define TU_LOG_USBH(...) TU_LOG(CFG_TUH_LOG_LEVEL, __VA_ARGS__) enum { diff --git a/src/tusb_option.h b/src/tusb_option.h index 7597392d6..4d2aa137c 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -299,6 +299,16 @@ #define CFG_TUSB_DEBUG 0 #endif +// Level where CFG_TUSB_DEBUG must be at least for USBH is logged +#ifndef CFG_TUH_LOG_LEVEL + #define CFG_TUH_LOG_LEVEL 2 +#endif + +// Level where CFG_TUSB_DEBUG must be at least for USBD is logged +#ifndef CFG_TUD_LOG_LEVEL + #define CFG_TUD_LOG_LEVEL 2 +#endif + // Memory section for placing buffer used for usb transferring. If MEM_SECTION is different for // host and device use: CFG_TUD_MEM_SECTION, CFG_TUH_MEM_SECTION instead #ifndef CFG_TUSB_MEM_SECTION diff --git a/test/hil/hil_pi4.json b/test/hil/hil_pi4.json index 560c3964e..17d9ff1fa 100644 --- a/test/hil/hil_pi4.json +++ b/test/hil/hil_pi4.json @@ -23,6 +23,15 @@ "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 8f8a3aa65..f9611b96e 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -34,6 +34,10 @@ import subprocess import json import glob +# for RPI double reset: install sudo apt install python3-gpiozero or sudo pip install gpiozero +from gpiozero import LED + + ENUM_TIMEOUT = 10 @@ -41,6 +45,8 @@ ENUM_TIMEOUT = 10 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 @@ -115,7 +121,7 @@ def flash_jlink(board, firmware): def flash_openocd(board, firmware): ret = subprocess.run( - f'openocd -c "adapter serial {board["flasher_sn"]}" {board["flasher_args"]} -c "program {firmware}" -c "reset init" -c "resume" -c "exit"', + 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 @@ -133,6 +139,35 @@ def flash_esptool(board, firmware): return ret +def doublereset_with_rpi_gpio(board): + # Off = 0 = Reset + led = 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(port): + time.sleep(0.5) + break + else: + time.sleep(0.5) + timeout = timeout - 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 + # ------------------------------------------------------------- # Tests # ------------------------------------------------------------- @@ -305,7 +340,7 @@ def main(config_file, board): for test in test_list: fw_list = [ - # cmake: esp32 use .bin file + # 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 From b343ac6d0f687af917ba7c9861c017c000166f0d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Dec 2023 19:05:44 +0700 Subject: [PATCH 2/4] reset usb before running test --- .github/workflows/build_esp.yml | 9 +++++---- .github/workflows/cmake_arm.yml | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build_esp.yml b/.github/workflows/build_esp.yml index 51df644a5..34d1a7d15 100644 --- a/.github/workflows/build_esp.yml +++ b/.github/workflows/build_esp.yml @@ -92,14 +92,15 @@ jobs: name: ${{ matrix.board }} path: cmake-build/cmake-build-${{ matrix.board }} - - name: Test on actual hardware - run: | - python3 test/hil/hil_test.py --board ${{ matrix.board }} hil_pi4.json - - name: Reset USB bus run: | for port in $(lspci | grep USB | cut -d' ' -f1); do echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind; sleep 1; echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind; + sleep 1; done + + - name: Test on actual hardware + run: | + python3 test/hil/hil_test.py --board ${{ matrix.board }} hil_pi4.json diff --git a/.github/workflows/cmake_arm.yml b/.github/workflows/cmake_arm.yml index 46dc7a93b..71b42b103 100644 --- a/.github/workflows/cmake_arm.yml +++ b/.github/workflows/cmake_arm.yml @@ -144,14 +144,15 @@ jobs: name: ${{ matrix.board }} path: cmake-build/cmake-build-${{ matrix.board }} - - name: Test on actual hardware - run: | - python3 test/hil/hil_test.py --board ${{ matrix.board }} hil_pi4.json - - name: Reset USB bus run: | for port in $(lspci | grep USB | cut -d' ' -f1); do echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind; sleep 1; echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind; + sleep 1; done + + - name: Test on actual hardware + run: | + python3 test/hil/hil_test.py --board ${{ matrix.board }} hil_pi4.json From 31c33ca8530ed1115a956b39c853dceb56d307db Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Dec 2023 19:18:08 +0700 Subject: [PATCH 3/4] tweak usb reset wait time, fix test script with IAR server --- .github/workflows/build_esp.yml | 4 ++-- .github/workflows/cmake_arm.yml | 4 ++-- test/hil/hil_test.py | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build_esp.yml b/.github/workflows/build_esp.yml index 34d1a7d15..bbbbc72ee 100644 --- a/.github/workflows/build_esp.yml +++ b/.github/workflows/build_esp.yml @@ -96,9 +96,9 @@ jobs: run: | for port in $(lspci | grep USB | cut -d' ' -f1); do echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind; - sleep 1; + sleep 0.5; echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind; - sleep 1; + sleep 3; done - name: Test on actual hardware diff --git a/.github/workflows/cmake_arm.yml b/.github/workflows/cmake_arm.yml index 71b42b103..ba531e9ea 100644 --- a/.github/workflows/cmake_arm.yml +++ b/.github/workflows/cmake_arm.yml @@ -148,9 +148,9 @@ jobs: run: | for port in $(lspci | grep USB | cut -d' ' -f1); do echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind; - sleep 1; + sleep 0.5; echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind; - sleep 1; + sleep 3; done - name: Test on actual hardware diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index f9611b96e..00751181e 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -34,8 +34,11 @@ import subprocess import json import glob -# for RPI double reset: install sudo apt install python3-gpiozero or sudo pip install gpiozero -from gpiozero import LED +# for RPI double reset +try: + import gpiozero +except ImportError: + pass ENUM_TIMEOUT = 10 @@ -141,7 +144,7 @@ def flash_esptool(board, firmware): def doublereset_with_rpi_gpio(board): # Off = 0 = Reset - led = LED(board["flasher_reset_pin"]) + led = gpiozero.LED(board["flasher_reset_pin"]) led.off() time.sleep(0.1) From 3da0d781701c6bb121645d7631e2a690aaadb6bf Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Dec 2023 20:16:47 +0700 Subject: [PATCH 4/4] minor tweaking --- test/hil/hil_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index 00751181e..7c662aeea 100644 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -162,11 +162,13 @@ def flash_bossac(board, firmware): timeout = ENUM_TIMEOUT while timeout: if os.path.exists(port): - time.sleep(0.5) break 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