|
1 | 1 | # Copyright: (c) 2025, Intel Corporation
|
| 2 | +# Copyright 2025 NXP |
2 | 3 | import json
|
3 | 4 | import logging
|
| 5 | +import os |
| 6 | +from collections.abc import Generator |
4 | 7 |
|
5 | 8 | import pytest
|
6 | 9 | 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 |
7 | 14 |
|
8 | 15 |
|
9 | 16 | def pytest_addoption(parser):
|
10 | 17 | parser.addoption('--testdata')
|
11 | 18 |
|
12 | 19 |
|
13 | 20 | @pytest.fixture
|
14 |
| -def probe_class(request, probe_path): |
| 21 | +def probe_class(request, probe_path, dft): |
15 | 22 | path = probe_path # Get path of power device
|
| 23 | + probe = None |
16 | 24 | if request.param == 'stm_powershield':
|
17 | 25 | from stm32l562e_dk.PowerShield import PowerShield
|
18 | 26 |
|
19 | 27 | probe = PowerShield() # Instantiate the power monitor probe
|
20 | 28 | probe.connect(path)
|
21 | 29 | probe.init()
|
22 | 30 |
|
| 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 | + |
23 | 38 | yield probe
|
24 | 39 |
|
25 |
| - if request.param == 'stm_powershield': |
| 40 | + if request.param in ['stm_powershield', 'general_powershield']: |
26 | 41 | probe.disconnect()
|
27 | 42 |
|
28 | 43 |
|
29 | 44 | @pytest.fixture(name='probe_path', scope='session')
|
30 | 45 | def fixture_probe_path(request, dut: DeviceAdapter):
|
| 46 | + probe_port = None |
31 | 47 | for fixture in dut.device_config.fixtures:
|
32 | 48 | if fixture.startswith('pm_probe'):
|
33 | 49 | probe_port = fixture.split(':')[1]
|
@@ -59,3 +75,74 @@ def fixture_test_data(request):
|
59 | 75 | pytest.fail(f"Missing required test data key: {key}")
|
60 | 76 |
|
61 | 77 | 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