Esp32 TWAI (CanBus) FreeRTOS problem

Hi
I used the codes from esp32 embedded examples for transmit and receive messages

These examples have one line in the receive message void :

if (twai_transmit(&message, pdMS_TO_TICKS(3000)) == ESP_OK) {
    printf("Message queued for transmission\n");
  } else {
    printf("Failed to queue message for transmission\n");

when i boot my code everything work fine.. i can read incoming messages from one connected device and can send

The problem is that when i call an other tast (from incoming message) to start after 8-9 seconds my canbus failing.
i am getting the message in my serial from the above code :

Failed to queue message for transmission

My program is big (almost 1000 lines) so i am posting the tasks about canbus

All my tasks ( 8 in total ) have the same level of priority and can run in both cores (not declared a dedicate core for the tasks)

#include "driver/twai.h"

// Pins used to connect to CAN bus transceiver:
#define RX_PIN 26
#define TX_PIN 27

// Intervall:
#define TRANSMIT_RATE_MS 1000
#define POLLING_RATE_MS 1000

static bool driver_installed = false;

unsigned long previousMillis = 0;  // will store last time a message was send
String texto;


Calls = 0;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);


  xTaskCreate(
    TaskTWAI,     // Task function.
    "Task TWAI",  // name of task.
    2048,         // Stack size of task
    NULL,         // parameter of the task
    1,            // priority of the task
    &Task_TWAI    // Task handle to keep track of created task
  );              // pin task to core 0
  //tskNO_AFFINITY); // pin task to core is automatic depends the load of each core



}

void loop() {
  // put your main code here, to run repeatedly:

}



void TaskTWAI(void *pvParameters) {
  // give some time at boot the cpu setup other parameters
  //vTaskDelay(1000 / portTICK_PERIOD_MS);

  // 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_NO_ACK);  // TWAI_MODE_NO_ACK , TWAI_MODE_LISTEN_ONLY , TWAI_MODE_NORMAL
  twai_timing_config_t t_config = TWAI_TIMING_CONFIG_50KBITS();  //Look in the api-reference for other speed sets.
  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_RX_QUEUE_FULL | TWAI_ALERT_TX_IDLE | TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_TX_FAILED | TWAI_ALERT_BUS_ERROR;
  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;



  for (;;) {
    if (!driver_installed) {
      // Driver not installed
      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: %d\n", twaistatus.bus_error_count);
    }

    if (alerts_triggered & TWAI_ALERT_TX_FAILED) {
      Serial.println("Alert: The Transmission failed.");
      Serial.printf("TX buffered: %d\t", twaistatus.msgs_to_tx);
      Serial.printf("TX error: %d\t", twaistatus.tx_error_counter);
      Serial.printf("TX failed: %d\n", twaistatus.tx_failed_count);
    }

    if (alerts_triggered & TWAI_ALERT_TX_SUCCESS) {
      Serial.println("Alert: The Transmission was successful.");
      Serial.printf("TX buffered: %d\t", twaistatus.msgs_to_tx);
    }

    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: %d\t", twaistatus.msgs_to_rx);
      Serial.printf("RX missed: %d\t", twaistatus.rx_missed_count);
      Serial.printf("RX overrun %d\n", twaistatus.rx_overrun_count);
    }

    // Check if message is received
    if (alerts_triggered & TWAI_ALERT_RX_DATA) {
      Serial.println("Testing line");
      // One or more messages received. Handle all.
      twai_message_t message;
      while (twai_receive(&message, 0) == ESP_OK) {
        handle_rx_message(message);
      }
    }
    // Send message
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= TRANSMIT_RATE_MS) {
      previousMillis = currentMillis;
      send_message();
    }
    vTaskDelay(10);
  }
}


static void send_message() {
  // Send message

  // Configure message to transmit
  twai_message_t message;
  message.identifier = 0x25;
  message.data_length_code = 8;
  for (int i = 0; i < 4; i++) {
    message.data[i] = 10;
  }

  // Queue message for transmission
  if (twai_transmit(&message, pdMS_TO_TICKS(3000)) == ESP_OK) {
    printf("Message queued for transmission\n");
  } else {
    printf("Failed to queue message for transmission\n");
    //twai_initiate_recovery();
    twai_stop();
    printf("twai Stoped\n");
    vTaskDelay(500);
    twai_start();
    printf("twai Started\n");
    vTaskDelay(500);
  }
  vTaskDelay(100);
}





///*

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: %x\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("");
  }

  // WORKING0
  if (message.identifier == 0x20) {
    // working also and with 02  instead of  0x02
    if (message.data[0] == 0x01) {
      if (Moving == false) {
        Serial.println("Down call");
        if (Calls != 0 && FloorPosition != 0) {
          Calls = 0;
        }
      }
    }
    if (message.data[0] == 0x02) {
      if (Moving == false) {
        Serial.println("up call");
        if (Calls != 1 && FloorPosition != 1) {
          Calls = 1;
        }
      }
    }
  }
}

is any method when i am getting this message to restart the canbus (not to have to hard reset the esp32)

i know that this is not the right way and have to find what is casing that...

Update
Ok i figure out why it is happening (not what causing it)
as i have read :

Bus-Off: The bus-off state is automatically entered when the TWAI controller's Transmit Error Counter becomes greater than or equal to 256. The bus-off state indicates the occurrence of severe errors on the bus or in the TWAI controller. Whilst in the bus-off state, the TWAI controller is unable to take part in any bus activities. To exit the bus-off state, the TWAI controller must undergo the bus recovery process.

the full message i am getting is this one :


Message queued for transmission

Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
Bus error count: 23
Alert: The Transmission was successful.
TX buffered: 0	

Message queued for transmission

Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
Bus error count: 27
Alert: The Transmission was successful.
TX buffered: 0	

Message queued for transmission
Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
Bus error count: 31
Alert: The Transmission was successful.
TX buffered: 0	

Message queued for transmission
Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
Bus error count: 33
Alert: The Transmission failed.
TX buffered: 0	TX error: 128	TX failed: 1

Failed to queue message for transmission
Failed to queue message for transmission
Failed to queue message for transmission
Failed to queue message for transmission

New Update...
Ok i finally discovered the problem.
was not a code problem... was a hardware problem

Low power supply... when i was calling the other task (driving some relays to open) i had a small power drop and the External Transceiver module got out of order...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.