This commit is contained in:
Matt Mets 2023-03-08 13:36:02 +01:00 committed by Michael Cardell Widerkrantz
parent 12f6575afd
commit 2cd7c9f8e3
No known key found for this signature in database
GPG key ID: D3DB3DDF57E704E5
6 changed files with 170 additions and 160 deletions

View file

@ -54,7 +54,7 @@ parameters = {
'pico_bootloader_target_dir': '/media/lab/RPI-RP2/'
}
tp1_pins = {
TP1_PINS = {
'5v_en': 7,
'tx': 8,
'rx': 9,
@ -75,9 +75,9 @@ tp1_pins = {
def enable_power() -> bool:
"""Enable power to the TK-1"""
d = IceFlasher()
d.gpio_set_direction(tp1_pins['5v_en'], True)
d.gpio_put(tp1_pins['5v_en'], True)
flasher = IceFlasher()
flasher.gpio_set_direction(TP1_PINS['5v_en'], True)
flasher.gpio_put(TP1_PINS['5v_en'], True)
time.sleep(0.3)
return True
@ -86,9 +86,9 @@ def enable_power() -> bool:
def disable_power() -> bool:
"""Disable power to the TK-1"""
time.sleep(.1)
d = IceFlasher()
d.gpio_set_direction(tp1_pins['5v_en'], True)
d.gpio_put(tp1_pins['5v_en'], False)
flasher = IceFlasher()
flasher.gpio_set_direction(TP1_PINS['5v_en'], True)
flasher.gpio_put(TP1_PINS['5v_en'], False)
return True
@ -102,7 +102,7 @@ def measure_voltages(device: IceFlasher,
sample_count -- number of samples to average
"""
adc_vals = [0.0, 0.0, 0.0]
for i in range(0, sample_count):
for _ in range(0, sample_count):
adc_vals = [
total + sample for total,
sample in zip(
@ -119,18 +119,16 @@ def voltage_test() -> bool:
enable_power()
d = IceFlasher()
vals = measure_voltages(d, 20)
d.close()
flasher = IceFlasher()
vals = measure_voltages(flasher, 20)
flasher.close()
disable_power()
print(
'voltages:',
', '.join(
'{:}V:{:.3f}'.format(
val[0],
val[1]) for val in vals.items()))
f'{val[0]}V:{val[1]:.3f}' for val in vals.items()))
if (
(abs(vals['1.2'] - 1.2) > .2)
| (abs(vals['2.5'] - 2.5) > .2)
@ -142,12 +140,13 @@ def voltage_test() -> bool:
def flash_validate_id() -> bool:
"""Read the ID from TK-1 SPI flash, and verify that it matches the expected value"""
"""Read the ID from TK-1 SPI flash, and compare to known values"""
result = run([
parameters['iceprog'],
'-t'
],
capture_output=True)
capture_output=True,
check=True)
disable_power()
err = result.stderr.split(b'\n')
@ -167,7 +166,7 @@ def flash_validate_id() -> bool:
if flash_type is None:
print('Flash ID invalid')
return False
print('Detected flash type: {:}'.format(flash_type))
print(f'Detected flash type: {flash_type}')
return True
return result.returncode == 0
@ -178,7 +177,8 @@ def flash_program() -> bool:
result = run([
parameters['iceprog'],
parameters['app_gateware']
])
],
check=True)
disable_power()
print(result)
@ -191,7 +191,8 @@ def flash_check() -> bool:
parameters['iceprog'],
'-c',
parameters['app_gateware']
])
],
check=True)
disable_power()
print(result)
@ -199,12 +200,12 @@ def flash_check() -> bool:
def test_extra_io() -> bool:
"""Test the TK-1 RTS, CTS, and GPIO1-4 lines by measuring a test pattern generated by the app_test gateware"""
"""Test the TK-1 RTS, CTS, and GPIO1-4 lines"""
d = IceFlasher()
for pin in tp1_pins.values():
d.gpio_set_direction(pin, False)
d.close()
flasher = IceFlasher()
for pin in TP1_PINS.values():
flasher.gpio_set_direction(pin, False)
flasher.close()
disable_power()
time.sleep(1)
@ -212,24 +213,24 @@ def test_extra_io() -> bool:
time.sleep(0.2)
d = IceFlasher()
d.gpio_put(tp1_pins['rts'], False)
d.gpio_set_direction(tp1_pins['rts'], True)
flasher = IceFlasher()
flasher.gpio_put(TP1_PINS['rts'], False)
flasher.gpio_set_direction(TP1_PINS['rts'], True)
expected_results = [1 << (i % 5) for i in range(9, -1, -1)]
results = []
for i in range(0, 10):
vals = d.gpio_get_all()
for _ in range(0, 10):
vals = flasher.gpio_get_all()
pattern = (vals >> 17) & 0b11111
# print(f'{vals:016x} {pattern:04x}')
results.append(pattern)
d.gpio_put(tp1_pins['rts'], True)
d.gpio_put(tp1_pins['rts'], False)
flasher.gpio_put(TP1_PINS['rts'], True)
flasher.gpio_put(TP1_PINS['rts'], False)
d.gpio_set_direction(tp1_pins['rts'], False)
d.close()
flasher.gpio_set_direction(TP1_PINS['rts'], False)
flasher.close()
disable_power()
@ -269,22 +270,22 @@ def inject_serial_number(
"68de5d27-e223-4874-bc76-a54d6e84068f")
replacement = encode_usb_strings.string_to_descriptor(serial_num)
f = bytearray(open(infile, 'rb').read())
with open(infile, 'rb') as fin:
firmware_data = bytearray(fin.read())
pos = f.find(magic)
pos = firmware_data.find(magic)
if pos < 0:
print('failed to find magic string')
exit(1)
raise ValueError('failed to find magic string')
f[pos:(pos + len(magic))] = replacement
firmware_data[pos:(pos + len(magic))] = replacement
with open(outfile, 'wb') as of:
of.write(f)
with open(outfile, 'wb') as fout:
fout.write(firmware_data)
def flash_ch552(serial_num: str) -> bool:
"""Flash an attached CH552 device with the USB CDC firmware, injected with the given serial number"""
"""Flash an attached CH552 device with the USB CDC firmware"""
print(serial_num)
inject_serial_number(
@ -296,7 +297,8 @@ def flash_ch552(serial_num: str) -> bool:
result = run([
parameters['chprog'],
parameters['ch552_firmware_injected']
])
],
check=True)
print(result)
return result.returncode == 0
@ -308,7 +310,8 @@ def erase_ch552() -> bool:
result = run([
parameters['chprog'],
parameters['ch552_firmware_blank']
])
],
check=True)
print(result)
return result.returncode == 0
@ -330,7 +333,7 @@ def find_serial_device(desc: dict[str, Any]) -> str:
def find_ch552(serial_num: str) -> bool:
"""Search all serial devices for one that has the correct description and serial number"""
"""Search for a serial device that has the correct description"""
time.sleep(1)
description = {
@ -350,18 +353,18 @@ def find_ch552(serial_num: str) -> bool:
def ch552_program() -> bool:
"""Load the CDC ACM firmware onto a CH552 with a randomly generated serial number, and verify that it boots correctly"""
"""Load the firmware onto a CH552, and verify that it boots"""
if not test_found_bootloader():
print('Error finding CH552!')
return False
serial = str(uuid.uuid4())
serial_num = str(uuid.uuid4())
if not flash_ch552(serial):
if not flash_ch552(serial_num):
print('Error flashing CH552!')
return False
if not find_ch552(serial):
if not find_ch552(serial_num):
print('Error finding flashed CH552!')
return False
@ -382,7 +385,7 @@ def ch552_erase() -> bool:
def test_txrx_touchpad() -> bool:
"""Test UART communication, RGB LED, and touchpad by asking the operator to interact with the touch pad"""
"""Test UART communication, RGB LED, and touchpad"""
description = {
'vid': 0x1207,
'pid': 0x8887,
@ -390,36 +393,33 @@ def test_txrx_touchpad() -> bool:
'product': 'MTA1-USB-V1'
}
s = serial.Serial(
dev = serial.Serial(
find_serial_device(description),
9600,
timeout=.2)
if not s.isOpen():
if not dev.isOpen():
print('couldn\'t find/open serial device')
return False
for _ in range(0, 5):
# Attempt to clear any buffered data from the serial port
s.write(b'0123')
dev.write(b'0123')
time.sleep(0.2)
s.read(20)
dev.read(20)
try:
s.write(b'0')
[count, touch_count] = s.read(2)
dev.write(b'0')
[count, touch_count] = dev.read(2)
print(
'read count:{:}, touch count:{:}'.format(
count, touch_count))
f'read count:{count}, touch count:{touch_count}')
input(
'\n\n\nPress touch pad once and check LED, then press Enter')
s.write(b'0')
[count_post, touch_count_post] = s.read(2)
dev.write(b'0')
[count_post, touch_count_post] = dev.read(2)
print(
'read count:{:}, touch count:{:}'.format(
count_post,
touch_count_post))
'read count:{count_post}, touch count:{touch_count_post}')
if (count_post -
count != 1) or (touch_count_post -
@ -428,8 +428,8 @@ def test_txrx_touchpad() -> bool:
continue
return True
except ValueError as e:
print(e)
except ValueError as error:
print(error)
continue
print('Max retries exceeded, failure!')
@ -450,9 +450,6 @@ def program_pico() -> bool:
parameters['pico_bootloader_target_dir'] +
firmware_filename)
# TODO: Test if the pico identifies as a USB-HID device
# after programming
return True
except FileNotFoundError:
time.sleep(0.1)
@ -514,64 +511,45 @@ def reset() -> None:
"""Attempt to reset the board after test failure"""
try:
disable_power()
except AttributeError as e:
except AttributeError:
pass
except OSError as e:
except OSError:
pass
except ValueError as e:
except ValueError:
pass
if __name__ == "__main__":
# Runs the non-interactive production tests continuously in a
# random order, to look for interaction bugs
def random_test_runner() -> None:
""""Run the non-interactive production tests in a random order
This routine is intended to be used for finding edge-cases with
the production tests. It runs the non-interactive tests (as well
as some nondestructive tests from the nvcm module) in a random
order, and runs continuously.
"""
import random
import pynvcm
def nvcm_read_info() -> bool:
tp1_pins = {
'5v_en': 7,
'sck': 10,
'mosi': 11,
'ss': 12,
'miso': 13,
'crst': 14,
'cdne': 15
}
pynvcm.sleep_flash(tp1_pins, 15)
nvcm = pynvcm.Nvcm(tp1_pins, 15)
"""Check that the nvcm read info command runs"""
pynvcm.sleep_flash(TP1_PINS, 15)
nvcm = pynvcm.Nvcm(TP1_PINS, 15)
nvcm.power_on()
nvcm.init()
nvcm.nvcm_enable()
nvcm.info()
nvcm.power_off()
return True
def nvcm_verify_blank() -> bool:
tp1_pins = {
'5v_en': 7,
'sck': 10,
'mosi': 11,
'ss': 12,
'miso': 13,
'crst': 14,
'cdne': 15
}
pynvcm.sleep_flash(tp1_pins, 15)
nvcm = pynvcm.Nvcm(tp1_pins, 15)
"""Verify that the NVCM memory is blank"""
pynvcm.sleep_flash(TP1_PINS, 15)
nvcm = pynvcm.Nvcm(TP1_PINS, 15)
nvcm.power_on()
nvcm.init()
nvcm.nvcm_enable()
nvcm.trim_blank_check()
nvcm.power_off()
return True
tests = [
nvcm_read_info,
nvcm_verify_blank,
@ -583,11 +561,21 @@ if __name__ == "__main__":
enable_power,
disable_power
]
parameters['iceprog'] = '/home/matt/repos/tillitis--icestorm/iceprog/iceprog'
pass_count = 0
while True:
i = random.randint(0, (len(tests) - 1))
print(f'\n\n{i} running: {tests[i].__name__}')
print(f'\n\n{pass_count}: running: {tests[i].__name__}')
if not tests[i]():
raise Exception('oops')
sys.exit(1)
pass_count += 1
if __name__ == "__main__":
import random
import pynvcm
import sys
random_test_runner()