| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import time | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  | import click | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | import serial | 
					
						
							|  |  |  | import subprocess | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | import glob | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-13 19:18:08 +07:00
										 |  |  | # for RPI double reset | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     import gpiozero | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | ENUM_TIMEOUT = 10 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +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 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         pattern = f'/dev/serial/by-id/usb-*_{id}-if{ifnum:02d}*' | 
					
						
							|  |  |  |         port_list = glob.glob(pattern) | 
					
						
							|  |  |  |         return port_list[0] | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | # Currently not used, left as reference | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  | def get_disk_dev(id, vendor_str, lun): | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |     # get usb disk by id | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     return f'/dev/disk/by-id/usb-{vendor_str}_Mass_Storage_{id}-0:{lun}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_hid_dev(id, vendor_str, product_str, event): | 
					
						
							|  |  |  |     return f'/dev/input/by-id/usb-{vendor_str}_{product_str}_{id}-{event}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def open_serial_dev(port): | 
					
						
							|  |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							|  |  |  |     ser = None | 
					
						
							|  |  |  |     while timeout: | 
					
						
							|  |  |  |         if os.path.exists(port): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 # slight delay since kernel may occupy the port briefly | 
					
						
							| 
									
										
										
										
											2023-11-29 18:11:50 +07:00
										 |  |  |                 time.sleep(0.5) | 
					
						
							|  |  |  |                 timeout = timeout - 0.5 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |                 ser = serial.Serial(port, timeout=1) | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             except serial.SerialException: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2023-11-29 18:11:50 +07:00
										 |  |  |         time.sleep(0.5) | 
					
						
							|  |  |  |         timeout = timeout - 0.5 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     assert timeout, 'Device not available' | 
					
						
							|  |  |  |     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
										 |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def flash_jlink(board, firmware): | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  |     script = ['halt', 'r', f'loadfile {firmware}', 'r', 'go', 'exit'] | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     with open('flash.jlink', 'w') as f: | 
					
						
							|  |  |  |         f.writelines(f'{s}\n' for s in script) | 
					
						
							|  |  |  |     ret = subprocess.run( | 
					
						
							| 
									
										
										
										
											2023-12-11 15:23:09 +07:00
										 |  |  |         f'JLinkExe -USB {board["flasher_sn"]} {board["flasher_args"]} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile flash.jlink', | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  |     os.remove('flash.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def flash_openocd(board, firmware): | 
					
						
							|  |  |  |     ret = subprocess.run( | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  |         f'openocd -c "adapter serial {board["flasher_sn"]}" {board["flasher_args"]} -c "program {firmware} reset exit"', | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							|  |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def flash_esptool(board, firmware): | 
					
						
							| 
									
										
										
										
											2023-12-11 15:23:09 +07:00
										 |  |  |     port = get_serial_dev(board["flasher_sn"], None, None, 0) | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     dir = os.path.dirname(firmware) | 
					
						
							|  |  |  |     with open(f'{dir}/config.env') as f: | 
					
						
							|  |  |  |         IDF_TARGET = json.load(f)['IDF_TARGET'] | 
					
						
							|  |  |  |     with open(f'{dir}/flash_args') as f: | 
					
						
							|  |  |  |         flash_args = f.read().strip().replace('\n', ' ') | 
					
						
							| 
									
										
										
										
											2023-12-11 15:23:09 +07:00
										 |  |  |     command = (f'esptool.py --chip {IDF_TARGET} -p {port} {board["flasher_args"]} ' | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |                f'--before=default_reset --after=hard_reset write_flash {flash_args}') | 
					
						
							|  |  |  |     ret = subprocess.run(command, shell=True, cwd=dir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							|  |  |  |     return ret | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  | def doublereset_with_rpi_gpio(board): | 
					
						
							|  |  |  |     # Off = 0 = Reset | 
					
						
							| 
									
										
										
										
											2023-12-13 19:18:08 +07:00
										 |  |  |     led = gpiozero.LED(board["flasher_reset_pin"]) | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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): | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             time.sleep(0.5) | 
					
						
							|  |  |  |             timeout = timeout - 0.5 | 
					
						
							| 
									
										
										
										
											2023-12-13 20:16:47 +07:00
										 |  |  |     assert timeout, 'bossac bootloader is not available' | 
					
						
							|  |  |  |     # sleep a bit more for bootloader to be ready | 
					
						
							|  |  |  |     time.sleep(0.5) | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | # Tests | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | def test_board_test(id): | 
					
						
							|  |  |  |     # Dummy test | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | def test_cdc_dual_ports(id): | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     port1 = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 0) | 
					
						
							|  |  |  |     port2 = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 2) | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     ser1 = open_serial_dev(port1) | 
					
						
							|  |  |  |     ser2 = open_serial_dev(port2) | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     # Echo test | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  |     str1 = b"test_no1" | 
					
						
							|  |  |  |     ser1.write(str1) | 
					
						
							|  |  |  |     ser1.flush() | 
					
						
							|  |  |  |     assert ser1.read(100) == str1.lower(), 'Port1 wrong data' | 
					
						
							|  |  |  |     assert ser2.read(100) == str1.upper(), 'Port2 wrong data' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     str2 = b"test_no2" | 
					
						
							|  |  |  |     ser2.write(str2) | 
					
						
							|  |  |  |     ser2.flush() | 
					
						
							|  |  |  |     assert ser1.read(100) == str2.lower(), 'Port1 wrong data' | 
					
						
							|  |  |  |     assert ser2.read(100) == str2.upper(), 'Port2 wrong data' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | def test_cdc_msc(id): | 
					
						
							|  |  |  |     # Echo test | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     port = get_serial_dev(id, 'TinyUSB', "TinyUSB_Device", 0) | 
					
						
							|  |  |  |     ser = open_serial_dev(port) | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     str = b"test_str" | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     ser.write(str) | 
					
						
							|  |  |  |     ser.flush() | 
					
						
							|  |  |  |     assert ser.read(100) == str, 'CDC wrong data' | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Block test | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     data = read_disk_file(id, '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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def test_cdc_msc_freertos(id): | 
					
						
							|  |  |  |     test_cdc_msc(id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | def test_dfu(id): | 
					
						
							|  |  |  |     # Wait device enum | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     while timeout: | 
					
						
							|  |  |  |         ret = subprocess.run(f'dfu-util -l', | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |                              shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         stdout = ret.stdout.decode() | 
					
						
							|  |  |  |         if f'serial="{id}"' in stdout and 'Found DFU: [cafe:4000]' in stdout: | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     assert timeout, 'Device not available' | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     # Test upload | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  |         os.remove('dfu0') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         os.remove('dfu1') | 
					
						
							|  |  |  |     except OSError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = subprocess.run(f'dfu-util -S {id} -a 0 -U dfu0', | 
					
						
							|  |  |  |                          shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							|  |  |  |     assert ret.returncode == 0, 'Upload failed' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = subprocess.run(f'dfu-util -S {id} -a 1 -U dfu1', | 
					
						
							|  |  |  |                          shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							|  |  |  |     assert ret.returncode == 0, 'Upload failed' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with open('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
										 |  |  | 
 | 
					
						
							|  |  |  |     with open('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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  |     os.remove('dfu0') | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     os.remove('dfu1') | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | def test_dfu_runtime(id): | 
					
						
							| 
									
										
										
										
											2023-08-08 17:34:07 +02:00
										 |  |  |     # Wait device enum | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     while timeout: | 
					
						
							|  |  |  |         ret = subprocess.run(f'dfu-util -l', | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |                              shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         stdout = ret.stdout.decode() | 
					
						
							|  |  |  |         if f'serial="{id}"' in stdout and 'Found Runtime: [cafe:4000]' in stdout: | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |     assert timeout, 'Device not available' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 13:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 22:08:59 +02:00
										 |  |  | def test_hid_boot_interface(id): | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     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') | 
					
						
							| 
									
										
										
										
											2023-08-08 22:08:59 +02:00
										 |  |  |     # Wait device enum | 
					
						
							| 
									
										
										
										
											2023-11-29 12:00:52 +07:00
										 |  |  |     timeout = ENUM_TIMEOUT | 
					
						
							| 
									
										
										
										
											2023-08-08 22:08:59 +02:00
										 |  |  |     while timeout: | 
					
						
							|  |  |  |         if os.path.exists(kbd) and os.path.exists(mouse1) and os.path.exists(mouse2): | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         time.sleep(1) | 
					
						
							|  |  |  |         timeout = timeout - 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert timeout, 'Device not available' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | def test_hid_composite_freertos(id): | 
					
						
							|  |  |  |     # TODO implement later | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							|  |  |  | # Main | 
					
						
							|  |  |  | # ------------------------------------------------------------- | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  | @click.command() | 
					
						
							|  |  |  | @click.argument('config_file') | 
					
						
							|  |  |  | @click.option('-b', '--board', multiple=True, default=None, help='Boards to test, all if not specified') | 
					
						
							|  |  |  | def main(config_file, board): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Hardware test on specified boards | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     config_file = os.path.join(os.path.dirname(__file__), config_file) | 
					
						
							|  |  |  |     with open(config_file) as f: | 
					
						
							| 
									
										
										
										
											2023-08-06 15:59:25 +02:00
										 |  |  |         config = json.load(f) | 
					
						
							| 
									
										
										
										
											2023-08-06 16:30:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |     # all possible tests | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |     all_tests = [ | 
					
						
							| 
									
										
										
										
											2023-11-28 18:27:40 +07:00
										 |  |  |         'cdc_dual_ports', 'cdc_msc', 'dfu', 'dfu_runtime', 'hid_boot_interface', | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |     if len(board) == 0: | 
					
						
							|  |  |  |         config_boards = config['boards'] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         config_boards = [e for e in config['boards'] if e['name'] in board] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for item in config_boards: | 
					
						
							|  |  |  |         print(f'Testing board:{item["name"]}') | 
					
						
							| 
									
										
										
										
											2023-12-11 15:23:09 +07:00
										 |  |  |         flasher = item['flasher'].lower() | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # default to all tests | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |         if 'tests' in item: | 
					
						
							|  |  |  |             test_list = item['tests'] | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |         else: | 
					
						
							|  |  |  |             test_list = all_tests | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |         # board_test is added last to disable board's usb | 
					
						
							|  |  |  |         test_list.append('board_test') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |         # remove skip_tests | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |         if 'tests_skip' in item: | 
					
						
							|  |  |  |             for skip in item['tests_skip']: | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |                 if skip in test_list: | 
					
						
							|  |  |  |                     test_list.remove(skip) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for test in test_list: | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |             fw_list = [ | 
					
						
							| 
									
										
										
										
											2023-12-13 18:54:00 +07:00
										 |  |  |                 # cmake: esp32 & samd51 use .bin file | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |                 f'cmake-build/cmake-build-{item["name"]}/device/{test}/{test}.elf', | 
					
						
							| 
									
										
										
										
											2023-12-11 14:52:21 +07:00
										 |  |  |                 f'cmake-build/cmake-build-{item["name"]}/device/{test}/{test}.bin', | 
					
						
							|  |  |  |                 # make | 
					
						
							| 
									
										
										
										
											2023-12-11 14:43:54 +07:00
										 |  |  |                 f'examples/device/{test}/_build/{item["name"]}/{test}.elf' | 
					
						
							| 
									
										
										
										
											2023-11-28 19:10:19 +07:00
										 |  |  |             ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |             fw = None | 
					
						
							|  |  |  |             for f in fw_list: | 
					
						
							|  |  |  |                 if os.path.isfile(f): | 
					
						
							|  |  |  |                     fw = f | 
					
						
							| 
									
										
										
										
											2023-11-28 19:10:19 +07:00
										 |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |             if fw is None: | 
					
						
							|  |  |  |                 print(f'Cannot find binary file for {test}') | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |                 sys.exit(-1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             print(f'  {test} ...', end='') | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 00:43:23 +07:00
										 |  |  |             # flash firmware. It may fail randomly, retry a few times | 
					
						
							|  |  |  |             for i in range(3): | 
					
						
							|  |  |  |                 ret = globals()[f'flash_{flasher}'](item, fw) | 
					
						
							|  |  |  |                 if ret.returncode == 0: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2024-01-24 12:10:17 +07:00
										 |  |  |                     print(f'Flashing failed, retry {i+1}') | 
					
						
							| 
									
										
										
										
											2024-01-24 00:43:23 +07:00
										 |  |  |                     time.sleep(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  |             assert ret.returncode == 0, 'Flash failed\n' + ret.stdout.decode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # run test | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  |             globals()[f'test_{test}'](item['uid']) | 
					
						
							| 
									
										
										
										
											2023-11-29 17:09:52 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 18:46:23 +07:00
										 |  |  |             print('OK') | 
					
						
							| 
									
										
										
										
											2023-12-11 14:29:11 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     main() |