Skip to content

Commit 59e3ca2

Browse files
committed
tests: scripts: add general power shield support
1. general power shield work with the adc_power_shield sample 2. config samples to config probe 3. add readme Signed-off-by: Hake Huang <[email protected]>
1 parent 5e7da13 commit 59e3ca2

File tree

12 files changed

+2449
-21
lines changed

12 files changed

+2449
-21
lines changed

scripts/pylib/power-twister-harness/README.rst

Lines changed: 462 additions & 0 deletions
Large diffs are not rendered by default.

scripts/pylib/power-twister-harness/conftest.py

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
11
# Copyright: (c) 2025, Intel Corporation
2+
# Copyright 2025 NXP
23
import json
34
import logging
5+
import os
6+
from collections.abc import Generator
47

58
import pytest
69
from twister_harness import DeviceAdapter
10+
from twister_harness.device.factory import DeviceFactory
11+
from twister_harness.exceptions import TwisterHarnessException
12+
from twister_harness.twister_harness_config import DeviceConfig
13+
from utils.UartSettings import UARTParser, UARTSettings
714

815

916
def pytest_addoption(parser):
1017
parser.addoption('--testdata')
1118

1219

1320
@pytest.fixture
14-
def probe_class(request, probe_path):
21+
def probe_class(request, probe_path, dft):
1522
path = probe_path # Get path of power device
23+
probe = None
1624
if request.param == 'stm_powershield':
1725
from stm32l562e_dk.PowerShield import PowerShield
1826

1927
probe = PowerShield() # Instantiate the power monitor probe
2028
probe.connect(path)
2129
probe.init()
2230

31+
if request.param == 'general_powershield':
32+
from general_adc_platform.GeneralAdcPowerMonitor import GeneralPowerShield
33+
34+
probe = GeneralPowerShield() # Instantiate the power monitor probe
35+
probe.connect(dft)
36+
probe.init()
37+
2338
yield probe
2439

25-
if request.param == 'stm_powershield':
40+
if request.param in ['stm_powershield', 'general_powershield']:
2641
probe.disconnect()
2742

2843

2944
@pytest.fixture(name='probe_path', scope='session')
3045
def fixture_probe_path(request, dut: DeviceAdapter):
46+
probe_port = None
3147
for fixture in dut.device_config.fixtures:
3248
if fixture.startswith('pm_probe'):
3349
probe_port = fixture.split(':')[1]
@@ -59,3 +75,74 @@ def fixture_test_data(request):
5975
pytest.fail(f"Missing required test data key: {key}")
6076

6177
return measurements_dict
78+
79+
80+
def determine_scope(fixture_name, config):
81+
"""Determine fixture scope based on command line options."""
82+
if dut_scope := config.getoption("--dut-scope", None):
83+
return dut_scope
84+
return 'function'
85+
86+
87+
@pytest.fixture(scope='session')
88+
def dft_object(
89+
request: pytest.FixtureRequest, probe_path: str, dut: DeviceAdapter
90+
) -> Generator[DeviceAdapter, None, None]:
91+
"""Return device object - without run application."""
92+
if probe_path:
93+
parser = UARTParser()
94+
settings: UARTSettings = parser.parse(probe_path)
95+
logging.info(f"uart settings {settings}")
96+
build_dir = dut.device_config.build_dir / "power_shield"
97+
if not os.path.exists(build_dir):
98+
os.mkdir(build_dir, 0o755)
99+
if settings:
100+
device_config: DeviceConfig = DeviceConfig(
101+
type="hardware",
102+
build_dir=build_dir,
103+
base_timeout=dut.device_config.base_timeout,
104+
flash_timeout=dut.device_config.flash_timeout,
105+
platform="genernal_adc_platform",
106+
serial=settings.port,
107+
baud=settings.baudrate,
108+
runner="",
109+
runner_params="",
110+
id="",
111+
product="General",
112+
serial_pty=None,
113+
flash_before="",
114+
west_flash_extra_args="",
115+
pre_script="",
116+
post_script="",
117+
post_flash_script="",
118+
fixtures="",
119+
extra_test_args="",
120+
)
121+
device_class: DeviceAdapter = DeviceFactory.get_device(device_config.type)
122+
device_object = device_class(device_config)
123+
try:
124+
yield device_object
125+
finally: # to make sure we close all running processes execution
126+
device_object.close()
127+
128+
129+
@pytest.fixture(scope=determine_scope)
130+
def dft(request: pytest.FixtureRequest, dft_object) -> Generator[DeviceAdapter, None, None]:
131+
"""Return launched HardwareAdapter device - with hardware device flashed and connected."""
132+
# Get twister harness config from pytest request
133+
# Initialize log files with test node name
134+
dft_object.initialize_log_files(request.node.name)
135+
try:
136+
# Launch the hardware adapter (flash and connect)
137+
try:
138+
dft_object.launch()
139+
logging.info(
140+
'DFT: HardwareAdapter launched for device %s', dft_object.device_config.serial
141+
)
142+
except TwisterHarnessException as e:
143+
logging.warning(f'TwisterFlashException ignored during launch: {e}')
144+
yield dft_object
145+
finally:
146+
# Ensure proper cleanup
147+
dft_object.close()
148+
logging.info('DFT: HardwareAdapter closed')

0 commit comments

Comments
 (0)