def upload_esp8266(target, aos_path): host_os = get_host_os() if host_os == None: error('Unsupported Operating System!') cmd = 'esptool.py' port = miniterm.ask_for_port() image_path = os.path.join(os.getcwd(), 'out', target, 'binary', target + '-0x1000.bin').replace('\\', '/') boot_bin_path = os.path.join( aos_path, 'platform/mcu/esp8266/bsp/boot_v1.7_921600.bin').replace('\\', '/') init_bin_path = os.path.join( aos_path, 'platform/mcu/esp8266/bsp/esp_init_data_default.bin').replace( '\\', '/') blank_bin_path = os.path.join( aos_path, 'platform/mcu/esp8266/bsp/blank.bin').replace('\\', '/') exec_cmd = [ cmd, '--chip', 'esp8266', '--port', port, '--baud', '921600', 'write_flash', '--flash_size', 'detect', '0x0', boot_bin_path, '0x3fc000', init_bin_path, '0x3fe000', blank_bin_path, '0x1000', image_path ] log("\n[INFO]:exec_cmd: %s\n" % exec_cmd) if (host_os == 'Win32'): ret = subprocess.call(exec_cmd, shell=True) log("---host_os:%s\n" % host_os) else: ret = subprocess.call(exec_cmd, stdout=sys.stdout, stderr=sys.stderr) log("---host_os:%s\n" % host_os) return ret
def upload_mk3060(target, aos_path): host_os = get_host_os() if host_os == None: error('Unsupported Operating System!') cmd = os.path.join(aos_path, 'build', 'aos_firmware_update.py').replace('\\', '/') device_port = miniterm.ask_for_port() log('\n[INFO]:cmd: %s\n' % cmd) addr = '0x13200' image_path = os.path.join(os.getcwd(), 'out', target, 'binary', target + '.bin').replace('\\', '/') log("\n[INFO]:image_path: '%s'\n" % image_path) exec_cmd = [ cmd, device_port, #/dev/ttyUSBx addr, image_path ] log("\n[INFO]:exec_cmd: %s\n" % exec_cmd) if (host_os == 'Win32'): ret = subprocess.call(exec_cmd, shell=True) log("---host_os:%s\n" % host_os) else: ret = subprocess.call(exec_cmd, stdout=sys.stdout, stderr=sys.stderr) log("---host_os:%s\n" % host_os) return ret
def upload_esp32(target, aos_path): host_os = get_host_os() if host_os == None: error('Unsupported Operating System!') cmd = 'esptool.py' port = miniterm.ask_for_port() image_path = os.path.join(os.getcwd(), 'out', target, 'binary', target + '.bin').replace('\\', '/') bootloader_path = os.path.join( aos_path, 'platform/mcu/esp32/bsp/bootloader.bin').replace('\\', '/') partition_path = os.path.join( aos_path, 'platform/mcu/esp32/bsp/custom_partitions.bin').replace('\\', '/') log("\n[INFO]:image_path: %s\n" % image_path) log("[INFO]:bootloader_path: %s\n" % bootloader_path) log("[INFO]:partition_path: %s\n" % partition_path) exec_cmd = [ cmd, '--chip', 'esp32', '--port', port, '--baud', '921600', '--before', 'default_reset', '--after', 'hard_reset', 'write_flash', '-z', '--flash_mode', 'dio', '--flash_freq', '40m', '--flash_size', 'detect', '0x1000', bootloader_path, '0x8000', partition_path, '0x10000', image_path ] log("\n[INFO]:exec_cmd: %s\n" % exec_cmd) if (host_os == 'Win32'): ret = subprocess.call(exec_cmd, shell=True) log("---host_os:%s\n" % host_os) else: ret = subprocess.call(exec_cmd, stdout=sys.stdout, stderr=sys.stderr) log("---host_os:%s\n" % host_os) return ret
def main(): # step 1: get binary file needsave = False myconfig = get_config() bin_files = get_bin_file() if bin_files: myconfig["binfile"] = bin_files needsave = True if not myconfig["binfile"]: print("no specified binary file") return print("binary file is %s" % myconfig["binfile"]) # step 2: get serial port if not myconfig["serialport"]: myconfig["serialport"] = miniterm.ask_for_port() if not myconfig["serialport"]: print("no specified serial port") return else: needsave = True while check_uart(myconfig["serialport"], myconfig['baudrate']) == False: myconfig["serialport"] = miniterm.ask_for_port() print("serial port is %s" % myconfig["serialport"]) print("the settings were restored in the file %s" % os.path.join(os.getcwd(), '.config_burn')) # step 3: burn binary file into flash bin_files = [] for bin_file in myconfig["binfile"]: filename = bin_file address = "0" if "#" in bin_file: filename = bin_file.split("#", 1)[0] address = bin_file.split("#", 1)[1] if address == "0" or address == "0x0" or address == "0x00": bin_files.append( (os.path.join(os.getcwd(), 'binary/ymodem_burn_xz.bin'), address)) else: bin_files.append((filename, address)) print("bin_files is ", bin_files) burn_bin_files(myconfig["serialport"], myconfig['baudrate'], bin_files) if needsave: save_config(myconfig)
def _run_upload_cmd(target, aos_path, cmd_file, program_path=None, bin_dir=None): """ Run the command from cmd file """ ret = 0 usermsg = None global PORT host_os = get_host_os() if not host_os: error("Unsupported Operating System!") configs = read_json(cmd_file) if not configs: error("Can not read flash configs from %s" % cmd_file) if 'description' in configs: usermsg = configs['description'] if usermsg: log(usermsg + '\n\n') if not PORT and '@PORT@' in configs['cmd']: PORT = miniterm.ask_for_port() exec_cmd = [] for item in configs['cmd']: if type(item) == dict: if host_os in item: item = item[host_os] else: error("Flash command is not defined for %s!" % host_os) if '@AOSROOT@/out/@TARGET@' in item: if program_path: item = item.replace('@AOSROOT@', program_path) item = item.replace('@AOSROOT@', aos_path) item = item.replace('@TARGET@', target) if PORT: item = item.replace('@PORT@', PORT) exec_cmd += [item] # Support user defined binaries' dir if bin_dir and os.path.isdir(bin_dir): for index, value in enumerate(exec_cmd): if '.bin' in value or '.cfg' in value or '.elf' in value: basename = os.path.basename(value) exec_cmd[index] = os.path.join(bin_dir, basename) info("Running cmd:\n\t'%s'" % ' '.join(exec_cmd)) if (host_os == 'Win32'): ret = subprocess.call(exec_cmd, shell=True) log("---host_os:%s\n" % host_os) else: ret = subprocess.call(exec_cmd, stdout=sys.stdout, stderr=sys.stderr) log("---host_os:%s\n" % host_os) return ret
def run(device, baud_rate): cli = Interface() if device is None: try: device = ask_for_port() except KeyboardInterrupt: sys.stderr.write(' User Abort\n') sys.exit(1) cli.configure(device=device, baudrate=baud_rate) cli.do_info() cli.cmdloop()
def configure(self, device, baudrate): """ Configure the interface with the appropriate serial parameters. """ while True: try: self.serial = Serial(device, baudrate=baudrate, timeout=.25) except serial.SerialException as e: sys.stderr.write(f'could not open port {device}: {e}\n') device = ask_for_port() else: break
def get_param(arguments=None): """Parse Command-Line parameters""" parser = OptionParser(usage="%prog [options] <data> [<connection>] ", version="%prog " + __version__) opt_con = OptionGroup(parser, 'Connection Settings') opt_con.add_option("-p", "--port", dest="port", help="serial port name ('-' to show port list)") opt_con.add_option("-b", "--baudrate", dest="baudrate", help="set baud rate") parser.add_option_group(opt_con) parser.add_option("-s", "--sleep", dest="sleep", help="wait before read") parser.add_option('-f', '--file', dest='file', help="settings file (json)", default=__file__.split('.')[0] + '.json') (opt, args) = parser.parse_args(arguments) if not os.path.isfile(opt.file): parser.error('Can\'t find data file \"{}\"!'.format(opt.file)) with open(opt.file) as f: json_data = json.load(f) if len(args) < 1: sec_data = 'default' else: sec_data = args[0] if sec_data in json_data["data"]: data = json_data["data"][sec_data] else: parser.error('Can\'t find \"{}\" data section in data file \"{}\"!'.format(sec_data, opt.file)) if len(args) > 1: sec_settings = args[1] else: sec_settings = 'default' if sec_settings in json_data["settings"]: settings = json_data["settings"][sec_settings] else: parser.error('Can\'t found \"{}\" settings section in data file \"{}\"!'.format(sec_settings, opt.file)) if opt.port is not None: if opt.port == '-': settings['port'] = ask_for_port() else: settings['port'] = opt.port if opt.baudrate is not None: settings['baudrate'] = opt.baudrate if opt.sleep is not None: settings['sleep'] = opt.baudrate return settings, data
def main2(): cmd_parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description='''Run and transfer file to system.''', ) cmd_parser.add_argument( '-d', '--device', default='', help='the serial device or the IP address of the pyboard') cmd_parser.add_argument('-b', '--baudrate', default=115200, help='the baud rate of the serial device') cmd_parser.add_argument('files', nargs='*', help='input transfer files') args = cmd_parser.parse_args() print(args) # download file # step 1: set config downloadconfig = get_downloadconfig() # step 2: get serial port if not downloadconfig["serialport"]: downloadconfig["serialport"] = args.device if not downloadconfig["serialport"]: downloadconfig["serialport"] = miniterm.ask_for_port() if not downloadconfig["serialport"]: print("no specified serial port") return else: needsave = True # step 3: get baudrate if not downloadconfig["baudrate"]: downloadconfig["baudrate"] = args.baudrate if not downloadconfig["baudrate"]: downloadconfig["baudrate"] = "115200" # step 4: get transfer file if not downloadconfig["filepath"]: downloadconfig["filepath"] = args.files if not downloadconfig["filepath"]: print('no file wait to transfer') return if os.path.isabs("".join(downloadconfig["filepath"])): filepath = "".join(downloadconfig["filepath"]) print('the filepath is abs path') else: basepath = os.path.abspath('.') filepath = basepath + '/' + "".join(downloadconfig["filepath"]) print('the filepath is not abs path') print("serial port is %s" % downloadconfig["serialport"]) print("transfer baudrate is %s" % downloadconfig["baudrate"]) # print(base_path(downloadconfig["filepath"])) print("filepath is %s" % filepath) # print("the settings were restored in the file %s" % os.path.join(os.getcwd(), '.config_burn')) # step 3: download file download_file(downloadconfig["serialport"], downloadconfig['baudrate'], filepath)
def main2(): cmd_parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description='''Run and transfer file to system.''',) cmd_parser.add_argument('-d', '--device', default='', help='the serial device or the IP address of the pyboard') cmd_parser.add_argument('-b', '--baudrate', default=115200, help='the baud rate of the serial device') cmd_parser.add_argument('-p', '--path', default='NULL', help='the save file path: SDCARD or DATA or NULL') cmd_parser.add_argument('files', nargs='*', help='input transfer files') args = cmd_parser.parse_args() logging.debug(args) # download file # step 1: set config downloadconfig = get_downloadconfig() # step 2: get serial port if not downloadconfig["serialport"]: downloadconfig["serialport"] = args.device if not downloadconfig["serialport"]: downloadconfig["serialport"] = miniterm.ask_for_port() if not downloadconfig["serialport"]: logging.debug("no specified serial port") return else: needsave = True # step 3: get baudrate if not downloadconfig["baudrate"]: downloadconfig["baudrate"] = args.baudrate if not downloadconfig["baudrate"]: downloadconfig["baudrate"] = "115200" # step 4: get saved file path of devices if not downloadconfig["savepath"]: downloadconfig["savepath"] = args.path if not downloadconfig["savepath"]: logging.debug('use default path') return # step 4: get transfer file if not downloadconfig["filepath"]: downloadconfig["filepath"] = args.files if not downloadconfig["filepath"]: logging.debug('no file wait to transfer') return if os.path.isabs("".join(downloadconfig["filepath"])): filepath = "".join(downloadconfig["filepath"]) logging.debug('the filepath is abs path') else: basepath = os.path.abspath('.') filepath = basepath + '/' + "".join(downloadconfig["filepath"]) logging.debug('the filepath is not abs path') print("") print("***** Params setting *****") print("serial port is %s" % downloadconfig["serialport"]) print("transfer baudrate is %s" % downloadconfig["baudrate"]) print("savepath is %s" % downloadconfig["savepath"]) print("filepath is %s" % filepath) print("***** Params setting end *****") print("") # step 3: download file download_file(downloadconfig["serialport"], downloadconfig['baudrate'], filepath, downloadconfig['savepath'])
def _run_upload_cmd(target, aos_path, cmd_file, program_path=None, bin_dir=None): """ Run the command from cmd file """ ret = 0 usermsg = None global PORT host_os = get_host_os() if not host_os: error("Unsupported Operating System!") is_in_docker = _check_in_docker(host_os) info("Check whether in docker: %s" % is_in_docker) if is_in_docker: host_os = _get_host_pc_os() if not host_os: error("Unsupported Operating System of Host PC!") configs = read_json(cmd_file) if not configs: error("Can not read flash configs from %s" % cmd_file) if 'description' in configs: usermsg = configs['description'] if usermsg: log(usermsg + '\n\n') needsave = False myconfig = {} if not PORT and '@PORT@' in configs['cmd']: myconfig = get_config(program_path if program_path else aos_path) if myconfig["serialport"]: if not _check_serial_port(myconfig["serialport"], is_in_docker): myconfig["serialport"] = "" if not myconfig["serialport"]: if not is_in_docker: PORT = miniterm.ask_for_port() else: PORT = _get_serial_port() myconfig["serialport"] = PORT needsave = True else: PORT = myconfig["serialport"] exec_cmd = [] for item in configs['cmd']: if type(item) == dict: if host_os in item: item = item[host_os] else: error("Flash command is not defined for %s!" % host_os) if '@AOSROOT@/out/@TARGET@' in item: if program_path: item = item.replace('@AOSROOT@', program_path) item = item.replace('@AOSROOT@', aos_path) item = item.replace('@TARGET@', target) if PORT: item = item.replace('@PORT@', PORT) exec_cmd += [item] # Support user defined binaries' dir if bin_dir and os.path.isdir(bin_dir): for index, value in enumerate(exec_cmd): if '.bin' in value or '.cfg' in value or '.elf' in value: basename = os.path.basename(value) exec_cmd[index] = os.path.join(bin_dir, basename) info("Running cmd:\n\t'%s'" % ' '.join(exec_cmd)) if not is_in_docker: if (host_os == 'Win32'): ret = subprocess.call(exec_cmd, shell=True) log("---host_os:%s\n" % host_os) else: ret = subprocess.call(exec_cmd, stdout=sys.stdout, stderr=sys.stderr) log("---host_os:%s\n" % host_os) else: ret = _burn_firmware(exec_cmd) if needsave and ret == 0: save_config(program_path if program_path else aos_path, myconfig) return ret
# %% print(ser) ser.flush() print(ser) # %% try: lines = ser.readlines(10) [print(line) for line in lines] except serial.SerialException: ser.close() ser.open() print("Serial connection interrupted") # %% ser.write(b"\nroot\nroot") # %% miniterm.ask_for_port() # %% *asyncio* | *serial_asyncio* class Output(asyncio.Protocol): def connection_made(self, transport): self.transport = transport print('port opened', transport) transport.serial.rts = False # You can manipulate Serial object via transport transport.write(b'Hello, World!\n') # Write serial data via transport def data_received(self, data): print('data received', repr(data)) if b'\n' in data: self.transport.close()