2019-10-03 12:26:44 -04:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-11-05 09:21:28 -05:00
|
|
|
import click
|
2021-02-01 05:40:03 -05:00
|
|
|
from idf_py_actions.errors import FatalError, NoSerialPortFoundError
|
2019-10-03 12:26:44 -04:00
|
|
|
from idf_py_actions.global_options import global_options
|
2021-01-25 21:49:01 -05:00
|
|
|
from idf_py_actions.tools import ensure_build_directory, get_sdkconfig_value, run_target, run_tool
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
PYTHON = sys.executable
|
|
|
|
|
|
|
|
|
|
|
|
def action_extensions(base_actions, project_path):
|
2020-11-26 08:28:18 -05:00
|
|
|
def _get_default_serial_port(args):
|
2019-10-03 12:26:44 -04:00
|
|
|
# Import is done here in order to move it after the check_environment() ensured that pyserial has been installed
|
|
|
|
try:
|
2020-11-26 08:28:18 -05:00
|
|
|
import serial.tools.list_ports
|
2021-01-25 21:49:01 -05:00
|
|
|
esptool_path = os.path.join(os.environ['IDF_PATH'], 'components/esptool_py/esptool/')
|
2020-11-26 08:28:18 -05:00
|
|
|
sys.path.insert(0, esptool_path)
|
|
|
|
import esptool
|
|
|
|
ports = list(sorted(p.device for p in serial.tools.list_ports.comports()))
|
2021-02-01 05:40:03 -05:00
|
|
|
# high baud rate could cause the failure of creation of the connection
|
|
|
|
esp = esptool.get_default_connected_device(serial_list=ports, port=None, connect_attempts=4,
|
|
|
|
initial_baud=115200)
|
|
|
|
if esp is None:
|
|
|
|
raise NoSerialPortFoundError(
|
|
|
|
"No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.")
|
|
|
|
|
|
|
|
serial_port = esp.serial_port
|
|
|
|
esp._port.close()
|
|
|
|
|
|
|
|
return serial_port
|
|
|
|
except NoSerialPortFoundError:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
raise FatalError('An exception occurred during detection of the serial port: {}'.format(e))
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
def _get_esptool_args(args):
|
2021-01-25 21:49:01 -05:00
|
|
|
esptool_path = os.path.join(os.environ['IDF_PATH'], 'components/esptool_py/esptool/esptool.py')
|
2021-04-15 10:19:25 -04:00
|
|
|
esptool_wrapper_path = os.environ.get('ESPTOOL_WRAPPER', '')
|
2019-10-03 12:26:44 -04:00
|
|
|
if args.port is None:
|
2020-11-26 08:28:18 -05:00
|
|
|
args.port = _get_default_serial_port(args)
|
2021-04-15 10:19:25 -04:00
|
|
|
result = [PYTHON]
|
|
|
|
if os.path.exists(esptool_wrapper_path):
|
|
|
|
result += [esptool_wrapper_path]
|
|
|
|
result += [esptool_path]
|
2021-01-25 21:49:01 -05:00
|
|
|
result += ['-p', args.port]
|
|
|
|
result += ['-b', str(args.baud)]
|
2019-10-03 12:26:44 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
with open(os.path.join(args.build_dir, 'flasher_args.json')) as f:
|
2019-10-03 12:26:44 -04:00
|
|
|
flasher_args = json.load(f)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
extra_esptool_args = flasher_args['extra_esptool_args']
|
|
|
|
result += ['--before', extra_esptool_args['before']]
|
|
|
|
result += ['--after', extra_esptool_args['after']]
|
|
|
|
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
|
|
|
|
|
2021-05-27 10:14:55 -04:00
|
|
|
def monitor(action, ctx, args, print_filter, monitor_baud, encrypted, timestamps, timestamp_format):
|
2019-10-03 12:26:44 -04:00
|
|
|
"""
|
|
|
|
Run idf_monitor.py to watch build output
|
|
|
|
"""
|
2020-11-26 08:28:18 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
desc_path = os.path.join(args.build_dir, 'project_description.json')
|
2019-10-03 12:26:44 -04:00
|
|
|
if not os.path.exists(desc_path):
|
|
|
|
ensure_build_directory(args, ctx.info_name)
|
2021-01-25 21:49:01 -05:00
|
|
|
with open(desc_path, 'r') as f:
|
2019-10-03 12:26:44 -04:00
|
|
|
project_desc = json.load(f)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
elf_file = os.path.join(args.build_dir, project_desc['app_elf'])
|
2019-10-03 12:26:44 -04:00
|
|
|
if not os.path.exists(elf_file):
|
|
|
|
raise FatalError("ELF file '%s' not found. You need to build & flash the project before running 'monitor', "
|
2021-01-25 21:49:01 -05:00
|
|
|
'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)
|
2021-01-25 21:49:01 -05:00
|
|
|
idf_monitor = os.path.join(os.environ['IDF_PATH'], 'tools/idf_monitor.py')
|
2019-10-03 12:26:44 -04:00
|
|
|
monitor_args = [PYTHON, idf_monitor]
|
2020-11-26 08:28:18 -05:00
|
|
|
esp_port = args.port or _get_default_serial_port(args)
|
2021-01-25 21:49:01 -05:00
|
|
|
monitor_args += ['-p', esp_port]
|
2019-11-05 09:21:28 -05:00
|
|
|
|
|
|
|
if not monitor_baud:
|
2021-01-25 21:49:01 -05:00
|
|
|
monitor_baud = os.getenv('IDF_MONITOR_BAUD') or os.getenv('MONITORBAUD') or project_desc['monitor_baud']
|
2019-11-05 09:21:28 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
monitor_args += ['-b', monitor_baud]
|
|
|
|
monitor_args += ['--toolchain-prefix', project_desc['monitor_toolprefix']]
|
2019-10-03 12:26:44 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
coredump_decode = get_sdkconfig_value(project_desc['config_file'], 'CONFIG_ESP_COREDUMP_DECODE')
|
2020-02-06 13:29:50 -05:00
|
|
|
if coredump_decode is not None:
|
2021-01-25 21:49:01 -05:00
|
|
|
monitor_args += ['--decode-coredumps', coredump_decode]
|
2020-02-06 13:29:50 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
target_arch_riscv = get_sdkconfig_value(project_desc['config_file'], 'CONFIG_IDF_TARGET_ARCH_RISCV')
|
2021-02-01 05:40:03 -05:00
|
|
|
monitor_args += ['--target', project_desc['target']]
|
2021-04-15 13:41:09 -04:00
|
|
|
revision = project_desc.get('rev')
|
|
|
|
if revision:
|
|
|
|
monitor_args += ['--revision', revision]
|
2021-02-01 05:40:03 -05:00
|
|
|
|
2020-11-19 21:19:04 -05:00
|
|
|
if target_arch_riscv:
|
2021-02-01 05:40:03 -05:00
|
|
|
monitor_args += ['--decode-panic', 'backtrace']
|
2020-11-19 21:19:04 -05:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
if print_filter is not None:
|
2021-01-25 21:49:01 -05:00
|
|
|
monitor_args += ['--print_filter', print_filter]
|
2019-10-03 12:26:44 -04:00
|
|
|
monitor_args += [elf_file]
|
|
|
|
|
2020-03-23 11:14:34 -04:00
|
|
|
if encrypted:
|
|
|
|
monitor_args += ['--encrypted']
|
|
|
|
|
2021-05-27 10:14:55 -04:00
|
|
|
if timestamps:
|
|
|
|
monitor_args += ['--timestamps']
|
|
|
|
|
|
|
|
if timestamp_format:
|
|
|
|
monitor_args += ['--timestamp-format', timestamp_format]
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
idf_py = [PYTHON] + _get_commandline_options(ctx) # commands to re-run idf.py
|
2021-01-25 21:49:01 -05:00
|
|
|
monitor_args += ['-m', ' '.join("'%s'" % a for a in idf_py)]
|
2019-10-03 12:26:44 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if 'MSYSTEM' in os.environ:
|
|
|
|
monitor_args = ['winpty'] + monitor_args
|
2021-02-01 05:40:03 -05:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
run_tool('idf_monitor', monitor_args, args.project_dir)
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
def flash(action, ctx, args):
|
|
|
|
"""
|
|
|
|
Run esptool to flash the entire project, from an argfile generated by the build system
|
|
|
|
"""
|
2020-07-20 08:51:47 -04:00
|
|
|
ensure_build_directory(args, ctx.info_name)
|
2020-11-26 08:28:18 -05:00
|
|
|
esp_port = args.port or _get_default_serial_port(args)
|
2021-01-25 21:49:01 -05:00
|
|
|
run_target(action, args, {'ESPBAUD': str(args.baud),'ESPPORT': esp_port})
|
2019-10-03 12:26:44 -04:00
|
|
|
|
|
|
|
def erase_flash(action, ctx, args):
|
2020-07-20 08:51:47 -04:00
|
|
|
ensure_build_directory(args, ctx.info_name)
|
2019-10-03 12:26:44 -04:00
|
|
|
esptool_args = _get_esptool_args(args)
|
2021-01-25 21:49:01 -05:00
|
|
|
esptool_args += ['erase_flash']
|
|
|
|
run_tool('esptool.py', esptool_args, args.build_dir)
|
2019-10-03 12:26:44 -04:00
|
|
|
|
2020-03-23 11:14:34 -04:00
|
|
|
def global_callback(ctx, global_args, tasks):
|
2021-01-25 21:49:01 -05:00
|
|
|
encryption = any([task.name in ('encrypted-flash', 'encrypted-app-flash') for task in tasks])
|
2020-03-23 11:14:34 -04:00
|
|
|
if encryption:
|
|
|
|
for task in tasks:
|
2021-01-25 21:49:01 -05:00
|
|
|
if task.name == 'monitor':
|
|
|
|
task.action_args['encrypted'] = True
|
2020-03-23 11:14:34 -04:00
|
|
|
break
|
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
baud_rate = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'names': ['-b', '--baud'],
|
|
|
|
'help': 'Baud rate for flashing.',
|
|
|
|
'scope': 'global',
|
|
|
|
'envvar': 'ESPBAUD',
|
|
|
|
'default': 460800,
|
2019-10-03 12:26:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
port = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'names': ['-p', '--port'],
|
|
|
|
'help': 'Serial port.',
|
|
|
|
'scope': 'global',
|
|
|
|
'envvar': 'ESPPORT',
|
|
|
|
'default': None,
|
2019-10-03 12:26:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
serial_actions = {
|
2021-01-25 21:49:01 -05:00
|
|
|
'global_action_callbacks': [global_callback],
|
|
|
|
'actions': {
|
|
|
|
'flash': {
|
|
|
|
'callback': flash,
|
|
|
|
'help': 'Flash the project.',
|
|
|
|
'options': global_options + [baud_rate, port],
|
2021-08-11 09:24:17 -04:00
|
|
|
'order_dependencies': ['all', 'erase-flash'],
|
|
|
|
},
|
|
|
|
'erase-flash': {
|
|
|
|
'callback': erase_flash,
|
|
|
|
'help': 'Erase entire flash chip. Deprecated alias: "erase_flash"',
|
|
|
|
'options': [baud_rate, port],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'erase_flash': {
|
|
|
|
'callback': erase_flash,
|
2021-08-11 09:24:17 -04:00
|
|
|
'deprecated': {
|
|
|
|
'removed': 'v5.0',
|
|
|
|
'message': 'Please use "erase-flash" instead.',
|
|
|
|
},
|
|
|
|
'hidden': True,
|
2021-01-25 21:49:01 -05:00
|
|
|
'help': 'Erase entire flash chip.',
|
|
|
|
'options': [baud_rate, port],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'monitor': {
|
|
|
|
'callback':
|
2019-11-05 09:21:28 -05:00
|
|
|
monitor,
|
2021-01-25 21:49:01 -05:00
|
|
|
'help':
|
|
|
|
'Display serial output.',
|
|
|
|
'options': [
|
2019-11-05 09:21:28 -05:00
|
|
|
port, {
|
2021-01-25 21:49:01 -05:00
|
|
|
'names': ['--print-filter', '--print_filter'],
|
|
|
|
'help':
|
|
|
|
('Filter monitor output. '
|
|
|
|
'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. '
|
2019-11-05 09:21:28 -05:00
|
|
|
'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", ...). '
|
2020-11-02 08:11:14 -05:00
|
|
|
'Not specifying a <log_level> or using "*" defaults to Verbose level. '
|
2019-11-05 09:21:28 -05:00
|
|
|
'Please see the IDF Monitor section of the ESP-IDF documentation '
|
|
|
|
'for a more detailed description and further examples.'),
|
2021-01-25 21:49:01 -05:00
|
|
|
'default':
|
2019-11-05 09:21:28 -05:00
|
|
|
None,
|
|
|
|
}, {
|
2021-01-25 21:49:01 -05:00
|
|
|
'names': ['--monitor-baud', '-B'],
|
|
|
|
'type':
|
2019-11-05 09:21:28 -05:00
|
|
|
click.INT,
|
2021-01-25 21:49:01 -05:00
|
|
|
'help': ('Baud rate for monitor. '
|
|
|
|
'If this option is not provided IDF_MONITOR_BAUD and MONITORBAUD '
|
|
|
|
'environment variables and project_description.json in build directory '
|
2019-11-05 09:21:28 -05:00
|
|
|
"(generated by CMake from project's sdkconfig) "
|
2021-01-25 21:49:01 -05:00
|
|
|
'will be checked for default value.'),
|
2020-03-23 11:14:34 -04:00
|
|
|
}, {
|
2021-01-25 21:49:01 -05:00
|
|
|
'names': ['--encrypted', '-E'],
|
|
|
|
'is_flag': True,
|
|
|
|
'help': ('Enable encrypted flash targets. '
|
|
|
|
'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.'),
|
2021-05-27 10:14:55 -04:00
|
|
|
}, {
|
|
|
|
'names': ['--timestamps'],
|
|
|
|
'is_flag': True,
|
|
|
|
'help': 'Print a time stamp in the beginning of each line.',
|
|
|
|
}, {
|
|
|
|
'names': ['--timestamp-format'],
|
|
|
|
'help': ('Set the formatting of timestamps compatible with strftime(). '
|
|
|
|
'For example, "%Y-%m-%d %H:%M:%S".'),
|
|
|
|
'default': None
|
2019-11-05 09:21:28 -05:00
|
|
|
}
|
2021-05-27 10:14:55 -04:00
|
|
|
|
2019-10-03 12:26:44 -04:00
|
|
|
],
|
2021-01-25 21:49:01 -05:00
|
|
|
'order_dependencies': [
|
|
|
|
'flash',
|
|
|
|
'encrypted-flash',
|
2021-08-11 09:24:17 -04:00
|
|
|
'partition-table-flash',
|
2021-01-25 21:49:01 -05:00
|
|
|
'bootloader-flash',
|
|
|
|
'app-flash',
|
|
|
|
'encrypted-app-flash',
|
2019-10-03 12:26:44 -04:00
|
|
|
],
|
|
|
|
},
|
2021-08-11 09:24:17 -04:00
|
|
|
'partition-table-flash': {
|
|
|
|
'callback': flash,
|
|
|
|
'help': 'Flash partition table only. Deprecated alias: "partition_table-flash".',
|
|
|
|
'options': [baud_rate, port],
|
|
|
|
'order_dependencies': ['partition-table', 'erase-flash'],
|
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'partition_table-flash': {
|
|
|
|
'callback': flash,
|
2021-08-11 09:24:17 -04:00
|
|
|
'hidden': True,
|
2021-01-25 21:49:01 -05:00
|
|
|
'help': 'Flash partition table only.',
|
|
|
|
'options': [baud_rate, port],
|
2021-08-11 09:24:17 -04:00
|
|
|
'order_dependencies': ['partition-table', 'erase-flash'],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'bootloader-flash': {
|
|
|
|
'callback': flash,
|
|
|
|
'help': 'Flash bootloader only.',
|
|
|
|
'options': [baud_rate, port],
|
2021-08-11 09:24:17 -04:00
|
|
|
'order_dependencies': ['bootloader', 'erase-flash'],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'app-flash': {
|
|
|
|
'callback': flash,
|
|
|
|
'help': 'Flash the app only.',
|
|
|
|
'options': [baud_rate, port],
|
2021-08-11 09:24:17 -04:00
|
|
|
'order_dependencies': ['app', 'erase-flash'],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'encrypted-app-flash': {
|
|
|
|
'callback': flash,
|
|
|
|
'help': 'Flash the encrypted app only.',
|
2021-08-11 09:24:17 -04:00
|
|
|
'order_dependencies': ['app', 'erase-flash'],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
2021-01-25 21:49:01 -05:00
|
|
|
'encrypted-flash': {
|
|
|
|
'callback': flash,
|
|
|
|
'help': 'Flash the encrypted project.',
|
2021-08-11 09:24:17 -04:00
|
|
|
'order_dependencies': ['all', 'erase-flash'],
|
2019-10-03 12:26:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return serial_actions
|