|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include "freertos/FreeRTOS.h" |
| 9 | +#include "freertos/task.h" |
| 10 | +#include "esp_log.h" |
| 11 | +#include "esp_check.h" |
| 12 | +#include "esp_twai.h" |
| 13 | +#include "esp_twai_onchip.h" |
| 14 | +#include "esp_isotp.h" |
| 15 | + |
| 16 | +static const char *TAG = "isotp_echo"; |
| 17 | + |
| 18 | +// Global variables for cleanup |
| 19 | +static esp_isotp_handle_t g_isotp_handle = NULL; |
| 20 | +static twai_node_handle_t g_twai_node = NULL; |
| 21 | +static esp_err_t isotp_echo_init(void); |
| 22 | +static esp_err_t isotp_echo_deinit(void); |
| 23 | + |
| 24 | +static void on_tx_done(esp_isotp_handle_t handle, uint32_t tx_size, void *user_arg) |
| 25 | +{ |
| 26 | + ESP_EARLY_LOGI(TAG, "TX complete: %lu bytes", (unsigned long)tx_size); |
| 27 | +} |
| 28 | + |
| 29 | +static void on_rx_done(esp_isotp_handle_t handle, const uint8_t *data, uint32_t size, void *user_arg) |
| 30 | +{ |
| 31 | + ESP_EARLY_LOGI(TAG, "RX complete: %lu bytes, echoing back...", (unsigned long)size); |
| 32 | + |
| 33 | + // Check handle validity using ESP-IDF standard macro for ISR context |
| 34 | + ESP_RETURN_VOID_ON_FALSE_ISR(handle, TAG, "Echo send failed: invalid handle"); |
| 35 | + |
| 36 | + // Echo back the received data immediately (ISR-safe API) |
| 37 | + esp_err_t err = esp_isotp_send(handle, data, size); |
| 38 | + if (unlikely(err != ESP_OK && err != ESP_ERR_NOT_FINISHED)) { |
| 39 | + ESP_EARLY_LOGE(TAG, "Echo send failed: %s", esp_err_to_name(err)); |
| 40 | + } |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +void app_main(void) |
| 45 | +{ |
| 46 | + ESP_LOGI(TAG, "ISO-TP Echo Demo started"); |
| 47 | + |
| 48 | + // Initialize the ISO-TP echo example |
| 49 | + ESP_ERROR_CHECK(isotp_echo_init()); |
| 50 | + |
| 51 | + // Main task will just sleep, the echo_task handles the ISO-TP communication |
| 52 | + while (1) { |
| 53 | + vTaskDelay(pdMS_TO_TICKS(10000)); |
| 54 | + } |
| 55 | + |
| 56 | + // Deinitialize the ISO-TP echo example |
| 57 | + isotp_echo_deinit(); |
| 58 | +} |
| 59 | + |
| 60 | +static void echo_task(void *arg) |
| 61 | +{ |
| 62 | + esp_isotp_handle_t isotp_handle = (esp_isotp_handle_t)arg; |
| 63 | + |
| 64 | + ESP_LOGI(TAG, "ISO-TP Echo task started"); |
| 65 | + |
| 66 | + while (1) { |
| 67 | + // Poll ISO-TP protocol state machine (timeouts, consecutive frames, etc.) |
| 68 | + ESP_ERROR_CHECK(esp_isotp_poll(isotp_handle)); |
| 69 | + |
| 70 | + // Small delay to ensure accurate STmin timing and prevent 100% CPU usage |
| 71 | + vTaskDelay(pdMS_TO_TICKS(CONFIG_EXAMPLE_ECHO_POLL_DELAY_MS)); |
| 72 | + } |
| 73 | + |
| 74 | + ESP_LOGI(TAG, "ISO-TP Echo task finished"); |
| 75 | + vTaskDelete(NULL); |
| 76 | +} |
| 77 | + |
| 78 | +static esp_err_t isotp_echo_init(void) |
| 79 | +{ |
| 80 | + twai_onchip_node_config_t twai_cfg = { |
| 81 | + .io_cfg = { |
| 82 | + .tx = CONFIG_EXAMPLE_TX_GPIO_NUM, |
| 83 | + .rx = CONFIG_EXAMPLE_RX_GPIO_NUM, |
| 84 | + }, |
| 85 | + .bit_timing.bitrate = CONFIG_EXAMPLE_BITRATE, |
| 86 | + .tx_queue_depth = CONFIG_EXAMPLE_TWAI_TX_QUEUE_DEPTH, |
| 87 | + .intr_priority = 0, |
| 88 | + }; |
| 89 | + |
| 90 | + ESP_ERROR_CHECK(twai_new_node_onchip(&twai_cfg, &g_twai_node)); |
| 91 | + |
| 92 | + esp_isotp_config_t isotp_cfg = { |
| 93 | + .tx_id = CONFIG_EXAMPLE_ISOTP_TX_ID, |
| 94 | + .rx_id = CONFIG_EXAMPLE_ISOTP_RX_ID, |
| 95 | + .tx_buffer_size = CONFIG_EXAMPLE_ISOTP_TX_BUFFER_SIZE, |
| 96 | + .rx_buffer_size = CONFIG_EXAMPLE_ISOTP_RX_BUFFER_SIZE, |
| 97 | + .tx_frame_pool_size = CONFIG_EXAMPLE_ISOTP_TX_FRAME_POOL_SIZE, |
| 98 | + .rx_callback = on_rx_done, |
| 99 | + .tx_callback = on_tx_done, |
| 100 | + .callback_arg = NULL, |
| 101 | + }; |
| 102 | + |
| 103 | + ESP_ERROR_CHECK(esp_isotp_new_transport(g_twai_node, &isotp_cfg, &g_isotp_handle)); |
| 104 | + |
| 105 | + // Create echo task |
| 106 | + BaseType_t task_ret = xTaskCreate(echo_task, "isotp_echo", CONFIG_EXAMPLE_ECHO_TASK_STACK_SIZE, |
| 107 | + g_isotp_handle, CONFIG_EXAMPLE_ECHO_TASK_PRIORITY, NULL); |
| 108 | + ESP_RETURN_ON_FALSE(task_ret == pdPASS, ESP_FAIL, TAG, "Failed to create echo task"); |
| 109 | + |
| 110 | + ESP_LOGI(TAG, "ISO-TP echo example's TX ID: 0x%X, RX ID: 0x%X", |
| 111 | + CONFIG_EXAMPLE_ISOTP_TX_ID, CONFIG_EXAMPLE_ISOTP_RX_ID); |
| 112 | + |
| 113 | + return ESP_OK; |
| 114 | +} |
| 115 | + |
| 116 | +static esp_err_t isotp_echo_deinit(void) |
| 117 | +{ |
| 118 | + ESP_RETURN_ON_FALSE(g_isotp_handle != NULL, ESP_OK, TAG, "ISO-TP echo example is not initialized"); |
| 119 | + |
| 120 | + esp_isotp_delete(g_isotp_handle); |
| 121 | + g_isotp_handle = NULL; |
| 122 | + |
| 123 | + if (g_twai_node) { |
| 124 | + twai_node_delete(g_twai_node); |
| 125 | + g_twai_node = NULL; |
| 126 | + } |
| 127 | + |
| 128 | + ESP_LOGI(TAG, "ISO-TP echo example deinitialized"); |
| 129 | + return ESP_OK; |
| 130 | +} |
0 commit comments