-
Notifications
You must be signed in to change notification settings - Fork 2
/
writedata.py
72 lines (63 loc) · 2.06 KB
/
writedata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import serial
import time
from intelhex import IntelHex
from config import *
# Path to hex file
f = data_targetFile
print(f)
# Serial port name
p = serialPort
print(p)
# Read hex file
ih = IntelHex()
ih.fromfile(f, format='hex')
if ih.addresses()[len(ih.addresses()) - 1] > 0x7FF:
a = input('The data is too large, max data is 2048 bytes, continue? y/n: ')
if a != 'Y' and a != 'y':
quit()
with serial.Serial(p, 9600) as ser:
time.sleep(2)
print(ser.readline().decode('utf-8'))
ser.write(b'\x50') # Enable programming
ih.dump()
print('Programming...')
print(ser.readline().decode('utf-8'))
conta = 0
for i in range(0, len(ih.addresses())):
addr = ih.addresses()[i]
if addr <= 0x7FF:
if conta == 256:
conta = 0
print(hex(addr))
conta += 1
ser.write(b'\x55')
ser.write(bytes([addr//256])) # high address byte
ser.write(bytes([addr%256])) # low address byte
ser.write(bytes([ih[addr]])) # data byte
#time.sleep(0.05)
ser.readline()
a = input('Programming done. Do you wish to verify? y/n: ')
conta = 0
if a == 'Y' or a == 'y':
print('Verifying...')
err = False
for i in range(0, len(ih.addresses())):
addr = ih.addresses()[i]
if addr <= 0x7FF:
if conta == 256:
conta = 0
print(hex(addr))
conta += 1
ser.write(b'\x54')
ser.write(bytes([addr//256])) # high address byte
ser.write(bytes([addr%256])) # low address byte
k = int(ser.readline().decode('utf-8'), 16)
if k != ih[addr]:
print('Error at address' + hex(addr))
print('Got %d, was %d' % (k, ih[addr]))
err = True
if not err:
print('Verification complete.')
ser.write(b'\x40') # End programming
print(ser.readline().decode('utf-8'))
print('Done.')