#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/twai.h"

#define TX_GPIO_NUM 26 //CONFIG_EXAMPLE_TX_GPIO_NUM
#define RX_GPIO_NUM 27 //CONFIG_EXAMPLE_RX_GPIO_NUM
#define MSG_ID      0x123
#define EXAMPLE_TAG "TWAI_test"

// Настройки
static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NO_ACK);

void print_alerts(uint32_t alerts)
{
    if (alerts & TWAI_ALERT_TX_SUCCESS)         ESP_LOGI(EXAMPLE_TAG, "ALERT: TX Success");
    if (alerts & TWAI_ALERT_RX_DATA)            ESP_LOGI(EXAMPLE_TAG, "ALERT: RX Data Available");
    if (alerts & TWAI_ALERT_ERR_PASS)           ESP_LOGW(EXAMPLE_TAG, "ALERT: Error Passive");
    if (alerts & TWAI_ALERT_BUS_OFF)            ESP_LOGE(EXAMPLE_TAG, "ALERT: Bus Off");
    if (alerts & TWAI_ALERT_RECOVERY_IN_PROGRESS) ESP_LOGW(EXAMPLE_TAG, "ALERT: Recovery In Progress");
    if (alerts & TWAI_ALERT_BUS_RECOVERED)      ESP_LOGI(EXAMPLE_TAG, "ALERT: Bus Recovered");
    if (alerts & TWAI_ALERT_TX_FAILED)          ESP_LOGE(EXAMPLE_TAG, "ALERT: TX Failed");
    if (alerts & TWAI_ALERT_ARB_LOST)           ESP_LOGW(EXAMPLE_TAG, "ALERT: Arbitration Lost");
    if (alerts & TWAI_ALERT_RX_QUEUE_FULL)      ESP_LOGW(EXAMPLE_TAG, "ALERT: RX Queue Full");
}

void app_main(void)
{
    twai_handle_t twai_handle;

    // Установка драйвера с alert-ами
    esp_err_t ret = twai_driver_install_v2(&g_config, &t_config, &f_config, &twai_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(EXAMPLE_TAG, "Driver install failed: %s", esp_err_to_name(ret));
        return;
    }
    ESP_LOGI(EXAMPLE_TAG, "TWAI driver installed");

    // Настройка alert-ов: следим за основными событиями
    uint32_t alert_flags = TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_RX_DATA | TWAI_ALERT_TX_FAILED |
                           TWAI_ALERT_ARB_LOST | TWAI_ALERT_BUS_OFF | TWAI_ALERT_ERR_PASS |
                           TWAI_ALERT_BUS_RECOVERED | TWAI_ALERT_RECOVERY_IN_PROGRESS |
                           TWAI_ALERT_RX_QUEUE_FULL;
    ESP_ERROR_CHECK(twai_reconfigure_alerts_v2(twai_handle, alert_flags, NULL));

    // Запуск драйвера
    ret = twai_start_v2(twai_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(EXAMPLE_TAG, "Driver start failed: %s", esp_err_to_name(ret));
        return;
    }
    ESP_LOGI(EXAMPLE_TAG, "TWAI driver started");

    // Сообщение loopback
    twai_message_t tx_msg = {
        .identifier = MSG_ID,
        .data_length_code = 1,
        .data = {0},
        .extd = 0,
        .rtr = 0,
        .ss = 0
    };

    uint8_t counter = 0;
    while (1) {
        // Отправка
        tx_msg.data[0] = counter++;
        ret = twai_transmit_v2(twai_handle, &tx_msg, pdMS_TO_TICKS(1000));
        if (ret != ESP_OK) {
            ESP_LOGE(EXAMPLE_TAG, "Transmit failed: %s", esp_err_to_name(ret));
        } else {
            ESP_LOGI(EXAMPLE_TAG, "Message sent. Data: %d", tx_msg.data[0]);
        }

        // Приём (loopback)
        twai_message_t rx_msg;
        ret = twai_receive_v2(twai_handle, &rx_msg, pdMS_TO_TICKS(1000));
        if (ret == ESP_OK) {
            ESP_LOGI(EXAMPLE_TAG, "Message received. ID: 0x%03lx, Data: %d", rx_msg.identifier, rx_msg.data[0]);
        } else {
            ESP_LOGW(EXAMPLE_TAG, "No message received: %s", esp_err_to_name(ret));
        }

        // Чтение alert-ов
        uint32_t alerts;
        
        ret = twai_read_alerts_v2(twai_handle, &alerts, pdMS_TO_TICKS(500));
        if (ret == ESP_OK && alerts != 0) {
            ESP_LOGI(EXAMPLE_TAG, "!!!!No alerts or read failed: %s", esp_err_to_name(ret));
            print_alerts(alerts);
        }  


        twai_status_info_t status;
        ret = twai_get_status_info_v2(twai_handle, &status);
        if (ret == ESP_OK) {
            //prit state
            ESP_LOGI(EXAMPLE_TAG, "TWAI State: %s",
                (status.state == TWAI_STATE_RUNNING) ? "Running" :
                (status.state == TWAI_STATE_STOPPED) ? "Stopped" :
                (status.state == TWAI_STATE_BUS_OFF) ? "Bus-Off" :
                (status.state == TWAI_STATE_RECOVERING) ? "Recovery" : "Unknown");
            ESP_LOGI(EXAMPLE_TAG, "Messages to TX: %"PRIu32"", status.msgs_to_tx);
            ESP_LOGI(EXAMPLE_TAG, "TX Error Counter: %"PRIu32"", status.tx_error_counter);
            ESP_LOGI(EXAMPLE_TAG, "Messages to RX: %"PRIu32"", status.msgs_to_rx);
            ESP_LOGI(EXAMPLE_TAG, "RX Error Counter: %"PRIu32"", status.rx_error_counter);
            ESP_LOGI(EXAMPLE_TAG, "TX Failed Count: %"PRIu32"", status.tx_failed_count);
            ESP_LOGI(EXAMPLE_TAG, "RX Missed Count: %"PRIu32"", status.rx_missed_count);
            ESP_LOGI(EXAMPLE_TAG, "RX Overrun Count: %"PRIu32"", status.rx_overrun_count);
            ESP_LOGI(EXAMPLE_TAG, "Arbitration Lost Count: %"PRIu32"", status.arb_lost_count);
            ESP_LOGI(EXAMPLE_TAG, "Bus Error Count: %"PRIu32"", status.bus_error_count);

            
        } else {
            ESP_LOGE(EXAMPLE_TAG, "Failed to get status: %s", esp_err_to_name(ret));
        }

        


        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

что не так


E (62808) TWAI_test: Transmit failed: ESP_ERR_INVALID_STATE
W (63808) TWAI_test: No message received: ESP_ERR_TIMEOUT
I (64308) TWAI_test: TWAI State: Bus-Off
I (64308) TWAI_test: Messages to TX: 0
I (64308) TWAI_test: TX Error Counter: 128
I (64308) TWAI_test: Messages to RX: 0
I (64308) TWAI_test: RX Error Counter: 0
I (64308) TWAI_test: TX Failed Count: 1
I (64318) TWAI_test: RX Missed Count: 0
I (64318) TWAI_test: RX Overrun Count: 0
I (64318) TWAI_test: Arbitration Lost Count: 0
I (64328) TWAI_test: Bus Error Count: 30