-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
1328 lines (1157 loc) · 48.4 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from apollo_fpga.gateware.flash_bridge import FlashBridgeConnection
from apollo_fpga.ecp5 import ECP5FlashBridgeProgrammer
from apollo_fpga import ApolloDebugger
from formatting import *
from errors import *
from ranges import *
from tycho import *
from eut import *
from selftest import *
from time import time, sleep
import state
import usb1
import os
import pickle
import subprocess
from greatfet import GreatFET
from tps55288 import TPS55288, CDC
context = usb1.USBContext()
vbus_registers = {
'CONTROL': REGISTER_CON_VBUS_EN,
'AUX': REGISTER_AUX_VBUS_EN,
}
passthrough_registers = {
'CONTROL': REGISTER_PASS_CONTROL,
'AUX': REGISTER_PASS_AUX,
'TARGET-C': REGISTER_PASS_TARGET_C,
}
typec_registers = {
'AUX': (REGISTER_AUX_TYPEC_CTL_ADDR, REGISTER_AUX_TYPEC_CTL_VALUE),
'TARGET-C': (REGISTER_TARGET_TYPEC_CTL_ADDR, REGISTER_TARGET_TYPEC_CTL_VALUE),
}
sbu_registers = {
'AUX': REGISTER_AUX_SBU,
'TARGET-C': REGISTER_TARGET_SBU
}
mon_voltage_registers = {
'CONTROL': 0x0A,
'AUX': 0x09,
'TARGET-C': 0x08,
'TARGET-A': 0x07,
}
mon_current_registers = {
'CONTROL': 0x0E,
'AUX': 0x0D,
'TARGET-C': 0x0C,
'TARGET-A': 0x0B,
}
phy_registers = {
'CONTROL': REGISTER_CONTROL_ADDR,
'AUX': REGISTER_AUX_ADDR,
'TARGET': REGISTER_TARGET_ADDR,
}
class Pin:
def __init__(self, inner):
self.inner = inner
def high(self):
with error_conversion(GF1Error):
self.inner.high()
def low(self):
with error_conversion(GF1Error):
self.inner.low()
def input(self):
with error_conversion(GF1Error):
return self.inner.input()
def write(self, high):
with error_conversion(GF1Error):
self.inner.write(high)
for name in gpio_allocations:
globals()[name] = Pin(None)
def setup():
with group("Setting up and checking test system"):
with group("Checking software dependencies"):
check_command("/usr/bin/gdb-multiarch")
check_command("/usr/sbin/fxload")
with task("Checking for udev rules"):
try:
file = open("/etc/udev/rules.d/60-tycho.rules", "r")
except OSError:
raise DependencyError("Required udev rules not installed. Please run 'make install-udev'.")
rules = file.readlines()
current_rules = open("60-tycho.rules", "r").readlines()
if rules != current_rules:
raise DependencyError("Required udev rules not up to date. Please run 'make install-udev'.")
with group("Checking for GreatFET"):
try:
find_device(0x1d50, 0x60e6,
"Great Scott Gadgets",
"GreatFET",
timeout=0)
except CynthionTestError:
raise GF1Error("GreatFET not detected. Check USB connections.")
with task("Connecting to GreatFET"):
try:
state.gf = GreatFET()
except Exception:
raise GF1Error(
"Could not connect to GreatFET. Check USB connections.")
expected = "git-v2021.2.1-65-g8d8be6f"
with task(f"Checking GreatFET firmware version is {info(expected)}"):
with error_conversion(GF1Error):
version = state.gf.firmware_version()
if version != expected:
raise GF1Error(f"GreatFET firmware version is {version}, expected {expected}.")
with task("Configuring GPIOs"):
for name, (position, output) in gpio_allocations.items():
with error_conversion(GF1Error):
pin = state.gf.gpio.get_pin(position)
globals()[name].inner = pin
if output is None:
pin.input()
elif output:
pin.high()
else:
pin.low()
with group("Checking for 24V supply"):
with task(f"Driving {info('D_TEST_PLUS')} high"):
D_TEST_PLUS.high()
mux_select('D_TEST_PLUS')
try:
test_voltage('D_TEST_PLUS', Range(3.2, 3.4))
except CynthionTestError:
raise PowerSupplyError("24V supply not detected. Check supply.")
with task(f"Releasing {info('D_TEST_PLUS')} drive"):
D_TEST_PLUS.input()
mux_disconnect()
with task("Configuring DC-DC converter"):
BOOST_EN.high()
state.boost = TPS55288(state.gf)
if state.boost.read(CDC) != 0b11100000:
raise TychoError("Failed to communicate with DC-DC converter.")
state.boost.disable()
try:
set_boost_supply(5.0, 0.1)
except SCPError:
raise TychoError("DC-DC converter detected a short circuit fault with no load.")
except OCPError:
raise TychoError("DC-DC converter detected an overcurrent fault with no load.")
except OVPError:
raise TychoError("DC-DC converter detected an overvoltage fault with no load.")
with group("Checking for Black Magic Probe"):
try:
bmp = find_device(0x1d50, 0x6018,
"Black Magic Debug",
"Black Magic Probe v1.9.1",
timeout=0)
state.blackmagic_port = (
"/dev/serial/by-id/usb-" +
bmp.getManufacturer().replace(' ', '_') + '_' +
bmp.getProduct().replace(' ', '_') + '_' +
bmp.getSerialNumber() + '-if00')
del bmp
except CynthionTestError:
raise BMPError(
"Black Magic Probe not detected. Check USB connections.")
def reset():
if state.gf is None:
return
try:
with error_conversion(GF1Error):
for name, (position, output) in gpio_allocations.items():
pin = globals()[name]
if output is None:
pin.input()
elif output:
pin.high()
else:
pin.low()
except CynthionTestError as error:
log("Additionally, while resetting test system:")
fail(error)
def pass_pressed():
return not PASS.input()
def fail_pressed():
return not FAIL.input()
def request(text):
# Wait for any currently pressed button to be released.
while pass_pressed() or fail_pressed():
sleep(0.01)
ask(text)
while True:
if fail_pressed():
raise FailButtonError("User pressed the FAIL button")
elif pass_pressed():
return
sleep(0.001)
def load_calibration():
with task("Loading calibration data"):
try:
file = open('calibration.dat', 'rb')
except FileNotFoundError:
raise CalibrationError("No calibration file found")
try:
state.calibration = pickle.load(file)
except Exception:
raise CalibrationError("Loading calibration file failed")
for field in (
'greatfet_serial',
'voltage_scale_lower',
'voltage_scale_upper',
'current_offset',
):
if field not in state.calibration:
raise CalibrationError(
f"Field '{field}' not found in calibration data")
if state.calibration['greatfet_serial'] != state.gf.serial_number():
raise CalibrationError("Calibration data is for a different tester")
class short_check():
def __init__(self, a, b, port):
super().__init__()
self.ident = (a, b, port)
self.group = group(f"Checking for {info(a)} to {info(b)} short on {info(port)}")
def __enter__(self):
self.group.__enter__()
def __exit__(self, exc_type, exc_value, exc_tb):
self.group.__exit__(exc_type, exc_value, exc_tb)
if exc_value is not None:
if isinstance(exc_value, ValueHighError):
a, b, port = self.ident
raise ShortError(f"Pins {a} and {b} on EUT {port} port appear to be shorted to each other.")
else:
wrap_exception(exc_value, GF1Error)
def check_for_shorts(port):
with group(f"Checking for shorts on {info(port)}"):
connect_tester_to(port)
connect_tester_cc_sbu_to(port)
with short_check('VBUS', 'GND', port):
set_pin('GND_EUT', True)
test_vbus(port, Range(0.0, 2.8), discharge=True)
set_pin('GND_EUT', None)
with short_check('VBUS', 'SBU2', port):
set_pin('SBU2_test', True)
test_vbus(port, Range(0.0, 2.1), discharge=True)
set_pin('SBU2_test', None)
with short_check('SBU2', 'CC1', port):
set_pin('SBU2_test', True)
test_voltage('CC1_test', Range(0.0, 1.5), discharge=True)
set_pin('SBU2_test', None)
with short_check('CC1', 'D-', port):
set_pin('CC1_test', True),
test_voltage('D_TEST_MINUS', Range(0.0, 1.5), discharge=True)
set_pin('CC1_test', None),
with short_check('D-', 'D+', port):
set_pin('D_TEST_PLUS', True),
test_voltage('D_TEST_MINUS', Range(0.0, 3.1), discharge=True)
set_pin('D_TEST_PLUS', None),
with short_check('D+', 'SBU1', port):
set_pin('SBU1_test', True)
test_voltage('D_TEST_PLUS', Range(0.0, 2.0), discharge=True)
set_pin('SBU1_test', None)
with short_check('SBU1', 'CC2', port):
set_pin('SBU1_test', True)
test_voltage('CC2_test', Range(0.0, 1.5), discharge=True)
set_pin('SBU1_test', None)
with short_check('CC2', 'VBUS', port):
set_pin('CC2_test', True)
test_vbus(port, Range(0.0, 2.1), discharge=True)
set_pin('CC2_test', None)
connect_tester_cc_sbu_to(None)
connect_tester_to(None)
def connect_grounds():
with task("Connecting EUT ground to Tycho ground"):
GND_EN.high()
def connect_tester_to(port):
connect_usb('tester', port)
def connect_host_to(port):
connect_usb('host', port)
def connect_usb(source, port):
if port is None:
msg = f"Disconnecting D+/D-"
else:
msg = f"Connecting {info(source)} D+/D- to {info(port)}"
with task(msg):
D_OEn_1.high()
states = {'host': 0, 'tester': 1}
D_S_1.write(states[source])
indices = {None: 0, 'TARGET-C': 1, 'AUX': 2, 'CONTROL': 3}
index = indices[port]
D_C0.write((index & 1) != 0)
D_C1.write((index & 2) != 0)
if port is None:
return
D_OEn_1.low()
def begin_cc_measurement(port):
connect_tester_cc_sbu_to(port)
V_DIV.high()
CC1_test.input()
CC2_test.input()
def end_cc_measurement():
V_DIV.low()
connect_tester_cc_sbu_to(None)
def check_cc_resistances(port):
expected_resistance = 5.1 * Range(0.9, 1.15)
with group(f"Checking CC resistances on {info(port)}"):
begin_cc_measurement(port)
for pin in ('CC1', 'CC2'):
check_cc_resistance(pin, expected_resistance)
end_cc_measurement()
def check_cc_resistance(pin, expected):
channel = f'{pin}_test'
with task(f"Checking voltage on {info(channel)}"):
mux_select(channel)
with error_conversion(GF1Error):
samples = state.gf.adc.read_samples(1000)
mux_disconnect()
voltage = (3.3 / 1024) * sum(samples) / len(samples)
result(f'{voltage:.2f} V')
switch_resistance = 0.17
resistance = - (voltage * 5.1) / (voltage - 3.3) - switch_resistance
return test_value("resistance", pin, resistance, 'kΩ', expected)
def test_leakage(port):
test_vbus(port, Range(0, 0.3 if port == 'TARGET-A' else 0.05), discharge=True)
def set_boost_supply(voltage, current):
with task(f"Setting DC-DC converter to {info(f'{voltage:.2f} V')} {info(f'{current:.2f} A')}"):
state.boost.set_voltage(voltage)
state.boost.set_current_limit(current)
state.boost.enable()
state.boost.check_fault()
def connect_boost_supply_to(*ports):
if ports == (None,):
msg = "Disconnecting DC-DC converter"
else:
msg = f"Connecting DC-DC converter to {str.join(' and ', map(info, ports))}"
with task(msg):
if 'CONTROL' in ports:
BOOST_VBUS_CON.high()
if 'AUX' in ports:
BOOST_VBUS_AUX.high()
if 'TARGET-C' in ports:
BOOST_VBUS_TC.high()
if 'CONTROL' not in ports:
BOOST_VBUS_CON.low()
if 'AUX' not in ports:
BOOST_VBUS_AUX.low()
if 'TARGET-C' not in ports:
BOOST_VBUS_TC.low()
state.boost.check_fault()
state.boost_port = ports[0]
def test_boost_current(expected):
V_DIV.low()
V_DIV_MULT.low()
pulldown = 100
mux_select('CDC')
cdc_voltage = measure_voltage(Range(0, 1.1))
mux_disconnect()
shunt_voltage = cdc_voltage * 0.05
shunt_resistance = 0.01
offset = state.calibration['current_offset']
shunt_current = max(shunt_voltage / shunt_resistance - offset, 0)
channel = vbus_channels[state.boost_port]
return test_value("current", channel, shunt_current, 'A', expected)
def mux_select(channel):
mux, pin = mux_channels[channel]
MUX1_EN.low()
MUX2_EN.low()
if mux == 0:
MUX1_A0.write(pin & 1)
MUX1_A1.write(pin & 2)
MUX1_A2.write(pin & 4)
MUX1_A3.write(pin & 8)
MUX1_EN.high()
else:
MUX2_A0.write(pin & 1)
MUX2_A1.write(pin & 2)
MUX2_A2.write(pin & 4)
MUX2_A3.write(pin & 8)
MUX2_EN.high()
def mux_disconnect():
MUX1_EN.low()
MUX2_EN.low()
def test_value(qty, src, value, unit, expected, ignore=False):
message = f"Checking {qty} on {info(src)} is within {info(f'{expected.lo:.2f}')} to {info(f'{expected.hi:.2f} {unit}')}: "
result = f"{value:.2f} {unit}"
if value < expected.lo:
item(message + Fore.RED + result)
if not ignore:
raise ValueLowError(f"{qty} too low on {src}: {value:.3f} {unit}, minimum was {expected.lo:.2f} {unit}")
elif value > expected.hi:
item(message + Fore.RED + result)
if not ignore:
raise ValueHighError(f"{qty} too high on {src}: {value:.3f} {unit}, maximum was {expected.hi:.2f} {unit}")
else:
item(message + Fore.GREEN + result)
return value
def measure_voltage(expected):
V_DIV.low()
pullup = 100
if expected.hi <= 6.6:
V_DIV_MULT.low()
pulldown = 100
cal = state.calibration['voltage_scale_lower']
else:
V_DIV_MULT.high()
pulldown = (100 * 22) / (100 + 22)
cal = state.calibration['voltage_scale_upper']
scale = 3.3 / 1024 * (pulldown + pullup) / pulldown
samples = state.gf.adc.read_samples(1000)
voltage = cal * scale * sum(samples) / len(samples)
return voltage
def test_voltage(channel, expected, discharge=False):
if discharge:
DISCHARGE.high()
mux_select(channel)
voltage = measure_voltage(expected)
mux_disconnect()
if discharge:
DISCHARGE.low()
return test_value("voltage", channel, voltage, 'V', expected)
def high_or_low(level):
return 'high' if level else 'low'
def set_pin(pin, level):
required = ('input' if level is None else
'output ' + high_or_low(level))
with task(f"Setting pin {info(pin)} to {info(required)}"):
pin = globals()[pin]
if level is None:
pin.input()
elif level:
pin.high()
else:
pin.low()
def test_pin(pin, level):
required = high_or_low(level)
with task(f"Checking pin {info(pin)} is {info(required)}"):
value = globals()[pin].input()
found = high_or_low(value)
if value != level:
raise ValueWrongError(f"Pin {pin} is {found}, should be {required}")
def disconnect_supply_and_discharge(port):
with task(f"Disconnecting supply and discharging {info(port)}"):
state.boost.disable()
discharge(port)
def discharge(port):
channel = vbus_channels[port]
V_DIV.low()
V_DIV_MULT.low()
DISCHARGE.high()
mux_select(channel)
while measure_voltage(Range(0, 25)) > 0.1:
sleep(0.05)
mux_disconnect()
DISCHARGE.low()
def test_clock():
reference_hz = 204000000 // 10
target_hz = 60000000
tolerance_ppm = 100
tolerance_hz = target_hz * tolerance_ppm / 1e6
expected = target_hz + Range(-tolerance_hz, tolerance_hz)
with error_conversion(GF1Error):
state.gf.apis.freq_count.setup_counters(reference_hz)
state.gf.apis.freq_count.setup_counters(reference_hz)
sleep(0.1)
frequency = state.gf.apis.freq_count.count_cycles() * 10
test_value("frequency", "CLK", frequency, 'Hz', expected)
def check_command(path):
name = os.path.basename(path)
with task(f"Checking for {info(name)}"):
if not os.path.exists(path):
raise DependencyError(f"No {name} at {path}. Install the {name} package.")
def run_command(cmd):
process = subprocess.run(cmd.split(" "),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if process.returncode != 0:
raise CommandError(
f"Command '{cmd}' failed with exit status {process.returncode}.\n\n" +
"Output of failed command:\n\n" +
f"{process.stdout.decode().rstrip()}")
return process
def flash_bootloader():
with group(f"Flashing Saturn-V bootloader to MCU via SWD"):
with task("Running flash script"):
script = open('flash-bootloader.gdb', 'w')
for line in open('flash-bootloader.template', 'r').readlines():
script.write(line.replace('BLACKMAGIC_PORT',
state.blackmagic_port))
script.close()
process = run_command('gdb-multiarch --batch -x flash-bootloader.gdb')
with task("Checking for MCU serial number"):
prefix = "Serial Number: 0x"
for line in process.stdout.decode().split('\n'):
if line.startswith(prefix):
serial_string = line[len(prefix):].rstrip()
break
else:
raise CommandError(
"MCU serial number not found in output:\n\n" +
f"{process.stdout.decode().rstrip()}")
serial_bytes = bytes.join(b'', [
int(serial_string[i:i+8], 16).to_bytes(4, byteorder='little')
for i in (0, 8, 16, 24)]) + b'\x00'
buffer = serial_bytes[0]
bits_left = 8
next_byte = 1
serial = ''
for count in range(26):
if bits_left < 5:
buffer <<= 8
buffer |= serial_bytes[next_byte] & 0xFF
next_byte += 1
bits_left += 8
bits_left -= 5
index = (buffer >> bits_left) & 0x1F
serial += chr(index + (ord('A') if index < 26 else ord('2') - 26))
state.mcu_serial = serial
result(serial)
def flash_firmware():
with task(f"Flashing Apollo to MCU via DFU"):
run_command('environment/bin/fwup-util -d 1d50:615c firmware.bin')
def test_saturnv_present():
with group(f"Checking for Saturn-V"):
return find_device(0x1d50, 0x615c,
"Saturn-V Project",
"Cynthion Bootloader",
state.mcu_serial)
def test_apollo_present():
with group(f"Checking for Apollo"):
find_device(0x1d50, 0x615c,
"Great Scott Gadgets",
"Apollo Debugger",
state.mcu_serial)
with task("Connecting to Apollo"):
apollo = ApolloDebugger()
return apollo
def test_bridge_present():
with group(f"Checking for flash bridge"):
return find_device(0x1209, 0x000f,
"Apollo Project",
"Configuration Flash Bridge")
def test_analyzer_present():
with group(f"Checking for analyzer"):
serial = hex(state.flash_serial)[2:].lower()
return find_device(0x1d50, 0x615b,
"Cynthion Project",
"USB Analyzer",
serial)
def simulate_program_button():
with group(f"Simulating pressing the {info('PROGRAM')} button"):
set_pin('nBTN_PROGRAM', False)
sleep(0.1)
set_pin('nBTN_PROGRAM', None)
def simulate_reset_button():
with group(f"Simulating pressing the {info('RESET')} button"):
set_pin('nBTN_RESET', False)
sleep(0.1)
set_pin('nBTN_RESET', None)
def set_debug_leds(apollo, bitmask):
with task(f"Setting debug LEDs to 0b{bitmask:05b}"):
apollo.set_led_pattern(bitmask)
def set_fpga_leds(apollo, bitmask):
with task(f"Setting FPGA LEDs to 0b{bitmask:05b}"):
apollo.registers.register_write(REGISTER_LEDS, bitmask)
assert(apollo.registers.register_read(REGISTER_LEDS) == bitmask)
def test_leds(apollo, device, leds, set_leds):
off = Range(3.1, 3.35)
with group(f"Testing {device} LEDs"):
for i in range(len(leds)):
with group(f"Testing {device} LED {info(i)}"):
# Turn on LED
set_leds(apollo, 1 << i)
# Check that this and only this LED is on,
# with the correct voltage.
for j, (testpoint, minimum, maximum) in enumerate(leds):
if i == j:
test_voltage(testpoint, Range(minimum, maximum))
else:
test_voltage(testpoint, off)
def test_jtag_scan(apollo):
with group("Checking JTAG scan chain"):
with task("Reading JTAG scan chain"):
with apollo.jtag as jtag:
devices = [(device.idcode(), device.description())
for device in jtag.enumerate()]
result(", ".join(
f"{info(f'0x{idcode:8X}')}: {info(desc)}"
for idcode, desc in devices))
if devices != [(0x21111043, "Lattice LFE5U-12F ECP5 FPGA")]:
raise ValueWrongError("JTAG scan chain did not include expected devices")
def unconfigure_fpga(apollo):
with apollo.jtag as jtag:
programmer = apollo.create_jtag_programmer(jtag)
with task("Unconfiguring FPGA"):
programmer.unconfigure()
def test_flash_id(apollo, expected_mfg, expected_part):
with group("Checking flash chip IDs"):
with apollo.jtag as jtag:
programmer = apollo.create_jtag_programmer(jtag)
with task("Checking flash ID"):
mfg, part = programmer.read_flash_id()
result(f"{info(f'0x{mfg:02X}')}, {info(f'0x{part:06X}')}")
if mfg != expected_mfg:
raise ValueWrongError(f"Wrong flash chip manufacturer ID: 0x{mfg:02X}")
if part != expected_part:
raise ValueWrongError(f"Wrong flash chip part ID: 0x{part:02X}")
with task("Reading flash UID"):
state.flash_serial = programmer.read_flash_uid()
result(f"0x{state.flash_serial:08X}")
def flash_bitstream(apollo, filename):
with group(f"Writing {info(filename)} to FPGA configuration flash"):
bitstream = open(filename, 'rb').read()
configure_fpga(apollo, 'flashbridge.bit')
request_control_handoff_to_fpga(apollo)
test_bridge_present()
with task("Connecting to flash bridge"):
bridge = FlashBridgeConnection()
programmer = ECP5FlashBridgeProgrammer(bridge=bridge)
with task("Writing flash"):
programmer.flash(bitstream)
def configure_fpga(apollo, filename):
with task(f"Configuring FPGA with {info(filename)}"):
bitstream = open(filename, 'rb').read()
with apollo.jtag as jtag:
programmer = apollo.create_jtag_programmer(jtag)
programmer.configure(bitstream)
def request_control_handoff_to_fpga(apollo):
with task(f"Requesting MCU handoff {info('CONTROL')} port to FPGA"):
apollo.allow_fpga_takeover_usb()
apollo.close()
def await_device(vid, pid, timeout):
with task(f"Looking for device with " +
f"VID: {info(f'0x{vid:04x}')}, " +
f"PID: {info(f'0x{pid:04x}')}"):
candidates = []
def callback(context, device, event):
candidates.append(device)
return False
callback_handle = context.hotplugRegisterCallback(
callback,
vendor_id=vid,
product_id=pid,
events=usb1.HOTPLUG_EVENT_DEVICE_ARRIVED,
flags=usb1.HOTPLUG_ENUMERATE)
end = time() + timeout
while True:
try:
# Loop through new candidates.
while device := candidates.pop():
try:
# Get bus and address of candidate.
bus = device.getBusNumber()
addr = device.getDeviceAddress()
# New device must be on the same bus as previously.
if state.last_bus is not None and bus != state.last_bus:
continue
# New device must have a different address to previous one.
if addr == state.last_addr:
continue
# This is the new device we're looking for.
context.hotplugDeregisterCallback(callback_handle)
state.last_bus = bus
state.last_addr = addr
return device
except usb1.USBError:
continue
except IndexError:
# No more new candidates.
pass
timeout = end - time()
if timeout > 0:
context.handleEventsTimeout(timeout)
else:
context.hotplugDeregisterCallback(callback_handle)
raise USBCommsError("Device not found")
def find_device(vid, pid, mfg=None, prod=None, serial=None, timeout=3):
device = await_device(vid, pid, timeout)
if mfg is not None:
with task(f"Checking manufacturer is {info(mfg)}"):
if (string := device.getManufacturer()) != mfg:
raise ValueWrongError(
f"Wrong manufacturer string: '{string}'")
if prod is not None:
with task(f"Checking product is {info(prod)}"):
if (string := device.getProduct()) != prod:
raise ValueWrongError(
f"Wrong product string: '{string}'")
if serial is not None:
with task(f"Checking serial is {info(serial)}"):
if (string := device.getSerialNumber()) != serial:
raise ValueWrongError(
f"Wrong serial string: '{string}'")
else:
with task(f"Reading serial number"):
result(device.getSerialNumber())
return device
def test_phy_vbus(apollo, phy, expected):
with task(f"Checking {info(phy)} PHY reads VBUS as " +
info(high_or_low(expected))):
reg = phy_registers[phy]
write_register(apollo, reg, 0x13)
status = read_register(apollo, reg + 1)
vbus = bool(status & 0x04)
if vbus != expected:
raise ValueWrongError("CONTROL PHY reads VBUS as " +
high_or_low(vbus) + ", expected " +
high_or_low(expected))
def run_self_test(apollo, test_target_monitor):
with group("Running self test"):
selftest = AssistedTester(apollo)
for method in [
selftest.test_debug_connection,
selftest.test_sideband_phy,
selftest.test_host_phy,
selftest.test_target_phy,
selftest.test_hyperram,
selftest.test_aux_typec_controller,
selftest.test_target_typec_controller,
selftest.test_power_monitor_controller,
]:
description = method.__name__.replace("test_", "")
with task(description):
try:
method(apollo)
except AssertionError:
raise SelfTestError(f"{description} self-test failed")
with task("Skipping PMOD I/O test"):
pass
with group("VBUS sensing"):
for phy, expected in (('CONTROL', 1), ('AUX', 0), ('TARGET', 0)):
test_phy_vbus(apollo, phy, expected)
connect_boost_supply_to('CONTROL', 'AUX', 'TARGET-C')
for phy, expected in (('AUX', 1), ('TARGET', 1)):
test_phy_vbus(apollo, phy, expected)
connect_boost_supply_to('CONTROL')
if not test_target_monitor:
return
with group("TARGET D+/D- sensing"):
connect_boost_supply_to('CONTROL', 'TARGET-C')
for func, otg, io, dp, dm, desc in (
(0x41, 0x06, 0x06, 0, 0, "D+/D- pulled low"),
(0x45, 0x04, 0x04, 0, 1, "D+ pulled low, D- pulled high"),
(0x45, 0x04, 0x06, 1, 0, "D+ pulled high, D- pulled low"),
):
with task(f"Configuring PHY with {desc}"):
write_register(apollo, REGISTER_TARGET_ADDR, 0x04)
write_register(apollo, REGISTER_TARGET_VALUE, func)
write_register(apollo, REGISTER_TARGET_ADDR, 0x0A)
write_register(apollo, REGISTER_TARGET_VALUE, otg)
write_register(apollo, REGISTER_TARGET_ADDR, 0x39)
write_register(apollo, REGISTER_TARGET_VALUE, io)
with task(f"Checking FPGA D+ pin is {info(high_or_low(dp))}"):
dp_read = read_register(apollo, REGISTER_SENSE_DP)
if dp_read != dp:
raise ValueWrongError("FPGA D+ sense pin was " +
high_or_low(dp_read) + ", expected " +
high_or_low(dp))
with task(f"Checking FPGA D- pin is {info(high_or_low(dm))}"):
dm_read = read_register(apollo, REGISTER_SENSE_DM)
if dm_read != dm:
raise ValueWrongError("FPGA D- sense pin was " +
high_or_low(dm_read) + ", expected " +
high_or_low(dm))
connect_boost_supply_to('CONTROL')
def test_fx2():
FX2_EN.high()
with error_conversion(FX2Error):
loader = find_device(0x04b4, 0x8613)
bus = loader.getBusNumber()
addr = loader.getDeviceAddress()
path = f"/dev/bus/usb/{bus:03d}/{addr:03d}"
with task("Loading FX2 firmware"):
run_command(f"/usr/sbin/fxload -t fx2lp -I fx2.ihx -D {path}")
device = find_device(0x04b4, 0x1003, None, "Cy-stream")
handle = device.open()
handle.claimInterface(0)
test_usb_hs_speed('TARGET-C', handle, 2, Range(35, 45))
FX2_EN.low()
def test_usb_hs(port):
pids = {'CONTROL': 0x0001, 'AUX': 0x0002, 'TARGET-C': 0x0003}
with group(f"Testing USB HS comms on {info(port)}"):
connect_host_to(port)
device = find_device(0x1209, pids[port], "LUNA", "speed test")
handle = device.open()
handle.claimInterface(0)
test_usb_hs_speed(port, handle, 1, Range(44, 50))
return handle
def test_usb_hs_speed(port, handle, endpoint, expected):
with task("Running speed test"):
speeds = [test_usb_hs_speed_single(port, handle, endpoint)
for repeat in range(3)]
test_value("transfer rate", port, max(speeds), 'MB/s', expected)
def test_usb_hs_speed_single(port, handle, endpoint):
TEST_DATA_SIZE = 1 * 1024 * 1024
TEST_TRANSFER_SIZE = 16 * 1024
TRANSFER_QUEUE_DEPTH = 16
total_data_exchanged = 0
failed_out = False
messages = {
1: "error'd out",
2: "timed out",
3: "was prematurely cancelled",
4: "was stalled",
5: "lost the device it was connected to",
6: "sent more data than expected."
}
def should_terminate():
return (total_data_exchanged > TEST_DATA_SIZE) or failed_out
def transfer_completed(transfer: usb1.USBTransfer):
nonlocal total_data_exchanged, failed_out
status = transfer.getStatus()
# If the transfer completed.
if status in (usb1.TRANSFER_COMPLETED,):
# Count the data exchanged in this packet...
total_data_exchanged += transfer.getActualLength()
# ... and if we should terminate, abort.
if should_terminate():
return
# Otherwise, re-submit the transfer.
transfer.submit()
else:
failed_out = status
# Submit a set of transfers to perform async comms with.
active_transfers = []
for _ in range(TRANSFER_QUEUE_DEPTH):
# Allocate the transfer...
transfer = handle.getTransfer()
transfer.setBulk(0x80 | endpoint,
TEST_TRANSFER_SIZE,
callback=transfer_completed,
timeout=1000)
# ... and store it.
active_transfers.append(transfer)
# Start our benchmark timer.
start_time = time()
# Submit our transfers all at once.
for transfer in active_transfers:
transfer.submit()
# Run our transfers until we get enough data.
while not should_terminate():
context.handleEvents()
# Figure out how long this took us.
end_time = time()
elapsed = end_time - start_time
# Cancel all of our active transfers.
for transfer in active_transfers:
if transfer.isSubmitted():
try:
transfer.cancel()
except usb1.USBErrorNotFound:
pass
# If we failed out; indicate it.
if failed_out:
raise USBCommsError(
f"Test failed because a transfer {messages[failed_out]}.")
speed = total_data_exchanged / elapsed / 1000000
return speed
def connect_tester_cc_sbu_to(port):
if port is None:
msg = "Disconnecting tester CC/SBU lines"
else:
msg = f"Connecting tester CC/SBU lines to {info(port)}"
with task(msg):
SIG1_OEn.high()
SIG2_OEn.high()
if port is None:
return
SIG1_S.write(port == 'CONTROL')
SIG2_S.write(port == 'TARGET-C')
SIG1_OEn.low()
SIG2_OEn.low()
def write_register(apollo, reg, value, verify=False):
apollo.registers.register_write(reg, value)
if verify:
readback = apollo.registers.register_read(reg)
if readback != value:
raise RegisterError(
f"Wrote 0x{value:02X} to register {reg} "
f"but read back 0x{readback:02X}")
def read_register(apollo, reg):
return apollo.registers.register_read(reg)
def enable_supply_input(apollo, port, enable):
with task(f"{'Enabling' if enable else 'Disabling'} supply input on {info(port)}"):
write_register(apollo, vbus_registers[port], enable)
def set_cc_levels(apollo, port, levels):
with task(f"Setting CC levels on {info(port)} to {info(levels)}"):
value = 0b01 * levels[0] | 0b10 * levels[1]
reg_addr, reg_val = typec_registers[port]
write_register(apollo, reg_addr, (0x02 << 8) | 1)
write_register(apollo, reg_val, value)