Skip to content

Commit 3c8804c

Browse files
nordic-seglnordic-piks
authored andcommitted
tests: subsys: rtt: Add basic test for RTT log backend
Add test to verify correct operation of the RTT log backend. Use JLinkRTTLoggerExe to collect the logs. Signed-off-by: Sebastian Głąb <[email protected]>
1 parent 3cc9875 commit 3c8804c

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-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/rtt/ @nrfconnect/ncs-low-level-test
945946
/tests/subsys/swo/ @nrfconnect/ncs-low-level-test
946947
/tests/subsys/usb/negotiated_speed/ @nrfconnect/ncs-low-level-test
947948
/tests/subsys/ipc/ @nrfconnect/ncs-low-level-test

scripts/ci/tags.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,10 @@ ci_samples_nrf54h20:
19281928
- zephyr/modules/hal_nordic/
19291929
- zephyr/soc/nordic/nrf54h/
19301930

1931+
ci_tests_subsys_rtt:
1932+
files:
1933+
- nrf/tests/subsys/rtt/
1934+
19311935
ci_tests_subsys_swo:
19321936
files:
19331937
- nrf/tests/subsys/swo/

tests/subsys/rtt/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_rtt)
11+
12+
FILE(GLOB app_sources src/main.c)
13+
target_sources(app PRIVATE ${app_sources})

tests/subsys/rtt/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_LOG=y
2+
CONFIG_USE_SEGGER_RTT=y
3+
CONFIG_LOG_BACKEND_RTT=y
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 JLinkRTTLoggerExe - {e}')
26+
27+
28+
def test_rtt_logging(dut: DeviceAdapter):
29+
"""
30+
Compile and flash test application on MCU.
31+
Tested core(s) uses RTT backend for logging.
32+
JLinkRTTLoggerExe 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"log_rtt: \d+: Hello from {PLATFORM}"
39+
40+
SWD_CONFIG = {
41+
'nrf52dk/nrf52832': {
42+
'device': 'nRF52832_xxAA',
43+
},
44+
'nrf52840dk/nrf52840': {
45+
'device': 'nRF52840_xxAA',
46+
},
47+
'nrf5340dk/nrf5340/cpuapp': {
48+
'device': 'nRF5340_xxAA_APP',
49+
},
50+
'nrf54l15dk/nrf54l05/cpuapp': {
51+
'device': 'nRF54L05_M33',
52+
},
53+
'nrf54l15dk/nrf54l10/cpuapp': {
54+
'device': 'nRF54L10_M33',
55+
},
56+
'nrf54l15dk/nrf54l15/cpuapp': {
57+
'device': 'nRF54L15_M33',
58+
},
59+
'nrf54lm20dk/nrf54lm20a/cpuapp': {
60+
'device': 'NRF54LM20A_M33',
61+
},
62+
'[email protected]/nrf54ls05b/cpuapp': {
63+
'device': 'NRF54LS05B_M33',
64+
},
65+
'nrf54lv10dk/nrf54lv10a/cpuapp': {
66+
'device': 'NRF54LV10A_M33',
67+
},
68+
'[email protected]/nrf54lv10a/cpuapp': {
69+
'device': 'NRF54LV10A_M33',
70+
},
71+
}
72+
73+
log_filename = f"{BUILD_DIR}/log_rtt.txt"
74+
try:
75+
Path(f"{log_filename}").unlink()
76+
logger.info("Old output file was deleted")
77+
except FileNotFoundError:
78+
pass
79+
80+
# Wait a bit for the core to boot
81+
time.sleep(2)
82+
83+
# use JLinkRTTLoggerExe to collect logs
84+
cmd = f"JLinkRTTLoggerExe -USB {SEGGER_ID}"
85+
cmd += f" -device {SWD_CONFIG[PLATFORM]['device']}"
86+
cmd += f" -If SWD -Speed 1000 -RTTChannel 0"
87+
cmd += f" {log_filename}"
88+
89+
try:
90+
logger.info(f"Executing:\n{cmd}")
91+
proc = subprocess.Popen(
92+
cmd.split(),
93+
stdin=subprocess.PIPE,
94+
stdout=subprocess.PIPE,
95+
stderr=subprocess.STDOUT,
96+
encoding='UTF-8',
97+
)
98+
except OSError as exc:
99+
logger.error(f"Unable to start JLinkRTTLoggerExe:\n{cmd=}\n{exc=}")
100+
101+
try:
102+
proc.wait(COLLECT_TIMEOUT)
103+
except subprocess.TimeoutExpired:
104+
pass
105+
finally:
106+
_kill(proc)
107+
outs, errs = proc.communicate()
108+
logger.info(f"{outs=}\n{errs=}")
109+
110+
# read logs
111+
with open(f"{log_filename}", errors="ignore") as log_file:
112+
log_file_content = log_file.read()
113+
114+
# if nothing in log_file, stop test
115+
assert(
116+
len(log_file_content) > 0
117+
), f"File {log_filename} is empty"
118+
119+
# Check if log file contains expected string
120+
expected_str = re.search(EXPECTED, log_file_content)
121+
assert expected_str is not None, f"Failed to match {EXPECTED} in {log_filename}"

tests/subsys/rtt/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_rtt, 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/rtt/testcase.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests:
2+
rtt.logging:
3+
tags: ci_tests_subsys_rtt
4+
harness: pytest
5+
harness_config:
6+
pytest_dut_scope: session
7+
pytest_root:
8+
- "pytest/test_rtt.py::test_rtt_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+
- nrf54ls05dk/nrf54ls05b/cpuapp
19+
- [email protected]/nrf54ls05b/cpuapp
20+
- nrf54lv10dk/nrf54lv10a/cpuapp
21+
integration_platforms:
22+
- nrf52840dk/nrf52840
23+
- nrf5340dk/nrf5340/cpuapp
24+
- nrf54l15dk/nrf54l15/cpuapp
25+
- nrf54lm20dk/nrf54lm20a/cpuapp

0 commit comments

Comments
 (0)