diff --git a/calibrate.py b/calibrate.py deleted file mode 100644 index 0efd294..0000000 --- a/calibrate.py +++ /dev/null @@ -1,110 +0,0 @@ - -from hashlib import sha256 -from binascii import hexlify, unhexlify -from os.path import isfile -from threading import Thread -from struct import unpack, pack - -from tls97 import tls -from usb97 import usb -from time import ctime -from sensor import write_hw_reg32, read_hw_reg32, identify_sensor -from flash import erase_flash, read_flash, get_fw_info, write_flash_all -from util import assert_status -from blobs import calibrate_prg - -#usb.trace_enabled=True -#tls.trace_enabled=True - -usb.open() -tls.parseTlsFlash(read_flash(1, 0, 0x1000)) -tls.open() - -# no idea what this is: -write_hw_reg32(0x8000205c, 7) -if read_hw_reg32(0x80002080) != 2: - raise Exception('Unexpected register value') - -dev=identify_sensor() -print('Sensor: %s' % dev.name) -# ^ TODO -- what is the real reason to detect HW at this stage? -- likely it is required to construct calibrate_prg - -fwi=get_fw_info(2) -if fwi == None: - raise Exception('No firmware detected') - -print('FWExt version %d.%d (%s), %d modules' % (fwi.major, fwi.minor, ctime(fwi.buildtime), len(fwi.modules))) - -def wait_82(): - calib_data=usb.read_82() - print('len=%d' % len(calib_data)) - with open('calib-data.bin', 'wb') as f: - f.write(calib_data) - - -if isfile('calib-data.bin'): - with open('calib-data.bin', 'rb') as f: - calib_data=f.read() - print('Calibration data loaded from a file.') -else: - # TODO Properly construct calibrate_prg. - # >>> 6f 000e 000000000000 - # <<< 0000 880d 0000 07000000 - # 0800 0000 9400 0e00 0300 0080 07000000 7e7f807f808080808080808080808080808080808080818081808180818080808080818081808080818081808180818081808180818081808180808081808180808081807f80808180808081808180818080808180818081808180818081808080818081808180818081808180818081808180818081808180818081808080808080808080807f807f807f807f7f7e7e - # a400 0000 0800 0e00 0200 0000 00000000 0d007100 - # b400 0000 0800 0e00 0800 0080 db000000 00000000 - # c400 0000 0400 0e00 0500 0080 1c6f0400 - # d000 0000 9400 0e00 0700 0080 07000000 2b23203c2d182e1e30182e1c321d341d341e321c301e1e241e201f201d1c321a301e1c211e21341f1e202024201f1e20201f212221221d221e23341e1d1e1d20341f1d193b341c1d1e35201e201c20221f341c1e1e1c221f201d21201e1c1f34242221201f20221f201e241e241d2020221e2420231d221e211e1f1e1e341c321e3220301d2d302f2d2c2b23223a211c - # 6c01 0000 1400 0e00 0f00 0080 05550007 7701002805720000080100020811e107 - # 8801 0000 0c00 0e00 1200 0080 07000000 7002 7800 7002 7800 - # - # Empty reply: - # >>> 6f 000a 000000000000 - # <<< 0000 880d 0000 00000000 - - #Thread(target=wait_82).start() - - rsp=tls.cmd(calibrate_prg) - assert_status(rsp) - print(rsp.hex()) - # ^ TODO check what the rest of the rsp means - - calib_data=usb.read_82() - print('len=%d' % len(calib_data)) - with open('calib-data.bin', 'wb') as f: - f.write(calib_data) - -class Line(): - def __init__(self, blob): - # what's with the rest of fields? - self.u0, self.u1, self.line, self.frame, self.u2, self.u3, self.u4, self.u5 = unpack(' 0: - hdr, blob4f = blob4f[:4], blob4f[4:] - id, l = unpack(' 0: - d, align, hsh, p = p[:12], p[12:12+4], p[12+4:12+4+32], p[12+4+32:] - if align != b'\0' * 4: - raise Exception('align should be blank') - m=sha256() - m.update(d) - if m.digest() != hsh: - raise Exception('Hash mismatch') - - print('block 1, blob: %s' % hexlify(d).decode()) - elif id == 5: - hdr, p = p[:4+4], p[4+4:] - - magic, keysz = unpack('>> 6f 000e 000000000000 + # <<< 0000 880d 0000 07000000 + # 0800 0000 9400 0e00 0300 0080 07000000 7e7f807f808080808080808080808080808080808080818081808180818080808080818081808080818081808180818081808180818081808180808081808180808081807f80808180808081808180818080808180818081808180818081808080818081808180818081808180818081808180818081808180818081808080808080808080807f807f807f807f7f7e7e + # a400 0000 0800 0e00 0200 0000 00000000 0d007100 + # b400 0000 0800 0e00 0800 0080 db000000 00000000 + # c400 0000 0400 0e00 0500 0080 1c6f0400 + # d000 0000 9400 0e00 0700 0080 07000000 2b23203c2d182e1e30182e1c321d341d341e321c301e1e241e201f201d1c321a301e1c211e21341f1e202024201f1e20201f212221221d221e23341e1d1e1d20341f1d193b341c1d1e35201e201c20221f341c1e1e1c221f201d21201e1c1f34242221201f20221f201e241e241d2020221e2420231d221e211e1f1e1e341c321e3220301d2d302f2d2c2b23223a211c + # 6c01 0000 1400 0e00 0f00 0080 05550007 7701002805720000080100020811e107 + # 8801 0000 0c00 0e00 1200 0080 07000000 7002 7800 7002 7800 + # + # Empty reply: + # >>> 6f 000a 000000000000 + # <<< 0000 880d 0000 00000000 + + rsp=tls.cmd(calibrate_prg) + assert_status(rsp) + print(rsp.hex()) + # ^ TODO check what the rest of the rsp means + + calib_data=usb.read_82() + print('len=%d' % len(calib_data)) + with open('calib-data.bin', 'wb') as f: + f.write(calib_data) + + lines=[calib_data[i:i+0x90+8] for i in range(0, len(calib_data), 0x90+8)] # TODO work out where "bytes per line" constant is comming from + lines=[Line(i) for i in lines] + frame4=[i.serialize() for i in lines if i.frame == 4] # why 4? + frame4=b''.join(frame4) + calib_data=pack('> 3, b[0] & 7 )) - elif b[0] & 0xc0 == 0xc0: - return (0xf, 2, 'Sample Repeat %x, %x, repeat=%x' % ( (b[0] & 0x38) >> 3, b[0] & 7, 0x100 if b[1] == 0 else b[1] )) - elif b[0] == 5: - return (5, 2, 'Marco %02x' % b[1]) - else: - raise Exception('Unhandled instruction %02x' % b[0]) - - - -def decode_tt(b): - pc=0 - while len(b) > 0: - (op, sz, txt) = decode_tt_inst(b) - if sz > len(b): - raise Exception('Truncated instruction') - print('%04x: %-6s %s' % (pc, hexlify(b[:sz]).decode(), txt)) - b = b[sz:] - pc += sz - -#b=enroll_prg[5:] -#b=short[5:] -def dump_prg(b): - while len(b) > 0: - (typ, sz), b = unpack(' 0: - (off, val), p = unpack(' 0: - (off, val), p = unpack('