-
Notifications
You must be signed in to change notification settings - Fork 1.4k
tests: subsys: swo: Add test that checks SWO log backend #24355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nordic-piks
merged 1 commit into
nrfconnect:main
from
nordic-segl:NRFX-8296_Add-test-for-SWO-logging
Sep 10, 2025
+192
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(log_swo) | ||
|
||
FILE(GLOB app_sources src/main.c) | ||
target_sources(app PRIVATE ${app_sources}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CONFIG_LOG=y | ||
CONFIG_LOG_BACKEND_SWO=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
|
||
import logging | ||
import re | ||
import subprocess | ||
from pathlib import Path | ||
import time | ||
|
||
import psutil | ||
from twister_harness import DeviceAdapter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# Kill parent process and all child processes (if started) | ||
def _kill(proc): | ||
try: | ||
for child in psutil.Process(proc.pid).children(recursive=True): | ||
child.kill() | ||
proc.kill() | ||
except Exception as e: | ||
logger.exception(f'Could not kill nrfutil - {e}') | ||
|
||
|
||
def test_swo_logging(dut: DeviceAdapter): | ||
""" | ||
Compile and flash test application on MCU. | ||
Tested core(s) uses SWO backend for logging. | ||
JLinkSWOViewerCLExe is used to collect logs. | ||
""" | ||
BUILD_DIR = str(dut.device_config.build_dir) | ||
PLATFORM = dut.device_config.platform | ||
SEGGER_ID = dut.device_config.id | ||
COLLECT_TIMEOUT = 10.0 | ||
EXPECTED = rf"\d+: Hello from {PLATFORM}" | ||
|
||
logger.debug(f"{dut.device_config=}") | ||
|
||
SWO_CONFIG = { | ||
'nrf52dk/nrf52832': { | ||
'device': 'nRF52832_xxAA', | ||
'cpufreq': 64000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf52840dk/nrf52840': { | ||
'device': 'nRF52840_xxAA', | ||
'cpufreq': 64000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf5340dk/nrf5340/cpuapp': { | ||
'device': 'nRF5340_xxAA_APP', | ||
'cpufreq': 64000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf54l15dk/nrf54l05/cpuapp': { | ||
'device': 'nRF54L05_M33', | ||
'cpufreq': 128000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf54l15dk/nrf54l10/cpuapp': { | ||
'device': 'nRF54L10_M33', | ||
'cpufreq': 128000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf54l15dk/nrf54l15/cpuapp': { | ||
'device': 'nRF54L15_M33', | ||
'cpufreq': 128000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf54lm20dk/nrf54lm20a/cpuapp': { | ||
'device': 'NRF54LM20A_M33', | ||
'cpufreq': 128000000, | ||
'swofreq': 1000000, | ||
}, | ||
'nrf54lv10dk/nrf54lv10a/cpuapp': { | ||
'device': 'NRF54LV10A_M33', | ||
'cpufreq': 128000000, | ||
'swofreq': 1000000, | ||
}, | ||
} | ||
|
||
log_filename = f"{BUILD_DIR}/log_swo.txt" | ||
try: | ||
Path(f"{log_filename}").unlink() | ||
logger.info("Old output file was deleted") | ||
except FileNotFoundError: | ||
pass | ||
|
||
# Wait a bit for the core to boot | ||
time.sleep(2) | ||
|
||
# use JLinkSWOViewerCLExe to collect logs | ||
cmd = f"JLinkSWOViewerCLExe -USB {SEGGER_ID}" | ||
cmd += f" -device {SWO_CONFIG[PLATFORM]['device']}" | ||
cmd += f" -cpufreq {SWO_CONFIG[PLATFORM]['cpufreq']}" | ||
cmd += f" -swofreq {SWO_CONFIG[PLATFORM]['swofreq']}" | ||
cmd += f" -itmmask 0xFFFF -outputfile {log_filename}" | ||
try: | ||
logger.info(f"Executing:\n{cmd}") | ||
proc = subprocess.Popen( | ||
cmd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
encoding='UTF-8', | ||
shell=True, | ||
) | ||
except OSError as exc: | ||
logger.error(f"Unable to start JLinkSWOViewerCLExe:\n{cmd=}\n{exc=}") | ||
|
||
try: | ||
proc.wait(COLLECT_TIMEOUT) | ||
except subprocess.TimeoutExpired: | ||
pass | ||
finally: | ||
_kill(proc) | ||
|
||
# read logs | ||
with open(f"{log_filename}", errors="ignore") as log_file: | ||
log_file_content = log_file.read() | ||
|
||
# if nothing in log_file, stop test | ||
assert( | ||
len(log_file_content) > 0 | ||
), f"File {log_filename} is empty" | ||
|
||
# Check if log file contains expected string | ||
expected_str = re.search(EXPECTED, log_file_content) | ||
assert expected_str is not None, f"Failed to match {EXPECTED} in {log_filename}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2025 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
*/ | ||
|
||
#include <zephyr/logging/log.h> | ||
LOG_MODULE_REGISTER(log_swo, LOG_LEVEL_INF); | ||
|
||
#include <zephyr/kernel.h> | ||
|
||
int main(void) | ||
{ | ||
int counter = 1; | ||
|
||
while (true) { | ||
LOG_INF("%d: Hello from %s", counter, CONFIG_BOARD_TARGET); | ||
counter++; | ||
k_msleep(1000); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
tests: | ||
swo.logging: | ||
tags: ci_tests_subsys_swo | ||
harness: pytest | ||
harness_config: | ||
pytest_dut_scope: session | ||
pytest_root: | ||
- "pytest/test_swo.py::test_swo_logging" | ||
timeout: 30 | ||
platform_allow: | ||
- nrf52dk/nrf52832 | ||
- nrf52840dk/nrf52840 | ||
- nrf5340dk/nrf5340/cpuapp | ||
- nrf54l15dk/nrf54l05/cpuapp | ||
- nrf54l15dk/nrf54l10/cpuapp | ||
- nrf54l15dk/nrf54l15/cpuapp | ||
- nrf54lm20dk/nrf54lm20a/cpuapp | ||
- nrf54lv10dk/nrf54lv10a/cpuapp | ||
integration_platforms: | ||
- nrf5340dk/nrf5340/cpuapp |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.