flash.py 600 B

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