Hi, I am trying to do CAN communication between Teensy 4.1 and Arduino Nano. For the communication between the Teensy and Nano, I am using a TJA1050 transceiver on Teensy side and MCP2515 CAN module on the Nano side. I am able to do CAN communication between two Nano using MCP2515, so I know that my mcp2515 is working properly.
When I am sending the data, it shows that the data is being sent from Teensy, but it stops sending data after 17 iterations and also the Arduino Nano is not receiving any data. I also tried setting the same bit rate but could not see anything being received on the Nano. I have used ACAN_T4 and and ACAN2515 libraries.
Code for Teensy 4.1
#ifndef __IMXRT1062__
#error "This sketch should be compiled for Teensy 4.x"
#endif
//-----------------------------------------------------------------
#include <ACAN_T4.h>
//-----------------------------------------------------------------
void setup () {
pinMode (LED_BUILTIN, OUTPUT) ;
Serial.begin (9600) ;
while (!Serial) {
delay (50) ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
ACAN_T4_Settings settings (125 * 1000) ; // 125 kbit/s
// settings.mBitRatePrescaler = 6;
//settings.mPropagationSegment = 5;
//settings.mPhaseSegment1 = 5;
//settings.mPhaseSegment2 = 5;
const uint32_t errorCode = ACAN_T4::can1.begin (settings) ;
Serial.print ("Bitrate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Serial.print ("Propagation Segment: ") ;
Serial.println (settings.mPropagationSegment) ;
Serial.print ("Phase segment 1: ") ;
Serial.println (settings.mPhaseSegment1) ;
Serial.print ("Phase segment 2: ") ;
Serial.println (settings.mPhaseSegment2) ;
Serial.print ("RJW: ") ;
Serial.println (settings.mRJW) ;
Serial.print ("Triple Sampling: ") ;
Serial.println (settings.mTripleSampling ? "yes" : "no") ;
Serial.print ("Actual bitrate: ") ;
Serial.print (settings.actualBitRate ()) ;
Serial.println (" bit/s") ;
Serial.print ("Exact bitrate ? ") ;
Serial.println (settings.exactBitRate () ? "yes" : "no") ;
Serial.print ("Distance from wished bitrate: ") ;
Serial.print (settings.ppmFromWishedBitRate ()) ;
Serial.println (" ppm") ;
Serial.print ("Sample point: ") ;
Serial.print (settings.samplePointFromBitStart ()) ;
Serial.println ("%") ;
if (0 == errorCode) {
Serial.println ("can1 ok") ;
} else {
Serial.print ("Error can1: 0x") ;
Serial.println (errorCode, HEX) ;
while (1) {
delay (100) ;
Serial.println ("Invalid setting") ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
}
}
//-----------------------------------------------------------------
static uint32_t gSendDate = 0 ;
static uint32_t gSentCount = 0 ;
//-----------------------------------------------------------------
void loop () {
CANMessage message ;
message.len = 8;
message.data[0] = 127;
message.data[1] = 43;
message.data[2] = 1;
if (gSendDate <= millis ()) {
//message.id = 0x542 ;
const bool ok = ACAN_T4::can1.tryToSend (message) ;
if (ok) {
gSendDate += 2000 ;
gSentCount += 1 ;
Serial.print ("Sent: ") ;
Serial.println (gSentCount) ;
}
}
}
Code for Arduino Nano
#include <ACAN2515.h>
// Error codes and possible causes:
// In case you see "Configuration error 0x1", the Arduino doesn't communicate
// with the 2515. You will get this error if there is no CAN shield or if
// the CS pin is incorrect.
// In case you see succes up to "Sent: 17" and from then on "Send failure":
// There is a problem with the interrupt. Check if correct pin is configured
//——————————————————————————————————————————————————————————————————————————————
static const byte MCP2515_CS = 10 ; // CS input of MCP2515 (adapt to your design)
static const byte MCP2515_INT = 3 ; // INT output of MCP2515 (adapt to your design)
//——————————————————————————————————————————————————————————————————————————————
// MCP2515 Driver object
//——————————————————————————————————————————————————————————————————————————————
ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;
//——————————————————————————————————————————————————————————————————————————————
// MCP2515 Quartz: adapt to your design
//——————————————————————————————————————————————————————————————————————————————
static const uint32_t QUARTZ_FREQUENCY = 16UL * 1000UL * 1000UL ; // 16 MHz
void setup () {
//--- Switch on builtin led
pinMode (LED_BUILTIN, OUTPUT) ;
digitalWrite (LED_BUILTIN, HIGH) ;
//--- Start serial
Serial.begin (9600) ;
//--- Wait for serial (blink led at 10 Hz during waiting)
while (!Serial) {
delay (50) ;
digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ;
}
//--- Begin SPI
SPI.begin () ;
//--- Configure ACAN2515
Serial.println ("Configure ACAN2515") ;
ACAN2515Settings settings (QUARTZ_FREQUENCY, 125UL * 1000UL) ; // CAN bit rate 125 kb/s
settings.mRequestedMode = ACAN2515Settings::NormalMode ; // Select normal mode
const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
if (errorCode == 0) {
Serial.print ("Bit Rate prescaler: ") ;
Serial.println (settings.mBitRatePrescaler) ;
Serial.print ("Propagation Segment: ") ;
Serial.println (settings.mPropagationSegment) ;
Serial.print ("Phase segment 1: ") ;
Serial.println (settings.mPhaseSegment1) ;
Serial.print ("Phase segment 2: ") ;
Serial.println (settings.mPhaseSegment2) ;
Serial.print ("SJW: ") ;
Serial.println (settings.mSJW) ;
Serial.print ("Triple Sampling: ") ;
Serial.println (settings.mTripleSampling ? "yes" : "no") ;
Serial.print ("Actual bit rate: ") ;
Serial.print (settings.actualBitRate ()) ;
Serial.println (" bit/s") ;
Serial.print ("Exact bit rate ? ") ;
Serial.println (settings.exactBitRate () ? "yes" : "no") ;
Serial.print ("Sample point: ") ;
Serial.print (settings.samplePointFromBitStart ()) ;
Serial.println ("%") ;
} else {
Serial.print ("Configuration error 0x") ;
Serial.println (errorCode, HEX) ;
}
}
//----------------------------------------------------------------------------------------------------------------------
static uint32_t gReceivedFrameCount = 0 ;
//——————————————————————————————————————————————————————————————————————————————
void loop () {
CANMessage frame ;
if (can.available ()) {
can.receive (frame) ;
int pwm = frame.data[0];
int angle = frame.data[1];
int dir = frame.data[2];
gReceivedFrameCount ++ ;
Serial.print ("Received: ") ;
Serial.print (gReceivedFrameCount) ;
Serial.print(" pwm ");
Serial.print(pwm);
Serial.print(" angle ");
Serial.print(angle);
Serial.print(" dir ");
Serial.println(dir);
}
}
If you can help me out with this problem, it will be a great help. Thank you.