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
4143# Currently not used, left as reference
42- def get_disk_dev (id , lun ):
44+ def get_disk_dev (id , vendor_str , lun ):
4345 # get usb disk by id
44- 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
4584
85+ time .sleep (1 )
86+ timeout = timeout - 1
4687
47- def get_hid_dev ( id , product , event ):
48- return f'/dev/input/by-id/usb-TinyUSB_ { product } _ { id } - { event } '
88+ assert timeout , 'Device not available'
89+ return None
4990
5091
92+ # -------------------------------------------------------------
93+ # Flash with debugger
94+ # -------------------------------------------------------------
5195def flash_jlink (sn , dev , firmware ):
5296 script = ['halt' , 'r' , f'loadfile { firmware } ' , 'r' , 'go' , 'exit' ]
5397 f = open ('flash.jlink' , 'w' )
@@ -61,7 +105,6 @@ def flash_jlink(sn, dev, firmware):
61105
62106
63107def flash_openocd (sn , args , firmware ):
64-
65108 ret = subprocess .run (f'openocd { args } -c "program { firmware } reset exit"' ,
66109 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
67110 stdout = ret .stdout .decode ()
@@ -77,26 +120,13 @@ def test_board_test(id):
77120
78121
79122def test_cdc_dual_ports (id ):
80- port1 = get_serial_dev (id , "TinyUSB_Device" , 0 )
81- port2 = get_serial_dev (id , "TinyUSB_Device" , 2 )
123+ port1 = get_serial_dev (id , 'TinyUSB' , "TinyUSB_Device" , 0 )
124+ port2 = get_serial_dev (id , 'TinyUSB' , "TinyUSB_Device" , 2 )
82125
83- # Wait device enum
84- timeout = 10
85- while timeout :
86- if os .path .exists (port1 ) and os .path .exists (port2 ):
87- break
88- time .sleep (1 )
89- timeout = timeout - 1
90-
91- assert timeout , 'Device not available'
126+ ser1 = open_serial_dev (port1 )
127+ ser2 = open_serial_dev (port2 )
92128
93129 # Echo test
94- ser1 = serial .Serial (port1 )
95- ser2 = serial .Serial (port2 )
96-
97- ser1 .timeout = 1
98- ser2 .timeout = 1
99-
100130 str1 = b"test_no1"
101131 ser1 .write (str1 )
102132 ser1 .flush ()
@@ -111,38 +141,17 @@ def test_cdc_dual_ports(id):
111141
112142
113143def test_cdc_msc (id ):
114- port = get_serial_dev (id , "TinyUSB_Device" , 0 )
115- file_list = [f'/media/blkUSB_{ id [- 8 :]} .02/README.TXT' , f'/media/{ os .getenv ("USER" )} /TinyUSB MSC/README.TXT' ]
116- # Wait device enum
117- timeout = 10
118- while timeout :
119- f_found = False
120- if os .path .exists (port ):
121- for file in file_list :
122- if os .path .isfile (file ):
123- f_found = True
124- f = open (file , 'rb' )
125- data = f .read ()
126- break
127-
128- if f_found :
129- break
130-
131- time .sleep (1 )
132- timeout = timeout - 1
133-
134- assert timeout , 'Device not available'
135-
136144 # Echo test
137- ser1 = serial . Serial ( port )
138- ser1 . timeout = 1
145+ port = get_serial_dev ( id , 'TinyUSB' , "TinyUSB_Device" , 0 )
146+ ser = open_serial_dev ( port )
139147
140148 str = b"test_str"
141- ser1 .write (str )
142- ser1 .flush ()
143- assert ser1 .read (100 ) == str , 'CDC wrong data'
149+ ser .write (str )
150+ ser .flush ()
151+ assert ser .read (100 ) == str , 'CDC wrong data'
144152
145153 # Block test
154+ data = read_disk_file (id , 'README.TXT' )
146155 readme = \
147156 b"This is tinyusb's MassStorage Class demo.\r \n \r \n \
148157 If you find any bugs or get any questions, feel free to file an\r \n \
@@ -153,7 +162,7 @@ def test_cdc_msc(id):
153162
154163def test_dfu (id ):
155164 # Wait device enum
156- timeout = 10
165+ timeout = ENUM_TIMEOUT
157166 while timeout :
158167 ret = subprocess .run (f'dfu-util -l' ,
159168 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
@@ -192,7 +201,7 @@ def test_dfu(id):
192201
193202def test_dfu_runtime (id ):
194203 # Wait device enum
195- timeout = 10
204+ timeout = ENUM_TIMEOUT
196205 while timeout :
197206 ret = subprocess .run (f'dfu-util -l' ,
198207 shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
@@ -206,11 +215,11 @@ def test_dfu_runtime(id):
206215
207216
208217def test_hid_boot_interface (id ):
209- kbd = get_hid_dev (id , 'TinyUSB_Device' , 'event-kbd' )
210- mouse1 = get_hid_dev (id , 'TinyUSB_Device' , 'if01-event-mouse' )
211- 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' )
212221 # Wait device enum
213- timeout = 10
222+ timeout = ENUM_TIMEOUT
214223 while timeout :
215224 if os .path .exists (kbd ) and os .path .exists (mouse1 ) and os .path .exists (mouse2 ):
216225 break
0 commit comments