TWAI node error 0x8
Posted: Thu Oct 23, 2025 5:59 am
I created the following code.
The node returns a 0x8 error and fails to send.
I created it based on the official example, but I can't figure out what's causing the problem.
Is anyone else experiencing the same situation?
The node returns a 0x8 error and fails to send.
I created it based on the official example, but I can't figure out what's causing the problem.
Is anyone else experiencing the same situation?
Code: Select all
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_twai.h"
#include "esp_twai_onchip.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#define TWAI_SENDER_TX_GPIO GPIO_NUM_43
#define TWAI_SENDER_RX_GPIO GPIO_NUM_44
#define TWAI_SENDER_ENABLE_GPIO GPIO_NUM_45
#define TWAI_QUEUE_DEPTH 10
#define TWAI_BITRATE 500000 // 500 kbps
// Message IDs
#define TWAI_DATA_ID 0x100
#define TWAI_HEARTBEAT_ID 0x7FF
#define TWAI_DATA_LEN 1000
static const char* TAG = "twai_sender";
typedef struct {
twai_frame_t frame;
uint8_t data[TWAI_FRAME_MAX_LEN];
} twai_sender_data_t;
// Transmission completion callback
static IRAM_ATTR bool twai_sender_tx_done_callback(
twai_node_handle_t handle, const twai_tx_done_event_data_t* edata,
void* user_ctx) {
if (!edata->is_tx_success) {
ESP_EARLY_LOGW(TAG, "Failed to transmit message, ID: 0x%X",
edata->done_tx_frame->header.id);
}
return false; // No task wake required
}
// Bus error callback
static IRAM_ATTR bool twai_sender_on_error_callback(
twai_node_handle_t handle, const twai_error_event_data_t* edata,
void* user_ctx) {
ESP_EARLY_LOGW(TAG, "TWAI node error: 0x%x", edata->err_flags.val);
return false; // No task wake required
}
void app_main(void) {
twai_node_handle_t sender_node = NULL;
printf(
"===================TWAI Sender Example "
"Starting...===================\n");
if (gpio_set_direction(TWAI_SENDER_ENABLE_GPIO, GPIO_MODE_OUTPUT) != ESP_OK) {
ESP_LOGE("Can::InitializePins()", "Enable pin");
}
if (gpio_set_level(TWAI_SENDER_ENABLE_GPIO, 1) != ESP_OK) {
ESP_LOGE("Can::InitializePins()", "Enable pin");
};
ESP_LOGW("Can::InitializePins()", "CAN pins initialized");
// Configure TWAI node
twai_onchip_node_config_t node_config = {
.io_cfg =
{
.tx = TWAI_SENDER_TX_GPIO,
.rx = TWAI_SENDER_RX_GPIO,
.quanta_clk_out = GPIO_NUM_NC,
.bus_off_indicator = GPIO_NUM_NC,
},
.bit_timing =
{
.bitrate = TWAI_BITRATE,
},
.fail_retry_cnt = 3,
.tx_queue_depth = TWAI_QUEUE_DEPTH,
.flags.enable_self_test = 1,
};
// Create TWAI node
ESP_ERROR_CHECK(twai_new_node_onchip(&node_config, &sender_node));
// Register transmission completion callback
twai_event_callbacks_t callbacks = {
.on_tx_done = twai_sender_tx_done_callback,
.on_error = twai_sender_on_error_callback,
};
ESP_ERROR_CHECK(
twai_node_register_event_callbacks(sender_node, &callbacks, NULL));
// Enable TWAI node
ESP_ERROR_CHECK(twai_node_enable(sender_node));
ESP_LOGI(TAG, "TWAI Sender started successfully");
while (1) {
// Send heartbeat message
uint64_t timestamp_usec = esp_timer_get_time();
twai_frame_t tx_frame = {
.header.id = TWAI_HEARTBEAT_ID,
.buffer = (uint8_t*)×tamp_usec,
.buffer_len = sizeof(timestamp_usec),
};
ESP_ERROR_CHECK(twai_node_transmit(sender_node, &tx_frame, 500));
ESP_LOGI(TAG, "Sending heartbeat message: %lld [µsec]", timestamp_usec);
vTaskDelay(pdMS_TO_TICKS(1000));
twai_node_status_t status;
twai_node_get_info(sender_node, &status, NULL);
if (status.state == TWAI_ERROR_BUS_OFF) {
ESP_LOGW(TAG, "Bus-off detected");
return;
}
}
ESP_ERROR_CHECK(twai_node_disable(sender_node));
ESP_ERROR_CHECK(twai_node_delete(sender_node));
}