Skip to content

Instantly share code, notes, and snippets.

@dekuNukem
Last active March 4, 2016 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekuNukem/0a5b482261914d0791fa to your computer and use it in GitHub Desktop.
Save dekuNukem/0a5b482261914d0791fa to your computer and use it in GitHub Desktop.
python script for uploading program to FAP computer
import sys
import time
import serial
ser = serial.Serial(sys.argv[1], 115200, timeout=0.5)
print("connected")
def read_eep(addr):
while 1:
print("reading addr " + str(addr) + "...")
ser.write(('r ' + str(addr) + '\r\n').encode())
result = ser.readline().decode().replace('\r\n', '')
try:
if result.startswith("rd:"):
result = result.lstrip('rd:').split(',')
if int(result[0].split("a=")[1]) != addr:
print("eepread: address mismatch")
continue
return int(result[1].split("d=")[1])
except Exception as e:
print("--------------- read exception -------------")
print(e)
print("----------")
continue
print("read timeout, retrying..")
def write_eep(addr, data):
while 1:
print("writing addr " + str(addr) + " " + str(data))
ser.write(('w ' + str(addr) + " " + str(data) + '\r\n').encode())
result = ser.readline().decode().replace('\r\n', '')
try:
if result.startswith("wr:"):
result = result.lstrip('rd:').split(',')
if int(result[0].split("a=")[1]) != addr:
print("eepwrite: address mismatch")
continue
if int(result[1].split("d=")[1]) != data:
print("eepwrite: data mismatch")
continue
return
except Exception as e:
print("--------------- read exception -------------")
print(e)
print("----------")
continue
print("write timeout, retrying..")
def zero_eeprom():
print("zeroing EEPROM...")
ser.write(('z\r\n').encode())
while 1:
result = ser.readline().decode().replace('\r\n', '')
if "zero complete" in result:
print("done")
return
zero_eeprom()
start_addr = 0
with open(sys.argv[2], "rb") as f:
while 1:
byte = f.read(1)
if byte == b"":
break
print(byte[0])
write_eep(start_addr, byte[0])
start_addr += 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment