def __init__(self): super(AVRProgrammer, self).__init__() self._usbiface = None self.supported_chips = [] self.avr = AVRISP() self._logging = None self._foundchip = False
class AVRProgrammer(object): def __init__(self): super(AVRProgrammer, self).__init__() self._usbiface = None self.supported_chips = [] self.avr = AVRISP() self._logging = None self._foundchip = False def setUSBInterface(self, iface): self._usbiface = iface self._foundchip = False self.avr.setUSB(iface) # self.avr.setChip(self.supported_chips[0]) def find(self): self._foundchip = False self.avr.enableISP(True) sig = self.avr.readSignature() # self.log("Signature = %02x %02x %02x" % (sig[0], sig[1], sig[2])) # Figure out which device? # Check if it's one we know about? for t in supported_avr: if ((sig[0] == t.signature[0]) and (sig[1] == t.signature[1]) and (sig[2] == t.signature[2])): self._foundchip = True self.log("Detected %s" % t.name) self.avr.setChip(t) break # Print signature of unknown device if self._foundchip == False: self.log("Detected Unknown Chip, sig=%2x %2x %2x" % (sig[0], sig[1], sig[2])) def erase(self): self.avr.eraseChip() def program(self, filename, memtype="flash", verify=True): f = IntelHex(filename) maxsize = self.avr._chip.memtypes[memtype]["size"] fsize = f.maxaddr() - f.minaddr() if fsize > maxsize: raise IOError("File %s appears to be %d bytes, larger than %s size of %d" % (filename, fsize, memtype, maxsize)) self.log("AVR Programming %s..." % memtype) QCoreApplication.processEvents() fdata = f.tobinarray(start=0) self.avr.writeMemory(0, fdata, memtype) self.log("AVR Reading %s..." % memtype) QCoreApplication.processEvents() # Do verify run rdata = self.avr.readMemory(0, len(fdata)) # memtype ? for i in range(0, len(fdata)): if fdata[i] != rdata[i]: raise IOError("Verify failed at 0x%04x, %x != %x" % (i, fdata[i], rdata[i])) self.log("Verified %s OK, %d bytes" % (memtype, fsize)) def close(self): self.avr.enableISP(False) def log(self, text): if self._logging is None: print text else: self._logging(text)