示例#1
0
文件: nxpprog.py 项目: jnugen/nxpprog
        panic("unsupported cpu %s" % cpu)

    if len(args) == 0:
        syntax()

    log("cpu=%s oscfreq=%d baud=%d" % (cpu, osc_freq, baud))

    device = args[0]

    prog = nxpprog(cpu, device, baud, osc_freq, xonxoff, control)

    if erase_only:
        prog.erase_all()
    elif start:
        prog.start(startaddr)
    else:
        if len(args) != 2:
            syntax()

        filename = args[1]

	if filetype == "ihex":
	    ih = ihex.ihex(filename)
	    (flash_addr_base, image) = ih.flatten()
	else:
            image = open(filename, "rb").read()

        prog.prog_image(image, flash_addr_base, erase_all)

        prog.start()
示例#2
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    # defaults
    osc_freq = 16000  # kHz
    baud = 115200
    cpu = "autodetect"
    flash_addr_base = 0
    erase_all = False
    erase_only = False
    verify = False
    verify_only = False
    blank_check = False
    xonxoff = False
    start = False
    control = False
    filetype = "autodetect"
    select_bank = False
    read = False
    readlen = 0
    get_serial_number = False
    udp = False
    port = -1
    mac = ""  # "0C-1D-12-E0-1F-10"

    optlist, args = getopt.getopt(argv[1:], '', [
        'cpu=', 'oscfreq=', 'baud=', 'addr=', 'start=', 'filetype=', 'bank=',
        'read=', 'len=', 'serialnumber', 'udp', 'port=', 'mac=', 'verify',
        'verifyonly', 'blankcheck', 'xonxoff', 'eraseall', 'eraseonly', 'list',
        'control'
    ])

    for o, a in optlist:
        if o == "--list":
            log("Supported cpus:")
            for val in sorted(cpu_parms.keys()):
                log(" %s" % val)
            sys.exit(0)
        if o == "--cpu":
            cpu = a
        elif o == "--xonxoff":
            xonxoff = True
        elif o == "--oscfreq":
            osc_freq = int(a)
        elif o == "--addr":
            flash_addr_base = int(a, 0)
        elif o == "--baud":
            baud = int(a)
        elif o == "--eraseall":
            erase_all = True
        elif o == "--eraseonly":
            erase_only = True
        elif o == "--verify":
            verify = True
        elif o == "--verifyonly":
            verify = True
            verify_only = True
        elif o == "--blankcheck":
            verify = True
            blank_check = True
        elif o == "--control":
            control = True
        elif o == "--filetype":
            filetype = a
            if not (filetype == "bin" or filetype == "ihex"):
                panic("Invalid filetype: %s" % filetype)
        elif o == "--start":
            start = True
            if a:
                startaddr = int(a, 0)
            else:
                startaddr = 0
        elif o == "--bank":
            select_bank = True
            bank = int(a)
        elif o == "--read":
            read = True
            readfile = a
        elif o == "--serialnumber":
            get_serial_number = True
        elif o == "--len":
            readlen = int(a)
        elif o == "--udp":
            udp = True
        elif o == "--port":
            port = int(a)
        elif o == "--mac":
            mac = a
        else:
            panic("Unhandled option: %s" % o)

    if cpu != "autodetect" and not cpu_parms.has_key(cpu):
        panic("Unsupported cpu %s" % cpu)

    if len(args) == 0:
        syntax()

    device = args[0]

    if udp:
        if '.' in device:
            if ':' in device:
                device, port = tuple(device.split(':'))
                port = int(port)
                if port < 0 or port > 65535:
                    panic("Bad port number: %d" % port)
            parts = [int(x) for x in device.split('.')]
            if len(parts) != 4 or min(parts) < 0 or max(parts) > 255:
                panic("Bad IPv4-address: %s" % device)
            device = '.'.join([str(x) for x in parts])
        elif ':' in device:
            # panic("Bad IPv6-address: %s" % device)
            pass
        else:
            panic("Bad IP-address: %s" % device)
        if port < 0:
            port = 41825
        if mac:
            parts = [int(x, 16) for x in mac.split('-')]
            if len(parts) != 6 or min(parts) < 0 or max(parts) > 255:
                panic("Bad MAC-address: %s" % mac)
            mac = '-'.join(['%02x' % x for x in parts])
            log("cpu=%s ip=%s:%d mac=%s" % (cpu, device, port, mac))
        else:
            log("cpu=%s ip=%s:%d" % (cpu, device, port))
    else:
        log("cpu=%s oscfreq=%d device=%s baud=%d" %
            (cpu, osc_freq, device, baud))

    prog = nxpprog(cpu, device, baud, osc_freq, xonxoff, control,
                   (device, port, mac) if udp else None, verify)

    if erase_only:
        prog.erase_all(verify)
    elif blank_check:
        prog.blank_check_all()
    elif start:
        prog.start(startaddr)
    elif select_bank:
        prog.select_bank(bank)
    elif get_serial_number:
        sn = prog.get_serial_number()
        sys.stdout.write(sn)
    elif read:
        if not readlen:
            panic("Read length is 0")
        fd = open(readfile, "w")
        prog.read_block(flash_addr_base, readlen, fd)
        fd.close()
    else:
        if len(args) != 2:
            syntax()

        filename = args[1]

        if filetype == "autodetect":
            filetype = "ihex" if filename.endswith('hex') else "bin"

        if filetype == "ihex":
            ih = ihex.ihex(filename)
            (flash_addr_base, image) = ih.flatten()
        else:
            image = open(filename, "rb").read()

        if not verify_only:
            start = time.time()
            success = prog.prog_image(image, flash_addr_base, erase_all,
                                      verify)
            stop = time.time()
            elapsed = stop - start
            log("Programmed %s in %.1f seconds" %
                ("successfully" if success else "with errors", elapsed))

        if verify:
            start = time.time()
            success = prog.verify_image(flash_addr_base, image)
            stop = time.time()
            elapsed = stop - start
            log("Verified %s in %.1f seconds" %
                ("successfully" if success else "with errors", elapsed))

        if not verify_only:
            prog.start(flash_addr_base)
示例#3
0
文件: nxpprog.py 项目: ulfen/nxpprog
def main(argv=None):
    if argv is None:
        argv = sys.argv

    # defaults
    osc_freq = 16000 # kHz
    baud = 115200
    cpu = "autodetect"
    flash_addr_base = 0
    erase_all = False
    erase_only = False
    verify = False
    verify_only = False
    blank_check = False
    xonxoff = False
    start = False
    control = False
    filetype = "autodetect"
    select_bank = False
    read = False
    readlen = 0
    get_serial_number = False
    udp = False
    port = -1
    mac = "" # "0C-1D-12-E0-1F-10"

    optlist, args = getopt.getopt(argv[1:], '',
            ['cpu=', 'oscfreq=', 'baud=', 'addr=', 'start=',
                'filetype=', 'bank=', 'read=', 'len=', 'serialnumber',
                'udp', 'port=', 'mac=', 'verify', 'verifyonly', 'blankcheck',
                'xonxoff', 'eraseall', 'eraseonly', 'list', 'control'])

    for o, a in optlist:
        if o == "--list":
            log("Supported cpus:")
            for val in sorted(cpu_parms.keys()):
                log(" %s" % val)
            sys.exit(0)
        if o == "--cpu":
            cpu = a
        elif o == "--xonxoff":
            xonxoff = True
        elif o == "--oscfreq":
            osc_freq = int(a)
        elif o == "--addr":
            flash_addr_base = int(a, 0)
        elif o == "--baud":
            baud = int(a)
        elif o == "--eraseall":
            erase_all = True
        elif o == "--eraseonly":
            erase_only = True
        elif o == "--verify":
            verify = True
        elif o == "--verifyonly":
            verify = True
            verify_only = True
        elif o == "--blankcheck":
            verify = True
            blank_check = True
        elif o == "--control":
            control = True
        elif o == "--filetype":
            filetype = a
            if not ( filetype == "bin" or filetype == "ihex" ):
                panic("Invalid filetype: %s" % filetype)
        elif o == "--start":
            start = True
            if a:
                startaddr = int(a, 0)
            else:
                startaddr = 0
        elif o == "--bank":
            select_bank = True
            bank = int(a)
        elif o == "--read":
            read = True
            readfile = a
        elif o == "--serialnumber":
            get_serial_number = True
        elif o == "--len":
            readlen = int(a)
        elif o == "--udp":
            udp = True
        elif o == "--port":
            port = int(a)
        elif o == "--mac":
            mac = a
        else:
            panic("Unhandled option: %s" % o)

    if cpu != "autodetect" and not cpu_parms.has_key(cpu):
        panic("Unsupported cpu %s" % cpu)

    if len(args) == 0:
        syntax()

    device = args[0]

    if udp:
        if '.' in device:
            if ':' in device:
                device, port = tuple(device.split(':'))
                port = int(port)
                if port<0 or port>65535:
                    panic("Bad port number: %d" % port)
            parts = [int(x) for x in device.split('.')]
            if len(parts)!=4 or min(parts)<0 or max(parts)>255:
                panic("Bad IPv4-address: %s" % device)
            device = '.'.join([str(x) for x in parts])
        elif ':' in device:
            # panic("Bad IPv6-address: %s" % device)
            pass
        else:
            panic("Bad IP-address: %s" % device)
        if port < 0:
            port = 41825
        if mac:
            parts = [int(x, 16) for x in mac.split('-')]
            if len(parts)!=6 or min(parts)<0 or max(parts)>255:
                panic("Bad MAC-address: %s" % mac)
            mac = '-'.join(['%02x'%x for x in parts])
            log("cpu=%s ip=%s:%d mac=%s" % (cpu, device, port, mac))
        else:
            log("cpu=%s ip=%s:%d" % (cpu, device, port))
    else:
        log("cpu=%s oscfreq=%d device=%s baud=%d" % (cpu, osc_freq, device, baud))

    prog = nxpprog(cpu, device, baud, osc_freq, xonxoff, control, (device, port, mac) if udp else None, verify)

    if erase_only:
        prog.erase_all(verify)
    elif blank_check:
        prog.blank_check_all()
    elif start:
        prog.start(startaddr)
    elif select_bank:
        prog.select_bank(bank)
    elif get_serial_number:
        sn = prog.get_serial_number()
        sys.stdout.write(sn)
    elif read:
        if not readlen:
            panic("Read length is 0")
        fd = open(readfile, "w")
        prog.read_block(flash_addr_base, readlen, fd)
        fd.close()
    else:
        if len(args) != 2:
            syntax()

        filename = args[1]

        if filetype == "autodetect":
            filetype = "ihex" if filename.endswith('hex') else "bin"

        if filetype == "ihex":
            ih = ihex.ihex(filename)
            (flash_addr_base, image) = ih.flatten()
        else:
            image = open(filename, "rb").read()

        if not verify_only:
            start = time.time()
            success = prog.prog_image(image, flash_addr_base, erase_all, verify)
            stop = time.time()
            elapsed = stop - start
            log("Programmed %s in %.1f seconds" % ("successfully" if success else "with errors", elapsed))

        if verify:
            start = time.time()
            success = prog.verify_image(flash_addr_base, image)
            stop = time.time()
            elapsed = stop - start
            log("Verified %s in %.1f seconds" % ("successfully" if success else "with errors", elapsed))

        if not verify_only:
            prog.start(flash_addr_base)
示例#4
0
                    fd.write(data)
                    logger.info("Data saved in %s", args.image_file)
                else:
                    logger.warn("No data could be read")
        else:
            if not args.image_file:
                parser.exit(1)

            filename = args.image_file
            input_file = Path(args.image_file)
            if not (input_file.exists() and input_file.is_file()):
                logger.error("File does not exist")
                parser.exit(1)

            if args.filetype == "ihex":
                ih = ihex.ihex(filename)
                (args.addr, image) = ih.flatten()
            else:
                with open(filename, "rb") as f:
                    image = f.read()

            prog.prog_image(image, args.addr, args.eraseall)

            prog.start(args.addr)
    finally:
        prog.finalize()

    if args.console:
        import signal
        import sys
        import time