def main():
    rom_fn, = sys.argv[1:]
    rom = open(rom_fn).read()

    with megarom.Port() as ser:
        print "\n* Port open.  Giving it a kick, and waiting for OK."
        ser.write("\n")
        r = read_until(ser, "OK")

        print "\n* Requesting chip ID and locking chip"
        ser.write("I\n")  # identify chip
        r = read_until(ser, "OK")
        m = re.search("Size = (\d+)", r)
        if not m:
            raise Exception("Chip identification failed")
        chip_size = int(m.group(1))
        print "\n* Chip size = %d bytes" % chip_size
        usb_block_size = 63 if (
            chip_size < 1048576
        ) else 1024  # atmega32u4 can't handle big usb chunks, but atsamd21 can

        if len(rom) != chip_size:
            raise Exception(
                "%s is %d bytes long, which does not match the flash capacity of %d bytes"
                % (rom_fn, len(rom), chip_size))

        print "\n* Start programming process"
        ser.write("P\n")  # program chip

        input_buf = ''
        done = 0
        while not done:
            input_buf += read_until(ser, "\n")
            while input_buf.find("\n") != -1:
                p = input_buf.find("\n") + 1
                line, input_buf = input_buf[:p], input_buf[p:]
                line = line.strip()
                print "parse", ` line `
                if line == "OK":
                    print "All done!"
                    done = 1
                    break
                m = re.search(r"^(\d+)\+(\d+)$", line)
                if not m: continue

                start, size = int(m.group(1)), int(m.group(2))
                print "* Sending data from %d-%d" % (start, start + size)
                blk = rom[start:start + size]
                #print `blk[:64]`
                while len(blk):
                    n = ser.write(blk[:usb_block_size])
                    if n:
                        blk = blk[n:]
                        #print "wrote %d bytes" % n
                    else:
                        time.sleep(0.01)
예제 #2
0
def main():
    with megarom.Port() as ser:
        print "\n* Port open.  Giving it a kick, and waiting for OK."
        ser.write("\n")
        r = read_until(ser, "", "OK")

        for try_number in range(5):
            print "\n* Requesting chip ID (try %d)" % (try_number + 1)
            ser.write("I\n")  # identify chip
            r = read_until(ser, "", "OK")
            print "CHIP ID TEXT %s" % `r`
            m = re.search("Size = (\d+)", r)
            if not m:
                raise Exception("Chip identification failed")
            chip_size = int(m.group(1))
            print "\n* Chip size = %d bytes" % chip_size
            if chip_size: break

        assert chip_size, "failed to identify chip"

        print "\n* Start read"
        ser.write("R\n")

        input_buf = ''
        done = 0
        start_time = time.time()
        while not done:
            input_buf = read_until(ser, input_buf, "\n")
            while input_buf.find("\n") != -1:
                p = input_buf.find("\n") + 1
                line, input_buf = input_buf[:p], input_buf[p:]
                line = line.strip()
                print "parse",`line`
                if line.find("Size = ") != -1:
                    done = 1
                    break
        input_buf = read_until(ser, input_buf, "DATA:")
        input_buf = input_buf[input_buf.find("DATA:") + 5:]
        while len(input_buf) < chip_size:
            r = ser.read(1024)
            if r:
                print `r`
                input_buf += r
        time_taken = time.time() - start_time
        print "Saving"
        contents, input_buf = input_buf[:chip_size], input_buf[chip_size:]
        open("read.rom", "w").write(contents)
        print "%d bytes read in %.2f s - md5 %s - sha1 %s" % (
            len(contents),
            time_taken,
            hashlib.md5(contents).hexdigest(),
            hashlib.sha1(contents).hexdigest(),
        )
        print "input:", `input_buf`
        read_until(ser, input_buf, "OK")
        print "Done!"
예제 #3
0
def main():
    with megarom.Port() as ser:
        print("\n* Port open.  Giving it a kick, and waiting for OK.")
        ser.write("\n")
        r = read_until(ser, "", "OK")

        for try_number in range(5):
            print("\n* Requesting chip ID (try %d)" % (try_number + 1))
            ser.write("I\n")  # identify chip
            r = read_until(ser, "", "OK")
            print("CHIP ID TEXT %s" % repr(r))
            m = re.search("Size = (\d+)", r)
            if not m:
                raise Exception("Chip identification failed")
            chip_size = int(m.group(1))
            print("\n* Chip size = %d bytes" % chip_size)
            if chip_size: break

        assert chip_size, "failed to identify chip"

        print("\n* Start read")
        ser.write("R\n")

        input_buf = ''
        done = 0
        start_time = time.time()
        while not done:
            input_buf = read_until(ser, input_buf, "\n")
            while input_buf.find("\n") != -1:
                p = input_buf.find("\n") + 1
                line, input_buf = input_buf[:p], input_buf[p:]
                line = line.strip()
                print("parse", repr(line))
                if line.find("Size = ") != -1:
                    done = 1
                    break
        input_buf = read_until(ser, input_buf, "DATA:")
        input_buf = input_buf[input_buf.find("DATA:") + 5:]
        last_reported = 0
        try:
            while len(input_buf) < chip_size:
                r = ser.read(1024)
                if r:
                    # print(`r`, len(input_buf), chip_size)
                    input_buf += r
                    if len(input_buf) - last_reported > 65536:
                        print(len(input_buf), chip_size)
                        last_reported = len(input_buf)
        except KeyboardInterrupt:
            print("Interrupted -- saving what we have")
        time_taken = time.time() - start_time
        print("Saving")
        contents, input_buf = input_buf[:chip_size], input_buf[chip_size:]
        open("read.rom", "w").write(contents)
        print("%d bytes read in %.2f s - md5 %s - sha1 %s" % (
            len(contents),
            time_taken,
            hashlib.md5(contents).hexdigest(),
            hashlib.sha1(contents).hexdigest(),
        ))
        print("input:", repr(input_buf))
        read_until(ser, input_buf, "OK")
        print("Done!")