Page 1 of 1

Help with twai driver and esp32

Posted: Tue Dec 02, 2025 11:01 am
by Acca84
Hello,
I'm trying to use esp32 and CJMCU-230 can transceiver to read data from a Motec car ECU
the ECU is connected to Motec USB to CAN module and to CJMCU-230 through RX and TX lines.

Everything works ok with the "TWAI receive" sketch example :

Code: Select all

/* ESP32 TWAI receive example.
  Receive messages and sends them over serial.

  Connect a CAN bus transceiver to the RX/TX pins.
  For example: SN65HVD230

  TWAI_MODE_LISTEN_ONLY is used so that the TWAI controller will not influence the bus.

  The API gives other possible speeds and alerts:
  https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/twai.html

  Example output from a can bus message:
  -> Message received
  -> Message is in Standard Format
  -> ID: 604
  -> Byte: 0 = 00, 1 = 0f, 2 = 13, 3 = 02, 4 = 00, 5 = 00, 6 = 08, 7 = 00

  Example output with alerts:
  -> Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
  -> Bus error count: 171
  -> Alert: The RX queue is full causing a received frame to be lost.
  -> RX buffered: 4  RX missed: 46 RX overrun 0

  created 05-11-2022 by Stephan Martin (designer2k2)
*/

#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "driver/twai.h"

// Pins used to connect to CAN bus transceiver:
#define RX_PIN 6
#define TX_PIN 7

// Interval:
#define POLLING_RATE_MS 1000

static bool driver_installed = false;

void setup() {
  // Start Serial:
  Serial.begin(500000);
  //Serial.setTxTimeoutMs(0);   

  // Initialize configuration structures using macro initializers
  twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
  twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS();  //Look in the api-reference for other speed sets.
  twai_filter_config_t f_config = { .acceptance_code = (0xE8 << 21),
                                      .acceptance_mask = ~(0x7FF << 21),
                                      .single_filter = true };
  //twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

  // Install TWAI driver
  if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
    Serial.println("Driver installed");
  } else {
    Serial.println("Failed to install driver");
    return;
  }

  // Start TWAI driver
  if (twai_start() == ESP_OK) {
    Serial.println("Driver started");
  } else {
    Serial.println("Failed to start driver");
    return;
  }

  // Reconfigure alerts to detect frame receive, Bus-Off error and RX queue full states
  uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL;
  if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
    Serial.println("CAN Alerts reconfigured");
  } else {
    Serial.println("Failed to reconfigure alerts");
    return;
  }

  // TWAI driver is now successfully installed and started
  driver_installed = true;
}

static void handle_rx_message(twai_message_t &message) {
  // Process received message
  if (message.extd) {
    Serial.println("Message is in Extended Format");
  } else {
    Serial.println("Message is in Standard Format");
  }
  Serial.printf("ID: %lx\nByte:", message.identifier);
  if (!(message.rtr)) {
    for (int i = 0; i < message.data_length_code; i++) {
      Serial.printf(" %d = %02x,", i, message.data[i]);
    }
    Serial.println("");
  }
}

void loop() {
  if (!driver_installed) {
    // Driver not installed
    Serial.println("Failed to install driver");
    delay(1000);
    return;
  }
  // Check if alert happened
  uint32_t alerts_triggered;
  twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS));
  twai_status_info_t twaistatus;
  twai_get_status_info(&twaistatus);

  // Handle alerts
  if (alerts_triggered & TWAI_ALERT_ERR_PASS) {
    Serial.println("Alert: TWAI controller has become error passive.");
  }
  if (alerts_triggered & TWAI_ALERT_BUS_ERROR) {
    Serial.println("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.");
    Serial.printf("Bus error count: %lu\n", twaistatus.bus_error_count);
  }
  if (alerts_triggered & TWAI_ALERT_RX_QUEUE_FULL) {
    Serial.println("Alert: The RX queue is full causing a received frame to be lost.");
    Serial.printf("RX buffered: %lu\t", twaistatus.msgs_to_rx);
    Serial.printf("RX missed: %lu\t", twaistatus.rx_missed_count);
    Serial.printf("RX overrun %lu\n", twaistatus.rx_overrun_count);




 
  }

  // Check if message is received
  if (alerts_triggered & TWAI_ALERT_RX_DATA) {
    // One or more messages received. Handle all.
    twai_message_t message;
    while (twai_receive(&message, 0) == ESP_OK) {
      handle_rx_message(message);
    }
  }
}
But sometimes, i have CAN bus errors (coming from Spark coil EMI, whereas my setup is in a metal box connected to ESP32 ground)

When those errors occurs, i have the following message in the Serial console, then nothing more :

Code: Select all

....
Message is in Standard Format
ID: e8
Byte: 0 = 00, 1 = 02, 2 = 00, 3 = 00, 4 = 00, 5 = 00, 6 = 00, 7 = 00,
Alert: The RX queue is full causing a received frame to be lost.
RX buffered: 0	RX missed: 594651	RX overrun 0
For recovering CAN BUS reading, i have to POWER OFF then ON my Motec ECU.
The weird thing is that the CANBUS to USB module continues to receive data without the need of this restart.

How can i recover my program without restarting ECU please ?

Sorry for my poor English
Thanks

Re: Help with twai driver and esp32

Posted: Wed Dec 03, 2025 7:15 am
by lichurbagan
ESP32 enters Bus-Off after EMI spikes. MoTeC USB-CAN keeps working. ESP32 TWAI never auto-recovers. You must reset the driver.
Enable the Bus-Off alert. Then stop, uninstall, and reinstall TWAI.

Key code steps:

1. Add `TWAI_ALERT_BUS_OFF`.
2. On Bus-Off, call a recover function.
3. Recovery: `twai_stop()`, `twai_driver_uninstall()`, reinstall configs, `twai_start()`.

This restores CAN without ECU reboot.
Reduce EMI too: Use twisted pair. Ensure 120Ω ends. Add a common-mode choke. Consider an isolated CAN transceiver.

Re: Help with twai driver and esp32

Posted: Wed Dec 03, 2025 1:04 pm
by Acca84
Ok, thanks, i'll try this
just a question : point 2/ is just a call to twai_initiate_recovery() function ?