@@ -31,14 +31,32 @@ def download_code(ihx_file, serial_port):
3131 return True
3232
3333def 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 "\r Verifying %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
124145def 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 ()
0 commit comments