@@ -172,7 +172,7 @@ def phase_setup(ser):
172172 if (packet ['timeout' ] or packet ['crc' ]):
173173 return 1
174174
175- verboseprint ('\t Got SVL Bootloader Version: ' +
175+ twopartprint ('\t ' , 'Got SVL Bootloader Version: ' +
176176 str (int .from_bytes (packet ['data' ], 'big' )))
177177 verboseprint ('\t Sending \' enter bootloader\' command' )
178178
@@ -194,6 +194,9 @@ def phase_bootload(ser):
194194
195195 frame_size = 512 * 4
196196
197+ resend_max = 64
198+ resend_count = 0
199+
197200 verboseprint ('\n phase:\t bootload' )
198201
199202 with open (args .binfile , mode = 'rb' ) as binfile :
@@ -206,23 +209,31 @@ def phase_bootload(ser):
206209 verboseprint ('\t have ' + str (total_len ) + ' bytes to send in ' + str (total_frames ) + ' frames' )
207210
208211 bl_done = False
209- while (not bl_done ):
212+ bl_failed = False
213+ while ((not bl_done ) and (not bl_failed )):
210214
211215 packet = wait_for_packet (ser ) # wait for indication by Artemis
212216 if (packet ['timeout' ] or packet ['crc' ]):
213217 print ('\n \t error receiving packet' )
214218 print (packet )
215219 print ('\n ' )
216- return 1
220+ bl_failed = True
221+ bl_done = True
217222
218223 if ( packet ['cmd' ] == SVL_CMD_NEXT ):
219224 # verboseprint('\tgot frame request')
220225 curr_frame += 1
226+ resend_count = 0
221227 elif ( packet ['cmd' ] == SVL_CMD_RETRY ):
222228 verboseprint ('\t \t retrying...' )
229+ resend_count += 1
230+ if ( resend_count >= resend_max ):
231+ bl_failed = True
232+ bl_done = True
223233 else :
224234 print ('unknown error' )
225- return 1
235+ bl_failed = True
236+ bl_done = True
226237
227238 if ( curr_frame <= total_frames ):
228239 frame_data = application [((curr_frame - 1 )* frame_size ):((curr_frame - 1 + 1 )* frame_size )]
@@ -234,8 +245,12 @@ def phase_bootload(ser):
234245 send_packet (ser , SVL_CMD_DONE , b'' )
235246 bl_done = True
236247
237- verboseprint ('\n \t ' )
238- print ('Upload complete' )
248+ if ( bl_failed == False ):
249+ twopartprint ('\n \t ' , 'Upload complete' )
250+ else :
251+ twopartprint ('\n \t ' , 'Upload failed' )
252+
253+ return bl_failed
239254
240255
241256
@@ -281,16 +296,23 @@ def phase_serial_port_help():
281296# ***********************************************************************************
282297def main ():
283298 try :
284- with serial . Serial ( args . port , args . baud , timeout = args . timeout ) as ser :
299+ num_tries = 3
285300
286- t_su = 0.15 # startup time for Artemis bootloader (experimentally determined - 0.095 sec min delay )
301+ print ( ' \n \n Artemis SVL Bootloader' )
287302
288- print ( ' \n \n Artemis SVL Bootloader' )
303+ for _ in range ( num_tries ):
289304
290- time .sleep (t_su ) # Allow Artemis to come out of reset
291- phase_setup (ser ) # Perform baud rate negotiation
305+ with serial .Serial (args .port , args .baud , timeout = args .timeout ) as ser :
292306
293- phase_bootload (ser ) # Bootload
307+ t_su = 0.15 # startup time for Artemis bootloader (experimentally determined - 0.095 sec min delay)
308+
309+ time .sleep (t_su ) # Allow Artemis to come out of reset
310+ phase_setup (ser ) # Perform baud rate negotiation
311+
312+ bl_failed = phase_bootload (ser ) # Bootload
313+
314+ if ( bl_failed == False ):
315+ break
294316
295317 except :
296318 phase_serial_port_help ()
@@ -341,4 +363,10 @@ def verboseprint(*args):
341363 else :
342364 verboseprint = lambda * a : None # do-nothing function
343365
366+ def twopartprint (verbosestr , printstr ):
367+ if args .verbose :
368+ print (verbosestr , end = '' )
369+
370+ print (printstr )
371+
344372 main ()
0 commit comments