Пример #1
0
def boot(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    bl.deselect(0)

    while MW.ok():
        sleep(0.25)
        bl.bootload()

    bl.stop()

    return 0
Пример #2
0
def name(mw, transport, args):
    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])
    name = args.name[0]

    if len(name) > MW.BootMsg.UIDAndName.NAME_LENGTH:
        print("Name must be at most %d bytes long" %
              (MW.BootMsg.UIDAndName.NAME_LENGTH))
        return 1

    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 0

    if not bl.select(uid):
        print("Cannot select device")
        retval = 1

    if not bl.write_name(uid, name):
        print("Cannot write module name")
        retval = 1

    if not bl.deselect(uid):
        print("Cannot deselect device")
        retval = 1

    bl.stop()

    return retval
Пример #3
0
def read_tags(mw, transport, args):
    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])

    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 0

    if not bl.select(uid):
        print("Cannot select device")
        retval = 1

    tags = bl.tags_read(uid, 0)
    if tags is None:
        print("Cannot read tags")
        retval = 1
    else:
        tmp = tags.replace(b'\xff', b'\x00')
        x = tmp.split(b'\0')
        for y in x:
            if len(y) > 0:
                print(y.decode('ascii'))

    if not bl.deselect(uid):
        print("Cannot deselect device")
        retval = 1

    bl.stop()

    return retval
Пример #4
0
def protocol_version(mw, transport, args):
    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])

    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 0

    if not bl.select(uid):
        print("Cannot select device")
        retval = 1
    else:
        pv = bl.protocol_version(uid)
        if pv is None:
            print("Cannot read protocol version")
            retval = 1
        else:
            print(pv)

    if not bl.deselect(uid):
        print("Cannot deselect device")
        retval = 1

    bl.stop()

    return retval
Пример #5
0
def read(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 1

    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])
    #    address = 0x08000000 + 20480 + 2048 +2048
    address = int(args.address[0])

    if not bl.select(uid):
        print("Cannot select device")
        return 1

    if not bl.ihex_read(uid, address):
        print("Cannot write module name")
        return 1

    bl.deselect(uid)

    #    bl.select("000000000000000000000000")
    #    if not bl.deselect(uid):
    #        print("Cannot deselect device")
    #        return 1

    bl.stop()

    return retval
Пример #6
0
def moduleid(mw, transport, args):
    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])
    id = int(args.id[0])

    if id > 255:
        print("ID must be <= 255    ")
        return 1

    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 0

    if not bl.select(uid):
        print("Cannot select device")
        retval = 1

    if not bl.write_id(uid, id):
        print("Cannot write module id")
        retval = 1

    if not bl.deselect(uid):
        print("Cannot deselect device")
        retval = 1

    bl.stop()

    return retval
Пример #7
0
def erase(mw, transport, args):
    what = args.what[0]

    if what == 'program':
        pass
    elif what == 'user':
        pass
    elif what == 'configuration':
        pass
    elif what == 'all':
        pass
    else:
        print("Erase what?")
        return 1

    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])

    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 0

    if not bl.select(uid):
        print("Cannot select device")
        return 1

    what = args.what[0]

    if what == 'program':
        if not bl.eraseProgram(uid):
            print("Cannot erase program")
            retval = 2

    if what == 'user':
        if not bl.eraseUserConfiguration(uid):
            print("Cannot erase user configuration")
            retval = 2

    if what == 'configuration':
        if not bl.eraseConfiguration(uid):
            print("Cannot erase user configuration")
            retval = 2

    if what == 'all':
        if not bl.eraseConfiguration(uid):
            print("Cannot erase configuration")
            retval = 2
        if not bl.eraseProgram(uid):
            print("Cannot erase program")
            retval = 2

    if not bl.deselect(uid):
        print("Cannot deselect device")
        retval = 1

    bl.stop()

    return retval
Пример #8
0
def ls(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)
    if args.interactive:
        while MW.ok():
            print("------------------------")
            for k in bl.getSlaves():
                print(("%08X" % k) + ', ' + bl._slavesTypes[k] + ', ' +
                      bl._slavesNames[k])
                pass

            bl.clear()
            sleep(2)
    else:
        sleep(3)
        for k in bl.getSlaves():
            print("%08X" % k)

    bl.stop()

    return 0
Пример #9
0
def reset_all(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 0

    if not bl.reset_all():
        retval = 1

    bl.stop()

    return retval
Пример #10
0
def boot(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    bl.deselect(0)

    while MW.ok():
        sleep(0.25)
        bl.bootload()

    bl.stop()

    return 0
Пример #11
0
def identify(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])

    retval = 1

    if bl.identify(uid):
        retval = 0

    bl.stop()

    return retval
Пример #12
0
def deselect(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])

    retval = 0

    if not bl.deselect(uid):
        print("Cannot deselect device")
        retval = 1

    bl.stop()

    return retval
Пример #13
0
def ls(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)
    if args.interactive:
        while MW.ok():
            print("------------------------")
            for k in bl.getSlaves():
                print(("%08X" % k) + ', ' + bl._slavesTypes[k] + ', ' + bl._slavesNames[k])
                pass

            bl.clear()
            sleep(2)
    else:
        sleep(3)
        for k in bl.getSlaves():
            print("%08X" % k)

    bl.stop()

    return 0
Пример #14
0
def load(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 1

    uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])
    ihex_file = args.file[0]

    if not uid in bl.getSlaves():
        print("Device is not in bootload mode")
        return 1

    if not bl.select(uid):
        print("Cannot select device")
        return 1

    try_again = True

    if try_again:
        desc = bl.describe_v3(uid)
        if desc is None:
            try_again = True
        else:
            print("TARGET:" + formatDescription_V3(uid, desc))
            try_again = False

    if try_again:
        desc = bl.describe_v2(uid)
        if desc is None:
            try_again = True
        else:
            print("TARGET:" + formatDescription_V2(uid, desc))
            try_again = False

    if try_again:
        retval = 1
    else:
        programSize = desc.program

        ih = IntelHex()
        ih.loadfile(ihex_file, format="hex")
        ih.padding = 0xFF
        bin = ih.tobinarray(size=programSize)
        crc = stm32_crc32_bytes(0xffffffff, bin)
        print("CRC: " + hex(crc))

        what = args.what[0]
        if what == 'program':
            if not bl.eraseProgram(uid):
                print("Cannot erase program")
                return 1

        with open(ihex_file) as f:
            data = f.read().splitlines()

        write_ihex(bl, data, crc)

        if what == 'program':
            if not bl.write_program_crc(uid, crc):
                print("Cannot write CRC")
                return 1

    bl.deselect(uid)

    bl.stop()

    return retval
Пример #15
0
def _main():
    retval = 1
    parser = _create_argsparser()
    args = parser.parse_args()

    logging.basicConfig(stream=sys.stderr,
                        level=verbosity2level(int(args.verbosity)))
    logging.debug('sys.argv = ' + repr(sys.argv))

    if args.action == 'hex_crc':
        retval = hex_crc(args)
        sys.exit(retval)

    # TODO: Automate transport construction from "--transport" args
    assert args.transport[0] == 'DebugTransport'
    assert args.transport[1] == 'SerialLineIO'
    lineio = MW.SerialLineIO(str(args.transport[2]), int(args.transport[3]))
    transport = MW.DebugTransport('dbgtra', lineio)

    mw = MW.Middleware.instance()
    mw.initialize()
    transport.open()

    if args.action == 'boot':
        retval = boot(mw, transport, args)

    if args.action == 'ls':
        retval = ls(mw, transport, args)

    if args.action == 'identify':
        retval = identify(mw, transport, args)

    # if args.action == 'select':
    #    retval = select(mw, transport, args)

    # if args.action == 'deselect':
    #    retval = deselect(mw, transport, args)

    if args.action == 'reset':
        retval = reset(mw, transport, args)

    if args.action == 'update':
        retval = update(mw, transport, args)

    if args.action == 'reset_all':
        retval = reset_all(mw, transport, args)

    if args.action == 'load':
        retval = load(mw, transport, args)

    if args.action == 'erase':
        retval = erase(mw, transport, args)

    if args.action == 'name':
        retval = name(mw, transport, args)

    if args.action == 'id':
        retval = moduleid(mw, transport, args)

    if args.action == 'describe':
        retval = describe(mw, transport, args)

    if args.action == 'read':
        retval = read(mw, transport, args)

    if args.action == 'bootload':
        retval = mw.reboot_remote(args.name[0], True)

    if args.action == 'reboot':
        retval = mw.reboot_remote(args.name[0], False)

    if args.action == 'protocol_version':
        retval = protocol_version(mw, transport, args)

    if args.action == 'tags':
        retval = read_tags(mw, transport, args)

    mw.uninitialize()
    transport.close()

    sys.exit(retval)
Пример #16
0
def describe(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()

    retval = 0

    if len(args.uid) == 1:
        sleep(1)
        uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])

        if not bl.select(uid):
            print("Cannot select device")
            return 1

        try_again = True

        if try_again:
            desc = bl.describe_v3(uid)
            if desc is None:
                try_again = True
            else:
                print(formatDescription_V3(uid, desc))
                try_again = False

        if try_again:
            desc = bl.describe_v2(uid)
            if desc is None:
                try_again = True
            else:
                print(formatDescription_V2(uid, desc))
                try_again = False

        if try_again:
            retval = 1

        if not bl.deselect(uid):
            print("Cannot deselect device")
            retval = 1

    else:
        sleep(3)

        uids = bl.getSlaves()

        for k in uids:
            uid = k
            if not bl.select(uid):
                print("Cannot select device")
                return 1

            try_again = True

            if try_again:
                desc = bl.describe_v3(uid)
                if desc is None:
                    try_again = True
                else:
                    print(formatDescription_V3(uid, desc))
                    try_again = False

            if try_again:
                desc = bl.describe_v2(uid)
                if desc is None:
                    try_again = True
                else:
                    print(formatDescription_V2(uid, desc))
                    try_again = False

            if try_again:
                retval = 1

            if not bl.deselect(uid):
                print("Cannot deselect device")
                retval = 1

    #bl.select(0xFFFFFFFF)
    #bl.deselect(0xFFFFFFFF)

    bl.stop()

    return retval
Пример #17
0
def update(mw, transport, args):
    bl = MW.Bootloader()
    bl.start()
    sleep(1)

    retval = 1

    uid = 0

    if len(args.uid) == 0 and len(bl.getSlaves()) == 1:
        uid = next(iter(bl.getSlaves()))
    else:
        uid = MW.BootMsg.UID.getUIDFromHexString(args.uid[0])
        if not uid in bl.getSlaves():
            print("Device is not in bootload mode")
            return 1

    if not bl.select(uid):
        print("Cannot select device")
        return 1

    try_again = True

    if try_again:
        desc = bl.describe_v3(uid)
        if desc is None:
            try_again = True
        else:
            print("TARGET:" + formatDescription_V3(uid, desc))
            try_again = False

    if try_again:
        desc = bl.describe_v2(uid)
        if desc is None:
            try_again = True
        else:
            print("TARGET:" + formatDescription_V2(uid, desc))
            try_again = False

    if try_again:
        retval = 1
    else:
        fwu_file = args.file[0]
        fwu = fwu_parse(fwu_file)

        if len(fwu.program) > 0:
            if not bl.eraseProgram(uid):
                print("Cannot erase program")
                return 1

        write_ihex(bl, fwu.program, fwu.program_crc)

        if len(fwu.program) > 0:
            if not bl.write_program_crc(uid, fwu.program_crc):
                print("Cannot write CRC")
                return 1

    bl.deselect(uid)

    bl.stop()

    return retval