blob: 50a765a032e19922a3c7f2a73ad150c54db351d9 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001#!/usr/bin/env python3
2
3import visa
4import time
5import sys
6
7
8def test_idn():
9 idn = inst.query("*idn?");
10 assert (idn == "TinyUSB,ModelNumber,SerialNumber,FirmwareVer123456\r\n")
11 assert (inst.is_4882_compliant)
12
13def test_echo(m,n):
14 longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 50
15 t0 = time.monotonic()
16
17 #Next try echo from 1 to 175 characters (200 is max buffer size on DUT)
18 for i in range(m,n):
19 #print(i)
20 x = longstr[0:i]
21 xt = x + inst.write_termination
22 y = inst.query(x)
23 #print(x)
24 #print (":".join("{:02x}".format(ord(c)) for c in xt))
25 #print (":".join("{:02x}".format(ord(c)) for c in y))
26 assert(xt == y), f"failed i={i}"
27 #inst.read_stb();# Just to make USB logging easier by sending a control query (bad thing is that this made things slow)
28 t = time.monotonic() - t0
29 print(f" elapsed: {t:0.3} sec")
30
31def test_trig():
32 # clear SRQ
33 inst.read_stb()
34 assert (inst.read_stb() == 0)
35 inst.assert_trigger()
36 time.sleep(0.3) # SRQ may have some delay
37 assert (inst.read_stb() & 0x40), "SRQ not set after 0.3 seconds"
38 assert (inst.read_stb() == 0)
39
40
41def test_mav():
42 inst.write("delay 50")
43 inst.read_stb() # clear STB
44 assert (inst.read_stb() == 0)
45 inst.write("123")
46 time.sleep(0.3)
47 assert (inst.read_stb() & 0x10), "MAV not set after 0.5 seconds"
48
49 rsp = inst.read()
50 assert(rsp == "123\r\n")
51
52
53def test_srq():
54 assert (inst.read_stb() == 0)
55 inst.write("123")
56
57 #inst.enable_event(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE)
58 #waitrsp = inst.wait_on_event(visa.constants.VI_EVENT_SERVICE_REQ, 5000)
59 #inst.discard_events(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE)
60 #inst.wait_for_srq()
61 time.sleep(0.3)
62 stb = inst.read_stb()
63 msg = "SRQ not set after 0.5 seconds, was {:02x}".format(stb)
64 assert (stb == 0x50),msg
65
66 assert (inst.read_stb() == 0x10), "SRQ set at second read!"
67
68 rsp = inst.read()
69 assert(rsp == "123\r\n")
70
71def test_read_timeout():
72 inst.timeout = 500
73 # First read with no MAV
74 inst.read_stb()
75 assert (inst.read_stb() == 0)
76 inst.write("delay 500")
77 t0 = time.monotonic()
78 try:
79 rsp = inst.read()
80 assert(false), "Read should have resulted in timeout"
81 except visa.VisaIOError:
82 print(" Got expected exception")
83 t = time.monotonic() - t0
84 assert ((t*1000.0) > (inst.timeout - 300))
85 assert ((t*1000.0) < (inst.timeout + 300))
86 print(f"Delay was {t:0.3}")
87 # Response is still in queue, so send a clear (to be more helpful to the next test)
88 inst.clear()
89 time.sleep(0.3)
90 assert(0 == (inst.read_stb() & 0x10)), "MAV not reset after clear"
91
92def test_abort_in():
93 inst.timeout = 200
94 # First read with no MAV
95 inst.read_stb()
96 assert (inst.read_stb() == 0)
97 inst.write("delay 500")
98 inst.write("xxx")
99 t0 = time.monotonic()
100 try:
101 rsp = inst.read()
102 assert(false), "Read should have resulted in timeout"
103 except visa.VisaIOError:
104 print(" Got expected exception")
105 t = time.monotonic() - t0
106 assert ((t*1000.0) > (inst.timeout - 300))
107 assert ((t*1000.0) < (inst.timeout + 300))
108 print(f" Delay was {t:0.3}")
109 # Response is still in queue, so send a clear (to be more helpful to the next test)
110 inst.timeout = 800
111 y = inst.read()
112 assert(y == "xxx\r\n")
113
114def test_indicate():
115 # perform indicator pulse
116 usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM)
117 retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=64, request_value=0x0000, index=usb_iface, length=0x0001)
118 assert((retv[1] == visa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}"
119
120
121def test_multi_read():
122 old_chunk_size = inst.chunk_size
123 longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 10
124 timeout = 10
125 x = longstr[0:174]
126 inst.chunk_size = 50 # Seems chunk size only applies to read but not write
127 inst.write(x)
128 # I'm not sure how to request just the remaining bit using a max count... so just read it all.
129 y = inst.read()
130 assert (x + "\r\n" == y)
131 #inst.chunk_size = old_chunk_size
132
133def test_stall_ep0():
134 usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM)
135 inst.read_stb()
136 # This is an invalid request, should create stall.
137 try:
138 retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=60, request_value=0x0000, index=usb_iface, length=0x0001)
139 assert false
140 except visa.VisaIOError:
141 pass
142
143 assert (inst.read_stb() == 0)
144
145
146rm = visa.ResourceManager()
147reslist = rm.list_resources("USB?::?*::INSTR")
148print(reslist)
149
150if (len(reslist) == 0):
151 sys.exit()
152
153inst = rm.open_resource(reslist[0]);
154inst.timeout = 3000
155
156inst.clear()
157
158print("+ IDN")
159test_idn()
160
161print("+test abort in")
162test_abort_in()
163
164
165inst.timeout = 2000
166
167print("+ multi read")
168test_multi_read()
169
170
171print("+ echo delay=0")
172inst.write("delay 0")
173test_echo(1,175)
174
175print("+ echo delay=2")
176inst.write("delay 2")
177test_echo(1,175)
178
179print("+ echo delay=150")
180inst.write("delay 150")
181test_echo(53,76)
182test_echo(165,170)
183
184print("+ Read timeout (no MAV)")
185test_read_timeout()
186
187print("+ Test EP0 stall recovery")
188test_stall_ep0()
189
190print("+ MAV")
191test_mav()
192
193print("+ SRQ")
194test_srq()
195
196print("+ indicate")
197test_indicate()
198
199print("+ TRIG")
200test_trig()
201
202# Untested:
203# abort bulk out
204# LLO, GTL, etc
205# Throughput rate?
206# Transmitting a message using multiple transfers
207
208inst.close()
209print("Test complete")