@@ -250,127 +250,128 @@ def argument(arg):
250250 async def interact (self , device , args , avr_iface ):
251251 await avr_iface .programming_enable ()
252252
253- signature = await avr_iface .read_signature ()
254- device = devices_by_signature [signature ]
255- self .logger .info ("device signature: %s (%s)" ,
256- "{:02x} {:02x} {:02x}" .format (* signature ),
257- "unknown" if device is None else device .name )
258-
259- if args .operation in (None , "identify" ):
260- return
261-
262- if device is None :
263- raise ProgramAVRError ("cannot operate on unknown device" )
264-
265- if device .erase_time is not None :
266- avr_iface .erase_time = device .erase_time
267-
268- if args .operation == "read" :
269- if args .fuses :
270- fuses = await avr_iface .read_fuse_range (range (device .fuses_size ))
271- if device .fuses_size > 2 :
272- self .logger .info ("fuses: low %s high %s extra %s" ,
273- f"{ fuses [0 ]:08b} " ,
274- f"{ fuses [1 ]:08b} " ,
275- f"{ fuses [2 ]:08b} " )
276- elif device .fuses_size > 1 :
277- self .logger .info ("fuses: low %s high %s" ,
278- f"{ fuses [0 ]:08b} " ,
279- f"{ fuses [1 ]:08b} " )
280- else :
281- self .logger .info ("fuse: %s" , f"{ fuses [0 ]:08b} " )
282-
283- if args .lock_bits :
284- lock_bits = await avr_iface .read_lock_bits ()
285- self .logger .info ("lock bits: %s" , f"{ lock_bits :08b} " )
286-
287- if args .calibration :
288- calibration = \
289- await avr_iface .read_calibration_range (range (device .calibration_size ))
290- self .logger .info ("calibration bytes: %s" ,
291- " " .join (["%02x" % b for b in calibration ]))
292-
293- if args .program :
294- program_file , program_fmt = args .program
295- self .logger .info ("reading program memory (%d bytes)" , device .program_size )
296- output_data (program_file ,
297- await avr_iface .read_program_memory_range (range (device .program_size )),
298- program_fmt )
299-
300- if args .eeprom :
301- eeprom_file , eeprom_fmt = args .eeprom
302- self .logger .info ("reading EEPROM (%d bytes)" , device .eeprom_size )
303- output_data (eeprom_file ,
304- await avr_iface .read_eeprom_range (range (device .eeprom_size )),
305- eeprom_fmt )
306-
307- if args .operation == "write-fuses" :
308- if args .high and device .fuses_size < 2 :
309- raise ProgramAVRError ("device does not have high fuse" )
310-
311- if args .low :
312- self .logger .info ("writing low fuse" )
313- await avr_iface .write_fuse (0 , args .low )
314- written = await avr_iface .read_fuse (0 )
315- if written != args .low :
316- raise ProgramAVRError ("verification of low fuse failed: %s" %
317- f"{ written :08b} != { args .low :08b} " )
318-
319- if args .high :
320- self .logger .info ("writing high fuse" )
321- await avr_iface .write_fuse (1 , args .high )
322- written = await avr_iface .read_fuse (1 )
323- if written != args .high :
324- raise ProgramAVRError ("verification of high fuse failed: %s" %
325- f"{ written :08b} != { args .high :08b} " )
326-
327- if args .extra :
328- self .logger .info ("writing extra fuse" )
329- await avr_iface .write_fuse (2 , args .extra )
330- written = await avr_iface .read_fuse (2 )
331- if written != args .extra :
332- raise ProgramAVRError ("verification of extra fuse failed: %s" %
333- f"{ written :08b} != { args .extra :08b} " )
334-
335- if args .operation == "write-lock" :
336- self .logger .info ("writing lock bits" )
337- await avr_iface .write_lock_bits (args .bits )
338- written = await avr_iface .read_lock_bits ()
339- if written != args .bits :
340- raise ProgramAVRError ("verification of lock bits failed: %s" %
341- f"{ written :08b} != { args .bits :08b} " )
342-
343- if args .operation == "write-program" :
344- self .logger .info ("erasing chip" )
345- await avr_iface .chip_erase ()
346-
347- program_file , program_fmt = args .file
348- data = input_data (program_file , program_fmt )
349- self .logger .info ("writing program memory (%d bytes)" ,
350- sum ([len (chunk ) for address , chunk in data ]))
351- for address , chunk in data :
352- chunk = bytes (chunk )
353- await avr_iface .write_program_memory_range (address , chunk , device .program_page )
354- written = await avr_iface .read_program_memory_range (range (address , address + len (chunk )))
355- if written != chunk :
356- raise ProgramAVRError ("verification failed at address %#06x: %s != %s" %
357- (address , written .hex (), chunk .hex ()))
358-
359- if args .operation == "write-eeprom" :
360- eeprom_file , eeprom_fmt = args .file
361- data = input_data (eeprom_file , eeprom_fmt )
362- self .logger .info ("writing EEPROM (%d bytes)" ,
363- sum ([len (chunk ) for address , chunk in data ]))
364- for address , chunk in data :
365- chunk = bytes (chunk )
366- await avr_iface .write_eeprom_range (address , chunk , device .eeprom_page )
367- written = await avr_iface .read_eeprom_range (range (address , len (chunk )))
368- if written != chunk :
369- raise ProgramAVRError ("verification failed at address %#06x: %s != %s" %
370- (address , written .hex (), chunk .hex ()))
371-
372- if args .operation == "erase" :
373- self .logger .info ("erasing device" )
374- await avr_iface .chip_erase ()
375-
376- await avr_iface .programming_disable ()
253+ try :
254+ signature = await avr_iface .read_signature ()
255+ device = devices_by_signature [signature ]
256+ self .logger .info ("device signature: %s (%s)" ,
257+ "{:02x} {:02x} {:02x}" .format (* signature ),
258+ "unknown" if device is None else device .name )
259+
260+ if args .operation in (None , "identify" ):
261+ return
262+
263+ if device is None :
264+ raise ProgramAVRError ("cannot operate on unknown device" )
265+
266+ if device .erase_time is not None :
267+ avr_iface .erase_time = device .erase_time
268+
269+ if args .operation == "read" :
270+ if args .fuses :
271+ fuses = await avr_iface .read_fuse_range (range (device .fuses_size ))
272+ if device .fuses_size > 2 :
273+ self .logger .info ("fuses: low %s high %s extra %s" ,
274+ f"{ fuses [0 ]:08b} " ,
275+ f"{ fuses [1 ]:08b} " ,
276+ f"{ fuses [2 ]:08b} " )
277+ elif device .fuses_size > 1 :
278+ self .logger .info ("fuses: low %s high %s" ,
279+ f"{ fuses [0 ]:08b} " ,
280+ f"{ fuses [1 ]:08b} " )
281+ else :
282+ self .logger .info ("fuse: %s" , f"{ fuses [0 ]:08b} " )
283+
284+ if args .lock_bits :
285+ lock_bits = await avr_iface .read_lock_bits ()
286+ self .logger .info ("lock bits: %s" , f"{ lock_bits :08b} " )
287+
288+ if args .calibration :
289+ calibration = \
290+ await avr_iface .read_calibration_range (range (device .calibration_size ))
291+ self .logger .info ("calibration bytes: %s" ,
292+ " " .join (["%02x" % b for b in calibration ]))
293+
294+ if args .program :
295+ program_file , program_fmt = args .program
296+ self .logger .info ("reading program memory (%d bytes)" , device .program_size )
297+ output_data (program_file ,
298+ await avr_iface .read_program_memory_range (range (device .program_size )),
299+ program_fmt )
300+
301+ if args .eeprom :
302+ eeprom_file , eeprom_fmt = args .eeprom
303+ self .logger .info ("reading EEPROM (%d bytes)" , device .eeprom_size )
304+ output_data (eeprom_file ,
305+ await avr_iface .read_eeprom_range (range (device .eeprom_size )),
306+ eeprom_fmt )
307+
308+ if args .operation == "write-fuses" :
309+ if args .high and device .fuses_size < 2 :
310+ raise ProgramAVRError ("device does not have high fuse" )
311+
312+ if args .low :
313+ self .logger .info ("writing low fuse" )
314+ await avr_iface .write_fuse (0 , args .low )
315+ written = await avr_iface .read_fuse (0 )
316+ if written != args .low :
317+ raise ProgramAVRError ("verification of low fuse failed: %s" %
318+ f"{ written :08b} != { args .low :08b} " )
319+
320+ if args .high :
321+ self .logger .info ("writing high fuse" )
322+ await avr_iface .write_fuse (1 , args .high )
323+ written = await avr_iface .read_fuse (1 )
324+ if written != args .high :
325+ raise ProgramAVRError ("verification of high fuse failed: %s" %
326+ f"{ written :08b} != { args .high :08b} " )
327+
328+ if args .extra :
329+ self .logger .info ("writing extra fuse" )
330+ await avr_iface .write_fuse (2 , args .extra )
331+ written = await avr_iface .read_fuse (2 )
332+ if written != args .extra :
333+ raise ProgramAVRError ("verification of extra fuse failed: %s" %
334+ f"{ written :08b} != { args .extra :08b} " )
335+
336+ if args .operation == "write-lock" :
337+ self .logger .info ("writing lock bits" )
338+ await avr_iface .write_lock_bits (args .bits )
339+ written = await avr_iface .read_lock_bits ()
340+ if written != args .bits :
341+ raise ProgramAVRError ("verification of lock bits failed: %s" %
342+ f"{ written :08b} != { args .bits :08b} " )
343+
344+ if args .operation == "write-program" :
345+ self .logger .info ("erasing chip" )
346+ await avr_iface .chip_erase ()
347+
348+ program_file , program_fmt = args .file
349+ data = input_data (program_file , program_fmt )
350+ self .logger .info ("writing program memory (%d bytes)" ,
351+ sum ([len (chunk ) for address , chunk in data ]))
352+ for address , chunk in data :
353+ chunk = bytes (chunk )
354+ await avr_iface .write_program_memory_range (address , chunk , device .program_page )
355+ written = await avr_iface .read_program_memory_range (range (address , address + len (chunk )))
356+ if written != chunk :
357+ raise ProgramAVRError ("verification failed at address %#06x: %s != %s" %
358+ (address , written .hex (), chunk .hex ()))
359+
360+ if args .operation == "write-eeprom" :
361+ eeprom_file , eeprom_fmt = args .file
362+ data = input_data (eeprom_file , eeprom_fmt )
363+ self .logger .info ("writing EEPROM (%d bytes)" ,
364+ sum ([len (chunk ) for address , chunk in data ]))
365+ for address , chunk in data :
366+ chunk = bytes (chunk )
367+ await avr_iface .write_eeprom_range (address , chunk , device .eeprom_page )
368+ written = await avr_iface .read_eeprom_range (range (address , len (chunk )))
369+ if written != chunk :
370+ raise ProgramAVRError ("verification failed at address %#06x: %s != %s" %
371+ (address , written .hex (), chunk .hex ()))
372+
373+ if args .operation == "erase" :
374+ self .logger .info ("erasing device" )
375+ await avr_iface .chip_erase ()
376+ finally :
377+ await avr_iface .programming_disable ()
0 commit comments