forked from mtirado1/at89s8252-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writedata.py
86 lines (78 loc) · 2.66 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import serial
import time
from intelhex import IntelHex
from config import *
# Path to hex file
f = data_targetFile
print('Reading file: ' + f)
# Serial port name
p = serialPort
print('Programmer port = ' + p)
# Read hex file
ih = IntelHex()
ih.fromfile(f, format='hex')
if ih.addresses()[len(ih.addresses()) - 1] >= at89s8252_max_data:
print('The file is too large ' + hex(ih.addresses()[len(ih.addresses()) - 1]) + '!')
print('Max space is ' + hex(at89s8252_max_data) + ' bytes, extra data will be ignored!')
a = input( '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 < at89s8252_max_data:
if conta == 0:
print()
print(hex(i) + ' ', end='')
if conta == 31:
conta = -1
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()
print('.', end = '')
print()
#a = input('Programming done. Do you wish to verify? y/n: ')
a = 'y'
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 < at89s8252_max_data:
if conta == 0:
print()
print(hex(i) + ' ', end='')
if conta == 31:
conta = -1
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]))
print('E', end = '')
err = True
else:
print('.', end = '')
print()
if not err:
print('Data verification complete.')
else:
rint('Data verification faillure.')
ser.write(b'\x40') # End programming
print(ser.readline().decode('utf-8'))
print('Done.')