| 
									
										
										
										
											2024-09-17 12:17:12 -07:00
										 |  |  | #!/usr/bin/env python3 | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # The MIT License (MIT) | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (c) 2023 HiFiPhile | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  | # of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  | # in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  | # copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  | # furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  | # all copies or substantial portions of the Software. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  | # THE SOFTWARE. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | # udev rules : | 
					
						
							| 
									
										
										
										
											2023-08-07 20:48:54 +02:00
										 |  |  | # ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", MODE="0666", PROGRAM="/bin/sh -c 'echo $$ID_SERIAL_SHORT | rev | cut -c -8 | rev'", SYMLINK+="ttyUSB_%c.%s{bInterfaceNumber}" | 
					
						
							|  |  |  | # ACTION=="add", SUBSYSTEM=="block", SUBSYSTEMS=="usb", ENV{ID_FS_USAGE}=="filesystem", MODE="0666", PROGRAM="/bin/sh -c 'echo $$ID_SERIAL_SHORT | rev | cut -c -8 | rev'", RUN{program}+="/usr/bin/systemd-mount --no-block --automount=yes --collect $devnode /media/blkUSB_%c.%s{bInterfaceNumber}" | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | import sys | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | import serial | 
					
						
							|  |  |  | import subprocess | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | import glob | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  | from multiprocessing import Pool | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  | import fs | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 11:04:53 +07:00
										 |  |  | ENUM_TIMEOUT = 30 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-28 11:43:14 +07:00
										 |  |  | STATUS_OK = "\033[32mOK\033[0m" | 
					
						
							|  |  |  | STATUS_FAILED = "\033[31mFailed\033[0m" | 
					
						
							|  |  |  | STATUS_SKIPPED = "\033[33mSkipped\033[0m" | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-13 19:12:26 +07:00
										 |  |  | verbose = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | WCH_RISCV_CONTENT = """
 | 
					
						
							|  |  |  | adapter driver wlinke | 
					
						
							|  |  |  | adapter speed 6000 | 
					
						
							|  |  |  | transport select sdi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | wlink_set_address 0x00000000 | 
					
						
							|  |  |  | set _CHIPNAME wch_riscv | 
					
						
							|  |  |  | sdi newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x00001 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | set _TARGETNAME $_CHIPNAME.cpu | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | target create $_TARGETNAME.0 wch_riscv -chain-position $_TARGETNAME | 
					
						
							|  |  |  | $_TARGETNAME.0 configure  -work-area-phys 0x20000000 -work-area-size 10000 -work-area-backup 1 | 
					
						
							|  |  |  | set _FLASHNAME $_CHIPNAME.flash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | flash bank $_FLASHNAME wch_riscv 0x00000000 0 0 0 $_TARGETNAME.0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo "Ready for Remote Connections" | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | # Path | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | OPENCOD_ADI_PATH = f'{os.getenv("HOME")}/app/openocd_adi' | 
					
						
							| 
									
										
										
										
											2024-11-12 19:00:00 +07:00
										 |  |  | TINYUSB_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | # get usb serial by id | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | def get_serial_dev(id, vendor_str, product_str, ifnum): | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     if vendor_str and product_str: | 
					
						
							|  |  |  |         # known vendor and product | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  |         vendor_str = vendor_str.replace(' ', '_') | 
					
						
							|  |  |  |         product_str = product_str.replace(' ', '_') | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         return f'/dev/serial/by-id/usb-{vendor_str}_{product_str}_{id}-if{ifnum:02d}' | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2023-12-11 15:23:09 +07:00
										 |  |  |         # just use id: mostly for cp210x/ftdi flasher | 
					
						
							| 
									
										
										
										
											2025-03-24 23:20:54 +07:00
										 |  |  |         pattern = f'/dev/serial/by-id/usb-*_{id}-if*' | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         port_list = glob.glob(pattern) | 
					
						
							|  |  |  |         return port_list[0] | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  | # get usb disk by id | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | def get_disk_dev(id, vendor_str, 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 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     while timeout > 0: | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |         if os.path.exists(port): | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2024-11-12 19:00:00 +07:00
										 |  |  |                 ser = serial.Serial(port, baudrate=115200, timeout=5) | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |                 break | 
					
						
							|  |  |  |             except serial.SerialException: | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |                 print(f'serial {port} not reaady {timeout} sec') | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |         time.sleep(0.1) | 
					
						
							|  |  |  |         timeout -= 0.1 | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     assert timeout > 0, f'Cannot open port f{port}' if os.path.exists(port) else f'Port {port} not existed' | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     return ser | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  | def read_disk_file(uid, lun, fname): | 
					
						
							|  |  |  |     # open_fs("fat://{dev}) require 'pip install pyfatfs' | 
					
						
							|  |  |  |     dev = get_disk_dev(uid, 'TinyUSB', lun) | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     while timeout > 0: | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  |         if os.path.exists(dev): | 
					
						
							| 
									
										
										
										
											2024-08-13 14:35:42 +07:00
										 |  |  |             fat = fs.open_fs(f'fat://{dev}?read_only=true') | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 with fat.open(fname, 'rb') as f: | 
					
						
							| 
									
										
										
										
											2024-08-13 14:35:42 +07:00
										 |  |  |                     data = f.read() | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  |             finally: | 
					
						
							|  |  |  |                 fat.close() | 
					
						
							| 
									
										
										
										
											2024-08-13 14:35:42 +07:00
										 |  |  |             assert data, f'Cannot read file {fname} from {dev}' | 
					
						
							|  |  |  |             return data | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |         time.sleep(1) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:35:42 +07:00
										 |  |  |         timeout -= 1 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     assert timeout > 0, f'Storage {dev} not existed' | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     return None | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2023-12-11 15:23:09 +07:00
										 |  |  | # Flashing firmware | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | def run_cmd(cmd, cwd=None): | 
					
						
							|  |  |  |     r = subprocess.run(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							| 
									
										
										
										
											2024-05-09 20:43:46 +07:00
										 |  |  |     if r.returncode != 0: | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |         title = f'COMMAND FAILED: {cmd}' | 
					
						
							|  |  |  |         print() | 
					
						
							| 
									
										
										
										
											2024-05-09 20:43:46 +07:00
										 |  |  |         if os.getenv('CI'): | 
					
						
							|  |  |  |             print(f"::group::{title}") | 
					
						
							|  |  |  |             print(r.stdout.decode("utf-8")) | 
					
						
							|  |  |  |             print(f"::endgroup::") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print(title) | 
					
						
							|  |  |  |             print(r.stdout.decode("utf-8")) | 
					
						
							| 
									
										
										
										
											2024-09-13 19:12:26 +07:00
										 |  |  |     elif verbose: | 
					
						
							|  |  |  |         print(cmd) | 
					
						
							|  |  |  |         print(r.stdout.decode("utf-8")) | 
					
						
							| 
									
										
										
										
											2024-05-09 20:43:46 +07:00
										 |  |  |     return r | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def flash_jlink(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-05-09 20:43:46 +07:00
										 |  |  |     script = ['halt', 'r', f'loadfile {firmware}.elf', 'r', 'go', 'exit'] | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  |     f_jlink = f'{board["name"]}_{os.path.basename(firmware)}.jlink' | 
					
						
							|  |  |  |     with open(f_jlink, 'w') as f: | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         f.writelines(f'{s}\n' for s in script) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     ret = run_cmd(f'JLinkExe -USB {flasher["uid"]} {flasher["args"]} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile {f_jlink}') | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  |     os.remove(f_jlink) | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     return ret | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_jlink(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     script = ['halt', 'r', 'go', 'exit'] | 
					
						
							|  |  |  |     f_jlink = f'{board["name"]}_reset.jlink' | 
					
						
							|  |  |  |     if not os.path.exists(f_jlink): | 
					
						
							|  |  |  |         with open(f_jlink, 'w') as f: | 
					
						
							|  |  |  |             f.writelines(f'{s}\n' for s in script) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     ret = run_cmd(f'JLinkExe -USB {flasher["uid"]} {flasher["args"]} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile {f_jlink}') | 
					
						
							| 
									
										
										
										
											2024-07-25 22:38:23 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-01 13:00:38 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def flash_stlink(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     return run_cmd(f'STM32_Programmer_CLI --connect port=swd sn={flasher["uid"]} --write {firmware}.elf --go') | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def reset_stlink(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     return run_cmd(f'STM32_Programmer_CLI --connect port=swd sn={flasher["uid"]} --rst --go') | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-01 13:00:38 +07:00
										 |  |  | def flash_stflash(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     ret = run_cmd(f'st-flash --serial {flasher["uid"]} write {firmware}.bin 0x8000000') | 
					
						
							| 
									
										
										
										
											2024-08-01 13:00:38 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_stflash(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     return subprocess.CompletedProcess(args=['dummy'], returncode=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def flash_openocd(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 18:23:58 +07:00
										 |  |  |     ret = run_cmd(f'openocd -c "tcl_port disabled" -c "gdb_port disabled" -c "adapter serial {flasher["uid"]}" ' | 
					
						
							| 
									
										
										
										
											2025-03-24 23:20:54 +07:00
										 |  |  |                   f'{flasher["args"]} -c "init; halt; program {firmware}.elf verify; reset; exit"') | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_openocd(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 18:23:58 +07:00
										 |  |  |     ret = run_cmd(f'openocd -c "tcl_port disabled" -c "gdb_port disabled" -c "adapter serial {flasher["uid"]}" ' | 
					
						
							| 
									
										
										
										
											2025-03-24 23:20:54 +07:00
										 |  |  |                   f'{flasher["args"]} -c "init; reset run; exit"') | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     return ret | 
					
						
							| 
									
										
										
										
											2024-08-08 21:38:49 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def flash_openocd_wch(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     f_wch = f"wch-riscv_{board['uid']}.cfg" | 
					
						
							|  |  |  |     if not os.path.exists(f_wch): | 
					
						
							|  |  |  |         with open(f_wch, 'w') as file: | 
					
						
							|  |  |  |             file.write(WCH_RISCV_CONTENT) | 
					
						
							| 
									
										
										
										
											2024-08-08 21:38:49 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     ret = run_cmd(f'openocd_wch -c "adapter serial {flasher["uid"]}" -f {f_wch} ' | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |                   f'-c "program {firmware}.elf reset exit"') | 
					
						
							|  |  |  |     return ret | 
					
						
							| 
									
										
										
										
											2024-08-08 21:38:49 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_openocd_wch(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-08-08 21:38:49 +07:00
										 |  |  |     f_wch = f"wch-riscv_{board['uid']}.cfg" | 
					
						
							|  |  |  |     if not os.path.exists(f_wch): | 
					
						
							|  |  |  |         with open(f_wch, 'w') as file: | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |             file.write(WCH_RISCV_CONTENT) | 
					
						
							| 
									
										
										
										
											2024-08-08 21:38:49 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     ret = run_cmd(f'openocd_wch -c "adapter serial {flasher["uid"]}" -f {f_wch} -c "program reset exit"') | 
					
						
							| 
									
										
										
										
											2024-08-08 21:38:49 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 17:15:07 +07:00
										 |  |  | def flash_openocd_adi(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     ret = run_cmd(f'{OPENCOD_ADI_PATH}/src/openocd -c "adapter serial {flasher["uid"]}" -s {OPENCOD_ADI_PATH}/tcl ' | 
					
						
							|  |  |  |                   f'{flasher["args"]} -c "program {firmware}.elf reset exit"') | 
					
						
							| 
									
										
										
										
											2024-08-18 17:15:07 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | def reset_openocd_adi(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     ret = run_cmd(f'{OPENCOD_ADI_PATH}/src/openocd -c "adapter serial {flasher["uid"]}" -s {OPENCOD_ADI_PATH}/tcl ' | 
					
						
							|  |  |  |                   f'{flasher["args"]} -c "program reset exit"') | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  | def flash_wlink_rs(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  |     # wlink use index for probe selection and lacking usb serial support | 
					
						
							|  |  |  |     ret = run_cmd(f'wlink flash {firmware}.elf') | 
					
						
							|  |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_wlink_rs(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     # wlink use index for probe selection and lacking usb serial support | 
					
						
							|  |  |  |     ret = run_cmd(f'wlink reset') | 
					
						
							|  |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def flash_esptool(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     port = get_serial_dev(flasher["uid"], None, None, 0) | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  |     fw_dir = os.path.dirname(f'{firmware}.bin') | 
					
						
							|  |  |  |     with open(f'{fw_dir}/config.env') as f: | 
					
						
							|  |  |  |         idf_target = json.load(f)['IDF_TARGET'] | 
					
						
							|  |  |  |     with open(f'{fw_dir}/flash_args') as f: | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         flash_args = f.read().strip().replace('\n', ' ') | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     command = (f'esptool.py --chip {idf_target} -p {port} {flasher["args"]} ' | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |                f'--before=default_reset --after=hard_reset write_flash {flash_args}') | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  |     ret = run_cmd(command, cwd=fw_dir) | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     return ret | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_esptool(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     return subprocess.CompletedProcess(args=['dummy'], returncode=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-15 14:36:31 +07:00
										 |  |  | def flash_uniflash(board, firmware): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							|  |  |  |     ret = run_cmd(f'dslite.sh {flasher["args"]} -f {firmware}.hex') | 
					
						
							| 
									
										
										
										
											2024-08-15 14:36:31 +07:00
										 |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | def reset_uniflash(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     return subprocess.CompletedProcess(args=['dummy'], returncode=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | # Tests: dual | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_dual_host_info_to_device_cdc(board): | 
					
						
							|  |  |  |     uid = board['uid'] | 
					
						
							| 
									
										
										
										
											2024-11-11 18:40:31 +07:00
										 |  |  |     declared_devs = [f'{d["vid_pid"]}_{d["serial"]}' for d in board['tests']['dev_attached']] | 
					
						
							| 
									
										
										
										
											2024-11-12 19:00:00 +07:00
										 |  |  |     port = get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 0) | 
					
						
							| 
									
										
										
										
											2024-11-11 18:40:31 +07:00
										 |  |  |     ser = open_serial_dev(port) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # read from cdc, first line should contain vid/pid and serial | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     data = ser.read(10000) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     ser.close() | 
					
						
							|  |  |  |     if len(data) == 0: | 
					
						
							|  |  |  |         assert False, 'No data from device' | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     lines = data.decode('utf-8', errors='ignore').splitlines() | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-11 18:40:31 +07:00
										 |  |  |     enum_dev_sn = [] | 
					
						
							|  |  |  |     for l in lines: | 
					
						
							|  |  |  |         vid_pid_sn = re.search(r'ID ([0-9a-fA-F]+):([0-9a-fA-F]+) SN (\w+)', l) | 
					
						
							|  |  |  |         if vid_pid_sn: | 
					
						
							|  |  |  |             print(f'\r\n  {l} ', end='') | 
					
						
							|  |  |  |             enum_dev_sn.append(f'{vid_pid_sn.group(1)}_{vid_pid_sn.group(2)}_{vid_pid_sn.group(3)}') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if set(declared_devs) != set(enum_dev_sn): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |         failed_msg = f'Expected {declared_devs}, Enumerated {enum_dev_sn}' | 
					
						
							| 
									
										
										
										
											2025-03-25 10:43:55 +07:00
										 |  |  |         print('\n'.join(lines)) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |         assert False, failed_msg | 
					
						
							| 
									
										
										
										
											2024-11-11 18:40:31 +07:00
										 |  |  |     return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | # Tests: host | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2024-11-12 19:00:00 +07:00
										 |  |  | def test_host_device_info(board): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-11-11 18:40:31 +07:00
										 |  |  |     declared_devs = [f'{d["vid_pid"]}_{d["serial"]}' for d in board['tests']['dev_attached']] | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     port = get_serial_dev(flasher["uid"], None, None, 0) | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     ser = open_serial_dev(port) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # reset device since we can miss the first line | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     ret = globals()[f'reset_{flasher["name"].lower()}'](board) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     assert ret.returncode == 0,  'Failed to reset device' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     data = ser.read(10000) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |     ser.close() | 
					
						
							| 
									
										
										
										
											2024-11-12 19:00:00 +07:00
										 |  |  |     if len(data) == 0: | 
					
						
							|  |  |  |         assert False, 'No data from device' | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     lines = data.decode('utf-8', errors='ignore').splitlines() | 
					
						
							| 
									
										
										
										
											2025-03-26 21:13:01 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     enum_dev_sn = [] | 
					
						
							|  |  |  |     for l in lines: | 
					
						
							|  |  |  |         vid_pid_sn = re.search(r'ID ([0-9a-fA-F]+):([0-9a-fA-F]+) SN (\w+)', l) | 
					
						
							|  |  |  |         if vid_pid_sn: | 
					
						
							|  |  |  |             print(f'\r\n  {l} ', end='') | 
					
						
							|  |  |  |             enum_dev_sn.append(f'{vid_pid_sn.group(1)}_{vid_pid_sn.group(2)}_{vid_pid_sn.group(3)}') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-30 23:16:53 +07:00
										 |  |  |     if set(declared_devs) != set(enum_dev_sn): | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |         failed_msg = f'Expected {declared_devs}, Enumerated {enum_dev_sn}' | 
					
						
							| 
									
										
										
										
											2025-03-25 10:43:55 +07:00
										 |  |  |         print('\n'.join(lines)) | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |         assert False, failed_msg | 
					
						
							| 
									
										
										
										
											2024-11-12 19:00:00 +07:00
										 |  |  |     return 0 | 
					
						
							| 
									
										
										
										
											2024-11-11 18:40:31 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | # Tests: device | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | def test_device_board_test(board): | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |     # Dummy test | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_cdc_dual_ports(board): | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     uid = board['uid'] | 
					
						
							| 
									
										
										
										
											2024-11-13 16:20:41 +07:00
										 |  |  |     port = [ | 
					
						
							|  |  |  |         get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 0), | 
					
						
							|  |  |  |         get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 2) | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     ser = [open_serial_dev(p) for p in port] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     str_test = [ b"test_no1", b"test_no2" ] | 
					
						
							|  |  |  |     # Echo test write to each port and read back | 
					
						
							|  |  |  |     for i in range(len(str_test)): | 
					
						
							|  |  |  |         s = str_test[i] | 
					
						
							|  |  |  |         l = len(s) | 
					
						
							|  |  |  |         ser[i].write(s) | 
					
						
							|  |  |  |         ser[i].flush() | 
					
						
							|  |  |  |         rd = [ ser[i].read(l) for i in range(len(ser)) ] | 
					
						
							|  |  |  |         assert rd[0] == s.lower(), f'Port1 wrong data: expected {s.lower()} was {rd[0]}' | 
					
						
							|  |  |  |         assert rd[1] == s.upper(), f'Port2 wrong data: expected {s.upper()} was {rd[1]}' | 
					
						
							|  |  |  |     ser[0].close() | 
					
						
							|  |  |  |     ser[1].close() | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_cdc_msc(board): | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     uid = board['uid'] | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     # Echo test | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     port = get_serial_dev(uid, 'TinyUSB', "TinyUSB_Device", 0) | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     ser = open_serial_dev(port) | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 19:32:57 +07:00
										 |  |  |     test_str = b"test_str" | 
					
						
							|  |  |  |     ser.write(test_str) | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     ser.flush() | 
					
						
							| 
									
										
										
										
											2024-11-13 19:32:57 +07:00
										 |  |  |     rd_str = ser.read(len(test_str)) | 
					
						
							| 
									
										
										
										
											2024-11-13 16:20:41 +07:00
										 |  |  |     ser.close() | 
					
						
							| 
									
										
										
										
											2024-11-13 19:32:57 +07:00
										 |  |  |     assert  rd_str == test_str, f'CDC wrong data: expected: {test_str} was {rd_str}' | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Block test | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  |     data = read_disk_file(uid,0,'README.TXT') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     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\ | 
					
						
							|  |  |  | issue at github.com/hathach/tinyusb" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 20:48:54 +02:00
										 |  |  |     assert data == readme, 'MSC wrong data' | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_cdc_msc_freertos(board): | 
					
						
							|  |  |  |     test_device_cdc_msc(board) | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_dfu(board): | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     uid = board['uid'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     # Wait device enum | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     while timeout > 0: | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  |         ret = run_cmd(f'dfu-util -l') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         stdout = ret.stdout.decode() | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |         if f'serial="{uid}"' in stdout and 'Found DFU: [cafe:4000]' in stdout: | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |             break | 
					
						
							|  |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     assert timeout > 0, 'Device not available' | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     f_dfu0 = f'dfu0_{uid}' | 
					
						
							|  |  |  |     f_dfu1 = f'dfu1_{uid}' | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     # Test upload | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  |         os.remove(f_dfu0) | 
					
						
							|  |  |  |         os.remove(f_dfu1) | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     except OSError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     ret = run_cmd(f'dfu-util -S {uid} -a 0 -U {f_dfu0}') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     assert ret.returncode == 0, 'Upload failed' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     ret = run_cmd(f'dfu-util -S {uid} -a 1 -U {f_dfu1}') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     assert ret.returncode == 0, 'Upload failed' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  |     with open(f_dfu0) as f: | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         assert 'Hello world from TinyUSB DFU! - Partition 0' in f.read(), 'Wrong uploaded data' | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  |     with open(f_dfu1) as f: | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         assert 'Hello world from TinyUSB DFU! - Partition 1' in f.read(), 'Wrong uploaded data' | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 19:23:35 +07:00
										 |  |  |     os.remove(f_dfu0) | 
					
						
							|  |  |  |     os.remove(f_dfu1) | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_dfu_runtime(board): | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     uid = board['uid'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 17:34:07 +02:00
										 |  |  |     # Wait device enum | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     while timeout > 0: | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  |         ret = run_cmd(f'dfu-util -l') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         stdout = ret.stdout.decode() | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |         if f'serial="{uid}"' in stdout and 'Found Runtime: [cafe:4000]' in stdout: | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |             break | 
					
						
							|  |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     assert timeout > 0, 'Device not available' | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_hid_boot_interface(board): | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     uid = board['uid'] | 
					
						
							|  |  |  |     kbd = get_hid_dev(uid, 'TinyUSB', 'TinyUSB_Device', 'event-kbd') | 
					
						
							|  |  |  |     mouse1 = get_hid_dev(uid, 'TinyUSB', 'TinyUSB_Device', 'if01-event-mouse') | 
					
						
							|  |  |  |     mouse2 = get_hid_dev(uid, 'TinyUSB', 'TinyUSB_Device', 'if01-mouse') | 
					
						
							| 
									
										
										
										
											2023-08-08 22:08:59 +02:00
										 |  |  |     # Wait device enum | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     while timeout > 0: | 
					
						
							| 
									
										
										
										
											2023-08-08 22:08:59 +02:00
										 |  |  |         if os.path.exists(kbd) and os.path.exists(mouse1) and os.path.exists(mouse2): | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 14:43:40 +07:00
										 |  |  |     assert timeout > 0, 'HID device not available' | 
					
						
							| 
									
										
										
										
											2023-08-08 22:08:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | def test_device_hid_composite_freertos(id): | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     # TODO implement later | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | # Main | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | # device tests | 
					
						
							| 
									
										
										
										
											2024-11-13 17:34:22 +07:00
										 |  |  | # note don't test 2 examples with cdc or 2 msc next to each other | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | device_tests = [ | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     'device/cdc_dual_ports', | 
					
						
							|  |  |  |     'device/dfu', | 
					
						
							| 
									
										
										
										
											2024-11-13 17:34:22 +07:00
										 |  |  |     'device/cdc_msc', | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     'device/dfu_runtime', | 
					
						
							| 
									
										
										
										
											2024-11-13 17:34:22 +07:00
										 |  |  |     'device/cdc_msc_freertos', | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     'device/hid_boot_interface', | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | ] | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | dual_tests = [ | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     'dual/host_info_to_device_cdc', | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  | host_test = [ | 
					
						
							|  |  |  |     'host/device_info', | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:19 +07:00
										 |  |  | def test_example(board, f1, example): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Test example firmware | 
					
						
							|  |  |  |     :param board: board dict | 
					
						
							|  |  |  |     :param f1: flags on | 
					
						
							|  |  |  |     :param example: example name | 
					
						
							|  |  |  |     :return: 0 if success/skip, 1 if failed | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     name = board['name'] | 
					
						
							|  |  |  |     err_count = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f1_str = "" | 
					
						
							|  |  |  |     if f1 != "": | 
					
						
							|  |  |  |         f1_str = '-f1_' + f1.replace(' ', '_') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fw_dir = f'{TINYUSB_ROOT}/cmake-build/cmake-build-{name}{f1_str}/{example}' | 
					
						
							|  |  |  |     if not os.path.exists(fw_dir): | 
					
						
							|  |  |  |         fw_dir = f'{TINYUSB_ROOT}/examples/cmake-build-{name}{f1_str}/{example}' | 
					
						
							|  |  |  |     fw_name = f'{fw_dir}/{os.path.basename(example)}' | 
					
						
							|  |  |  |     print(f'{name+f1_str:40} {example:30} ... ', end='') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not os.path.exists(fw_dir) or not (os.path.exists(f'{fw_name}.elf') or os.path.exists(f'{fw_name}.bin')): | 
					
						
							|  |  |  |         print('Skip (no binary)') | 
					
						
							|  |  |  |         return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if verbose: | 
					
						
							|  |  |  |         print(f'Flashing {fw_name}.elf') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # flash firmware. It may fail randomly, retry a few times | 
					
						
							| 
									
										
										
										
											2025-04-17 16:58:26 +07:00
										 |  |  |     max_rety = 3 | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:19 +07:00
										 |  |  |     for i in range(max_rety): | 
					
						
							|  |  |  |         ret = globals()[f'flash_{board["flasher"]["name"].lower()}'](board, fw_name) | 
					
						
							|  |  |  |         if ret.returncode == 0: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 globals()[f'test_{example.replace("/", "_")}'](board) | 
					
						
							|  |  |  |                 print('OK') | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             except Exception as e: | 
					
						
							|  |  |  |                 if i == max_rety - 1: | 
					
						
							|  |  |  |                     err_count += 1 | 
					
						
							|  |  |  |                     print(STATUS_FAILED) | 
					
						
							|  |  |  |                     print(f'  {e}') | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     print() | 
					
						
							|  |  |  |                     print(f'  Test failed: {e}, retry {i+2}/{max_rety}') | 
					
						
							|  |  |  |                     time.sleep(1) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print(f'Flashing failed, retry {i+2}/{max_rety}') | 
					
						
							|  |  |  |             time.sleep(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ret.returncode != 0: | 
					
						
							|  |  |  |         err_count += 1 | 
					
						
							|  |  |  |         print(f'Flash {STATUS_FAILED}') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return err_count | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  | def test_board(board): | 
					
						
							|  |  |  |     name = board['name'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:37:29 +07:00
										 |  |  |     flasher = board['flasher'] | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # default to all tests | 
					
						
							| 
									
										
										
										
											2024-11-13 17:01:54 +07:00
										 |  |  |     test_list = [] | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |     if 'tests' in board: | 
					
						
							|  |  |  |         board_tests = board['tests'] | 
					
						
							| 
									
										
										
										
											2024-11-13 13:08:30 +07:00
										 |  |  |         if 'device' in board_tests and board_tests['device'] == True: | 
					
						
							|  |  |  |             test_list += list(device_tests) | 
					
						
							|  |  |  |         if 'dual' in board_tests and board_tests['dual'] == True: | 
					
						
							|  |  |  |             test_list += dual_tests | 
					
						
							|  |  |  |         if 'host' in board_tests and board_tests['host'] == True: | 
					
						
							|  |  |  |             test_list += host_test | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |         if 'only' in board_tests: | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  |             test_list = board_tests['only'] | 
					
						
							| 
									
										
										
										
											2024-08-22 18:06:48 +07:00
										 |  |  |         if 'skip' in board_tests: | 
					
						
							|  |  |  |             for skip in board_tests['skip']: | 
					
						
							|  |  |  |                 if skip in test_list: | 
					
						
							|  |  |  |                     test_list.remove(skip) | 
					
						
							| 
									
										
										
										
											2024-09-18 20:27:34 +07:00
										 |  |  |                     print(f'{name:25} {skip:30} ... Skip') | 
					
						
							| 
									
										
										
										
											2024-09-30 10:58:19 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     err_count = 0 | 
					
						
							| 
									
										
										
										
											2024-10-04 17:38:57 +07:00
										 |  |  |     flags_on_list = [""] | 
					
						
							|  |  |  |     if 'build' in board and 'flags_on' in board['build']: | 
					
						
							|  |  |  |         flags_on_list = board['build']['flags_on'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for f1 in flags_on_list: | 
					
						
							| 
									
										
										
										
											2024-10-04 15:01:03 +07:00
										 |  |  |         for test in test_list: | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:19 +07:00
										 |  |  |             err_count += test_example(board, f1, test) | 
					
						
							| 
									
										
										
										
											2024-10-04 15:01:03 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:19 +07:00
										 |  |  |     # flash board_test last to disable board's usb | 
					
						
							|  |  |  |     test_example(board, flags_on_list[0], 'device/board_test') | 
					
						
							| 
									
										
										
										
											2024-11-13 18:23:58 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-12 16:39:25 +07:00
										 |  |  |     return err_count | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-13 13:16:46 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  | def main(): | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Hardware test on specified boards | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-13 19:12:26 +07:00
										 |  |  |     global verbose | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 17:59:04 +07:00
										 |  |  |     duration = time.time() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  |     parser = argparse.ArgumentParser() | 
					
						
							|  |  |  |     parser.add_argument('config_file', help='Configuration JSON file') | 
					
						
							|  |  |  |     parser.add_argument('-b', '--board', action='append', default=[], help='Boards to test, all if not specified') | 
					
						
							| 
									
										
										
										
											2024-09-13 19:12:26 +07:00
										 |  |  |     parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     config_file = args.config_file | 
					
						
							|  |  |  |     boards = args.board | 
					
						
							| 
									
										
										
										
											2024-09-13 19:12:26 +07:00
										 |  |  |     verbose = args.verbose | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-15 17:46:20 +07:00
										 |  |  |     # if config file is not found, try to find it in the same directory as this script | 
					
						
							|  |  |  |     if not os.path.exists(config_file): | 
					
						
							|  |  |  |         config_file = os.path.join(os.path.dirname(__file__), config_file) | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |     with open(config_file) as f: | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         config = json.load(f) | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  |     if len(boards) == 0: | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |         config_boards = config['boards'] | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-05-10 18:55:43 +07:00
										 |  |  |         config_boards = [e for e in config['boards'] if e['name'] in boards] | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 17:12:28 +07:00
										 |  |  |     with Pool(processes=os.cpu_count()) as pool: | 
					
						
							| 
									
										
										
										
											2024-08-28 11:43:14 +07:00
										 |  |  |         err_count = sum(pool.map(test_board, config_boards)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 17:59:04 +07:00
										 |  |  |     duration = time.time() - duration | 
					
						
							| 
									
										
										
										
											2024-08-28 11:43:14 +07:00
										 |  |  |     print() | 
					
						
							|  |  |  |     print("-" * 30) | 
					
						
							| 
									
										
										
										
											2024-09-18 17:59:04 +07:00
										 |  |  |     print(f'Total failed: {err_count} in {duration:.1f}s') | 
					
						
							| 
									
										
										
										
											2024-08-28 11:43:14 +07:00
										 |  |  |     print("-" * 30) | 
					
						
							| 
									
										
										
										
											2024-08-19 20:09:32 +07:00
										 |  |  |     sys.exit(err_count) | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |