-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlpcisp.py
349 lines (291 loc) · 10.6 KB
/
lpcisp.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import binascii
import time
import enum
import serial
part_numbers = {
0x0402FF01: 'LPC2141',
0x0402FF11: 'LPC2142',
0x0402FF12: 'LPC2144',
0x0402FF23: 'LPC2146',
0x0402FF25: 'LPC2148',
0x1600F701: 'LPC2361',
0x1600FF22: 'LPC2362',
0x1600F902: 'LPC2364',
0x1600E823: 'LPC2365',
0x1600F923: 'LPC2366',
0x1600E825: 'LPC2367',
0x1600F925: 'LPC2368',
0x1700E825: 'LPC2377',
0x1700FD25: 'LPC2378',
0x1700FF35: 'LPC2387',
0x1800F935: 'LPC2387 (older)',
0x1800FF35: 'LPC2388',
}
class ReturnCode(enum.Enum):
CMD_SUCCESS = 0
INVALID_COMMAND = 1
SRC_ADDR_ERROR = 2
DST_ADDR_ERROR = 3
SRC_ADDR_NOT_MAPPED = 4
DST_ADDR_NOT_MAPPED = 5
COUNT_ERROR = 6
INVALID_SECTOR = 7
SECTOR_NOT_BLANK = 8
SECTOR_NOT_PREP_WRITE_OP = 9
COMPARE_ERROR = 10
BUSY = 11
PARAM_ERROR = 12
ADDR_ERROR = 13
ADDR_NOT_MAPPED = 14
CMD_LOCKED = 15
INVALID_CODE = 16
INVALID_BAUD_RATE = 17
INVALID_STOP_BIT = 18
CODE_READ_PROTECTION_ENABLED = 19
INVALID_FLASH_UNIT = 20
USER_CODE_CHECKSUM = 21
ERROR_SETTING_ACTIVE_PARTITION = 22
class ISP(object):
def __init__(self, port, baud, stopbits=1, timeout=2.0, tgtclk=12000, sync_timeout=30.0):
# Don't allow an infinite timeout, this class won't work if that is used
assert timeout
# Target clock must be > 10MHz
assert int(tgtclk) >= 10000
self._args = {
'port': port,
'baud': int(baud),
'stopbits': int(stopbits),
'timeout': int(timeout),
'tgtclk': int(tgtclk),
'sync_timeout': int(sync_timeout),
}
self._echo = True
if self._args['stopbits'] == 2:
ser_stopbits = serial.STOPBITS_TWO
else:
# default to 1 stop bit
ser_stopbits = serial.STOPBITS_ONE
s_args = {
'port': self._args['port'],
'baudrate': self._args['baud'],
'stopbits': ser_stopbits,
'timeout': self._args['timeout'],
}
self.s = serial.Serial(**s_args)
self.synchronize()
@property
def echo(self):
return self._echo
@echo.setter
def echo(self, mode):
assert isinstance(mode, bool)
response = self._echo_cmd(mode)
if response == 'OK':
self._echo = mode
def reset(self, delay=0.5):
self.s.dtr = True
self.s.rts = True
time.sleep(delay)
self.s.dtr = False
time.sleep(delay)
def cancel_cmd(self):
# Cancel the command to reduce weird debug states
self.s.write(b'\x1b')
# If echo is on read that one char back
if self.echo:
self.s.read(1)
def cmd(self, cmd, return_code=True, lines=None, timeout=None):
# Allow the default timeout to be overridden
if timeout:
self.s.timeout = timeout
# Ensure command is bytes
if not isinstance(cmd, bytes):
cmd = cmd.encode()
# If the command was not in bytes check if it needs \r\n appended
if cmd[-2:] != b'\r\n':
cmd += b'\r\n'
self.s.write(cmd)
response_lines = []
# If echo is on the first response line may be an echo of the command
if self.echo:
response_lines.append(self.s.read_until())
# If the line read does not match the command count it as one of the
# response lines
if response_lines[0] != cmd:
lines -= 1
# If a return code is expected, read that now
if return_code:
response_lines.append(self.s.read_until())
if lines is not None:
# if there is a specific number of lines to receive, read them now
response_lines.extend([self.s.read_until() for i in range(lines)])
else:
# Otherwise just read until the timeout is reached
response_lines.extend(self.s.readlines())
#print(f'{cmd} -> {response_lines}')
# If the timeout was modified restore the default
if timeout:
self.s.timeout = self._args['timeout']
# If echo is on the first response may be an echo of the command, drop
# that
if len(response_lines) >= 1 and self.echo and response_lines[0] == cmd:
response_lines = response_lines[1:]
# If the response flag is set convert the next response line into the
# return code
if len(response_lines) >= 1 and return_code:
retcode = ReturnCode(int(response_lines[0]))
if retcode != ReturnCode.CMD_SUCCESS:
self.cancel_cmd()
raise Exception(f'Command "{cmd}" Failed: {retcode}')
response_lines = response_lines[1:]
# convert from bytes to string and drop the \r\n
converted_responses = [l[:-2].decode('latin-1') for l in response_lines]
if not converted_responses:
response = None
elif len(converted_responses) == 1:
response = converted_responses[0]
else:
response = converted_responses
return response
def synchronize(self):
start = time.time()
while True:
now = time.time()
if now - start > 10.0:
raise Exception('Unable to synchronize!')
self.reset()
# Don't use self.cmd() because we don't want \r\n appended the sync
# byte
response = self.cmd(b'?', return_code=False, lines=1)
# Response is bytes because self.cmd() wasn't used
if response != 'Synchronized':
continue
response = self.cmd('Synchronized', return_code=False, lines=1)
if response != 'OK':
continue
response = self.cmd(f'{self._args["tgtclk"]}', return_code=False, lines=1)
if response != 'OK':
continue
# success!
break
def unlock(self, code=23130):
return self.cmd(f'U {code}')
def set_baud_rate(self):
return self.cmd(f'B {self._args["baud"]} {self._args["stopbits"]}')
def _echo_cmd(self, mode):
self.cmd('A 1')
def write_to_ram(self, data, addr, size):
raise NotImplementedError
def _checksum(self, data):
checksum = 0
for b in data:
checksum += b
# 2 or 4-byte checksum?
checksum &= 0xFFFFFFFF
return checksum
def uudecode(self, encoded):
# The first char of the line is the length. The ISP tends to repeat the
# last byte for padding which weirds out python so round the size of the
# line up to a multiple of 3.
size = encoded[0] - 0x20
pad = size % 3
if pad:
encoded = bytes([encoded[0] + (3 - pad)]) + encoded[1:]
decoded = binascii.a2b_uu(encoded)
# Now drop the padding bytes from the decoded data
decoded = decoded[:size]
#print(f'{encoded} -> {decoded}')
return decoded
def _read_data(self, size, timeout=60.0):
#print(f'reading {size} bytes')
# Override serial timeout
self.s.timeout = None
start = time.time()
data = b''
block_data = b''
blocks = 0
while len(data) < size:
now = time.time()
if now - start > timeout:
self.cancel_cmd()
raise Exception('Read data timeout!')
line = self.s.read_until()
#print(line)
# See if this is a checksum line
try:
recvd_checksum = int(line)
checksum = self._checksum(block_data)
if checksum == recvd_checksum:
#print(f'BLOCK {blocks} OK ({checksum})')
self.cmd('OK', return_code=False, lines=0)
blocks += 1
data += block_data
else:
#print(f'BLOCK {blocks} ERROR ({checksum})')
self.cmd('RESEND', return_code=False, lines=0)
block_data = b''
except ValueError:
decoded = self.uudecode(line)
block_data += decoded
# restore the default timeout
self.s.timeout = self._args['timeout']
return data
def read_memory(self, addr, size, timeout=60.0):
# Ensure address is word-aligned
assert addr % 4 == 0
assert size % 4 == 0
self.cmd(f'R {addr} {size}', lines=0)
return self._read_data(size, timeout=timeout)
def unprotect_sector(self, start_sector, end_sector=None):
#if end_sector is None:
# end_sector = start_sector
#assert end_sector >= start_sector
#self.cmd(f'P {start_sector} {end_sector}')
raise NotImplementedError
def copy_ram_to_flash(self, flash_addr, ram_addr, size):
# Ensure flash_addr is sector-aligned
assert flash_addr % 256 == 0
assert size in [256, 512, 1024, 4096]
#self.cmd(f'C {flash_addr} {ram_addr} {size}')
raise NotImplementedError
def go(self, addr, mode='A'):
assert addr % 4 == 0
assert mode in ['A', 'T']
return self.cmd(f'G {addr} {mode}')
def erase_sectors(self, start_sector, end_sector=None):
#if end_sector is None:
# end_sector = start_sector
#assert end_sector >= start_sector
#self.cmd(f'E {start_sector} {end_sector}')
raise NotImplementedError
def blank_check_sector(self, start_sector, end_sector=None):
if end_sector is None:
end_sector = start_sector
assert end_sector >= start_sector
self.cmd(f'I {start_sector} {end_sector}')
def read_part_id(self, read_rev=False):
part_response = self.cmd('J')
try:
part = part_numbers[int(part_response[1])]
except KeyError:
part = f'UNKNOWN ({part_response[1]})'
if read_rev:
#rev_bytes = self.read_memory(0x7FFFE070, 4)
rev_bytes = self.read_memory(0x0007E070, 4)
try:
rev_val = int(rev_bytes)
if rev_val == 0:
rev = '-'
elif rev_val >= 1 and rev_val <= 26:
rev = 'A' + rev_val
else:
rev = f'UNKNOWN ({rev_bytes.hex()})'
except ValueError:
rev = f'UNKNOWN ({rev_bytes.hex()})'
return (part, rev)
else:
return part
def read_boot_code_version(self):
return self.cmd('K')
def compare(self, addr1, addr2, size):
self.cmd(f'M {addr1} {addr2} {size}')