Skip to content

Commit 052cd54

Browse files
nordic-seglnordic-piks
authored andcommitted
tests: subsys: swo: Add test that checks SWO log backend
Add test which checks correct operation of an SWO log backend. Use JLinkSWOViewerCLExe to collect the logs. Signed-off-by: Sebastian Głąb <[email protected]>
1 parent 771e0fa commit 052cd54

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@
942942
/tests/subsys/partition_manager/region/ @nordicjm @tejlmand
943943
/tests/subsys/partition_manager/static_pm_file/ @nordicjm @tejlmand
944944
/tests/subsys/pcd/ @nrfconnect/ncs-pluto
945+
/tests/subsys/swo/ @nrfconnect/ncs-low-level-test
945946
/tests/subsys/usb/negotiated_speed/ @nrfconnect/ncs-low-level-test
946947
/tests/subsys/ipc/ @nrfconnect/ncs-low-level-test
947948
/tests/tfm/ @nrfconnect/ncs-aegir @magnev

scripts/ci/tags.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,3 +1916,7 @@ ci_samples_nrf54h20:
19161916
- nrf/samples/nrf54h20/
19171917
- zephyr/modules/hal_nordic/
19181918
- zephyr/soc/nordic/nrf54h/
1919+
1920+
ci_tests_subsys_swo:
1921+
files:
1922+
- nrf/tests/subsys/swo/

tests/subsys/swo/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(log_swo)
11+
12+
FILE(GLOB app_sources src/main.c)
13+
target_sources(app PRIVATE ${app_sources})

tests/subsys/swo/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_LOG=y
2+
CONFIG_LOG_BACKEND_SWO=y
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
import logging
8+
import re
9+
import subprocess
10+
from pathlib import Path
11+
import time
12+
13+
import psutil
14+
from twister_harness import DeviceAdapter
15+
16+
logger = logging.getLogger(__name__)
17+
18+
# Kill parent process and all child processes (if started)
19+
def _kill(proc):
20+
try:
21+
for child in psutil.Process(proc.pid).children(recursive=True):
22+
child.kill()
23+
proc.kill()
24+
except Exception as e:
25+
logger.exception(f'Could not kill nrfutil - {e}')
26+
27+
28+
def test_swo_logging(dut: DeviceAdapter):
29+
"""
30+
Compile and flash test application on MCU.
31+
Tested core(s) uses SWO backend for logging.
32+
JLinkSWOViewerCLExe is used to collect logs.
33+
"""
34+
BUILD_DIR = str(dut.device_config.build_dir)
35+
PLATFORM = dut.device_config.platform
36+
SEGGER_ID = dut.device_config.id
37+
COLLECT_TIMEOUT = 10.0
38+
EXPECTED = rf"\d+: Hello from {PLATFORM}"
39+
40+
logger.debug(f"{dut.device_config=}")
41+
42+
SWO_CONFIG = {
43+
'nrf52dk/nrf52832': {
44+
'device': 'nRF52832_xxAA',
45+
'cpufreq': 64000000,
46+
'swofreq': 1000000,
47+
},
48+
'nrf52840dk/nrf52840': {
49+
'device': 'nRF52840_xxAA',
50+
'cpufreq': 64000000,
51+
'swofreq': 1000000,
52+
},
53+
'nrf5340dk/nrf5340/cpuapp': {
54+
'device': 'nRF5340_xxAA_APP',
55+
'cpufreq': 64000000,
56+
'swofreq': 1000000,
57+
},
58+
'nrf54l15dk/nrf54l05/cpuapp': {
59+
'device': 'nRF54L05_M33',
60+
'cpufreq': 128000000,
61+
'swofreq': 1000000,
62+
},
63+
'nrf54l15dk/nrf54l10/cpuapp': {
64+
'device': 'nRF54L10_M33',
65+
'cpufreq': 128000000,
66+
'swofreq': 1000000,
67+
},
68+
'nrf54l15dk/nrf54l15/cpuapp': {
69+
'device': 'nRF54L15_M33',
70+
'cpufreq': 128000000,
71+
'swofreq': 1000000,
72+
},
73+
'nrf54lm20dk/nrf54lm20a/cpuapp': {
74+
'device': 'NRF54LM20A_M33',
75+
'cpufreq': 128000000,
76+
'swofreq': 1000000,
77+
},
78+
'nrf54lv10dk/nrf54lv10a/cpuapp': {
79+
'device': 'NRF54LV10A_M33',
80+
'cpufreq': 128000000,
81+
'swofreq': 1000000,
82+
},
83+
}
84+
85+
log_filename = f"{BUILD_DIR}/log_swo.txt"
86+
try:
87+
Path(f"{log_filename}").unlink()
88+
logger.info("Old output file was deleted")
89+
except FileNotFoundError:
90+
pass
91+
92+
# Wait a bit for the core to boot
93+
time.sleep(2)
94+
95+
# use JLinkSWOViewerCLExe to collect logs
96+
cmd = f"JLinkSWOViewerCLExe -USB {SEGGER_ID}"
97+
cmd += f" -device {SWO_CONFIG[PLATFORM]['device']}"
98+
cmd += f" -cpufreq {SWO_CONFIG[PLATFORM]['cpufreq']}"
99+
cmd += f" -swofreq {SWO_CONFIG[PLATFORM]['swofreq']}"
100+
cmd += f" -itmmask 0xFFFF -outputfile {log_filename}"
101+
try:
102+
logger.info(f"Executing:\n{cmd}")
103+
proc = subprocess.Popen(
104+
cmd,
105+
stdout=subprocess.PIPE,
106+
stderr=subprocess.STDOUT,
107+
encoding='UTF-8',
108+
shell=True,
109+
)
110+
except OSError as exc:
111+
logger.error(f"Unable to start JLinkSWOViewerCLExe:\n{cmd=}\n{exc=}")
112+
113+
try:
114+
proc.wait(COLLECT_TIMEOUT)
115+
except subprocess.TimeoutExpired:
116+
pass
117+
finally:
118+
_kill(proc)
119+
120+
# read logs
121+
with open(f"{log_filename}", errors="ignore") as log_file:
122+
log_file_content = log_file.read()
123+
124+
# if nothing in log_file, stop test
125+
assert(
126+
len(log_file_content) > 0
127+
), f"File {log_filename} is empty"
128+
129+
# Check if log file contains expected string
130+
expected_str = re.search(EXPECTED, log_file_content)
131+
assert expected_str is not None, f"Failed to match {EXPECTED} in {log_filename}"

tests/subsys/swo/src/main.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
LOG_MODULE_REGISTER(log_swo, LOG_LEVEL_INF);
9+
10+
#include <zephyr/kernel.h>
11+
12+
int main(void)
13+
{
14+
int counter = 1;
15+
16+
while (true) {
17+
LOG_INF("%d: Hello from %s", counter, CONFIG_BOARD_TARGET);
18+
counter++;
19+
k_msleep(1000);
20+
}
21+
}

tests/subsys/swo/testcase.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests:
2+
swo.logging:
3+
tags: ci_tests_subsys_swo
4+
harness: pytest
5+
harness_config:
6+
pytest_dut_scope: session
7+
pytest_root:
8+
- "pytest/test_swo.py::test_swo_logging"
9+
timeout: 30
10+
platform_allow:
11+
- nrf52dk/nrf52832
12+
- nrf52840dk/nrf52840
13+
- nrf5340dk/nrf5340/cpuapp
14+
- nrf54l15dk/nrf54l05/cpuapp
15+
- nrf54l15dk/nrf54l10/cpuapp
16+
- nrf54l15dk/nrf54l15/cpuapp
17+
- nrf54lm20dk/nrf54lm20a/cpuapp
18+
- nrf54lv10dk/nrf54lv10a/cpuapp
19+
integration_platforms:
20+
- nrf5340dk/nrf5340/cpuapp

0 commit comments

Comments
 (0)