teensy1717
Member
Hi, I am trying to achieve a CAN communication between a Teensy 4.1 and an arduino nano. For this,
I am using a TJA1050 transceiver connected to the Teensy 4.1 and a MCP2515 CAN controller
module connected to the arduino nano. I have successfully used CAN communication between two
arduino nano’s using two MCP2515 controllers, so I know that the mcp2515 is functioning correctly.
The problem lies in the Teensy-nano CAN communication. When I try to send data from the teensy
to the nano, it shows that data is being sent but stops after 17 iterations and none of this is received
by the nano.
I also tried setting the same bit rate but could not see anything being received on the Nano. If
anyone can help me out with this problem, it will be a great help.
Thank you.
My connections are as follows:
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>
//————————————————————————————————————————————
——————————————————————————————————
// MCP2515 connections:
// - standard SPI pins for SCK, MOSI and MISO
// - a digital output for CS
// - interrupt input pin for INT
//————————————————————————————————————————————
——————————————————————————————————
// use B connections for MISO, MOSI, SCK, #9 or #10 for CS (as you want),
// #2 or #3 for INT (as you want).
//————————————————————————————————————————————
——————————————————————————————————
// 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
//————————————————————————————————————————————
——————————————————————————————————
// SETUP
//————————————————————————————————————————————
——————————————————————————————————
void setup () {
//--- Switch on builtin led
pinMode (LED_BUILTIN, OUTPUT) ;
digitalWrite (LED_BUILTIN, HIGH) ;
//--- Start serial
Serial.begin (38400) ;
//--- 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);
}
}
//————————————————————————————————————————————
——————————————————————————————————
I am using a TJA1050 transceiver connected to the Teensy 4.1 and a MCP2515 CAN controller
module connected to the arduino nano. I have successfully used CAN communication between two
arduino nano’s using two MCP2515 controllers, so I know that the mcp2515 is functioning correctly.
The problem lies in the Teensy-nano CAN communication. When I try to send data from the teensy
to the nano, it shows that data is being sent but stops after 17 iterations and none of this is received
by the nano.
I also tried setting the same bit rate but could not see anything being received on the Nano. If
anyone can help me out with this problem, it will be a great help.
Thank you.
My connections are as follows:
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>
//————————————————————————————————————————————
——————————————————————————————————
// MCP2515 connections:
// - standard SPI pins for SCK, MOSI and MISO
// - a digital output for CS
// - interrupt input pin for INT
//————————————————————————————————————————————
——————————————————————————————————
// use B connections for MISO, MOSI, SCK, #9 or #10 for CS (as you want),
// #2 or #3 for INT (as you want).
//————————————————————————————————————————————
——————————————————————————————————
// 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
//————————————————————————————————————————————
——————————————————————————————————
// SETUP
//————————————————————————————————————————————
——————————————————————————————————
void setup () {
//--- Switch on builtin led
pinMode (LED_BUILTIN, OUTPUT) ;
digitalWrite (LED_BUILTIN, HIGH) ;
//--- Start serial
Serial.begin (38400) ;
//--- 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);
}
}
//————————————————————————————————————————————
——————————————————————————————————
Last edited: