-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcanloader.py
More file actions
248 lines (216 loc) · 8.17 KB
/
canloader.py
File metadata and controls
248 lines (216 loc) · 8.17 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
'''
Load firmware to OpenLCB device using FLIP CAN ISP protocol. Requires IntelHex library, http://www.bialix.com/intelhex/
@author: D.E. Goodman-Wilson
@author: Bob Jacobsen
'''
import connection as connection
from canbootloaderutils import *
from intelhex import IntelHex
CAN_SELECT_NODE = 0x00
CAN_PROG_START = 0x01
CAN_PROG_DATA = 0x02
CAN_DISPLAY_DATA = 0x03
CAN_START_APPLICATION = 0x04
CAN_SELECT_MEM_PAGE = 0x06
CAN_ERROR = 0x06
#TODO
BOOTLOADER_VERSION = 0xA5
def selectNode(CRIS, NNB, connection, verbose) :
print "In selectNode"
frame = makeframestring(CRIS, CAN_SELECT_NODE, [NNB])
response = False
resp_frame = ''
counter = 100
while (not response) and counter:
print "sending CAN_SELECT_NODE"
connection.network.send(frame)
resp_frame = connection.network.receive()
if (resp_frame != None) and isFrameType(resp_frame, CAN_SELECT_NODE, CRIS):
print "node selected!"
response = True
else:
counter -= 1
if(counter == 0): return False
#now examine the response more carefully
data = bodyArray(resp_frame)
print "data contains ",
print data
if(data[0] >= BOOTLOADER_VERSION) and (data[1] == 1):
print "selectNode success! Done"
return True
print "selectNode fail!"
return False
def expect(good, bad, connection, verbose) :
counter = 0
while counter <= 1000:
response = connection.network.receive()
if response != None:
response = response.strip()
if response == good:
return True
elif bad != None and response == bad:
return False
else:
counter += 1
return False
FLASH_SPACE = 0
EEPROM_SPACE = 1
SIGNATURE_SPACE = 2
BOOTLOADER_INFORMATION_SPACE = 3
BOOTLOADER_CONFIGURATION_SPACE = 4
DEVICE_REGISTER_SPACE = 5
def selectMemorySpace(space, CRIS, connection, verbose):
frame = makeframestring(CRIS, CAN_SELECT_MEM_PAGE, [0x03, space, 0x00])
expected_response = makeframestring(CRIS, CAN_SELECT_MEM_PAGE, [0x00])
error_response = makeframestring(CRIS, CAN_ERROR, [0x00])
connection.network.send(frame)
return expect(expected_response, error_response, connection, verbose)
def selectMemoryPage(page, CRIS, connection, verbose):
frame = makeframestring(CRIS, CAN_SELECT_MEM_PAGE, [0x02, FLASH_SPACE, page])
expected_response = makeframestring(CRIS, CAN_SELECT_MEM_PAGE, [0x00])
error_response = makeframestring(CRIS, CAN_ERROR, [0x00])
connection.network.send(frame)
return expect(expected_response, error_response, connection, verbose)
def eraseMemory(CRIS, connection, verbose) :
print "eraseMemory"
frame = makeframestring(CRIS, CAN_PROG_START, [0x80, 0xFF, 0xFF])
expected_response = makeframestring(CRIS, CAN_PROG_START, [0x00])
print expected_response
error_response = makeframestring(CRIS, CAN_ERROR, [0x00])
connection.network.send(frame)
return expect(expected_response, error_response, connection, verbose)
def startWrite(start, end, CRIS, connection, verbose) :
frame = makeframestring(CRIS, CAN_PROG_START, [0x00, start>>8, start & 0xFF, end>>8, end & 0xFF])
expected_response = makeframestring(CRIS, CAN_PROG_START, None)
error_response = makeframestring(CRIS, CAN_ERROR, [0x00])
connection.network.send(frame)
return expect(expected_response, error_response, connection, verbose)
def writeMemory(ih, zero_index, start, end, CRIS, connection, verbose) :
if verbose: print "WRITE"
if not startWrite(start, end, CRIS, connection, verbose) :
return False
address = start
while(address < end):
incr = min(8, (end+1) - address)
#copy data
data = []
for i in range(incr):
data.append(ih[zero_index+i])
frame = makeframestring(CRIS, CAN_PROG_DATA, data)
connection.network.send(frame)
done = False
while not done:
response = connection.network.receive()
if(response != None) and isFrameType(response, CAN_PROG_DATA, CRIS):
payload = bodyArray(response)
if payload[0] != 0x00 and payload[0] != 0x02:
print "ERROR WRITING"
return False
else:
done = True
address += incr
zero_index += incr
return True
def verifyMemory(ih, CRIS, connection, verbose) :
if verbose: print "VERIFY"
address = ih.minaddr()
while(address < ih.maxaddr()):
incr = min(8, (ih.maxaddr()+1) - address)
frame = makeframestring(CRIS, CAN_DISPLAY_DATA, [0x00, address>>8, address&0xFF, (address+incr-1)>>8, (address+incr-1)&0xFF])
connection.network.send(frame)
done = False
while not done:
response = connection.network.receive()
if (response != None) and isFrameType(response, CAN_DISPLAY_DATA, CRIS):
payload = bodyArray(response)
for i in range(len(payload)):
if payload[i] != ih[address + i]:
print "ERROR IN VERIFY!"
exit
address += len(payload)
done = True
return True
def startApplication(CRIS, connection, verbose) :
frame = makeframestring(CRIS, CAN_START_APPLICATION, [0x03, 0x00])
connection.network.send(frame)
def usage() :
print ""
print "TODO Called standalone, will send one CAN datagram message"
print " and display response."
print ""
print "Expect a single datagram reply in return"
print "e.g. [1Esssddd] 4C"
print "from destination alias to source alias"
print ""
print "Default connection detail taken from connection.py"
print ""
print "-d --dest dest alias (default 0x"+hex(connection.testNodeAlias).upper()+")"
print "-t find destination alias semi-automatically"
print "-v verbose"
print "-V Very verbose"
if __name__ == '__main__':
import getopt, sys
# argument processing
dest = connection.testNodeAlias
hexfile = 'Io_16P_16C_default.hex'
verbose = False
identifynode = False
try:
opts, remainder = getopt.getopt(sys.argv[1:], "f:d:vVt", ["dest=", "alias=", "content="])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for opt, arg in opts:
if opt == "-v":
verbose = True
elif opt == "-V" :
connection.network.verbose = True
verbose = True
elif opt in ("-d", "--dest"): # needs hex processing
dest = int(arg)
elif opt == "-t":
identifynode = True
elif opt == "-f":
hexfile = arg
else:
assert False, "unhandled option"
# if identifynode :
# import getUnderTestAlias
# dest, nodeID = getUnderTestAlias.get(alias, None, verbose)
############ main code
#fake an alias
#dest = 0x703
#CRIS = (dest>>4) & 0xFF
#NNB = dest & 0x0F
#use universal address, will program any node in bootloader mode on network. Will probably break if more than one :(
CRIS = 0x00
NNB = 0xFF
if not selectNode(CRIS, NNB, connection, verbose): exit(1)
#connected, now what?
#first, verify that this is an AT90CAN128
# selectMemorySpace(SIGNATURE_SPACE, CRIS, connection, verbose)
#first, select the Flash memory space
# selectMemorySpace(FLASH_SPACE, CRIS, connection, verbose)
# selectMemoryPage(0, CRIS, connection, verbose)
#now, read the HEX file, and start writing
ih = IntelHex(hexfile)
address = ih.minaddr()
max = ih.maxaddr()
if(max > 0xFFFF): #have to do this in two pages
max = 0xFFFF
#now, start programming
eraseMemory(CRIS, connection, verbose)
writeMemory(ih, ih.minaddr(), ih.minaddr(), max, CRIS, connection, verbose)
#and now, verify
verifyMemory(ih, CRIS, connection, verbose)
#finally, set bootloader flag, and start application
selectMemorySpace(EEPROM_SPACE, CRIS, connection, verbose)
writeMemory([0x00, 0x00], 0x00, 0x0FF8, 0x0FF9, CRIS, connection, verbose)
frame = makeframestring(CRIS, CAN_DISPLAY_DATA, [0x00, 0x0F, 0xF7, 0x0F, 0xFF])
connection.network.send(frame)
resp_frame = connection.network.receive()
resp_frame = connection.network.receive()
startApplication(CRIS, connection, verbose)