3232import subprocess
3333import json
3434
35+ ENUM_TIMEOUT = 10
3536
36- def get_serial_dev (id , product , ifnum ):
37+
38+ def get_serial_dev (id , vendor_str , product_str , ifnum ):
3739 # get usb serial by id
38- return f'/dev/serial/by-id/usb-TinyUSB_ { product } _{ id } -if{ ifnum :02d} '
40+ return f'/dev/serial/by-id/usb-{ vendor_str } _ { product_str } _{ id } -if{ ifnum :02d} '
3941
4042
41- def get_disk_dev (id , lun ):
43+ # Currently not used, left as reference
44+ def get_disk_dev (id , vendor_str , lun ):
4245 # get usb disk by id
43- return f'/dev/disk/by-id/usb-TinyUSB_Mass_Storage_{ id } -0:{ lun } '
46+ return f'/dev/disk/by-id/usb-{ vendor_str } _Mass_Storage_{ id } -0:{ lun } '
47+
48+
49+ def get_hid_dev (id , vendor_str , product_str , event ):
50+ return f'/dev/input/by-id/usb-{ vendor_str } _{ product_str } _{ id } -{ event } '
51+
52+
53+ def open_serial_dev (port ):
54+ timeout = ENUM_TIMEOUT
55+ ser = None
56+ while timeout :
57+ if os .path .exists (port ):
58+ try :
59+ # slight delay since kernel may occupy the port briefly
60+ time .sleep (0.2 )
61+ ser = serial .Serial (port , timeout = 1 )
62+ break
63+ except serial .SerialException :
64+ pass
65+ time .sleep (0.8 )
66+ timeout = timeout - 1
67+ assert timeout , 'Device not available or Cannot open port'
68+ return ser
69+
70+
71+ def read_disk_file (id , fname ):
72+ # on different self-hosted, the mount point is different
73+ file_list = [
74+ f'/media/blkUSB_{ id [- 8 :]} .02/{ fname } ' ,
75+ f'/media/{ os .getenv ("USER" )} /TinyUSB MSC/{ fname } '
76+ ]
77+ timeout = ENUM_TIMEOUT
78+ while timeout :
79+ for file in file_list :
80+ if os .path .isfile (file ):
81+ with open (file , 'rb' ) as f :
82+ data = f .read ()
83+ return data
4484
85+ time .sleep (1 )
86+ timeout = timeout - 1
4587
46- def get_hid_dev ( id , product , event ):
47- return f'/dev/input/by-id/usb-TinyUSB_ { product } _ { id } - { event } '
88+ assert timeout , 'Device not available'
89+ return None
4890
4991
92+ # -------------------------------------------------------------
93+ # Flash with debugger
94+ # -------------------------------------------------------------
5095def flash_jlink (sn , dev , firmware ):
5196 script = ['halt' , 'r' , f'loadfile { firmware } ' , 'r' , 'go' , 'exit' ]
5297 f = open ('flash.jlink' , 'w' )
@@ -59,31 +104,29 @@ def flash_jlink(sn, dev, firmware):
59104 assert ret .returncode == 0 , 'Flash failed\n ' + stdout
60105
61106
107+ def flash_openocd (sn , args , firmware ):
108+ ret = subprocess .run (f'openocd { args } -c "program { firmware } reset exit"' ,
109+ shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
110+ stdout = ret .stdout .decode ()
111+ assert ret .returncode == 0 , 'Flash failed\n ' + stdout
112+
113+
114+ # -------------------------------------------------------------
115+ # Tests
116+ # -------------------------------------------------------------
62117def test_board_test (id ):
63118 # Dummy test
64119 pass
65120
66- def test_cdc_dual_ports (id ):
67- port1 = get_serial_dev (id , "TinyUSB_Device" , 0 )
68- port2 = get_serial_dev (id , "TinyUSB_Device" , 2 )
69121
70- # Wait device enum
71- timeout = 10
72- while timeout :
73- if os .path .exists (port1 ) and os .path .exists (port2 ):
74- break
75- time .sleep (1 )
76- timeout = timeout - 1
122+ def test_cdc_dual_ports (id ):
123+ port1 = get_serial_dev (id , 'TinyUSB' , "TinyUSB_Device" , 0 )
124+ port2 = get_serial_dev (id , 'TinyUSB' , "TinyUSB_Device" , 2 )
77125
78- assert timeout , 'Device not available'
126+ ser1 = open_serial_dev (port1 )
127+ ser2 = open_serial_dev (port2 )
79128
80129 # Echo test
81- ser1 = serial .Serial (port1 )
82- ser2 = serial .Serial (port2 )
83-
84- ser1 .timeout = 1
85- ser2 .timeout = 1
86-
87130 str1 = b"test_no1"
88131 ser1 .write (str1 )
89132 ser1 .flush ()
@@ -98,42 +141,28 @@ def test_cdc_dual_ports(id):
98141
99142
100143def test_cdc_msc (id ):
101- port = get_serial_dev (id , "TinyUSB_Device" , 0 )
102- file = f'/media/blkUSB_{ id [- 8 :]} .02/README.TXT'
103- # Wait device enum
104- timeout = 10
105- while timeout :
106- if os .path .exists (port ) and os .path .isfile (file ):
107- break
108- time .sleep (1 )
109- timeout = timeout - 1
110-
111- assert timeout , 'Device not available'
112-
113144 # Echo test
114- ser1 = serial .Serial (port )
115-
116- ser1 .timeout = 1
145+ port = get_serial_dev (id , 'TinyUSB' , "TinyUSB_Device" , 0 )
146+ ser = open_serial_dev (port )
117147
118148 str = b"test_str"
119- ser1 .write (str )
120- ser1 .flush ()
121- assert ser1 .read (100 ) == str , 'CDC wrong data'
149+ ser .write (str )
150+ ser .flush ()
151+ assert ser .read (100 ) == str , 'CDC wrong data'
122152
123153 # Block test
124- f = open (file , 'rb' )
125- data = f .read ()
126-
154+ data = read_disk_file (id , 'README.TXT' )
127155 readme = \
128156 b"This is tinyusb's MassStorage Class demo.\r \n \r \n \
129157 If you find any bugs or get any questions, feel free to file an\r \n \
130158 issue at github.com/hathach/tinyusb"
131159
132160 assert data == readme , 'MSC wrong data'
133161
162+
134163def test_dfu (id ):
135164 # Wait device enum
136- timeout = 10
165+ timeout = ENUM_TIMEOUT
137166 while timeout :
138167 ret = subprocess .run (f'dfu-util -l' ,
139168 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
@@ -172,7 +201,7 @@ def test_dfu(id):
172201
173202def test_dfu_runtime (id ):
174203 # Wait device enum
175- timeout = 10
204+ timeout = ENUM_TIMEOUT
176205 while timeout :
177206 ret = subprocess .run (f'dfu-util -l' ,
178207 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
@@ -186,11 +215,11 @@ def test_dfu_runtime(id):
186215
187216
188217def test_hid_boot_interface (id ):
189- kbd = get_hid_dev (id , 'TinyUSB_Device' , 'event-kbd' )
190- mouse1 = get_hid_dev (id , 'TinyUSB_Device' , 'if01-event-mouse' )
191- mouse2 = get_hid_dev (id , 'TinyUSB_Device' , 'if01-mouse' )
218+ kbd = get_hid_dev (id , 'TinyUSB' , ' TinyUSB_Device' , 'event-kbd' )
219+ mouse1 = get_hid_dev (id , 'TinyUSB' , ' TinyUSB_Device' , 'if01-event-mouse' )
220+ mouse2 = get_hid_dev (id , 'TinyUSB' , ' TinyUSB_Device' , 'if01-mouse' )
192221 # Wait device enum
193- timeout = 10
222+ timeout = ENUM_TIMEOUT
194223 while timeout :
195224 if os .path .exists (kbd ) and os .path .exists (mouse1 ) and os .path .exists (mouse2 ):
196225 break
@@ -211,11 +240,13 @@ def test_hid_boot_interface(id):
211240
212241 # all possible tests, board_test is last to disable board's usb
213242 all_tests = [
214- 'cdc_dual_ports' , 'cdc_msc' , 'dfu' , 'dfu_runtime' , 'hid_boot_interface' , 'board_test'
243+ 'cdc_dual_ports' , 'cdc_msc' , 'dfu' , 'dfu_runtime' , 'hid_boot_interface' ,
244+ 'board_test'
215245 ]
216246
217247 for board in config ['boards' ]:
218248 print (f'Testing board:{ board ["name" ]} ' )
249+ debugger = board ['debugger' ].lower ()
219250
220251 # default to all tests
221252 if 'tests' in board :
@@ -230,18 +261,27 @@ def test_hid_boot_interface(id):
230261 test_list .remove (skip )
231262
232263 for test in test_list :
233- mk_elf = f'examples/device/{ test } /_build/{ board ["name" ]} /{ test } .elf'
234- cmake_elf = f'cmake-build/cmake-build-{ board ["name" ]} /device/{ test } /{ test } .elf'
235- if os .path .isfile (cmake_elf ):
236- elf = cmake_elf
237- elif os .path .isfile (mk_elf ):
238- elf = mk_elf
239- else :
264+ # cmake, make, download from artifacts
265+ elf_list = [
266+ f'cmake-build/cmake-build-{ board ["name" ]} /device/{ test } /{ test } .elf' ,
267+ f'examples/device/{ test } /_build/{ board ["name" ]} /{ test } .elf' ,
268+ f'{ test } .elf'
269+ ]
270+
271+ elf = None
272+ for e in elf_list :
273+ if os .path .isfile (e ):
274+ elf = e
275+ break
276+
277+ if elf is None :
240278 print (f'Cannot find firmware file for { test } ' )
241279 sys .exit (- 1 )
242280
243- if board [ ' debugger' ]. lower () == 'jlink' :
281+ if debugger == 'jlink' :
244282 flash_jlink (board ['debugger_sn' ], board ['cpu' ], elf )
283+ elif debugger == 'openocd' :
284+ flash_openocd (board ['debugger_sn' ], board ['debugger_args' ], elf )
245285 else :
246286 # ToDo
247287 pass
0 commit comments