Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: 4.0 CAN Communication

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    1

    4.0 CAN Communication

    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...30-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[i]); 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!

  2. #2
    For CAN FD you need to use CRX3 and CTX4. That is the pins on the back side of the Teensy board.

    CRX1/CTX1 or CRX2/CTX2 is CAN2.0B not FD.

    You would need two 120R terminating resistors in your network.

    You also need to setup the baud rate is well. Add this:
    FD.begin();
    CANFD_timings_t config;
    config.clock = CLK_24MHz;
    config.baudrate = 500000; // 500kbps arbitration rate
    config.baudrateFD = 2000000; // 2000kbps data rate
    config.propdelay = 190;
    config.bus_length = 1;
    config.sample = 75;
    FD.setRegions(64);
    FD.setBaudRate(config, 1, 1);
    FD.onReceive(canSniff);
    FD.setMBFilter(ACCEPT_ALL);
    FD.setMBFilter(MB13, 0x1);
    FD.setMBFilter(MB12, 0x1, 0x3);
    FD.setMBFilterRange(MB8, 0x1, 0x04);
    FD.enableMBInterrupt(MB8);
    FD.enableMBInterrupt(MB12);
    FD.enableMBInterrupt(MB13);
    FD.enhanceFilter(MB8);
    FD.enhanceFilter(MB10);
    FD.distribute();
    FD.mailboxStatus();

  3. #3
    Senior Member
    Join Date
    Apr 2019
    Posts
    152
    Do you need to use CAN to communicate between the two T4.0 ?

    might be easier to use Serial

    what is your reason for using CAN ?

  4. #4
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,134
    Check your constructor, you need to use "CAN2" and remove "FD" from FlexCAN_T4FD, CAN2 is pins 0 and 1 and you need this to be able to communicate with T3

    FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> FD;
    Thats a CANFD constructor

    FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can0;
    This is a CAN2.0 constructor you need to use instead

    Use the same functions as posted in the IFCT examples on github on FlexCAN_T4 library, with a new exception you must call begin() before everything else
    Last edited by tonton81; 10-28-2019 at 06:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •