|
| 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}" |
0 commit comments