TWAI node error 0x8

techie317
Posts: 1
Joined: Thu Oct 23, 2025 2:58 am

TWAI node error 0x8

Postby techie317 » 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?

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*)&timestamp_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));
}

LaWi14
Posts: 13
Joined: Wed Nov 20, 2019 6:05 pm

Re: TWAI node error 0x8

Postby LaWi14 » Mon Nov 03, 2025 8:04 pm

I compiled your code for my ESP32C3 and flashed it: it runs w/o any problems when the TX/RX GPIOs are connected to a CAN transceiver, e.g. SN65230.

Code: Select all

I (257) main_task: Started on CPU0
I (257) main_task: Calling app_main()
===================TWAI Sender Example Starting...===================
W (267) Can::InitializePins(): CAN pins initialized
I (277) twai_sender: TWAI Sender started successfully
I (277) twai_sender: Sending heartbeat message: 49333 [µsec]
I (1287) twai_sender: Sending heartbeat message: 1053619 [µsec]
I (2287) twai_sender: Sending heartbeat message: 2053610 [µsec]
I (3287) twai_sender: Sending heartbeat message: 3053610 [µsec]
I (4287) twai_sender: Sending heartbeat message: 4053610 [µsec]
When I disconnect the RX or TX GPIO from the transceiver I get:

Code: Select all

I (257) main_task: Started on CPU0
I (257) main_task: Calling app_main()
===================TWAI Sender Example Starting...===================
W (267) Can::InitializePins(): CAN pins initialized
I (277) twai_sender: TWAI Sender started successfully
W (277) twai_sender: TWAI node error: 0x2
W (277) twai_sender: Failed to transmit message, ID: 0x7FF
W (287) twai_sender: TWAI node error: 0x2
I (277) twai_sender: Sending heartbeat message: 49333 [µsec]
This indicates an

Code: Select all

bit_err
, whereas your 0x8 should be an

Code: Select all

stuff_err
.

BTW: where did you take your example from? In the current stable IDF there are no examples, I could just find some test code.

Who is online

Users browsing this forum: No registered users and 6 guests