Forum Rule: Always post complete source code & details to reproduce any issue!
Page 16 of 17 FirstFirst ... 6 14 15 16 17 LastLast
Results 376 to 400 of 407

Thread: FlexCAN_T4 - FlexCAN for Teensy 4

  1. #376
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    looks good, what kinda transceiver you are using? some have reported to be reading fine but not transmitting

    Code:
    CAN.sendMsgBuf(5376, 1, 2, canMsg)
    try specifying length of 2? on new code your specifying 8

    Some ecus are specific to responding to certain lengths
    Last edited by tonton81; 09-27-2020 at 03:20 PM.

  2. #377
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    Good idea. tried that but no difference.

  3. #378
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    can you plug in your other mcu, just as a reader, to see if teensy is actually sending something on the bus?

    also check your bus termination, your old CAN board may have been using a resistor and your new transceiver isn't. both ends of the bus must be properly terminated

    you can test this with a resistance meter on the CANH/L contacts of breakoutboards while theyre disconnected. if you have a 120 or 60 ohm resistance on old board, you need to make sure the transceiver on T4 is equivalent
    Last edited by tonton81; 09-27-2020 at 05:44 PM.

  4. #379
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    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
    }

  5. #380
    It doesn't work if you just connect TX to RX. Check out: http://www.mikrocontroller.net/attac...ens_AP2921.pdf

  6. #381
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Interesting data sheet, but in my case I am not connecting connecting multiple TX lines together. In my final application I will be using CAN transceivers to talk with remote systems, this is just a sanity check step to test out the library function, etc.

    Can someone explain why connecting the TX output of 1 CAN channel to the single RX input of another should not work. I don't see anything in the electrical spec that should be an issue, but perhaps I'm missing it , the data manual is over 3000 pages after all. Does the RX require a pull-up? If this is the case I would have assumed internal pull-up would have been initialized by the driver. I guess I'll look into that.

  7. #382
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    you definately need the transceiver. you can gerry rig a diode and resistors to possibly make it work, but stability wont be guarenteed obviously

  8. #383
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Can you explain why this is? With a short wire connecting can1tx to can1rx it is basically the same as loopback mode, signal looks clean on the scope. I put a 10K pullup to 3.3V (which doesn't appear to change anything).

  9. #384
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    All CAN controllers need transceivers, it doesn't matter what mocrocontroller you have, even vehicles have them in the ECUs, it's just the way it is. There is a diode hack somewhere on google but like I said it's not meant or designed to be directly wired.

  10. #385
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    I'm sorry for poor English
    Thank you for the wonderful library.

    No problem with can2.0 using this library
    I was able to confirm that it works.
    Next, I would like to check with canfd

    Example of reception by canfd
    And
    Example of sending by canfd
    Can you tell me each?

  11. #386
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    Code:
    #include <FlexCAN_T4.h>
    
    FlexCAN_T4FD<CAN3, RX_SIZE_128, TX_SIZE_128> FD;
    
    void setup(void) {
      FD.begin();
    
      CANFD_timings_t config;
      config.clock = CLK_24MHz;
      config.baudrate = 1000000;
      config.baudrateFD = 2000000;
      config.propdelay = 190;
      config.bus_length = 1;
      config.sample = 70;
      FD.setRegions(64);
      FD.setBaudRate(config);
      FD.onReceive(canSniff);
      FD.enableMBInterrupts();
      FD.mailboxStatus();
    }
    
    void canSniff(const CANFD_message_t &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("  EDL: "); Serial.print(msg.edl );
      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();
    }
    
    void loop() {
      FD.events();
    }
    it's mostly the same as CAN2.0 layout.
    for the sending part, just make sure you use CANFD_message_t

    red shows the changes needed to use FD

  12. #387
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    Thank you for your quick answer
    That would help a lot



    I'm sorry for being ignorant
    Let me ask you the following
    config.baudrate =
    1000000;
    config.baudrateFD = 2000000;

    Can you tell us the difference between baudrate and baudrateFD?

    The baud rate of the bus I want to receive is 500kbps canFD.

    Should both be set to 500kbps in my case?

  13. #388
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    CANFD has 2 baudrates.

    Nominal max is 1Mbps for CAN2.0 and FD
    The second rate for FD is for it's data, which can go up to 8Mbps, provided your transceiver is capable (some transceivers do 5 max). If you prefer to match the speed its fine, you can put 500000 for both. The code I posted enables 64 byte mailboxes, so you can send/receive up to 64 bytes per frame

  14. #389
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    tonton81
    Thank you so much.


    With the code you learned
    I will try it tomorrow.

  15. #390
    Quote Originally Posted by dpj View Post
    Can you explain why this is? With a short wire connecting can1tx to can1rx it is basically the same as loopback mode, signal looks clean on the scope. I put a 10K pullup to 3.3V (which doesn't appear to change anything).
    Two further aspects must be considered:
    1. With can1tx connected to can2rx, can1rx is expecting to see the bus state at all times, as if connected to can1tx.
    2. During the ACK slot bit of a standard frame, can1rx is expecting another node to overwrite the recessive bit sent by can1tx with a dominant bit to ACK the message. If this ACK bit is recessive (not ACK'd), then the controller will resend the frame ASAP.

    With a wire shorting from can1tx to can1rx you are satisfying the first condition above, but not the second. The frame is never properly ACK'd, so it is almost certainly being sent repeatedly. You could confirm this with the scope by checking if it is only sending a frame ever ~100ms.

    For the frame to be ACK'd by can2tx, you will need transceivers or diodes to prevent the ACK bit from being a short circuit from can1tx (high) to can2tx (low).

  16. #391
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    Here is a way you can try it without a transceiver, beware that the pullup is at 5V, teensy pins are not 5V tolerant. If you have issues communicating after that theres not much we can do software wise anyways
    Attached Files Attached Files

  17. #392
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Yes, thanks. I deduced overnight that it probably had to do with the ack. I have can1 and can2 connected with a pulled up local bus with diodes as described in the siemens apnote, (Pulled up to 3.3V) and it is working fine. I still didn't understand why the single port connection can1tx to can1rx (hardwired loopback), but if the channel will not ack itself that would explain it.

    This lets me verify software operation and design, before I make a board with the transceiver.

  18. #393
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    Results of checking using the code you gave me yesterday
    An ID with 8 bytes or more of data could not be received.

    Here are the changes

    CANFD_timings_t config;
    config.clock = CLK_24MHz;
    config.baudrate = 500000;
    config.baudrateFD = 2000000;
    config.propdelay = 190;
    config.bus_length = 1;
    config.sample = 70;
    FD.setRegions(32);
    FD.setBaudRate(config);

    The transceiver I'm using is 2561FD

    Please let me know if you have any idea

  19. #394
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    > 8 definately does work because TeensyCAN used 64byte transfers. what did serial monitor show for output?

    what device is it connected to? that device must have BOTH matching rates for it to work

    with code above you should be able to receive/send 32 bytes max

  20. #395
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    Thank you tonton81

    The ID with 32 bytes of data was ignored and did not appear on the serial monitor.
    (It has been confirmed that an ID with 32 bytes of data is transmitted on the bus by a different canFD measuring instrument.)

    It is connected to the canfd vehicle bus.

  21. #396
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    sure the rate is correct? try another clock to shift the timings, FD supports multiple timing parameters for the same bitrates (due to advanced calculation algorithms built in a separate file).
    First try playing with the CLK, 24 is default for oscillator. change it to peripheral clock 60

    also since your tool is up, can you try sending a up to 32 byte message and see what it says?

    do you receive anything other than the one your not getting? or just specific frame not receiving? by default it accepts all traffic, trying to send data from teensy and verifying it on your analyzer will show if the bitrates/timings match with your car, if you don't see anything, check the transceiver wiring, make sure the enable pin (if any) is grounded to enable the transceiver

    teensy must also be grounded to the car as well, not just using the CAN lines, if you are using USB laptop to test teensy in car, both teensy and laptop have no common ground, so make sure you have a ground wire from teensy GND to the car's chasis. the instrument already has the ground attached via the plug im sure (CANH,CANL,GND), but teensy is wired without connector so validate CANH,CANL,GND connection from teensy to car
    Last edited by tonton81; 10-01-2020 at 08:59 AM.

  22. #397
    Junior Member
    Join Date
    Sep 2020
    Posts
    6
    Thank you tonton81

    I think the baud rate is definitely below.
    config.baudrate = 500000;
    config.baudrateFD = 2000000;

    The reason is that different canfd instruments can measure at the same baud rate.

    Try changing the CLK from 24 to 60. Is there a possibility that FD cannot be measured if CLK is slow?

  23. #398
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    no thats just the clock for the controller, the bitrate calculation is based off that.

    Code:
    config.clock = CLK_60MHz;
    but liike i said if your FD rate is not set properly you wont get any frames because the CRC will fail and the controller will toss it. make sure BOTH devices are 500000 & 2000000, if the 2nd rate is not correct for your vehicle, you will NOT be able to read/write.

    you also told me yesturday that you wanted 500000 for both, but today you put 500000 & 2000000. guessing the rate for your device is very hard to determine why "it won't work". you need to source your device specs, or get a scope to confirm the rate, else it will never work. FD is hard to diagnose without the proper FD rate. Your bus didn't crash right? thats because nominal speed was correct so it didnt corrupt CAN traffic already on the bus since it won arbitration. However, the 2nd rate, if it was wrong, fails the CRC as its computed wrong on the bus and the nodes just toss it out, and arbitrations continue. So you need to get the EXACT rate for your car's bus to get it to a usuable state.

    FD was scope tested working at 2, 4, 6, and 8Mbps by skpang, so they are calculated correctly
    Last edited by tonton81; 10-01-2020 at 01:33 PM.

  24. #399
    Junior Member
    Join Date
    Mar 2019
    Location
    France
    Posts
    17
    Dear tonton81, dear all,

    I thought been enough fluent with c++, but I realized that not, in tryng to deeply used the FlexCAN_T4 library.
    So, I want to made a function with parameters : can_bus number (1,2 or 3), number of mailbox, id CAN
    Something like this
    void CommonCAN_init(FlexCAN_T4 Bus, uint16_t MB, uint16_t ID_CAN)
    {
    Bus->setMBFilter(MB,ID_CAN);
    Bus->enhanceFilter(MB);
    ....
    }

    Normaly, I don't have difficuties to manage class' pointer, but with this library my understunding of code reach a step to high for me.
    Did someone have a idea of how I can achieve that?
    Thank a lot in adavance,

  25. #400
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,449
    it's a template compile time object. You may pass the object to your other program in a function after. Mailboxes can be changed at runtime too (setMaxMB()), your function is neither a pointer nor a reference from what I see, but I gather you want to pass more than one bus into your function? try &bus or *bus before using it inside your function with & or ->

    Also thats the template class you are probably not going to go far since those objects are on a base class

    you should use FlexCAN_T4_Base instead of FlexCAN_T4, because the runtime object is of base class not template

    It may have shown that in the compile error log as well

    wait, no the sketch has the compile time object of class FlexCAN_T4, the library itself uses it's own base class. Try just using the & or * beside Bus in your function
    Last edited by tonton81; 10-25-2020 at 12:55 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
  •