Fix and refactor disconnect
This commit is contained in:
parent
b3230814a8
commit
a1fa53456e
110
stcgal.py
110
stcgal.py
@ -1585,6 +1585,44 @@ class StcGal:
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise NameError("invalid option '%s' (%s)" % (kv[0], e))
|
raise NameError("invalid option '%s' (%s)" % (kv[0], e))
|
||||||
|
|
||||||
|
def program_mcu(self):
|
||||||
|
code_size = self.protocol.model.code
|
||||||
|
ee_size = self.protocol.model.eeprom
|
||||||
|
|
||||||
|
bindata = opts.code_binary.read()
|
||||||
|
|
||||||
|
# warn if it overflows
|
||||||
|
if len(bindata) > code_size:
|
||||||
|
print("WARNING: code_binary overflows into eeprom segment!", file=sys.stderr)
|
||||||
|
if len(bindata) > (code_size + ee_size):
|
||||||
|
print("WARNING: code_binary truncated!", file=sys.stderr)
|
||||||
|
bindata = bindata[0:code_size + ee_size]
|
||||||
|
|
||||||
|
# add eeprom data if supplied
|
||||||
|
if opts.eeprom_binary:
|
||||||
|
eedata = opts.eeprom_binary.read()
|
||||||
|
if len(eedata) > ee_size:
|
||||||
|
print("WARNING: eeprom_binary truncated!", file=sys.stderr)
|
||||||
|
eedata = eedata[0:ee_size]
|
||||||
|
if len(bindata) < code_size:
|
||||||
|
bindata += bytes(code_size - len(bindata))
|
||||||
|
elif len(bindata) > code_size:
|
||||||
|
print("WARNING: eeprom_binary overlaps code_binary!", file=sys.stderr)
|
||||||
|
bindata = bindata[0:code_size]
|
||||||
|
bindata += eedata
|
||||||
|
|
||||||
|
# pad to 256 byte boundary
|
||||||
|
if len(bindata) % 256:
|
||||||
|
bindata += bytes(256 - len(bindata) % 256)
|
||||||
|
|
||||||
|
if opts.option: self.emit_options(opts.option)
|
||||||
|
|
||||||
|
self.protocol.handshake()
|
||||||
|
self.protocol.erase_flash(len(bindata), code_size)
|
||||||
|
self.protocol.program_flash(bindata)
|
||||||
|
self.protocol.program_options()
|
||||||
|
self.protocol.disconnect()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try: self.protocol.connect()
|
try: self.protocol.connect()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
@ -1598,60 +1636,28 @@ class StcGal:
|
|||||||
print("Serial communication error: %s" % e, file=sys.stderr)
|
print("Serial communication error: %s" % e, file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if opts.code_binary:
|
try:
|
||||||
try:
|
if opts.code_binary:
|
||||||
code_size = self.protocol.model.code
|
self.program_mcu()
|
||||||
ee_size = self.protocol.model.eeprom
|
return 0
|
||||||
|
else:
|
||||||
bindata = opts.code_binary.read()
|
|
||||||
|
|
||||||
# warn if it overflows
|
|
||||||
if len(bindata) > code_size:
|
|
||||||
print("WARNING: code_binary overflows into eeprom segment!", file=sys.stderr)
|
|
||||||
if len(bindata) > (code_size + ee_size):
|
|
||||||
print("WARNING: code_binary truncated!", file=sys.stderr)
|
|
||||||
bindata = bindata[0:code_size + ee_size]
|
|
||||||
|
|
||||||
# add eeprom data if supplied
|
|
||||||
if opts.eeprom_binary:
|
|
||||||
eedata = opts.eeprom_binary.read()
|
|
||||||
if len(eedata) > ee_size:
|
|
||||||
print("WARNING: eeprom_binary truncated!", file=sys.stderr)
|
|
||||||
eedata = eedata[0:ee_size]
|
|
||||||
if len(bindata) < code_size:
|
|
||||||
bindata += bytes(code_size - len(bindata))
|
|
||||||
elif len(bindata) > code_size:
|
|
||||||
print("WARNING: eeprom_binary overlaps code_binary!", file=sys.stderr)
|
|
||||||
bindata = bindata[0:code_size]
|
|
||||||
bindata += eedata
|
|
||||||
|
|
||||||
# pad to 256 byte boundary
|
|
||||||
if len(bindata) % 256:
|
|
||||||
bindata += bytes(256 - len(bindata) % 256)
|
|
||||||
|
|
||||||
if opts.option: self.emit_options(opts.option)
|
|
||||||
|
|
||||||
self.protocol.handshake()
|
|
||||||
self.protocol.erase_flash(len(bindata), code_size)
|
|
||||||
self.protocol.program_flash(bindata)
|
|
||||||
self.protocol.program_options()
|
|
||||||
self.protocol.disconnect()
|
self.protocol.disconnect()
|
||||||
return 0
|
return 0
|
||||||
except NameError as e:
|
except NameError as e:
|
||||||
print("Option error: %s" % e, file=sys.stderr)
|
print("Option error: %s" % e, file=sys.stderr)
|
||||||
self.protocol.disconnect()
|
self.protocol.disconnect()
|
||||||
return 1
|
return 1
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
print("Communication error: %s" % e, file=sys.stderr)
|
print("Communication error: %s" % e, file=sys.stderr)
|
||||||
self.protocol.disconnect()
|
self.protocol.disconnect()
|
||||||
return 1
|
return 1
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("interrupted")
|
print("interrupted")
|
||||||
self.protocol.disconnect()
|
self.protocol.disconnect()
|
||||||
return 2
|
return 2
|
||||||
except serial.serialutil.SerialException as e:
|
except serial.serialutil.SerialException as e:
|
||||||
print("Serial communication error: %s" % e, file=sys.stderr)
|
print("Serial communication error: %s" % e, file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user