Hello, I just got a Teensy 4.1 a few days back and have been trying to get CAN examples working. Yesterday I just discovered this forum which point to the source repository so I downloaded the latest FlexCAN_T4-master.zip and unpacked and did a diff with version I had in Arduino library which turned out to be an older version. Still have same issue with latest library.
I've direct wired CAN1TX - CAN2RX and CAN1RX to CAN2TX (teensy pins 23-1, and 22-0), no can driver chip in use. I've been attempting to transmit msg from one port and receive on the other, using some modified examples. It appears the transmit works, but nothing is received. I see activity on the lines with a scope.
Today I tried loopback mode and that works as I'd expect with both ports.
Loopback test:
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
CAN_message_t msg,msgout;
#if 1
#define CANX can1
#define CANNAME "CAN1"
#else
#define CANX can2
#define CANNAME "CAN2"
#endif
static uint8_t hex[17] = "0123456789abcdef";
// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working>>4 ] );
Serial.write( hex[ working&15 ] );
}
// Serial.write('\r');
// Serial.write('\n');
}
void setup(void) {
Serial.begin(115200);
delay(1000);
Serial.println(F("Hello Teensy 4.1 CAN Loopback Test."));
CANX.begin();
CANX.setBaudRate(250000);
CANX.mailboxStatus();
CANX.enableLoopBack(1);
delay(9000);
msgout.flags.extended = 0;
msgout.id = 0x100;
msgout.len = 8;
msgout.buf[0] = 10;
msgout.buf[1] = 20;
msgout.buf[2] = 0;
msgout.buf[3] = 100;
msgout.buf[4] = 128;
msgout.buf[5] = 64;
msgout.buf[6] = 32;
msgout.buf[7] = 16;
}
static int iteration = 1;
void loop() {
msgout.id = 0x100;
if(CANX.write(msgout)){
Serial.print("Msg sent on ");
Serial.println(CANNAME);
}
msgout.id = 0x101;
msgout.buf[7] += 1;
if(msgout.buf[7] == 0) msgout.buf[6] += 1;
delay(5);
// CANX.mailboxStatus();
// delay(800);
if ( CANX.read(msg) ) {
Serial.print(CANNAME);
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: ");
hexDump(msg.len, msg.buf);
Serial.print(" TS: "); Serial.println(msg.timestamp);
}
Serial.print("Iteration ");
Serial.println(iteration++);
delay(5);
while(iteration > 8); //hang here
}
Direct wired test:
=========================================================
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
CAN_message_t msg,msgout;
static uint8_t hex[17] = "0123456789abcdef";
// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working>>4 ] );
Serial.write( hex[ working&15 ] );
}
//Serial.write('\r');
//Serial.write('\n');
}
void setup(void) {
Serial.begin(115200);
delay(1000);
Serial.println(F("Hello Teensy 4.1 dual CAN Test."));
can1.begin();
can1.setBaudRate(250000);
can2.begin();
can2.setBaudRate(250000);
can1.mailboxStatus();
can2.mailboxStatus();
delay(9000);
msgout.flags.extended = 0;
msgout.id = 0x100;
msgout.len = 8;
msgout.buf[0] = 10;
msgout.buf[1] = 20;
msgout.buf[2] = 0;
msgout.buf[3] = 100;
msgout.buf[4] = 128;
msgout.buf[5] = 64;
msgout.buf[6] = 32;
msgout.buf[7] = 16;
}
static int iteration = 1;
void loop() {
msgout.id = 0x100;
if(can1.write(msgout))
Serial.println("Msg sent on can1");
msgout.id = 0x101;
msgout.buf[7] += 1;
if(msgout.buf[7] == 0) msgout.buf[6] += 1;
/* if(can2.write(msgout))
Serial.println("Msg sent on can2");
msgout.buf[7] += 1;
if(msgout.buf[7] == 0) msgout.buf[6] += 1;
*/
delay(100);
// can1.mailboxStatus();
// can2.mailboxStatus();
// delay(800);
/* if ( can1.read(msg) ) {
Serial.print("CAN1 ");
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: ");
hexDump(msg.len, msg.buf);
Serial.print(" TS: "); Serial.println(msg.timestamp);
}
else */ if ( can2.read(msg) ) {
Serial.print("CAN2 ");
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: ");
hexDump(msg.len, msg.buf);
Serial.print(" TS: "); Serial.println(msg.timestamp);
}
Serial.print("Iteration ");
Serial.println(iteration++);
delay(100);
while(iteration >8); //hang here
}