@tonton81, first of all , thanks for providing this amazing library. What I was trying to do it to send a CAN2.0 message using CAN1 on Teensy 4.0 and receive it on the same Teensy 4.0 using CAN2. The CAN transceiver I used was MCP2551. Baud rate is 1MHz. Rs pin was connected to GND and two 120 ohm resistors were connected to CANH and CANL. When I write() the CAN package and read() the CAN bus immediately after the write(), I can get the package without any problem. However, if I attached the canStiff() as an interrupt, it seemed that that interrupt was never triggered. Could you please help with this problem? Attached are my code and a picture showing my connection.
Code:
#include "Arduino.h"
#include "FlexCAN_T4.h"
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
void canSniff(const CAN_message_t &msg);
void setup() {
Serial.begin(9600);
can1.begin();
can2.begin();
can1.setBaudRate(1000000);
can2.setBaudRate(1000000);
can1.enableFIFO();
can1.enableMBInterrupt(FIFO);
can1.onReceive(FIFO, canSniff);
can2.enableFIFO();
can2.enableMBInterrupt(FIFO);
can2.onReceive(FIFO, canSniff);
delay(1000);
Serial.println("CAN setup finished");
}
void loop() {
can1.events();
can2.events();
if (Serial.available()) {
char c = Serial.read();
if (c == 'a') {
Serial.println("Sending ...");
CAN_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;
can1.write(msg);
// delay(100);
// can2.read(msg);
// Serial.print("MB ");
// Serial.print(msg.mb);
// Serial.print(" OVERRUN: ");
// Serial.print(msg.flags.overrun);
// Serial.print(" LEN: ");
// Serial.print(msg.len);
// Serial.print(" EXT: ");
// Serial.print(msg.flags.extended);
// Serial.print(" TS: ");
// Serial.print(msg.timestamp);
// Serial.print(" ID: ");
// Serial.print(msg.id, HEX);
// Serial.print(" Buffer: ");
// for ( uint8_t i = 0; i < msg.len; i++ ) {
// Serial.print(msg.buf[i], HEX);
// Serial.print(" ");
// }
// Serial.println();
}
}
delay(100);
}
void canSniff(const CAN_message_t &msg) {
Serial.println("Interrupted");
Serial.print("MB ");
Serial.print(msg.mb);
Serial.print(" OVERRUN: ");
Serial.print(msg.flags.overrun);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" TS: ");
Serial.print(msg.timestamp);
Serial.print(" ID: ");
Serial.print(msg.id, HEX);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf[i], HEX);
Serial.print(" ");
}
Serial.println();
}