mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-08 07:27:29 -06:00
Fix code style violations in the avr_isp
This commit is contained in:
parent
8c629c511c
commit
a4a0dfdf89
4 changed files with 77 additions and 77 deletions
|
@ -5,21 +5,21 @@ This is a python 3 conversion of the code created by David Braam for the Cura pr
|
||||||
"""
|
"""
|
||||||
|
|
||||||
avrChipDB = {
|
avrChipDB = {
|
||||||
'ATMega1280': {
|
"ATMega1280": {
|
||||||
'signature': [0x1E, 0x97, 0x03],
|
"signature": [0x1E, 0x97, 0x03],
|
||||||
'pageSize': 128,
|
"pageSize": 128,
|
||||||
'pageCount': 512,
|
"pageCount": 512,
|
||||||
},
|
},
|
||||||
'ATMega2560': {
|
"ATMega2560": {
|
||||||
'signature': [0x1E, 0x98, 0x01],
|
"signature": [0x1E, 0x98, 0x01],
|
||||||
'pageSize': 128,
|
"pageSize": 128,
|
||||||
'pageCount': 1024,
|
"pageCount": 1024,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
def getChipFromDB(sig):
|
def getChipFromDB(sig):
|
||||||
for chip in avrChipDB.values():
|
for chip in avrChipDB.values():
|
||||||
if chip['signature'] == sig:
|
if chip["signature"] == sig:
|
||||||
return chip
|
return chip
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -11,36 +11,36 @@ def readHex(filename):
|
||||||
Read an verify an intel hex file. Return the data as an list of bytes.
|
Read an verify an intel hex file. Return the data as an list of bytes.
|
||||||
"""
|
"""
|
||||||
data = []
|
data = []
|
||||||
extraAddr = 0
|
extra_addr = 0
|
||||||
f = io.open(filename, "r")
|
f = io.open(filename, "r")
|
||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if len(line) < 1:
|
if len(line) < 1:
|
||||||
continue
|
continue
|
||||||
if line[0] != ':':
|
if line[0] != ":":
|
||||||
raise Exception("Hex file has a line not starting with ':'")
|
raise Exception("Hex file has a line not starting with ':'")
|
||||||
recLen = int(line[1:3], 16)
|
rec_len = int(line[1:3], 16)
|
||||||
addr = int(line[3:7], 16) + extraAddr
|
addr = int(line[3:7], 16) + extra_addr
|
||||||
recType = int(line[7:9], 16)
|
rec_type = int(line[7:9], 16)
|
||||||
if len(line) != recLen * 2 + 11:
|
if len(line) != rec_len * 2 + 11:
|
||||||
raise Exception("Error in hex file: " + line)
|
raise Exception("Error in hex file: " + line)
|
||||||
checkSum = 0
|
check_sum = 0
|
||||||
for i in range(0, recLen + 5):
|
for i in range(0, rec_len + 5):
|
||||||
checkSum += int(line[i*2+1:i*2+3], 16)
|
check_sum += int(line[i*2+1:i*2+3], 16)
|
||||||
checkSum &= 0xFF
|
check_sum &= 0xFF
|
||||||
if checkSum != 0:
|
if check_sum != 0:
|
||||||
raise Exception("Checksum error in hex file: " + line)
|
raise Exception("Checksum error in hex file: " + line)
|
||||||
|
|
||||||
if recType == 0:#Data record
|
if rec_type == 0:#Data record
|
||||||
while len(data) < addr + recLen:
|
while len(data) < addr + rec_len:
|
||||||
data.append(0)
|
data.append(0)
|
||||||
for i in range(0, recLen):
|
for i in range(0, rec_len):
|
||||||
data[addr + i] = int(line[i*2+9:i*2+11], 16)
|
data[addr + i] = int(line[i*2+9:i*2+11], 16)
|
||||||
elif recType == 1: #End Of File record
|
elif rec_type == 1: #End Of File record
|
||||||
pass
|
pass
|
||||||
elif recType == 2: #Extended Segment Address Record
|
elif rec_type == 2: #Extended Segment Address Record
|
||||||
extraAddr = int(line[9:13], 16) * 16
|
extra_addr = int(line[9:13], 16) * 16
|
||||||
else:
|
else:
|
||||||
print(recType, recLen, addr, checkSum, line)
|
print(rec_type, rec_len, addr, check_sum, line)
|
||||||
f.close()
|
f.close()
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -14,18 +14,18 @@ class IspBase():
|
||||||
Base class for ISP based AVR programmers.
|
Base class for ISP based AVR programmers.
|
||||||
Functions in this class raise an IspError when something goes wrong.
|
Functions in this class raise an IspError when something goes wrong.
|
||||||
"""
|
"""
|
||||||
def programChip(self, flashData):
|
def programChip(self, flash_data):
|
||||||
""" Program a chip with the given flash data. """
|
""" Program a chip with the given flash data. """
|
||||||
self.curExtAddr = -1
|
self.cur_ext_addr = -1
|
||||||
self.chip = chipDB.getChipFromDB(self.getSignature())
|
self.chip = chipDB.getChipFromDB(self.getSignature())
|
||||||
if not self.chip:
|
if not self.chip:
|
||||||
raise IspError("Chip with signature: " + str(self.getSignature()) + "not found")
|
raise IspError("Chip with signature: " + str(self.getSignature()) + "not found")
|
||||||
self.chipErase()
|
self.chipErase()
|
||||||
|
|
||||||
print("Flashing %i bytes" % len(flashData))
|
print("Flashing %i bytes" % len(flash_data))
|
||||||
self.writeFlash(flashData)
|
self.writeFlash(flash_data)
|
||||||
print("Verifying %i bytes" % len(flashData))
|
print("Verifying %i bytes" % len(flash_data))
|
||||||
self.verifyFlash(flashData)
|
self.verifyFlash(flash_data)
|
||||||
print("Completed")
|
print("Completed")
|
||||||
|
|
||||||
def getSignature(self):
|
def getSignature(self):
|
||||||
|
|
|
@ -19,10 +19,10 @@ class Stk500v2(ispBase.IspBase):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.serial = None
|
self.serial = None
|
||||||
self.seq = 1
|
self.seq = 1
|
||||||
self.lastAddr = -1
|
self.last_addr = -1
|
||||||
self.progressCallback = None
|
self.progress_callback = None
|
||||||
|
|
||||||
def connect(self, port = 'COM22', speed = 115200):
|
def connect(self, port = "COM22", speed = 115200):
|
||||||
if self.serial is not None:
|
if self.serial is not None:
|
||||||
self.close()
|
self.close()
|
||||||
try:
|
try:
|
||||||
|
@ -82,49 +82,49 @@ class Stk500v2(ispBase.IspBase):
|
||||||
|
|
||||||
def writeFlash(self, flash_data):
|
def writeFlash(self, flash_data):
|
||||||
#Set load addr to 0, in case we have more then 64k flash we need to enable the address extension
|
#Set load addr to 0, in case we have more then 64k flash we need to enable the address extension
|
||||||
page_size = self.chip['pageSize'] * 2
|
page_size = self.chip["pageSize"] * 2
|
||||||
flashSize = page_size * self.chip['pageCount']
|
flash_size = page_size * self.chip["pageCount"]
|
||||||
print("Writing flash")
|
print("Writing flash")
|
||||||
if flashSize > 0xFFFF:
|
if flash_size > 0xFFFF:
|
||||||
self.sendMessage([0x06, 0x80, 0x00, 0x00, 0x00])
|
self.sendMessage([0x06, 0x80, 0x00, 0x00, 0x00])
|
||||||
else:
|
else:
|
||||||
self.sendMessage([0x06, 0x00, 0x00, 0x00, 0x00])
|
self.sendMessage([0x06, 0x00, 0x00, 0x00, 0x00])
|
||||||
load_count = (len(flash_data) + page_size - 1) / page_size
|
load_count = (len(flash_data) + page_size - 1) / page_size
|
||||||
for i in range(0, int(load_count)):
|
for i in range(0, int(load_count)):
|
||||||
recv = self.sendMessage([0x13, page_size >> 8, page_size & 0xFF, 0xc1, 0x0a, 0x40, 0x4c, 0x20, 0x00, 0x00] + flash_data[(i * page_size):(i * page_size + page_size)])
|
recv = self.sendMessage([0x13, page_size >> 8, page_size & 0xFF, 0xc1, 0x0a, 0x40, 0x4c, 0x20, 0x00, 0x00] + flash_data[(i * page_size):(i * page_size + page_size)])
|
||||||
if self.progressCallback is not None:
|
if self.progress_callback is not None:
|
||||||
if self._has_checksum:
|
if self._has_checksum:
|
||||||
self.progressCallback(i + 1, load_count)
|
self.progress_callback(i + 1, load_count)
|
||||||
else:
|
else:
|
||||||
self.progressCallback(i + 1, load_count*2)
|
self.progress_callback(i + 1, load_count*2)
|
||||||
|
|
||||||
def verifyFlash(self, flashData):
|
def verifyFlash(self, flash_data):
|
||||||
if self._has_checksum:
|
if self._has_checksum:
|
||||||
self.sendMessage([0x06, 0x00, (len(flashData) >> 17) & 0xFF, (len(flashData) >> 9) & 0xFF, (len(flashData) >> 1) & 0xFF])
|
self.sendMessage([0x06, 0x00, (len(flash_data) >> 17) & 0xFF, (len(flash_data) >> 9) & 0xFF, (len(flash_data) >> 1) & 0xFF])
|
||||||
res = self.sendMessage([0xEE])
|
res = self.sendMessage([0xEE])
|
||||||
checksum_recv = res[2] | (res[3] << 8)
|
checksum_recv = res[2] | (res[3] << 8)
|
||||||
checksum = 0
|
checksum = 0
|
||||||
for d in flashData:
|
for d in flash_data:
|
||||||
checksum += d
|
checksum += d
|
||||||
checksum &= 0xFFFF
|
checksum &= 0xFFFF
|
||||||
if hex(checksum) != hex(checksum_recv):
|
if hex(checksum) != hex(checksum_recv):
|
||||||
raise ispBase.IspError('Verify checksum mismatch: 0x%x != 0x%x' % (checksum & 0xFFFF, checksum_recv))
|
raise ispBase.IspError("Verify checksum mismatch: 0x%x != 0x%x" % (checksum & 0xFFFF, checksum_recv))
|
||||||
else:
|
else:
|
||||||
#Set load addr to 0, in case we have more then 64k flash we need to enable the address extension
|
#Set load addr to 0, in case we have more then 64k flash we need to enable the address extension
|
||||||
flashSize = self.chip['pageSize'] * 2 * self.chip['pageCount']
|
flash_size = self.chip["pageSize"] * 2 * self.chip["pageCount"]
|
||||||
if flashSize > 0xFFFF:
|
if flash_size > 0xFFFF:
|
||||||
self.sendMessage([0x06, 0x80, 0x00, 0x00, 0x00])
|
self.sendMessage([0x06, 0x80, 0x00, 0x00, 0x00])
|
||||||
else:
|
else:
|
||||||
self.sendMessage([0x06, 0x00, 0x00, 0x00, 0x00])
|
self.sendMessage([0x06, 0x00, 0x00, 0x00, 0x00])
|
||||||
|
|
||||||
loadCount = (len(flashData) + 0xFF) / 0x100
|
load_count = (len(flash_data) + 0xFF) / 0x100
|
||||||
for i in range(0, int(loadCount)):
|
for i in range(0, int(load_count)):
|
||||||
recv = self.sendMessage([0x14, 0x01, 0x00, 0x20])[2:0x102]
|
recv = self.sendMessage([0x14, 0x01, 0x00, 0x20])[2:0x102]
|
||||||
if self.progressCallback is not None:
|
if self.progress_callback is not None:
|
||||||
self.progressCallback(loadCount + i + 1, loadCount*2)
|
self.progress_callback(load_count + i + 1, load_count*2)
|
||||||
for j in range(0, 0x100):
|
for j in range(0, 0x100):
|
||||||
if i * 0x100 + j < len(flashData) and flashData[i * 0x100 + j] != recv[j]:
|
if i * 0x100 + j < len(flash_data) and flash_data[i * 0x100 + j] != recv[j]:
|
||||||
raise ispBase.IspError('Verify error at: 0x%x' % (i * 0x100 + j))
|
raise ispBase.IspError("Verify error at: 0x%x" % (i * 0x100 + j))
|
||||||
|
|
||||||
def sendMessage(self, data):
|
def sendMessage(self, data):
|
||||||
message = struct.pack(">BBHB", 0x1B, self.seq, len(data), 0x0E)
|
message = struct.pack(">BBHB", 0x1B, self.seq, len(data), 0x0E)
|
||||||
|
@ -138,12 +138,12 @@ class Stk500v2(ispBase.IspBase):
|
||||||
self.serial.write(message)
|
self.serial.write(message)
|
||||||
self.serial.flush()
|
self.serial.flush()
|
||||||
except SerialTimeoutException:
|
except SerialTimeoutException:
|
||||||
raise ispBase.IspError('Serial send timeout')
|
raise ispBase.IspError("Serial send timeout")
|
||||||
self.seq = (self.seq + 1) & 0xFF
|
self.seq = (self.seq + 1) & 0xFF
|
||||||
return self.recvMessage()
|
return self.recvMessage()
|
||||||
|
|
||||||
def recvMessage(self):
|
def recvMessage(self):
|
||||||
state = 'Start'
|
state = "Start"
|
||||||
checksum = 0
|
checksum = 0
|
||||||
while True:
|
while True:
|
||||||
s = self.serial.read()
|
s = self.serial.read()
|
||||||
|
@ -152,31 +152,31 @@ class Stk500v2(ispBase.IspBase):
|
||||||
b = struct.unpack(">B", s)[0]
|
b = struct.unpack(">B", s)[0]
|
||||||
checksum ^= b
|
checksum ^= b
|
||||||
#print(hex(b))
|
#print(hex(b))
|
||||||
if state == 'Start':
|
if state == "Start":
|
||||||
if b == 0x1B:
|
if b == 0x1B:
|
||||||
state = 'GetSeq'
|
state = "GetSeq"
|
||||||
checksum = 0x1B
|
checksum = 0x1B
|
||||||
elif state == 'GetSeq':
|
elif state == "GetSeq":
|
||||||
state = 'MsgSize1'
|
state = "MsgSize1"
|
||||||
elif state == 'MsgSize1':
|
elif state == "MsgSize1":
|
||||||
msgSize = b << 8
|
msg_size = b << 8
|
||||||
state = 'MsgSize2'
|
state = "MsgSize2"
|
||||||
elif state == 'MsgSize2':
|
elif state == "MsgSize2":
|
||||||
msgSize |= b
|
msg_size |= b
|
||||||
state = 'Token'
|
state = "Token"
|
||||||
elif state == 'Token':
|
elif state == "Token":
|
||||||
if b != 0x0E:
|
if b != 0x0E:
|
||||||
state = 'Start'
|
state = "Start"
|
||||||
else:
|
else:
|
||||||
state = 'Data'
|
state = "Data"
|
||||||
data = []
|
data = []
|
||||||
elif state == 'Data':
|
elif state == "Data":
|
||||||
data.append(b)
|
data.append(b)
|
||||||
if len(data) == msgSize:
|
if len(data) == msg_size:
|
||||||
state = 'Checksum'
|
state = "Checksum"
|
||||||
elif state == 'Checksum':
|
elif state == "Checksum":
|
||||||
if checksum != 0:
|
if checksum != 0:
|
||||||
state = 'Start'
|
state = "Start"
|
||||||
else:
|
else:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ def portList():
|
||||||
values = _winreg.EnumValue(key, i)
|
values = _winreg.EnumValue(key, i)
|
||||||
except:
|
except:
|
||||||
return ret
|
return ret
|
||||||
if 'USBSER' in values[0]:
|
if "USBSER" in values[0]:
|
||||||
ret.append(values[1])
|
ret.append(values[1])
|
||||||
i+=1
|
i+=1
|
||||||
return ret
|
return ret
|
||||||
|
@ -205,7 +205,7 @@ def runProgrammer(port, filename):
|
||||||
def main():
|
def main():
|
||||||
""" Entry point to call the stk500v2 programmer from the commandline. """
|
""" Entry point to call the stk500v2 programmer from the commandline. """
|
||||||
import threading
|
import threading
|
||||||
if sys.argv[1] == 'AUTO':
|
if sys.argv[1] == "AUTO":
|
||||||
print(portList())
|
print(portList())
|
||||||
for port in portList():
|
for port in portList():
|
||||||
threading.Thread(target=runProgrammer, args=(port,sys.argv[2])).start()
|
threading.Thread(target=runProgrammer, args=(port,sys.argv[2])).start()
|
||||||
|
@ -216,5 +216,5 @@ def main():
|
||||||
programmer.programChip(intelHex.readHex(sys.argv[2]))
|
programmer.programChip(intelHex.readHex(sys.argv[2]))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue