zephyr/scripts/west_commands/tests/test_blackmagicprobe.py
Fabio Baltieri d0377d30e3 scripts: runners: add connect_srst support for blackmagicprobe
Black Magic Probe supports a "monitor connect_srst" command [1] to
configure whether to assert reset when connecting to the target. This is
useful to reprogram a target that may be idling in a low power state,
with an otherwise unresponsive debug core.

Adding a --connect-srst runner option for enabling this for "flash" and
"debug" operations to ensure that programming works in that case, but
also explicitly disabling it for "attach", to allow attaching to a
running target without changing its state.

Not turning this on by default since apparently some MCUs disable the
SWD interface while in reset.

[1] https://github.com/blacksphere/blackmagic/wiki/Useful-GDB-commands

Signed-off-by: Fabio Baltieri <fabio.baltieri@gmail.com>
2021-11-22 22:20:02 -05:00

91 lines
3.4 KiB
Python

# Copyright (c) 2018 Foundries.io
# Copyright (c) 2019 Nordic Semiconductor ASA.
#
# SPDX-License-Identifier: Apache-2.0
import argparse
from unittest.mock import patch, call
import pytest
from runners.blackmagicprobe import BlackMagicProbeRunner
from conftest import RC_KERNEL_ELF, RC_GDB
TEST_GDB_SERIAL = 'test-gdb-serial'
# Expected subprocesses to be run for each command. Using the
# runner_config fixture (and always specifying gdb-serial) means we
# don't get 100% coverage, but it's a starting out point.
EXPECTED_COMMANDS = {
'attach':
([RC_GDB,
'-ex', "set confirm off",
'-ex', "target extended-remote {}".format(TEST_GDB_SERIAL),
'-ex', "monitor swdp_scan",
'-ex', "attach 1",
'-ex', "file {}".format(RC_KERNEL_ELF)],),
'debug':
([RC_GDB,
'-ex', "set confirm off",
'-ex', "target extended-remote {}".format(TEST_GDB_SERIAL),
'-ex', "monitor swdp_scan",
'-ex', "attach 1",
'-ex', "file {}".format(RC_KERNEL_ELF),
'-ex', "load {}".format(RC_KERNEL_ELF)],),
'flash':
([RC_GDB,
'-ex', "set confirm off",
'-ex', "target extended-remote {}".format(TEST_GDB_SERIAL),
'-ex', "monitor swdp_scan",
'-ex', "attach 1",
'-ex', "load {}".format(RC_KERNEL_ELF),
'-ex', "kill",
'-ex', "quit",
'-silent'],),
}
EXPECTED_CONNECT_SRST_COMMAND = {
'attach': 'monitor connect_srst disable',
'debug': 'monitor connect_srst enable',
'flash': 'monitor connect_srst enable',
}
def require_patch(program):
assert program == RC_GDB
@pytest.mark.parametrize('command', EXPECTED_COMMANDS)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
@patch('runners.core.ZephyrBinaryRunner.check_call')
def test_blackmagicprobe_init(cc, req, command, runner_config):
'''Test commands using a runner created by constructor.'''
runner = BlackMagicProbeRunner(runner_config, TEST_GDB_SERIAL)
runner.run(command)
assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS[command]]
@pytest.mark.parametrize('command', EXPECTED_COMMANDS)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
@patch('runners.core.ZephyrBinaryRunner.check_call')
def test_blackmagicprobe_create(cc, req, command, runner_config):
'''Test commands using a runner created from command line parameters.'''
args = ['--gdb-serial', TEST_GDB_SERIAL]
parser = argparse.ArgumentParser()
BlackMagicProbeRunner.add_parser(parser)
arg_namespace = parser.parse_args(args)
runner = BlackMagicProbeRunner.create(runner_config, arg_namespace)
runner.run(command)
assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS[command]]
@pytest.mark.parametrize('command', EXPECTED_CONNECT_SRST_COMMAND)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
@patch('runners.core.ZephyrBinaryRunner.check_call')
def test_blackmagicprobe_connect_srst(cc, req, command, runner_config):
'''Test that commands list the correct connect_srst value when enabled.'''
args = ['--gdb-serial', TEST_GDB_SERIAL, '--connect-srst']
parser = argparse.ArgumentParser()
BlackMagicProbeRunner.add_parser(parser)
arg_namespace = parser.parse_args(args)
runner = BlackMagicProbeRunner.create(runner_config, arg_namespace)
runner.run(command)
expected = EXPECTED_CONNECT_SRST_COMMAND[command]
assert expected in cc.call_args_list[0][0][0]