Hi,
I've been hacking a commercial automotive long range radar.
So far it works well, however, I'm facing issues regarding the throughput of the MCP2515 (on one of those chinese breakout boards with 8MHz crystal), and/or SPI / Arduino UNO.
The procotol is standard frames at 500kbps, the sensor is sending around 1500 frames with 8-bytes of payload per second. For continous operation, I have to the MCP in standard mode to ACK the incoming packages.
I'm using Cory's MCP library (GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library).
If just receiving and counting incoming frames, I get about 500 packages/s (out of 1200-1500 / s).
To clarify: The receive loop is running about 25.000 times per second, but only 500 times I'm getting a low CS read (or alternatively a positive CAN.checkReceive() return <-- lower throughput than digitalRead).
This is a quick example of what I'm doing:
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
unsigned char rxBufInverse[8];
#define CAN0_INT 2
MCP_CAN CAN0(9);
long unsigned int totalMessages=0;
unsigned long lastOutput=0;
void setup()
{
Serial.begin(921600);
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("MCP2515 Library Receive Example...");
}
void loop()
{
if (millis()-lastOutput>=1000) {
Serial.print("total ");
Serial.println(totalMessages);
totalMessages=0;
lastOutput=millis();
}
if(/*CAN0.checkReceive() == CAN_MSGAVAIL*/ !digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBufInverse); // Read data: len = data length, buf = data byte(s)
totalMessages++;
}
}
So does anyone have experience with high-load CAN busses?
I've been asking myself:
- Is the 8MHz crystal sufficient for 500kbps operation?
- Are the receive buffers correctly utilized by Cory's library?
- The radar might be bursting out ~40 frames in less than 1ms, is the SPI communication / digitalRead fast enough to cover that? (alternatively, can an ISR be used and improve the performance)
- If the MCP2515 isn't suited, are there alternative options for CAN transceivers (even for different microcontrollers) which are proven to be more performant?
Thanks,
Florian
