Hello Folks!
I’m extremely excited about the new MIT Licensed CAN library for the MCP25625 can interface devices. Thank you Particle for making this library! Its going to be a game changer for sure!
I have hit a slight bump in the road when it comes to implementation. I was migrating an early example of CAN_Receive.cpp from the MCP_CAN_RK repository, which was working nominally with the mcp-can library, but when I run it with the can-mcp25x library, I get an error where the MCP initialization fails and the mode does not connect.
Here is my example file. (modified to work with J1939 and read vehicle speed at 500k)
// CAN Receive Example - For Tracker One
// Remember to "Particle: Configure Project for Device" ->
// and selected 1.5.4-rc.1 and Tracker platform.
// Example Updated: Adam Baumgartner, 9-18-2020
#include <mcp_can.h>
#include <SPI.h>
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(AUTOMATIC);
SerialLogHandler logHandler;
long unsigned int rxId;
const long unsigned int J1939speed = 0x18FEF100;
long unsigned int rxId_test;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];
char outputString[128];
unsigned int canSpeed = 65535;
int actualSpeed = 0;
MCP_CAN canInterface(CAN_CS, SPI1);
void setup()
{
Serial.begin(115200);
// Wait 10 seconds for USB debug serial to be connected (plus 1 more)
waitFor(Serial.isConnected, 10000);
delay(1000);
pinMode(CAN_PWR, OUTPUT);
digitalWrite(CAN_PWR, HIGH);
pinMode(CAN_STBY, OUTPUT);
digitalWrite(CAN_STBY, LOW);
pinMode(CAN_INT, INPUT);
// Hardware reset the CAN controller. Not really necessary, but doesn't hurt.
pinMode(CAN_RST, OUTPUT);
digitalWrite(CAN_RST, LOW);
delay(100);
digitalWrite(CAN_RST, HIGH);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(canInterface.begin(MCP_EID0, CAN_500KBPS, MCP_20MHZ) == CAN_OK){
Serial.println("MCP2515 Initialized Successfully!");
}else{
Serial.println("Error Initializing MCP2515...");
}
canInterface.setMode(MODE_NORMAL);
pinMode(CAN_INT, INPUT);
}
void loop(){
if(!digitalRead(CAN_INT))
{
canInterface.readMsgBufID(&rxId, &len, rxBuf);
if((rxId & 0x1FFFFFFF) == J1939speed){
canSpeed = (rxBuf[2] << 8) | rxBuf[1];
actualSpeed = canSpeed >> 8;
sprintf(outputString, " Speed kph: %d", actualSpeed);
Serial.print(outputString);
Serial.println();
}
}
}
And here is I get over serial monitor if when I run it.
For the migration instructions I followed the details at the bottom of AN017 Tracker CAN page.
I was looking to have the CAN initialization occur successfully and also fix having the modem fail to power on.
When I revert to using the MCP_CAN_RK library, and the slight typo changes, everything goes back to normal. I think the modem being shutdown and CAN chip not initializing are connected somehow.