Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import visa |
| 4 | import time |
| 5 | import sys |
| 6 | |
| 7 | |
| 8 | def test_idn(): |
| 9 | idn = inst.query("*idn?"); |
| 10 | assert (idn == "TinyUSB,ModelNumber,SerialNumber,FirmwareVer123456\r\n") |
| 11 | assert (inst.is_4882_compliant) |
| 12 | |
| 13 | def 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 | |
| 31 | def 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 | |
| 41 | def 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 | |
| 53 | def 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 | |
| 71 | def 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 | |
| 92 | def 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 | |
| 114 | def 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 | |
| 121 | def 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 | |
| 133 | def 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 | |
| 146 | rm = visa.ResourceManager() |
| 147 | reslist = rm.list_resources("USB?::?*::INSTR") |
| 148 | print(reslist) |
| 149 | |
| 150 | if (len(reslist) == 0): |
| 151 | sys.exit() |
| 152 | |
| 153 | inst = rm.open_resource(reslist[0]); |
| 154 | inst.timeout = 3000 |
| 155 | |
| 156 | inst.clear() |
| 157 | |
| 158 | print("+ IDN") |
| 159 | test_idn() |
| 160 | |
| 161 | print("+test abort in") |
| 162 | test_abort_in() |
| 163 | |
| 164 | |
| 165 | inst.timeout = 2000 |
| 166 | |
| 167 | print("+ multi read") |
| 168 | test_multi_read() |
| 169 | |
| 170 | |
| 171 | print("+ echo delay=0") |
| 172 | inst.write("delay 0") |
| 173 | test_echo(1,175) |
| 174 | |
| 175 | print("+ echo delay=2") |
| 176 | inst.write("delay 2") |
| 177 | test_echo(1,175) |
| 178 | |
| 179 | print("+ echo delay=150") |
| 180 | inst.write("delay 150") |
| 181 | test_echo(53,76) |
| 182 | test_echo(165,170) |
| 183 | |
| 184 | print("+ Read timeout (no MAV)") |
| 185 | test_read_timeout() |
| 186 | |
| 187 | print("+ Test EP0 stall recovery") |
| 188 | test_stall_ep0() |
| 189 | |
| 190 | print("+ MAV") |
| 191 | test_mav() |
| 192 | |
| 193 | print("+ SRQ") |
| 194 | test_srq() |
| 195 | |
| 196 | print("+ indicate") |
| 197 | test_indicate() |
| 198 | |
| 199 | print("+ TRIG") |
| 200 | test_trig() |
| 201 | |
| 202 | # Untested: |
| 203 | # abort bulk out |
| 204 | # LLO, GTL, etc |
| 205 | # Throughput rate? |
| 206 | # Transmitting a message using multiple transfers |
| 207 | |
| 208 | inst.close() |
| 209 | print("Test complete") |