2019-10-03 12:26:44 -04:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-11-05 09:21:28 -05:00
|
|
|
import click
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
from idf_py_actions.errors import FatalError
|
|
|
|
from idf_py_actions.global_options import global_options
|
2020-02-06 13:29:50 -05:00
|
|
|
from idf_py_actions.tools import ensure_build_directory, run_tool, run_target, get_sdkconfig_value
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# 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 RuntimeError(
|
|
|
|
"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()
|
|
|
|
result = [PYTHON, esptool_path]
|
|
|
|
result += ["-p", args.port]
|
|
|
|
result += ["-b", str(args.baud)]
|
|
|
|
|
|
|
|
with open(os.path.join(args.build_dir, "flasher_args.json")) as f:
|
|
|
|
flasher_args = json.load(f)
|
|
|
|
|
|
|
|
extra_esptool_args = flasher_args["extra_esptool_args"]
|
2019-09-26 22:19:08 -04:00
|
|
|
result += ["--before", extra_esptool_args["before"]]
|
2019-10-03 12:26:44 -04:00
|
|
|
result += ["--after", extra_esptool_args["after"]]
|
2019-11-28 08:09:45 -05:00
|
|
|
result += ["--chip", extra_esptool_args["chip"]]
|
|
|
|
if not extra_esptool_args["stub"]:
|
|
|
|
result += ["--no-stub"]
|
2019-10-03 12:26:44 -04:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _get_commandline_options(ctx):
|
|
|
|
""" Return all the command line options up to first action """
|
|
|
|
# This approach ignores argument parsing done Click
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for arg in sys.argv:
|
|
|
|
if arg in ctx.command.commands_with_aliases:
|
|
|
|
break
|
|
|
|
|
|
|
|
result.append(arg)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2020-03-23 11:14:34 -04:00
|
|
|
def monitor(action, ctx, args, print_filter, monitor_baud, encrypted):
|
2019-10-03 12:26:44 -04:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
with open(desc_path, "r") as f:
|
|
|
|
project_desc = json.load(f)
|
|
|
|
|
|
|
|
elf_file = os.path.join(args.build_dir, project_desc["app_elf"])
|
|
|
|
if not os.path.exists(elf_file):
|
|
|
|
raise FatalError("ELF file '%s' not found. You need to build & flash the project before running 'monitor', "
|
|
|
|
"and the binary on the device must match the one in the build directory exactly. "
|
2019-07-23 15:37:31 -04:00
|
|
|
"Try '%s flash monitor'." % (elf_file, ctx.info_name), ctx)
|
2019-10-03 12:26:44 -04:00
|
|
|
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]
|
2019-11-05 09:21:28 -05:00
|
|
|
|
|
|
|
if not monitor_baud:
|
|
|
|
if os.getenv("IDF_MONITOR_BAUD"):
|
|
|
|
monitor_baud = os.getenv("IDF_MONITOR_BAUD", None)
|
|
|
|
elif os.getenv("MONITORBAUD"):
|
|
|
|
monitor_baud = os.getenv("MONITORBAUD", None)
|
|
|
|
else:
|
|
|
|
monitor_baud = project_desc["monitor_baud"]
|
|
|
|
|
|
|
|
monitor_args += ["-b", monitor_baud]
|
2019-10-03 12:26:44 -04:00
|
|
|
monitor_args += ["--toolchain-prefix", project_desc["monitor_toolprefix"]]
|
|
|
|
|
2020-02-06 13:29:50 -05:00
|
|
|
coredump_decode = get_sdkconfig_value(project_desc["config_file"], "CONFIG_ESP32_CORE_DUMP_DECODE")
|
|
|
|
if coredump_decode is not None:
|
|
|
|
monitor_args += ["--decode-coredumps", coredump_decode]
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if print_filter is not None:
|
|
|
|
monitor_args += ["--print_filter", print_filter]
|
|
|
|
monitor_args += [elf_file]
|
|
|
|
|
2020-03-23 11:14:34 -04:00
|
|
|
if encrypted:
|
|
|
|
monitor_args += ['--encrypted']
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
idf_py = [PYTHON] + _get_commandline_options(ctx) # commands to re-run idf.py
|
|
|
|
monitor_args += ["-m", " ".join("'%s'" % a for a in idf_py)]
|
|
|
|
|
|
|
|
if "MSYSTEM" in os.environ:
|
|
|
|
monitor_args = ["winpty"] + monitor_args
|
|
|
|
run_tool("idf_monitor", monitor_args, args.project_dir)
|
|
|
|
|
|
|
|
def flash(action, ctx, args):
|
2020-02-14 02:24:11 -05:00
|
|
|
ensure_build_directory(args, ctx.info_name)
|
2019-10-03 12:26:44 -04:00
|
|
|
"""
|
|
|
|
Run esptool to flash the entire project, from an argfile generated by the build system
|
|
|
|
"""
|
2019-12-19 20:55:39 -05:00
|
|
|
if args.port is None:
|
|
|
|
args.port = _get_default_serial_port()
|
|
|
|
|
|
|
|
run_target(action, args, {"ESPPORT": args.port,
|
|
|
|
"ESPBAUD": str(args.baud)})
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
def erase_flash(action, ctx, args):
|
|
|
|
esptool_args = _get_esptool_args(args)
|
|
|
|
esptool_args += ["erase_flash"]
|
|
|
|
run_tool("esptool.py", esptool_args, args.build_dir)
|
|
|
|
|
2020-03-23 11:14:34 -04:00
|
|
|
def global_callback(ctx, global_args, tasks):
|
|
|
|
encryption = any([task.name in ("encrypted-flash", "encrypted-app-flash") for task in tasks])
|
|
|
|
if encryption:
|
|
|
|
for task in tasks:
|
|
|
|
if task.name == "monitor":
|
|
|
|
task.action_args["encrypted"] = True
|
|
|
|
break
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
baud_rate = {
|
|
|
|
"names": ["-b", "--baud"],
|
2019-11-05 09:21:28 -05:00
|
|
|
"help": "Baud rate for flashing.",
|
2019-10-03 12:26:44 -04:00
|
|
|
"scope": "global",
|
|
|
|
"envvar": "ESPBAUD",
|
|
|
|
"default": 460800,
|
|
|
|
}
|
|
|
|
|
|
|
|
port = {
|
|
|
|
"names": ["-p", "--port"],
|
|
|
|
"help": "Serial port.",
|
|
|
|
"scope": "global",
|
|
|
|
"envvar": "ESPPORT",
|
|
|
|
"default": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
serial_actions = {
|
2020-03-23 11:14:34 -04:00
|
|
|
"global_action_callbacks": [global_callback],
|
2019-10-03 12:26:44 -04:00
|
|
|
"actions": {
|
|
|
|
"flash": {
|
|
|
|
"callback": flash,
|
|
|
|
"help": "Flash the project.",
|
|
|
|
"options": global_options + [baud_rate, port],
|
2020-02-14 02:24:11 -05:00
|
|
|
"order_dependencies": ["all", "erase_flash"],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
"erase_flash": {
|
|
|
|
"callback": erase_flash,
|
|
|
|
"help": "Erase entire flash chip.",
|
|
|
|
"options": [baud_rate, port],
|
|
|
|
},
|
|
|
|
"monitor": {
|
2019-11-05 09:21:28 -05:00
|
|
|
"callback":
|
|
|
|
monitor,
|
|
|
|
"help":
|
|
|
|
"Display serial output.",
|
2019-10-03 12:26:44 -04:00
|
|
|
"options": [
|
2019-11-05 09:21:28 -05:00
|
|
|
port, {
|
2019-10-03 12:26:44 -04:00
|
|
|
"names": ["--print-filter", "--print_filter"],
|
2019-11-05 09:21:28 -05:00
|
|
|
"help":
|
|
|
|
("Filter monitor output.\n"
|
|
|
|
"Restrictions on what to print can be specified as a series of <tag>:<log_level> items "
|
|
|
|
"where <tag> is the tag string and <log_level> is a character from the set "
|
|
|
|
"{N, E, W, I, D, V, *} referring to a level. "
|
|
|
|
'For example, "tag1:W" matches and prints only the outputs written with '
|
|
|
|
'ESP_LOGW("tag1", ...) or at lower verbosity level, i.e. ESP_LOGE("tag1", ...). '
|
|
|
|
'Not specifying a <log_level> or using "*" defaults to Verbose level.\n'
|
|
|
|
'Please see the IDF Monitor section of the ESP-IDF documentation '
|
|
|
|
'for a more detailed description and further examples.'),
|
|
|
|
"default":
|
|
|
|
None,
|
|
|
|
}, {
|
|
|
|
"names": ["--monitor-baud", "-B"],
|
|
|
|
"type":
|
|
|
|
click.INT,
|
|
|
|
"help": ("Baud rate for monitor.\n"
|
|
|
|
"If this option is not provided IDF_MONITOR_BAUD and MONITORBAUD "
|
|
|
|
"environment variables and project_description.json in build directory "
|
|
|
|
"(generated by CMake from project's sdkconfig) "
|
|
|
|
"will be checked for default value."),
|
2020-03-23 11:14:34 -04:00
|
|
|
}, {
|
|
|
|
"names": ["--encrypted", "-E"],
|
|
|
|
"is_flag": True,
|
|
|
|
"help": ("Enable encrypted flash targets.\n"
|
|
|
|
"IDF Monitor will invoke encrypted-flash and encrypted-app-flash targets "
|
|
|
|
"if this option is set. This option is set by default if IDF Monitor was invoked "
|
|
|
|
"together with encrypted-flash or encrypted-app-flash target."),
|
2019-11-05 09:21:28 -05:00
|
|
|
}
|
2019-10-03 12:26:44 -04:00
|
|
|
],
|
|
|
|
"order_dependencies": [
|
|
|
|
"flash",
|
2020-03-23 11:14:34 -04:00
|
|
|
"encrypted-flash",
|
2019-10-03 12:26:44 -04:00
|
|
|
"partition_table-flash",
|
|
|
|
"bootloader-flash",
|
|
|
|
"app-flash",
|
2020-03-23 11:14:34 -04:00
|
|
|
"encrypted-app-flash",
|
2019-10-03 12:26:44 -04:00
|
|
|
],
|
|
|
|
},
|
|
|
|
"partition_table-flash": {
|
|
|
|
"callback": flash,
|
|
|
|
"help": "Flash partition table only.",
|
|
|
|
"options": [baud_rate, port],
|
2020-02-14 02:24:11 -05:00
|
|
|
"order_dependencies": ["partition_table", "erase_flash"],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
"bootloader-flash": {
|
|
|
|
"callback": flash,
|
|
|
|
"help": "Flash bootloader only.",
|
|
|
|
"options": [baud_rate, port],
|
2020-02-14 02:24:11 -05:00
|
|
|
"order_dependencies": ["bootloader", "erase_flash"],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
"app-flash": {
|
|
|
|
"callback": flash,
|
|
|
|
"help": "Flash the app only.",
|
|
|
|
"options": [baud_rate, port],
|
2020-02-14 02:24:11 -05:00
|
|
|
"order_dependencies": ["app", "erase_flash"],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
"encrypted-app-flash": {
|
|
|
|
"callback": flash,
|
|
|
|
"help": "Flash the encrypted app only.",
|
2020-02-14 02:24:11 -05:00
|
|
|
"order_dependencies": ["app", "erase_flash"],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
"encrypted-flash": {
|
|
|
|
"callback": flash,
|
|
|
|
"help": "Flash the encrypted project.",
|
2020-02-14 02:24:11 -05:00
|
|
|
"order_dependencies": ["all", "erase_flash"],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return serial_actions
|