Merge branch 'feature/idf-list-ports' into 'master'

idf.py: Use function for port detection from esptool.py in idf.py and idf_monitor.py

Closes IDFGH-3104

See merge request espressif/esp-idf!11359
This commit is contained in:
Ivan Grokhotkov 2021-01-08 21:36:08 +08:00
commit bac77b4bf7
2 changed files with 19 additions and 38 deletions

View File

@ -991,24 +991,12 @@ class Monitor(object):
def main():
def _get_default_serial_port():
"""
Same logic for detecting serial port as esptool.py and idf.py: reverse sort by name and choose the first port.
"""
try:
ports = list(reversed(sorted(p.device for p in serial.tools.list_ports.comports())))
return ports[0]
except Exception:
return '/dev/ttyUSB0'
parser = argparse.ArgumentParser("idf_monitor - a serial output monitor for esp-idf")
parser.add_argument(
'--port', '-p',
help='Serial port device',
default=os.environ.get('ESPTOOL_PORT', _get_default_serial_port())
default=os.environ.get('ESPTOOL_PORT', '/dev/ttyUSB0')
)
parser.add_argument(

View File

@ -12,28 +12,25 @@ PYTHON = sys.executable
def action_extensions(base_actions, project_path):
def _get_default_serial_port():
""" Return a default serial port. esptool can do this (smarter), but it can create
inconsistencies where esptool.py uses one port and idf_monitor uses another.
Same logic as esptool.py search order, reverse sort by name and choose the first port.
"""
def _get_default_serial_port(args):
# Import is done here in order to move it after the check_environment() ensured that pyserial has been installed
import serial.tools.list_ports
ports = list(reversed(sorted(p.device for p in serial.tools.list_ports.comports())))
try:
print("Choosing default port %s (use '-p PORT' option to set a specific serial port)" %
ports[0].encode("ascii", "ignore"))
return ports[0]
except IndexError:
raise FatalError(
"No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.")
import serial.tools.list_ports
esptool_path = os.path.join(os.environ["IDF_PATH"], "components/esptool_py/esptool/")
sys.path.insert(0, esptool_path)
import esptool
ports = list(sorted(p.device for p in serial.tools.list_ports.comports()))
esp = esptool.get_default_connected_device(serial_list=ports, port=None, connect_attempts=3,
initial_baud=args.baud)
return esp.serial_port
except Exception:
raise FatalError("No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.")
def _get_esptool_args(args):
esptool_path = os.path.join(os.environ["IDF_PATH"], "components/esptool_py/esptool/esptool.py")
if args.port is None:
args.port = _get_default_serial_port()
args.port = _get_default_serial_port(args)
result = [PYTHON, esptool_path]
result += ["-p", args.port]
result += ["-b", str(args.baud)]
@ -66,8 +63,7 @@ def action_extensions(base_actions, project_path):
"""
Run idf_monitor.py to watch build output
"""
if args.port is None:
args.port = _get_default_serial_port()
desc_path = os.path.join(args.build_dir, "project_description.json")
if not os.path.exists(desc_path):
ensure_build_directory(args, ctx.info_name)
@ -81,8 +77,8 @@ def action_extensions(base_actions, project_path):
"Try '%s flash monitor'." % (elf_file, ctx.info_name), ctx)
idf_monitor = os.path.join(os.environ["IDF_PATH"], "tools/idf_monitor.py")
monitor_args = [PYTHON, idf_monitor]
if args.port is not None:
monitor_args += ["-p", args.port]
esp_port = args.port or _get_default_serial_port(args)
monitor_args += ["-p", esp_port]
if not monitor_baud:
monitor_baud = os.getenv("IDF_MONITOR_BAUD") or os.getenv("MONITORBAUD") or project_desc["monitor_baud"]
@ -117,11 +113,8 @@ def action_extensions(base_actions, project_path):
Run esptool to flash the entire project, from an argfile generated by the build system
"""
ensure_build_directory(args, ctx.info_name)
if args.port is None:
args.port = _get_default_serial_port()
run_target(action, args, {"ESPPORT": args.port,
"ESPBAUD": str(args.baud)})
esp_port = args.port or _get_default_serial_port(args)
run_target(action, args, {"ESPBAUD": str(args.baud),"ESPPORT": esp_port})
def erase_flash(action, ctx, args):
ensure_build_directory(args, ctx.info_name)