diff --git a/components/driver/test_apps/twai/pytest_twai.py b/components/driver/test_apps/twai/pytest_twai.py index 688ba267e72..e0f7b2e1178 100644 --- a/components/driver/test_apps/twai/pytest_twai.py +++ b/components/driver/test_apps/twai/pytest_twai.py @@ -1,8 +1,9 @@ # SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: CC0-1.0 import logging +import os import subprocess -from time import sleep +import time import pytest from can import Bus @@ -26,19 +27,33 @@ def test_twai_self(dut: Dut) -> None: dut.run_all_single_board_cases(group='twai-loop-back') +def esp_enter_flash_mode(dut: Dut) -> None: + ser = dut.serial.proc + ser.setRTS(True) # EN Low + time.sleep(0.5) + ser.setDTR(True) # GPIO0 Low + ser.setRTS(False) # EN High + dut.expect('waiting for download', timeout=2) + ser.setDTR(False) # Back RTS/DTR to 1/1 to avoid affect to esptool + + +can_env = os.getenv('CAN_PORT', 'can0') +print(f'CAN_PORT={can_env}') + + @pytest.fixture(name='socket_can') def fixture_create_socket_can() -> Bus: # Set up the socket CAN with the bitrate - start_command = 'sudo ip link set can0 up type can bitrate 250000' - stop_command = 'sudo ip link set can0 down' + start_command = f'sudo ip link set {can_env} up type can bitrate 250000' + stop_command = f'sudo ip link set {can_env} down' subprocess.run(start_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) - bus = Bus(interface='socketcan', channel='can0', bitrate=250000) + bus = Bus(interface='socketcan', channel=f'{can_env}', bitrate=250000) yield bus # test invoked here bus.shutdown() subprocess.run(stop_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) -@pytest.mark.twai_std +@pytest.mark.twai_adapter @pytest.mark.parametrize( 'config', [ @@ -50,24 +65,27 @@ def fixture_create_socket_can() -> Bus: 'target', ['esp32', 'esp32c3', 'esp32c6', 'esp32h2', 'esp32s2', 'esp32s3', 'esp32p4'], indirect=['target'] ) def test_twai_listen_only(dut: Dut, socket_can: Bus) -> None: - dut.expect_exact('Press ENTER to see the list of tests') + try: + dut.expect_exact('Press ENTER to see the list of tests') - # TEST_CASE("twai_listen_only", "[twai]") - dut.write('"twai_listen_only"') + # TEST_CASE("twai_listen_only", "[twai]") + dut.write('"twai_listen_only"') - # wait the DUT to block at the receive API - sleep(0.03) + # wait the DUT to block at the receive API + time.sleep(0.1) - message = Message( - arbitration_id=0x123, - is_extended_id=False, - data=[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], - ) - socket_can.send(message, timeout=0.2) - dut.expect_unity_test_output() + message = Message( + arbitration_id=0x123, + is_extended_id=False, + data=[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], + ) + socket_can.send(message, timeout=0.2) + dut.expect_unity_test_output() + finally: + esp_enter_flash_mode(dut) -@pytest.mark.twai_std +@pytest.mark.twai_adapter @pytest.mark.parametrize( 'config', [ @@ -78,26 +96,32 @@ def test_twai_listen_only(dut: Dut, socket_can: Bus) -> None: @idf_parametrize( 'target', ['esp32', 'esp32c3', 'esp32c6', 'esp32h2', 'esp32s2', 'esp32s3', 'esp32p4'], indirect=['target'] ) -def test_twai_remote_request(dut: Dut, socket_can: Bus) -> None: - dut.expect_exact('Press ENTER to see the list of tests') +def test_legacy_twai_remote_request(dut: Dut, socket_can: Bus) -> None: + try: + dut.expect_exact('Press ENTER to see the list of tests') - # TEST_CASE("twai_remote_request", "[twai]") - dut.write('"twai_remote_request"') + # TEST_CASE("twai_remote_request", "[twai]") + dut.write('"twai_remote_request"') - while True: - req = socket_can.recv(timeout=0.2) - # wait for the remote request frame - if req is not None and req.is_remote_frame: - break + deadline = time.time() + 2.0 + req = None + while time.time() < deadline: + req = socket_can.recv(timeout=0.2) + if req is not None and req.is_remote_frame: + break - logging.info(f'Received message: {req}') + if req is None: + raise Exception('Remote frame not received') + logging.info(f'Received message: {req}') - reply = Message( - arbitration_id=req.arbitration_id, - is_extended_id=req.is_extended_id, - data=[0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80], - ) - socket_can.send(reply, timeout=0.2) - print('send', reply) + reply = Message( + arbitration_id=req.arbitration_id, + is_extended_id=req.is_extended_id, + data=[0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80], + ) + socket_can.send(reply, timeout=0.2) + print('send', reply) - dut.expect_unity_test_output() + dut.expect_unity_test_output() + finally: + esp_enter_flash_mode(dut) diff --git a/examples/peripherals/twai/twai_alert_and_recovery/main/Kconfig.projbuild b/examples/peripherals/twai/twai_alert_and_recovery/main/Kconfig.projbuild index 1688672cec3..6468f064425 100644 --- a/examples/peripherals/twai/twai_alert_and_recovery/main/Kconfig.projbuild +++ b/examples/peripherals/twai/twai_alert_and_recovery/main/Kconfig.projbuild @@ -5,8 +5,7 @@ menu "Example Configuration" config EXAMPLE_TX_GPIO_NUM int "TX GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 21 if IDF_TARGET_ESP32 - default 0 + default 4 help This option selects the GPIO pin used for the TX signal. Connect the TX signal to your transceiver. @@ -14,8 +13,7 @@ menu "Example Configuration" config EXAMPLE_RX_GPIO_NUM int "RX GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX - default 22 if IDF_TARGET_ESP32 - default 2 + default 5 help This option selects the GPIO pin used for the RX signal. Connect the RX signal to your transceiver. diff --git a/examples/peripherals/twai/twai_alert_and_recovery/pytest_twai_alert_recovery_example.py b/examples/peripherals/twai/twai_alert_and_recovery/pytest_twai_alert_recovery_example.py index a6c4a33ef23..35d9f7b5b8c 100644 --- a/examples/peripherals/twai/twai_alert_and_recovery/pytest_twai_alert_recovery_example.py +++ b/examples/peripherals/twai/twai_alert_and_recovery/pytest_twai_alert_recovery_example.py @@ -5,7 +5,7 @@ from pytest_embedded import Dut from pytest_embedded_idf.utils import idf_parametrize -@pytest.mark.twai_transceiver +@pytest.mark.twai_adapter @idf_parametrize('target', ['esp32'], indirect=['target']) def test_twai_alert_recovery_example(dut: Dut) -> None: dut.expect_exact('TWAI Alert and Recovery: Driver installed') diff --git a/examples/peripherals/twai/twai_self_test/main/Kconfig.projbuild b/examples/peripherals/twai/twai_self_test/main/Kconfig.projbuild index 1688672cec3..6468f064425 100644 --- a/examples/peripherals/twai/twai_self_test/main/Kconfig.projbuild +++ b/examples/peripherals/twai/twai_self_test/main/Kconfig.projbuild @@ -5,8 +5,7 @@ menu "Example Configuration" config EXAMPLE_TX_GPIO_NUM int "TX GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX - default 21 if IDF_TARGET_ESP32 - default 0 + default 4 help This option selects the GPIO pin used for the TX signal. Connect the TX signal to your transceiver. @@ -14,8 +13,7 @@ menu "Example Configuration" config EXAMPLE_RX_GPIO_NUM int "RX GPIO number" range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX - default 22 if IDF_TARGET_ESP32 - default 2 + default 5 help This option selects the GPIO pin used for the RX signal. Connect the RX signal to your transceiver. diff --git a/examples/peripherals/twai/twai_self_test/pytest_twai_self_test_example.py b/examples/peripherals/twai/twai_self_test/pytest_twai_self_test_example.py index 1b1d8ec98ca..a4c1537a5cb 100644 --- a/examples/peripherals/twai/twai_self_test/pytest_twai_self_test_example.py +++ b/examples/peripherals/twai/twai_self_test/pytest_twai_self_test_example.py @@ -5,7 +5,7 @@ from pytest_embedded import Dut from pytest_embedded_idf.utils import idf_parametrize -@pytest.mark.twai_transceiver +@pytest.mark.twai_adapter @idf_parametrize('target', ['esp32'], indirect=['target']) def test_twai_self_test_example(dut: Dut) -> None: dut.expect_exact('TWAI Self Test: Driver installed') diff --git a/tools/ci/idf_pytest/constants.py b/tools/ci/idf_pytest/constants.py index 8e1ff75e9ab..4f7d869f46b 100644 --- a/tools/ci/idf_pytest/constants.py +++ b/tools/ci/idf_pytest/constants.py @@ -70,7 +70,6 @@ ENV_MARKERS = { 'flash_multi': 'Multiple flash chips tests', 'psram': 'Chip has 4-line psram', 'ir_transceiver': 'runners with a pair of IR transmitter and receiver', - 'twai_transceiver': 'runners with a TWAI PHY transceiver', 'flash_encryption_wifi_high_traffic': 'Flash Encryption runners with wifi high traffic support', 'ethernet': 'ethernet runner', 'ethernet_flash_8m': 'ethernet runner with 8mb flash', @@ -120,7 +119,7 @@ ENV_MARKERS = { 'sdio_master_slave': 'Test sdio multi board, esp32+esp32', 'sdio_multidev_32_c6': 'Test sdio multi board, esp32+esp32c6', 'usj_device': 'Test usb_serial_jtag and usb_serial_jtag is used as serial only (not console)', - 'twai_std': 'twai runner with all twai supported targets connect to usb-can adapter', + 'twai_adapter': 'runner with multiple twai_std', 'esp32c3eco7': 'esp32c3 major version(v1.1) chips', 'esp32c2eco4': 'esp32c2 major version(v2.0) chips', }