CAN_BUS Received get crashed with ESP32

Hi, I'm working with two ESP32 with a MCP2515 device to create a CAN-BUS network, my problem is the ESP32 that is getting the data, when it receives the message it get crashed. I know how to solve the problem, here is the code of the code to send messages:

#include <SPI.h>
#include "mcp_can.h"

#ifdef ESP32
  #define SPI_CS 5
  #define CAN_INT 4 
#else
  #define SPI_CS 10
  #define CAN_INT 2 
#endif

MCP_CAN CAN0(SPI_CS);

long id = 0x53;
byte data[1];

void setup() {
  Serial.begin(115200);
  
  while(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK)Serial.println("Error Initializing MCP2515...");
  CAN0.setMode(MCP_NORMAL);
  Serial.println("MCP2515 Initialized Successfully!");

}

void loop() {
  if(Serial.available()){
    char c = Serial.read();
    Serial.print("Valor de ");
    Serial.print(c);
    Serial.print(" enviar: ");
    Serial.println(c,HEX);

    data[0] = c;

    byte sndState = CAN0.sendMsgBuf(id,0,1,data);

    if(sndState == CAN_OK){
      Serial.println("Message Sent Successfully!");
    }
    else {
    Serial.println("Error Sending Message...");
    }
    delay(100);
  }
}

And this is the code to receive the message:

#include <SPI.h>
#include "mcp_can.h"

#ifdef ESP32
  #define SPI_CS 5
  #define CAN_INT 4 
#else
  #define SPI_CS 10
  #define CAN_INT 2 
#endif

MCP_CAN CAN0(SPI_CS);

void setup() {
 Serial.begin(115200);
  
  while(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK)Serial.println("Error Initializing MCP2515...");
  CAN0.setMode(MCP_NORMAL);
  pinMode(CAN_INT,INPUT);
  Serial.println("MCP2515 Initialized Successfully!");
}

unsigned long rxId;
unsigned char len;
unsigned char rxBuff[8];

void loop() {
  if(!digitalRead(CAN_INT))                          
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuff);              
  }   
}

the error when I get a Message is the next:

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

if anybody knows how to solve this problem or knows another library to work with the ESP32 and MCP2515, please let me to know, I will be greatefull

Post a simple schematic showing the network, all termination etc, Hopefully it is at least a meter or more in length.

Why not just solve the problem?

Here are some links about the Guru meditation error:

don't know if it will help but I added PULLUP to

 pinMode(CAN0_INT, INPUT_PULLUP);  // Configuring pin for /INT input *** added PULLUP ***

worth noting that the esp32 has a builtin Espressif Two-Wire Automotive Interface (TWAI) CAN interface
have a look at esp32-can-bus-demo-code-for-adafruit-adafruit-can-pal-can-bus-transceiver
the ESP32 requires a cjmcu-1051 CAN transceiver or similar to interface to the CANbus

Hi I already solved, it was a problem where I put the variables and I put them at the top:

#include <SPI.h>
#include "mcp_can.h"

#ifdef ESP32
  #define SPI_CS 5
  #define CAN_INT 4 
#else
  #define SPI_CS 10
  #define CAN_INT 2 
#endif

MCP_CAN CAN0(SPI_CS);

unsigned long rxId;
unsigned char len;
unsigned char rxBuff[8];
bool interrupt =  false;
byte is_received;

void ISR_CAN(){
  interrupt = true;
}

void setup() {
 Serial.begin(115200);
  
  while(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK)Serial.println("Error Initializing MCP2515...");
  CAN0.setMode(MCP_NORMAL);
  pinMode(CAN_INT,INPUT);
  Serial.println("MCP2515 Initialized Successfully!");
  attachInterrupt(digitalPinToInterrupt(CAN_INT),ISR_CAN,FALLING);
}

now the problem I have the ESP32 receives the message but only once, then it doesnt receive anything if I still send information, and the mesagge is empty

The complete code is this:

#include <SPI.h>
#include "mcp_can.h"

#ifdef ESP32
  #define SPI_CS 5
  #define CAN_INT 4 
#else
  #define SPI_CS 10
  #define CAN_INT 2 
#endif

MCP_CAN CAN0(SPI_CS);

unsigned long rxId;
unsigned char len;
unsigned char rxBuff[8];
bool interrupt =  false;
byte is_received;

void ISR_CAN(){
  interrupt = true;
}

void setup() {
 Serial.begin(115200);
  
  while(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK)Serial.println("Error Initializing MCP2515...");
  CAN0.setMode(MCP_NORMAL);
  pinMode(CAN_INT,INPUT);
  Serial.println("MCP2515 Initialized Successfully!");
  attachInterrupt(digitalPinToInterrupt(CAN_INT),ISR_CAN,FALLING);
}


void loop() {
  if(interrupt){
    Serial.println("Message received");
    CAN0.readMsgBuf(&rxId,&len, rxBuff);
    
    Serial.println("Buffer: ");
    for(int i=0;i<8;i++){
      Serial.print(rxBuff[i]);
      Serial.print("\t");
    }
    Serial.println();
    interrupt = false;
  }
}

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