Skip to content

Commit

Permalink
targets/rp2040: Add --chip argument to unit_test_server.py
Browse files Browse the repository at this point in the history
Change-Id: I4ce58927e6e44028abc3a064602e05d79456725f
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/241194
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
Commit-Queue: Erik Gilling <konkers@google.com>
  • Loading branch information
konkers authored and CQ Bot Account committed Oct 11, 2024
1 parent 6bdaaa9 commit de832f1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/showcases/sense/tutorial/device_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Run on-device tests
$ bazelisk run \
@pigweed//targets/rp2040/py:unit_test_server \
-- --debug-probe-only
-- --chip RP2040 --debug-probe-only
INFO: Analyzed target @@pigweed~//targets/rp2040/py:unit_test_server (134 packages loaded, 13872 targets configured).
INFO: Found 1 target...
Target @@pigweed~//targets/rp2040/py:unit_test_server up-to-date:
Expand Down
22 changes: 16 additions & 6 deletions targets/rp2040/py/rp2040_utils/flasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
_PICOTOOL_COMMAND = 'picotool'


def flash(board_info: PicoBoardInfo, binary: Path) -> bool:
def flash(board_info: PicoBoardInfo, chip: str, binary: Path) -> bool:
"""Load `binary` onto `board_info` and wait for the device to become
available.
Returns whether or not flashing was successful."""
if isinstance(board_info, PicoDebugProbeBoardInfo):
return _load_debugprobe_binary(board_info, binary)
return _load_debugprobe_binary(board_info, chip, binary)
if not _load_picotool_binary(board_info, binary):
return False
if not _wait_for_serial_port(board_info):
Expand Down Expand Up @@ -181,7 +181,7 @@ def _wait_for_serial_port(board_info: PicoBoardInfo) -> bool:


def _load_debugprobe_binary(
board_info: PicoDebugProbeBoardInfo, binary: Path
board_info: PicoDebugProbeBoardInfo, chip: str, binary: Path
) -> bool:
"""Flash a binary to this device using a debug probe, returning success
or failure."""
Expand All @@ -203,7 +203,7 @@ def _load_debugprobe_binary(
'--probe',
probe,
'--chip',
'RP2040',
chip,
'--speed',
'10000',
str(elf_path),
Expand All @@ -229,7 +229,7 @@ def _load_debugprobe_binary(
'--probe',
probe,
'--chip',
'RP2040',
chip,
)
_LOG.debug('Resetting ==> %s', ' '.join(reset_cmd))
process = subprocess.run(
Expand Down Expand Up @@ -292,6 +292,16 @@ def create_flash_parser() -> argparse.ArgumentParser:
action='store_true',
help='Output additional logs as the script runs',
)
parser.add_argument(
'--chip',
dest='chip',
type=str,
choices=[
'RP2040',
'RP2350',
],
help='RP2 chip connected to a debug probe (RP2040 or RP2350)',
)

return parser

Expand Down Expand Up @@ -362,7 +372,7 @@ def main():
pw_cli.log.install(level=log_level)
board = device_from_args(args, interactive=True)
_LOG.info('Flashing bus %s port %s', board.bus, board.port)
flashed = flash(board, args.binary)
flashed = flash(board, args.chip, args.binary)
sys.exit(0 if flashed else 1)


Expand Down
5 changes: 3 additions & 2 deletions targets/rp2040/py/rp2040_utils/rpc_unit_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ def create_test_runner_parser() -> argparse.ArgumentParser:
def run_test_on_board(
board: PicoBoardInfo,
baud_rate: int,
chip: str,
binary: Path,
test_timeout_seconds: float,
) -> bool:
"""Run an RPC unit test on this device.
Returns whether it succeeded.
"""
if not flash(board, binary):
if not flash(board, chip, binary):
return False
serial_device = serial.Serial(board.serial_port, baud_rate, timeout=0.1)
reader = rpc.SerialReader(serial_device, 8192)
Expand Down Expand Up @@ -98,7 +99,7 @@ def main():
)
board = device_from_args(args, interactive=False)
test_passed = run_test_on_board(
board, args.baud, args.binary, args.test_timeout
board, args.baud, args.chip, args.binary, args.test_timeout
)
sys.exit(0 if test_passed else 1)

Expand Down
18 changes: 16 additions & 2 deletions targets/rp2040/py/rp2040_utils/unit_test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def parse_args():
action='store_true',
help='Output additional logs as the script runs',
)
parser.add_argument(
'--chip',
dest='chip',
type=str,
choices=[
'RP2040',
'RP2350',
],
help='RP2 chip connected to a debug probe (RP2040 or RP2350)',
)

return parser.parse_args()

Expand All @@ -99,7 +109,7 @@ def generate_runner(command: str, arguments: list[str]) -> str:


def generate_server_config(
include_picos: bool = True, include_debug_probes: bool = True
chip: str, include_picos: bool = True, include_debug_probes: bool = True
) -> IO[bytes]:
"""Returns a temporary generated file for use as the server config."""
boards = device_detector.detect_boards(
Expand Down Expand Up @@ -145,6 +155,8 @@ def generate_server_config(
str(board.bus),
'--usb-port',
str(board.port),
'--chip',
chip,
]
config_file.write(
generate_runner(_TEST_RUNNER_COMMAND, test_runner_args).encode(
Expand All @@ -158,14 +170,15 @@ def generate_server_config(
def launch_server(
server_config: IO[bytes] | None,
server_port: int | None,
chip: str,
include_picos: bool,
include_debug_probes: bool,
) -> int:
"""Launch a device test server with the provided arguments."""
if server_config is None:
# Auto-detect attached boards if no config is provided.
server_config = generate_server_config(
include_picos, include_debug_probes
chip, include_picos, include_debug_probes
)

cmd = [_TEST_SERVER_COMMAND, '-config', server_config.name]
Expand All @@ -190,6 +203,7 @@ def main():
exit_code = launch_server(
args.server_config,
args.server_port,
args.chip,
not args.debug_probe_only,
not args.pico_only,
)
Expand Down
8 changes: 4 additions & 4 deletions targets/rp2040/upstream.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ a custom hardware setup that makes parallel on-device testing easier.

.. code-block:: console
$ bazelisk run //targets/rp2040/py:unit_test_server -- --debug-probe-only
$ bazelisk run //targets/rp2040/py:unit_test_server -- --chip RP2040 --debug-probe-only
.. tab-item:: Standalone Raspberry Pi Pico
:sync: single_pico

.. code-block:: console
$ bazelisk run //targets/rp2040/py:unit_test_server
$ bazelisk run //targets/rp2040/py:unit_test_server -- --chip RP2040
.. tab-item:: PicoPico
:sync: picopico

.. code-block:: console
$ bazelisk run //targets/rp2040/py:unit_test_server
$ bazelisk run //targets/rp2040/py:unit_test_server -- --chip RP2040
#. Open another terminal and run the tests:
Expand Down Expand Up @@ -354,7 +354,7 @@ server configuration file with ``--server-config``.

.. code-block:: console
$ python -m rp2040_utils.unit_test_server
$ python -m rp2040_utils.unit_test_server --chip RP2040
.. tip::

Expand Down

0 comments on commit de832f1

Please sign in to comment.