Skip to content

Commit 606e17f

Browse files
committed
read (and verify) arbitrary block sizes. optionally read to file.
1 parent b7c091d commit 606e17f

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

bootload.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,32 @@ def download_code(ihx_file, serial_port):
3131
return True
3232

3333
def verify_code(ihx_file, serial_port):
34+
can_read_any= None
3435
for line in ihx_file.readlines():
3536
record_type = int(line[7:9], 16)
3637
if (record_type == 0x00):
37-
length= int(line[1:3], 16)
38-
start_addr= int(line[3:7], 16)
39-
data= line[9:9+(length*2)]
40-
# we can only read 16 byte chunks
41-
block_length= ((length / 16) + 1) * 16
38+
length = int(line[1:3], 16)
39+
start_addr = int(line[3:7], 16)
40+
data = line[9:9+(length*2)]
41+
# first time around, check if we can only read 16 byte chunks
42+
if can_read_any == None:
43+
can_read_any = False
44+
do_flash_read(serial_port, start_addr, 1)
45+
for read_data in serial_port:
46+
read_data = read_data.strip()
47+
if not read_data:
48+
continue
49+
if not read_data == ":00000001FF":
50+
can_read_any = True
51+
else:
52+
break
53+
if not can_read_any:
54+
print "*** warning! this version of CC-Bootloader can only read 16 byte blocks!"
55+
print "*** upgrade recommended!"
56+
if can_read_any:
57+
block_length= length
58+
else:
59+
block_length= ((length / 16) + 1) * 16
4260
print "\rVerifying %04d bytes at address: %04X" % (length, start_addr),
4361
do_flash_read(serial_port, start_addr, block_length)
4462
verify_data= ''
@@ -113,13 +131,16 @@ def do_flash_read(serial_port, start_addr, length):
113131
serial_port.write(":02%04X25%04X%02X\n" % (start_addr, length, chksum))
114132

115133

116-
def flash_read(serial_port, start_addr, length):
134+
def flash_read(ihx_file, serial_port, start_addr, length):
117135
do_flash_read(serial_port, start_addr, length)
118136
for line in serial_port:
119137
if not line == "\n":
138+
if(ihx_file):
139+
ihx_file.write(line)
140+
else:
120141
print line,
121-
if (line == ":00000001FF\n"):
122-
break
142+
if (line == ":00000001FF\n"):
143+
break
123144

124145
def print_usage():
125146
import sys
@@ -159,11 +180,11 @@ def print_usage():
159180
determine which page the user code starts on please check the
160181
USER_CODE_BASE setting in main.h.
161182
162-
read <start_addr> <len>
183+
read <start_addr> <len> [hex_file]
163184
164-
Reads len bytes from flash memory starting from start_addr. start_addr and
165-
len should be specified in hexadecimal (e.g. 0x1234) and len must be a multiple
166-
of 16. Output is compatible with download and verify commands.
185+
Reads len bytes from flash memory starting from start_addr and optionally
186+
write to hex_file. start_addr and len should be specified in hexadecimal
187+
(e.g. 0x1234).
167188
168189
verify <hex_file>
169190
@@ -212,8 +233,16 @@ def print_usage():
212233
if (len(options) < 2):
213234
print_usage()
214235
else:
215-
flash_read(serial_port, int(options[0], 16), int(options[1], 16))
216-
236+
ihx_file = None
237+
if(len(options) == 3):
238+
try:
239+
ihx_filename = options[2]
240+
ihx_file = open(ihx_filename, 'w')
241+
print 'reading to:', ihx_filename
242+
except:
243+
print "couldn't open output file:", ihx_filename
244+
exit(2)
245+
flash_read(ihx_file, serial_port, int(options[0], 16), int(options[1], 16))
217246

218247
else:
219248
print_usage()

src/intel_hex.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,38 +164,44 @@ void to_hex16_ascii(char buff[], uint16_t x) {
164164

165165
void ihx_read_print(__xdata uint8_t* start_addr, uint16_t len) {
166166
__xdata char buff[45];
167-
uint8_t byte, sum, i;
167+
uint8_t byte, sum, i, chunk;
168168

169-
while (len >= 0x10) {
169+
while (len) {
170170
sum = 0;
171+
if(len > 0x10)
172+
chunk = 0x10;
173+
else
174+
chunk = len;
171175

172176
buff[0] = ':';
173-
// Record length is 0x10
174-
to_hex8_ascii(&buff[1], 0x10);
175-
sum += 0x10;
177+
to_hex8_ascii(&buff[1], chunk);
178+
sum += chunk;
176179
// Write address into buffer
177180
to_hex16_ascii(&buff[3], (uint16_t)start_addr);
178181
sum += (uint16_t)start_addr & 0xFF;
179182
sum += ((uint16_t)start_addr >> 8) & 0xFF;
180183
// Write record type into buffer
181184
to_hex8_ascii(&buff[7], 0x00);
182185
// Write data bytes into buffer
183-
for (i=0; i<0x10; i++) {
186+
for (i=0; i<chunk; i++) {
184187
byte = *(start_addr+i);
185188
sum += byte;
186189
to_hex8_ascii(&buff[9 + 2*i], byte);
187190
}
188191
// Write checksum into buffer
189-
to_hex8_ascii(&buff[41], (uint8_t)(-(int8_t)sum));
190-
buff[43] = '\n';
191-
buff[44] = 0;
192+
to_hex8_ascii(&buff[9 + 2*i], (uint8_t)(-(int8_t)sum));
193+
buff[11 + 2*i] = '\n';
194+
buff[12 + 2*i] = 0;
192195

193196
// Print buffer over usb
194197
usb_putstr(buff);
195198

196199
// Updates for next go round
197-
start_addr += 0x10;
198-
len -= 0x10;
200+
if(len > 0x10) {
201+
start_addr += 0x10;
202+
len -= 0x10;
203+
} else // we're done
204+
len = 0;
199205
}
200206
usb_putstr(":00000001FF\n");
201207
}

0 commit comments

Comments
 (0)