flashfloppy/scripts/openocd/flash.py
2020-08-21 09:02:19 +01:00

22 lines
600 B
Python

# flash.py <hex_filename>
import os, sys, time, telnetlib
cmd = 'reset init ; flash write_image erase %s ; reset\n' % sys.argv[1]
# Start the OpenOCD daemon in the background and connect via telnet
def open_ocd():
os.system('openocd -f scripts/openocd/f1.cfg &')
while True:
time.sleep(0.5)
try:
t = telnetlib.Telnet('localhost', 4444)
except:
pass
else:
return t
with open_ocd() as t:
t.write(cmd.encode('utf-8'))
t.write('shutdown\n'.encode('utf-8'))
t.read_all() # Waits for EOF (telnet session shutdown)