summaryrefslogtreecommitdiff
path: root/src/write_eeprom.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/write_eeprom.py')
-rwxr-xr-xsrc/write_eeprom.py64
1 files changed, 45 insertions, 19 deletions
diff --git a/src/write_eeprom.py b/src/write_eeprom.py
index 9b76e51..e0a8604 100755
--- a/src/write_eeprom.py
+++ b/src/write_eeprom.py
@@ -2,9 +2,11 @@
import serial
import argparse
import os
+from time import sleep
+from math import ceil
#we send syn, recv ack
-SYN_MAGIC = 0xdeafbeef
+SYN_MAGIC = 0xdeadbeef
ACK_MAGIC = 0xf00dd00d
EEPROM_PAGESIZE = 64
@@ -18,33 +20,57 @@ args = parser.parse_args()
eeprom_file = open("./compiled_eeprom", "rb")
eeprom_filesize = os.fstat(eeprom_file.fileno()).st_size
+page_len = ceil(eeprom_filesize / EEPROM_PAGESIZE)
-s = serial.Serial(args.port, args.baud, timeout = 0)
-s.write(SYN_MAGIC)
+s = serial.Serial(port=args.port, baudrate=args.baud, parity=serial.PARITY_NONE, timeout=0, dsrdtr=1)
-def handle_writer_error():
+#slow method
+def write_hwf(data):
+ debugval = 0
+ print("writing")
+ data_parsed = [data[b:b+1] for b in range(len(data))]
+ for c in data_parsed:
+ print(debugval)
+ debugval += 1
+ s.rts = 1
+ while not s.cts:
+ pass
+ s.rts = 0
+ s.write(c)
+
+def read_hwf(length):
+ print("reading")
+ msg = b''
+ for c in range(length):
+ s.rts = 1
+ while not s.cts:
+ pass
+ s.rts = 0
+ msg += s.read(size=1)
+ return(msg)
+
+def handle_writer_error(write_status):
if write_status == 1: # verification failed
print("Verification failed on page {}!".format(page))
elif write_status == 0xff:
print("Unknown error writing page {}!".format(page))
+#while True:
+# write_hwf(b"abcdef\r\n")
+# print(read_hwf(10))
-while not s.read(4) == ACK_MAGIC:
- pass
-
-print("Device detected, writing eeprom...")
-for page in range(0, int(eeprom_filesize / EEPROM_PAGESIZE)):
- s.write(eeprom_file.read(EEPROM_PAGESIZE))
- write_status = s.read(1)
- if not write_status == 0:
- handle_writer_error(write_status)
+write_hwf(page_len.to_bytes(1, 'little'))
-if eeprom_file.tell() < eeprom_filesize:
- s.write(eeprom_file.read())
- [s.write(0) for b in range(0, eeprom_filesize % EEPROM_PAGESIZE)]
- write_status = s.read(1)
+for page in range(0, page_len):
+ print(page)
+ if EEPROM_PAGESIZE > (eeprom_filesize - eeprom_file.tell()):
+ write_hwf(eeprom_file.read())
+ [write_hwf(b'\x00') for b in range(0, eeprom_filesize % EEPROM_PAGESIZE)]
+ else:
+ write_hwf(eeprom_file.read(EEPROM_PAGESIZE))
+ write_status = read_hwf(1)
+ print("WRITE STATUS: {}".format(write_status))
if not write_status == 0:
handle_writer_error(write_status)
-
-print("Done writing to EEPROM! You are still alone!")
+print("Done writing to EEPROM!\n")