2828import os
2929import sys
3030import time
31+ import click
3132import serial
3233import subprocess
3334import json
@@ -42,7 +43,7 @@ def get_serial_dev(id, vendor_str, product_str, ifnum):
4243 # known vendor and product
4344 return f'/dev/serial/by-id/usb-{ vendor_str } _{ product_str } _{ id } -if{ ifnum :02d} '
4445 else :
45- # just use id: mostly for cp210x/ftdi debugger
46+ # just use id: mostly for cp210x/ftdi flasher
4647 pattern = f'/dev/serial/by-id/usb-*_{ id } -if{ ifnum :02d} *'
4748 port_list = glob .glob (pattern )
4849 return port_list [0 ]
@@ -99,34 +100,34 @@ def read_disk_file(id, fname):
99100
100101
101102# -------------------------------------------------------------
102- # Flash with debugger
103+ # Flashing firmware
103104# -------------------------------------------------------------
104105def flash_jlink (board , firmware ):
105106 script = ['halt' , 'r' , f'loadfile { firmware } ' , 'r' , 'go' , 'exit' ]
106107 with open ('flash.jlink' , 'w' ) as f :
107108 f .writelines (f'{ s } \n ' for s in script )
108109 ret = subprocess .run (
109- f'JLinkExe -USB { board ["debugger_sn " ]} -device { board ["cpu " ]} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile flash.jlink' ,
110+ f'JLinkExe -USB { board ["flasher_sn " ]} { board ["flasher_args " ]} -if swd -JTAGConf -1,-1 -speed auto -NoGui 1 -ExitOnError 1 -CommandFile flash.jlink' ,
110111 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
111112 os .remove ('flash.jlink' )
112113 return ret
113114
114115
115116def flash_openocd (board , firmware ):
116117 ret = subprocess .run (
117- f'openocd -c "adapter serial { board ["debugger_sn " ]} " { board ["debugger_args " ]} -c "program { firmware } reset exit"' ,
118+ f'openocd -c "adapter serial { board ["flasher_sn " ]} " { board ["flasher_args " ]} -c "program { firmware } reset exit"' ,
118119 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
119120 return ret
120121
121122
122123def flash_esptool (board , firmware ):
123- port = get_serial_dev (board ["debugger_sn " ], None , None , 0 )
124+ port = get_serial_dev (board ["flasher_sn " ], None , None , 0 )
124125 dir = os .path .dirname (firmware )
125126 with open (f'{ dir } /config.env' ) as f :
126127 IDF_TARGET = json .load (f )['IDF_TARGET' ]
127128 with open (f'{ dir } /flash_args' ) as f :
128129 flash_args = f .read ().strip ().replace ('\n ' , ' ' )
129- command = (f'esptool.py --chip { IDF_TARGET } -p { port } { board ["debugger_args " ]} '
130+ command = (f'esptool.py --chip { IDF_TARGET } -p { port } { board ["flasher_args " ]} '
130131 f'--before=default_reset --after=hard_reset write_flash { flash_args } ' )
131132 ret = subprocess .run (command , shell = True , cwd = dir , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
132133 return ret
@@ -262,64 +263,76 @@ def test_hid_composite_freertos(id):
262263# -------------------------------------------------------------
263264# Main
264265# -------------------------------------------------------------
265- if __name__ == '__main__' :
266- if len (sys .argv ) != 2 :
267- print ('Usage:' )
268- print ('python hitl_test.py config.json' )
269- sys .exit (- 1 )
270-
271- with open (f'{ os .path .dirname (__file__ )} /{ sys .argv [1 ]} ' ) as f :
266+ @click .command ()
267+ @click .argument ('config_file' )
268+ @click .option ('-b' , '--board' , multiple = True , default = None , help = 'Boards to test, all if not specified' )
269+ def main (config_file , board ):
270+ """
271+ Hardware test on specified boards
272+ """
273+ config_file = os .path .join (os .path .dirname (__file__ ), config_file )
274+ with open (config_file ) as f :
272275 config = json .load (f )
273276
274277 # all possible tests
275278 all_tests = [
276279 'cdc_dual_ports' , 'cdc_msc' , 'dfu' , 'dfu_runtime' , 'hid_boot_interface' ,
277280 ]
278281
279- for board in config ['boards' ]:
280- print (f'Testing board:{ board ["name" ]} ' )
281- debugger = board ['debugger' ].lower ()
282+ if len (board ) == 0 :
283+ config_boards = config ['boards' ]
284+ else :
285+ config_boards = [e for e in config ['boards' ] if e ['name' ] in board ]
286+
287+ for item in config_boards :
288+ print (f'Testing board:{ item ["name" ]} ' )
289+ flasher = item ['flasher' ].lower ()
282290
283291 # default to all tests
284- if 'tests' in board :
285- test_list = board ['tests' ]
292+ if 'tests' in item :
293+ test_list = item ['tests' ]
286294 else :
287295 test_list = all_tests
288296
289297 # board_test is added last to disable board's usb
290298 test_list .append ('board_test' )
291299
292300 # remove skip_tests
293- if 'tests_skip' in board :
294- for skip in board ['tests_skip' ]:
301+ if 'tests_skip' in item :
302+ for skip in item ['tests_skip' ]:
295303 if skip in test_list :
296304 test_list .remove (skip )
297305
298306 for test in test_list :
299- # cmake, make, download from artifacts
300- elf_list = [
301- f'cmake-build/cmake-build-{ board ["name" ]} /device/{ test } /{ test } .elf' ,
302- f'examples/device/{ test } /_build/{ board ["name" ]} /{ test } .elf' ,
303- f'{ test } .elf'
307+ fw_list = [
308+ # cmake: esp32 use .bin file
309+ f'cmake-build/cmake-build-{ item ["name" ]} /device/{ test } /{ test } .elf' ,
310+ f'cmake-build/cmake-build-{ item ["name" ]} /device/{ test } /{ test } .bin' ,
311+ # make
312+ f'examples/device/{ test } /_build/{ item ["name" ]} /{ test } .elf'
304313 ]
305314
306- elf = None
307- for e in elf_list :
308- if os .path .isfile (e ):
309- elf = e
315+ fw = None
316+ for f in fw_list :
317+ if os .path .isfile (f ):
318+ fw = f
310319 break
311320
312- if elf is None :
313- print (f'Cannot find firmware file for { test } ' )
321+ if fw is None :
322+ print (f'Cannot find binary file for { test } ' )
314323 sys .exit (- 1 )
315324
316325 print (f' { test } ...' , end = '' )
317326
318327 # flash firmware
319- ret = locals ()[f'flash_{ debugger } ' ](board , elf )
328+ ret = globals ()[f'flash_{ flasher } ' ](item , fw )
320329 assert ret .returncode == 0 , 'Flash failed\n ' + ret .stdout .decode ()
321330
322331 # run test
323- locals ()[f'test_{ test } ' ](board ['uid' ])
332+ globals ()[f'test_{ test } ' ](item ['uid' ])
324333
325334 print ('OK' )
335+
336+
337+ if __name__ == '__main__' :
338+ main ()
0 commit comments