test_twai_interactive.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <inttypes.h>
  8. #include "sdkconfig.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_attr.h"
  12. #include "unity.h"
  13. #include "unity_test_utils.h"
  14. #include "driver/twai.h"
  15. #include "soc/soc_caps.h"
  16. #include "esp_log.h"
  17. #if CONFIG_TWAI_ISR_IN_IRAM
  18. static void IRAM_ATTR test_delay_post_cache_disable(void *args)
  19. {
  20. esp_rom_delay_us(1000);
  21. }
  22. #endif
  23. TEST_CASE("twai_listen_only", "[twai]")
  24. {
  25. twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();
  26. twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
  27. twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(0, 2, TWAI_MODE_LISTEN_ONLY);
  28. #if CONFIG_TWAI_ISR_IN_IRAM
  29. g_config.intr_flags |= ESP_INTR_FLAG_IRAM;
  30. #endif
  31. // listen only mode doesn't need a tx queue
  32. g_config.tx_queue_len = 0;
  33. TEST_ESP_OK(twai_driver_install(&g_config, &t_config, &f_config));
  34. TEST_ESP_OK(twai_start());
  35. #if CONFIG_TWAI_ISR_IN_IRAM
  36. printf("disable flash cache and check if we can still receive the frame\n");
  37. for (int i = 0; i < 100; i++) {
  38. unity_utils_run_cache_disable_stub(test_delay_post_cache_disable, NULL);
  39. }
  40. #endif
  41. uint8_t expected_data[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  42. twai_message_t rx_msg;
  43. TEST_ESP_OK(twai_receive(&rx_msg, portMAX_DELAY));
  44. TEST_ASSERT_EQUAL(0x123, rx_msg.identifier);
  45. ESP_LOG_BUFFER_HEX("rx", rx_msg.data, rx_msg.data_length_code);
  46. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_data, rx_msg.data, rx_msg.data_length_code);
  47. TEST_ESP_OK(twai_stop());
  48. TEST_ESP_OK(twai_driver_uninstall());
  49. }
  50. TEST_CASE("twai_remote_request", "[twai]")
  51. {
  52. twai_handle_t bus_handle;
  53. twai_timing_config_t t_config = TWAI_TIMING_CONFIG_250KBITS();
  54. twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
  55. twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(0, 2, TWAI_MODE_NORMAL);
  56. g_config.controller_id = 2;
  57. TEST_ESP_OK(twai_driver_install_v2(&g_config, &t_config, &f_config, &bus_handle));
  58. TEST_ESP_OK(twai_start_v2(bus_handle));
  59. twai_message_t req_msg = {
  60. .identifier = 0x6688,
  61. .data_length_code = 8,
  62. .rtr = true, // remote request
  63. .extd = true,// extended ID
  64. };
  65. TEST_ESP_OK(twai_transmit_v2(bus_handle, &req_msg, portMAX_DELAY));
  66. ESP_LOGI("TWAI", "send remote frame");
  67. uint8_t expected_data[8] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80};
  68. twai_message_t res_msg;
  69. TEST_ESP_OK(twai_receive_v2(bus_handle, &res_msg, portMAX_DELAY));
  70. ESP_LOGI("TWAI", "receive with id %lx\n", res_msg.identifier);
  71. TEST_ASSERT_EQUAL(0x6688, res_msg.identifier);
  72. ESP_LOG_BUFFER_HEX("rx", res_msg.data, res_msg.data_length_code);
  73. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_data, res_msg.data, res_msg.data_length_code);
  74. TEST_ESP_OK(twai_stop_v2(bus_handle));
  75. TEST_ESP_OK(twai_driver_uninstall_v2(bus_handle));
  76. }