Hi all,
Currently decently new to Teensy and am trying out CAN communication for a project of mine--first goal is to get two Teensy 4.0s chatting to one another. I'm using the new Flex Can library for 4.0s (https://github.com/tonton81/FlexCAN_T4) and this CAN chip https://www.amazon.com/LILIERS-CJMCU-230-SN65HVD230-transceiver communication/dp/B07BF6C1M5#detail-bullets. So far I've been able to get successful communication between two 3.2s using an older Flexcan library but have been unsuccessful with the 4.0s.
My wiring set up between the 4.0s and the 3.s is the same with the exception that the CRX and CTX are connected to 0 and 1 respectively. I'm (well hopefully?) confident the wiring is correct due to successful testing with the 3.2s. My code for the transceiver and receiver is pasted below: (mostly built off of the provided library example).
Transmitter:
#include <circular_buffer.h>
#include <FlexCAN_T4.h>
#include <imxrt_flexcan.h>
#include <kinetis_flexcan.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> FD;
void setup(void) {
pinMode(13, OUTPUT);
Serial.begin(115200); delay(500);
pinMode(6, OUTPUT); digitalWrite(6, LOW);
digitalWrite(13, HIGH);
FD.begin();
FD.setRegions(64);
}
void loop() {
CANFD_message_t msg;
msg.len = 8; msg.id = 0x321;
msg.buf[0] = 1; msg.buf[1] = 2; msg.buf[2] = 3; msg.buf[3] = 4;
msg.buf[4] = 5; msg.buf[5] = 6; msg.buf[6] = 7; msg.buf[7] = 8;
FD.write(msg);
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
Receiver:
#include <circular_buffer.h>
#include <FlexCAN_T4.h>
#include <imxrt_flexcan.h>
#include <kinetis_flexcan.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> FD;
void setup(void) {
pinMode(13, OUTPUT);
Serial.begin(115200); delay(500);
pinMode(6, OUTPUT); digitalWrite(6, LOW);
digitalWrite(13, HIGH);
FD.begin();
FD.setRegions(64);
}
void loop() {
CANFD_message_t msg;
msg.len = 8; msg.id = 0x321;
msg.buf[0] = 1; msg.buf[1] = 2; msg.buf[2] = 3; msg.buf[3] = 4;
msg.buf[4] = 5; msg.buf[5] = 6; msg.buf[6] = 7; msg.buf[7] = 8;
if ( FD.read(msg) ) {
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
Serial.print("MB: "); Serial.print(msg.mb);
Serial.print(" ID: 0x"); Serial.print(msg.id, HEX );
Serial.print(" EXT: "); Serial.print(msg.flags.extended );
Serial.print(" LEN: "); Serial.print(msg.len);
Serial.print(" DATA: ");
for ( uint8_t i = 0; i < 8; i++ ) {
Serial.print(msg.buf); Serial.print(" ");
}
Serial.print(" TS: "); Serial.println(msg.timestamp);
}
delay(500);
}
Any advice where am I going wrong? Or good resources/documentation to take a peek at?
And then lastly just a few small questions that I'm curious about:
1) A lot of what I've read online has indicated that CANbus chips require terminating resistors at the end. I had this in my original wiring setup but upon testing with the 3.2s CANbus communication worked with and without the resistors? What gives? Are they really needed?
2)On teensy 4.0 it seems like you can use CRX1/CTX1 or CRX2/CTX2. What's the difference? Is there a preference? Do you need to indicate in the code which one you are using?
Anyways any words of advice is appreciated,
Cheers!
Currently decently new to Teensy and am trying out CAN communication for a project of mine--first goal is to get two Teensy 4.0s chatting to one another. I'm using the new Flex Can library for 4.0s (https://github.com/tonton81/FlexCAN_T4) and this CAN chip https://www.amazon.com/LILIERS-CJMCU-230-SN65HVD230-transceiver communication/dp/B07BF6C1M5#detail-bullets. So far I've been able to get successful communication between two 3.2s using an older Flexcan library but have been unsuccessful with the 4.0s.
My wiring set up between the 4.0s and the 3.s is the same with the exception that the CRX and CTX are connected to 0 and 1 respectively. I'm (well hopefully?) confident the wiring is correct due to successful testing with the 3.2s. My code for the transceiver and receiver is pasted below: (mostly built off of the provided library example).
Transmitter:
#include <circular_buffer.h>
#include <FlexCAN_T4.h>
#include <imxrt_flexcan.h>
#include <kinetis_flexcan.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> FD;
void setup(void) {
pinMode(13, OUTPUT);
Serial.begin(115200); delay(500);
pinMode(6, OUTPUT); digitalWrite(6, LOW);
digitalWrite(13, HIGH);
FD.begin();
FD.setRegions(64);
}
void loop() {
CANFD_message_t msg;
msg.len = 8; msg.id = 0x321;
msg.buf[0] = 1; msg.buf[1] = 2; msg.buf[2] = 3; msg.buf[3] = 4;
msg.buf[4] = 5; msg.buf[5] = 6; msg.buf[6] = 7; msg.buf[7] = 8;
FD.write(msg);
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
Receiver:
#include <circular_buffer.h>
#include <FlexCAN_T4.h>
#include <imxrt_flexcan.h>
#include <kinetis_flexcan.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> FD;
void setup(void) {
pinMode(13, OUTPUT);
Serial.begin(115200); delay(500);
pinMode(6, OUTPUT); digitalWrite(6, LOW);
digitalWrite(13, HIGH);
FD.begin();
FD.setRegions(64);
}
void loop() {
CANFD_message_t msg;
msg.len = 8; msg.id = 0x321;
msg.buf[0] = 1; msg.buf[1] = 2; msg.buf[2] = 3; msg.buf[3] = 4;
msg.buf[4] = 5; msg.buf[5] = 6; msg.buf[6] = 7; msg.buf[7] = 8;
if ( FD.read(msg) ) {
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
Serial.print("MB: "); Serial.print(msg.mb);
Serial.print(" ID: 0x"); Serial.print(msg.id, HEX );
Serial.print(" EXT: "); Serial.print(msg.flags.extended );
Serial.print(" LEN: "); Serial.print(msg.len);
Serial.print(" DATA: ");
for ( uint8_t i = 0; i < 8; i++ ) {
Serial.print(msg.buf); Serial.print(" ");
}
Serial.print(" TS: "); Serial.println(msg.timestamp);
}
delay(500);
}
Any advice where am I going wrong? Or good resources/documentation to take a peek at?
And then lastly just a few small questions that I'm curious about:
1) A lot of what I've read online has indicated that CANbus chips require terminating resistors at the end. I had this in my original wiring setup but upon testing with the 3.2s CANbus communication worked with and without the resistors? What gives? Are they really needed?
2)On teensy 4.0 it seems like you can use CRX1/CTX1 or CRX2/CTX2. What's the difference? Is there a preference? Do you need to indicate in the code which one you are using?
Anyways any words of advice is appreciated,
Cheers!