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

Thread: How to do CAN communication between Teensy 4.1 and Arduino Nano

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    5

    How to do CAN communication between Teensy 4.1 and Arduino Nano

    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:
    Click image for larger version. 

Name:	Screenshot_20210907-142814.jpg 
Views:	28 
Size:	42.1 KB 
ID:	25770
    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 by teensy1717; 09-07-2021 at 12:31 PM.

  2. #2
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Hi Teensy1717,

    Just a quick comment from a noob... the mcp2515 is capable of running in enhanced mode, but I don't think the TJA1050 has an enhanced mode. you could check that your in a mode that both chips are capable of. Just a thought... as that would stall any communication due to a different layout on the can packets!

    best regards

    Simon.M

  3. #3
    Senior Member
    Join Date
    Jan 2015
    Location
    UK
    Posts
    174
    Have you got a 120R terminator on each node ?

    I would use the FlexCAN_T4.h library on the Teensy.

    https://github.com/tonton81/FlexCAN_T4

  4. #4
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Hi Simon,
    The MCP2515 Can module that I am using has a TJA1050 transceiver in it. Also, with the Teensy 4.1 I am using the TJA1050 transceiver. So, I don't think there should be any issues.
    I did the CAN communication between two Arduino Nano boards using the same MCP2515 module, one with each controller. It worked perfectly fine so there should not be any issues about the enhancement mode.
    Please correct me if I am wrong.
    For your reference I have attached the link of the MCP2515 module and the TJA1050 module below.
    https://robu.in/product/tja1050-can-...erface-module/
    https://robu.in/product/mcp2515-can-...utine-arduino/

    Thank you!

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Hi skpang,
    Yes, I have used the terminating resistors on both the ends.
    I'll try it with FlexCAN_T4.h too.
    Thanks.

  6. #6
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Hi teensy1717,

    I had a look at the two modules that you posted links to, They are 5v modules and the teensy 4.1 is a 3.3v device. This might mean the signals going into the can bus modules are out of spec ( due to being 1.7 v too low).

    two options spring to mind, level shifters (voltage translators) placed between your teensy 4.1 and the can bus module on the data lines ( the can bus would then be powered from a 5 volt power source), it's bi directional and translates the correct voltage levels for either device. The only other thing is finding a can bus that can run from 3.3 volt levels.

    best regards

    Simon.M

  7. #7
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Hi Simon,
    I've already used a level shifter which is denoted by the "Logic level" block in the block diagram. So the problem lies somewhere else I guess :/

  8. #8
    Senior Member
    Join Date
    Jan 2015
    Location
    UK
    Posts
    174
    Use FlexCAN_T4.h library and if you still having problems use a scope and check you have pulses on the CTX pin when you are transmitting. You should also see pulses on the CRX pin.

  9. #9
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    hi teensy1717,

    well... the last thing i can suggest is swap the rx and tx lines. sometimes things get labelled meaning tx to tx and rx to rx, but could mean you pair teensy rx to can module tx and teensy tx to rx can module. if not that then i am out of ideas.

    best regards

    Simon.M

  10. #10
    Junior Member
    Join Date
    Sep 2021
    Posts
    5
    Hi Simon and skpang,

    The issue has been solved. I changed the crystal frequency in the Arduino Nano code to 8MHz because the Can controller module that I am using has a 8MHz crystal. In the reference/ example code they had given 16MHz as the frequency and I was using the same because when I did the communication between the two Nano boards using the same controller module with 16MHz, it was working. I still have no idea how the issue was resolved and why nano-nano was working at 16MHz.

    Anyway thanks a lot for posting your replies.

    Best regards

Tags for this Thread

Posting Permissions

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