forked from mtirado1/at89s8252-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteprogramfast3.py
140 lines (128 loc) · 5.17 KB
/
writeprogramfast3.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import serial
import time
from intelhex import IntelHex
from config import *
# Path to hex file
f = code_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] >= at89s8253_max_program:
print('The file is too large ' + hex(ih.addresses()[len(ih.addresses()) - 1]) + '!')
print('Max space is ' + hex(at89s8253_max_program) + ' 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'\x60') # Enable programming
ih.dump()
print('Programming...')
print(ser.readline().decode('utf-8'))
ultimo = 0
# for i in range(0, len(ih.addresses()), 64):
for i in range(0x0, at89s8253_max_program, 64):
#addr = ih.addresses()[i]
#addr = 0
if i < at89s8253_max_program:
#if conta == 255:
# print(hex(addr))
#if conta == 256:
# conta = 0
#conta += 1
print(hex(i) + " ", end = '')
ser.write(b'\x74')
#ser.write(bytes([addr//256])) # high address byte
#ser.write(bytes([addr%256])) # low address byte
ser.write(bytes([ultimo//256])) # high address byte
ser.write(bytes([ultimo%256])) # low address byte
for o in range(0, 64):
#ultimo = addr + o;
ser.write(bytes([ih[ultimo]])) # data byte
print('.', end = '')
ultimo = ultimo + 1;
#time.sleep(0.05)
print()
ser.readline()
if ultimo > ih.maxaddr():
break
#if ultimo != ih.addresses()[len(ih.addresses()) - 2 ]:
# print('manca ultimo:' + hex(ultimo) + ' len:' + hex(ih.addresses()[len(ih.addresses()) - 2]))
# addr = ultimo + 1;
# ser.write(b'\x74')
# ser.write(bytes([addr//256])) # high address byte
# ser.write(bytes([addr%256])) # low address byte
# for o in range(0, 64):
# ultimo = addr + o;
# ser.write(bytes([ih[ultimo]])) # data byte
# print('.', end = '')
# print()
# for i in range(ultimo + 1, len(ih.addresses())):
# addr = ih.addresses()[i]
# if addr < at89s8253_max_program:
# ser.write(b'\x61')
# 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: ')
a = 'y'
#conta = 0
if a == 'Y' or a == 'y':
print('Verifying...')
err = False
#for i in range(0, len(ih.addresses()), 64):
ultimo = 0
for i in range(0x0, at89s8253_max_program, 64):
#addr = ih.addresses()[i]
#print(hex(i) + " ", end = '')
if i < at89s8253_max_program:
print(hex(i) + " ", end = '')
ser.write(b'\x72')
ser.write(bytes([ultimo//256])) # high address byte
ser.write(bytes([ultimo%256])) # low address byte
for o in range(0, 64):
k = int(ser.readline().decode('utf-8'), 16)
#print('.', end = '')
if k != ih[ultimo]:
#print()
#print('Error at address' + hex(addr))
#print('Got %d, was %d' % (k, ih[addr]))
err = True
print('E', end = '')
else:
print('.', end = '')
ultimo = ultimo + 1;
print()
if ultimo > ih.maxaddr():
break
#if ultimo != ih.addresses()[len(ih.addresses()) - 2 ]:
#print('manca ultimo:' + hex(ultimo) + ' len:' + hex(ih.addresses()[len(ih.addresses()) - 2]))
# print(hex(ultimo+1))
# for i in range(ultimo + 1, len(ih.addresses())):
# addr = ih.addresses()[i]
# if addr < at89s8253_max_program:
# ser.write(b'\x62')
# ser.write(bytes([addr//256])) # high address byte
# ser.write(bytes([addr%256])) # low address byte
# k = int(ser.readline().decode('utf-8'), 16)
# print('.', end = '')
# if k != ih[addr]:
# print()
# print('Error at address' + hex(addr))
# print('Got %d, was %d' % (k, ih[addr]))
# err = True
# print()
if not err:
print('Program verification complete.')
else:
print('Program verification faillure.')
ser.write(b'\x40') # End programming
print(ser.readline().decode('utf-8'))
print('Done.')