Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
improve flasher
Browse files Browse the repository at this point in the history
  • Loading branch information
doudz committed Sep 20, 2020
1 parent b7a6375 commit 7c10fbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions zigate/flasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __getattr__(self, *args, **kwargs):
import usb


logger = logging.getLogger(__name__)
logger = logging.getLogger('ZiGate Flasher')
_responses = {}

ZIGATE_CHIP_ID = 0x10408686
Expand Down Expand Up @@ -293,7 +293,7 @@ def write_flash_to_file(ser, filename):
cur = ZIGATE_FLASH_START
flash_end = ZIGATE_FLASH_END

print('reading old flash to %s' % filename)
logger.info('Backup firmware to %s', filename)
with open(filename, 'wb') as fd:
fd.write(ZIGATE_BINARY_VERSION)
read_bytes = 128
Expand All @@ -308,11 +308,13 @@ def write_flash_to_file(ser, filename):
if cur == 0:
(flash_end,) = struct.unpack('>L', res.data[0x20:0x24])
fd.write(res.data)
print(int(cur*100/flash_end), '%', end='\r')
cur += read_bytes
logger.info('Backup firmware done')


def write_file_to_flash(ser, filename):
print('writing new flash from %s' % filename)
logger.info('Writing new firmware from %s', filename)
with open(filename, 'rb') as fd:
ser.write(req_flash_erase())
res = read_response(ser)
Expand All @@ -338,7 +340,9 @@ def write_file_to_flash(ser, filename):
if not res.ok:
print('writing failed at 0x%08x, status: 0x%x, data: %s' % (cur, res.status, data.hex()))
raise SystemExit(1)
print(int(cur*100/flash_end), '%', end='\r')
cur += read_bytes
logger.info('Writing new firmware done')


def erase_EEPROM(ser, pdm_only=False):
Expand All @@ -365,7 +369,7 @@ def flash(serialport='auto', write=None, save=None, erase=False, pdm_only=False)
check_chip_id(ser)
flash_type = get_flash_type(ser)
mac_address = get_mac(ser)
print('Found MAC-address: %s' % mac_address)
logger.info('Found MAC-address: %s', mac_address)
if write or save or erase:
select_flash(ser, flash_type)

Expand All @@ -377,7 +381,7 @@ def flash(serialport='auto', write=None, save=None, erase=False, pdm_only=False)

if erase:
erase_EEPROM(ser, pdm_only)
change_baudrate(ser, 38400)
# change_baudrate(ser, 38400)


def upgrade_firmware(port):
Expand Down Expand Up @@ -418,6 +422,7 @@ def main():
parser.add_argument('--gpio', help='Configure GPIO for PiZiGate flash', action='store_true', default=False)
parser.add_argument('--din', help='Configure USB for ZiGate DIN flash', action='store_true', default=False)
args = parser.parse_args()
logger.setLevel(logging.INFO)
if args.debug:
logger.setLevel(logging.DEBUG)

Expand Down Expand Up @@ -460,13 +465,13 @@ def main():
logger.exception("Could not open serial device %s", args.serialport)
raise SystemExit(1)

atexit.register(change_baudrate, ser, 38400)
# atexit.register(change_baudrate, ser, 38400)

change_baudrate(ser, 115200)
check_chip_id(ser)
flash_type = get_flash_type(ser)
mac_address = get_mac(ser)
logger.info('Found MAC-address: %s' % mac_address)
logger.info('Found MAC-address: %s', mac_address)
if args.write or args.save: # or args.erase:
select_flash(ser, flash_type)

Expand All @@ -479,6 +484,7 @@ def main():
# if args.erase:
# erase_EEPROM(ser, args.pdm_only)


if args.gpio:
logger.info('Put PiZiGate in running mode')
GPIO.output(27, GPIO.HIGH) # GPIO2
Expand All @@ -495,4 +501,5 @@ def main():


if __name__ == "__main__":
logging.basicConfig()
main()
2 changes: 1 addition & 1 deletion zigate/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
#


__version__ = '0.40.8'
__version__ = '0.40.9'

0 comments on commit 7c10fbf

Please sign in to comment.