2022-05-10 18:20:02 +00:00
|
|
|
import serial
|
|
|
|
import struct
|
|
|
|
import sys
|
2023-06-03 06:33:01 +00:00
|
|
|
import time
|
2022-05-10 18:20:02 +00:00
|
|
|
|
|
|
|
offset = 0x800
|
|
|
|
tty = 'tty'
|
|
|
|
baud = 115200
|
|
|
|
chunksize = 128
|
|
|
|
|
|
|
|
|
|
|
|
def write(s, offset, dat):
|
|
|
|
for i in range(0, len(dat), chunksize):
|
|
|
|
chunk = dat[i: i + chunksize]
|
|
|
|
cmd = struct.pack('<cII', b'c', offset + i, len(chunk))
|
|
|
|
|
|
|
|
print(f'Sending {len(chunk)} bytes @0x{offset + i:04x}')
|
|
|
|
|
|
|
|
s.write(cmd)
|
|
|
|
s.write(chunk)
|
|
|
|
|
|
|
|
ack = s.read(2)
|
|
|
|
if len(ack) < 2:
|
|
|
|
raise RuntimeError(f'timeout waiting for full ack. got {ack}')
|
|
|
|
if ack[0] != b'a'[0]:
|
|
|
|
raise RuntimeError(f'expected ack, got this instead: {ack}')
|
|
|
|
print(f'Ack! len={ack[1]}')
|
|
|
|
|
|
|
|
|
|
|
|
def jump(s, offset):
|
|
|
|
addr = struct.pack('<I', offset)
|
|
|
|
|
|
|
|
s.write(b'j')
|
|
|
|
s.write(addr)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
binfile = sys.argv[1]
|
|
|
|
with open(binfile, 'rb') as f:
|
|
|
|
dat = f.read()
|
|
|
|
|
|
|
|
s = serial.Serial(tty, baud, timeout=1)
|
|
|
|
write(s, offset, dat)
|
|
|
|
jump(s, offset)
|
2023-06-03 06:33:01 +00:00
|
|
|
last_dat = time.time()
|
2022-05-10 18:20:02 +00:00
|
|
|
|
2022-06-19 07:50:22 +00:00
|
|
|
while True:
|
|
|
|
dat = s.read()
|
|
|
|
if not dat:
|
2023-06-03 06:33:01 +00:00
|
|
|
if time.time() - last_dat > 1.0:
|
|
|
|
print('Data timeout')
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
last_dat = time.time()
|
2022-06-19 07:50:22 +00:00
|
|
|
sys.stdout.buffer.write(dat)
|
|
|
|
|
2022-05-10 18:20:02 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|