pytest_twai.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: CC0-1.0
  3. import logging
  4. from time import sleep
  5. import pytest
  6. from can import Bus, Message
  7. from pytest_embedded import Dut
  8. @pytest.mark.esp32
  9. @pytest.mark.esp32c3
  10. @pytest.mark.esp32c6
  11. @pytest.mark.esp32h2
  12. @pytest.mark.esp32s2
  13. @pytest.mark.esp32s3
  14. @pytest.mark.esp32p4
  15. @pytest.mark.generic
  16. @pytest.mark.parametrize(
  17. 'config',
  18. [
  19. 'release',
  20. ],
  21. indirect=True,
  22. )
  23. def test_twai_self(dut: Dut) -> None:
  24. dut.run_all_single_board_cases(group='twai-loop-back')
  25. @pytest.fixture(name='socket_can', scope='module')
  26. def fixture_create_socket_can() -> Bus:
  27. # See README.md for instructions on how to set up the socket CAN with the bitrate
  28. bus = Bus(interface='socketcan', channel='can0', bitrate=250000)
  29. yield bus
  30. bus.shutdown()
  31. @pytest.mark.esp32
  32. @pytest.mark.esp32c3
  33. @pytest.mark.esp32c6
  34. @pytest.mark.esp32h2
  35. @pytest.mark.esp32s2
  36. @pytest.mark.esp32s3
  37. @pytest.mark.esp32p4
  38. @pytest.mark.skip(reason='Runner not set up yet')
  39. @pytest.mark.parametrize(
  40. 'config',
  41. [
  42. 'iram_safe',
  43. ],
  44. indirect=True,
  45. )
  46. def test_twai_listen_only(dut: Dut, socket_can: Bus) -> None:
  47. dut.expect_exact('Press ENTER to see the list of tests')
  48. # TEST_CASE("twai_listen_only", "[twai]")
  49. dut.write('"twai_listen_only"')
  50. # wait the DUT to block at the receive API
  51. sleep(0.03)
  52. message = Message(
  53. arbitration_id=0x123,
  54. is_extended_id=False,
  55. data=[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  56. )
  57. socket_can.send(message, timeout=0.2)
  58. dut.expect_unity_test_output()
  59. @pytest.mark.esp32
  60. @pytest.mark.esp32c3
  61. @pytest.mark.esp32c6
  62. @pytest.mark.esp32h2
  63. @pytest.mark.esp32s2
  64. @pytest.mark.esp32s3
  65. @pytest.mark.esp32p4
  66. @pytest.mark.skip(reason='Runner not set up yet')
  67. @pytest.mark.parametrize(
  68. 'config',
  69. [
  70. 'release',
  71. ],
  72. indirect=True,
  73. )
  74. def test_twai_remote_request(dut: Dut, socket_can: Bus) -> None:
  75. dut.expect_exact('Press ENTER to see the list of tests')
  76. # TEST_CASE("twai_remote_request", "[twai]")
  77. dut.write('"twai_remote_request"')
  78. while True:
  79. req = socket_can.recv(timeout=0.2)
  80. # wait for the remote request frame
  81. if req is not None and req.is_remote_frame:
  82. break
  83. logging.info(f'Received message: {req}')
  84. reply = Message(
  85. arbitration_id=req.arbitration_id,
  86. is_extended_id=req.is_extended_id,
  87. data=[0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80],
  88. )
  89. socket_can.send(reply, timeout=0.2)
  90. print('send', reply)
  91. dut.expect_unity_test_output()