# Driver for Mboot, the MicroPython boot loader # MIT license; Copyright (c) 2018 Damien P. George import struct, time, os, hashlib I2C_CMD_ECHO = 1 I2C_CMD_GETID = 2 I2C_CMD_GETCAPS = 3 I2C_CMD_RESET = 4 I2C_CMD_CONFIG = 5 I2C_CMD_GETLAYOUT = 6 I2C_CMD_MASSERASE = 7 I2C_CMD_PAGEERASE = 8 I2C_CMD_SETRDADDR = 9 I2C_CMD_SETWRADDR = 10 I2C_CMD_READ = 11 I2C_CMD_WRITE = 12 I2C_CMD_COPY = 13 I2C_CMD_CALCHASH = 14 I2C_CMD_MARKVALID = 15 class Bootloader: def __init__(self, i2c, addr): self.i2c = i2c self.addr = addr self.buf1 = bytearray(1) try: self.i2c.writeto(addr, b"") except OSError: raise Exception("no I2C mboot device found") def wait_response(self): start = time.ticks_ms() while 1: try: self.i2c.readfrom_into(self.addr, self.buf1) n = self.buf1[0] break except OSError: time.sleep_us(500) if time.ticks_diff(time.ticks_ms(), start) > 5000: raise Exception("timeout") if n >= 129: raise Exception(n) if n == 0: return b"" else: return self.i2c.readfrom(self.addr, n) def wait_empty_response(self): ret = self.wait_response() if ret: raise Exception("expected empty response got %r" % ret) else: return None def echo(self, data): self.i2c.writeto(self.addr, struct.pack("