Forum Rule: Always post complete source code & details to reproduce any issue!
Page 17 of 20 FirstFirst ... 7 15 16 17 18 19 ... LastLast
Results 401 to 425 of 477

Thread: FlexCAN_T4 - FlexCAN for Teensy 4

  1. #401
    Junior Member
    Join Date
    Mar 2019
    Location
    France
    Posts
    17
    Thanks alot for your fast reply
    ... I have to really switch on my brain, cause I'm still lost.

    Juste a some question : Is there 64MailBox per CAN or for 64 in total for the 3 can bus?

    I made :
    [CODE]
    FlexCAN_T4<CAN2, RX_SIZE_64, TX_SIZE_16> Can_COM1;

    void BusCan_InitMB(FlexCAN_T4* BusCan)
    {
    BusCan->setMBFilter(MB2,0x10AAFF00);
    BusCan->enhanceFilter(MB2);
    }

    void setup()
    {
    Can_COM1.begin();
    Can_COM1.setBaudRate(BAUTRATE_CAN_COM1);
    Can_COM1.setMBFilter(REJECT_ALL);
    BusCan_InitMeca(&Can_COM1);
    }
    [ /CODE]

    But the template, make me confused...
    There is of course error:
    - variable or field 'BusCan_InitMB' declared void
    - missing template arguments before 'BusCan'

    Tonton81, I know it's c++ "tips", but can I abuse more and ask for your lights?

  2. #402
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    yes, each controller has 64 mailboxes, they can be changed except the CANFD mode, size depends on mailbox data capacity.

    Yes I see the error here, as a workaround until it can be figured out, the CAN frames have identifiers of which bus they came from, so you can see the received frame has msg.bus 2 set, meaning it came from CAN2

    This is why we can pass all 3 busses callbacks to one function in a user sketch and differentiate which frame came from where
    Last edited by tonton81; 10-25-2020 at 03:21 PM.

  3. #403
    Junior Member
    Join Date
    Mar 2019
    Location
    France
    Posts
    17
    To sum-up, it means, if we use CAN2.0, there are theoretically 192 (64*3) Mail Boxes ? one CAN controler per channel CAN? not sure to well undestund https://www.nxp.com/webapp/Download?colCode=IMXRT1060RM page 2515

    For my initial request, I'm totaly lost... I'will have a break and a coffee...
    But before, how could I pass all 3 busses callbacks to one function, as you said?
    In fact, my aim, is to made a recursive function, to initialised some frames to one MailBox, with a specific CAN ID (filter), and affected to one of the 3 bus. Is there an elegant way to do that?

    Again, thank a lot

  4. #404
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    192 mailboxes total in CAN2.0 mode, all 3 CAN controllers, yes. each bus has 64 max.

    you can pass all your busses into one callback in your sketch. if you receive a frame from CAN2 for example, "msg.bus" will return 1 or 2 or 3, depending on if it came from bus CAN1 CAN2 or CAN3, in this case, the frame you received from CAN2 will have msg.bus set to 2, so you know it came from CAN2

  5. #405
    Junior Member
    Join Date
    Mar 2019
    Location
    France
    Posts
    17
    Ok understood, to made something generic for reading all CanX.readMB(msg) and after I check msg.bus.
    But what's happens for the init of MBs, can we do something generic/recursive? to avoid code manualy each filters MB Can1.setMBFilter(X) , Can2.setMBFilter(Y)...

  6. #406
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    well filters you need to set one by one no? what do you want to configure exactly? why not use the object in the function itself? what is your purpose for the project?

  7. #407
    Junior Member
    Join Date
    Oct 2020
    Location
    Bronx
    Posts
    3
    Good info. thanks

  8. #408
    Junior Member
    Join Date
    Dec 2019
    Posts
    14
    Trying to understand message ID length, 29 bit vs 11 bit .

    MCP2551 will read standard 11 bit message ID 0x5FC but this library will read as extended 29 bit message ID 0x17F00010 .
    Same network, same data. Sending data in standard format with the library and a SN65HVD23X will keep the 11 bit standard message ID as 0x5FC and online modules will respond correctly.

    Why is this, what I'm missing ?

  9. #409
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    the library reads both, that is correct. you probably didnt set the extended flag on the other library but by default both types of mailboxes extended and standard are setup to read the frames on the bus of both types on FlexCAN_T4.

  10. #410
    Junior Member
    Join Date
    Dec 2019
    Posts
    14
    Can I set the FlexCan_T4 to only show Standard?
    I figured out how to set it whenever I send data but not when receiving.

  11. #411
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    yes if you want only standard frames just change the extended ones to standard. use mailboxStatus() to view the layouts and you'll know which to change. example: myCan.setMB(5, RX, STD)
    STD for standard and EXT for extended

  12. #412
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Hi guys, 1st post (long time Embedded developer, first time teensy user).
    I'm doing some benchmarks on CAN-FD for a project - got a couple of teensy 4.1's back-to-back with skpang's breakout.
    All working great, even at 12MBits/s, so well done guys!

    Question on the timestamps... Any easy way to get the transmit TS? Maybe trigger an INT when message hits the bus to read back the TS?
    Would like to get accurate round-trip times and synchronise clocks.

    Cheers.

  13. #413
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    yes it may be possible to add transmit callbacks, you can handle your own timestamp in your callback if you wish, unless you want the flexcan one specifically? then it would have to read the MB buffer but not sure if the timestamp is stored on sent, i know the transmit does have interrupt on completion though. testing would need to be done to see if we can read back the data at the time of sent mailbox and maybe return that to user callback (IF the data is still there, and IF the timestamp is modified after transmit)

  14. #414
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    It's the Tx TS recorded when the message actually makes it on the bus after any arbitration delays.

    OK, I'll look into it.

    For reference, time synchronisation over CAN:
    https://www.autosar.org/fileadmin/us...yncOverCAN.pdf

    Thanks

  15. #415
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Can some one show me how to upload this on a teensy3.2?

  16. #416
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    1) set boards manager to 3.2
    2) select FlexCAN_T4 example FIFO with interrupt
    3) in the example make sure CAN0 is set in the FlexCAN_T4 constructor

    should be fine

  17. #417
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Quote Originally Posted by tonton81 View Post
    1) set boards manager to 3.2
    2) select FlexCAN_T4 example FIFO with interrupt
    3) in the example make sure CAN0 is set in the FlexCAN_T4 constructor

    should be fine
    Can you post a screen shot how it would look when I upload it?

    this is what im using..

    https://copperhilltech.com/teensy-3-...reakout-board/

    gm vehicle 2017

    baud 500000

    can2.0

  18. #418
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    Code:
    #include <FlexCAN_T4.h>
    FlexCAN_T4<CAN0, RX_SIZE_256, TX_SIZE_16> Can0;
    
    void setup(void) {
      Serial.begin(115200); delay(400);
      pinMode(6, OUTPUT); digitalWrite(6, LOW); /* optional tranceiver enable pin */
      Can0.begin();
      Can0.setBaudRate(500000);
      Can0.setMaxMB(16);
      Can0.enableFIFO();
      Can0.enableFIFOInterrupt();
      Can0.onReceive(canSniff);
      Can0.mailboxStatus();
    }
    
    void canSniff(const CAN_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(" 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() {
      Can0.events();
    }

  19. #419
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Quote Originally Posted by tonton81 View Post
    Code:
    #include <FlexCAN_T4.h>
    FlexCAN_T4<CAN0, RX_SIZE_256, TX_SIZE_16> Can0;
    
    void setup(void) {
      Serial.begin(115200); delay(400);
      pinMode(6, OUTPUT); digitalWrite(6, LOW); /* optional tranceiver enable pin */
      Can0.begin();
      Can0.setBaudRate(500000);
      Can0.setMaxMB(16);
      Can0.enableFIFO();
      Can0.enableFIFOInterrupt();
      Can0.onReceive(canSniff);
      Can0.mailboxStatus();
    }
    
    void canSniff(const CAN_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(" 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() {
      Can0.events();
    }
    That work THANKS.

    How can I monitor just

    Can ID 0x1F5

  20. #420
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    put this at top line of canSniff function:

    if ( msg.id != 0x1F5 ) return;

    filtering it to view only specific IDs is also possible

  21. #421
    Junior Member
    Join Date
    Nov 2020
    Posts
    4
    Tonton81

    Sorry but can you do it in red on above script you did. Im trying to learn where and what function go.

    Also do you do programming? I willing to compensate you.

  22. #422
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    Quote Originally Posted by p_shep View Post
    It's the Tx TS recorded when the message actually makes it on the bus after any arbitration delays.

    OK, I'll look into it.

    For reference, time synchronisation over CAN:
    https://www.autosar.org/fileadmin/us...yncOverCAN.pdf

    Thanks
    I just added transmit callback support. It is same as onReceive, just use onTransmit(MB8, callback) (mailbox specific) or onTransmit(callback) (global). The demo canSniff function can be used as a callback for onTransmit to show what has been sent on the bus and with current timestamp.

  23. #423
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Quote Originally Posted by tonton81 View Post
    I just added transmit callback support. It is same as onReceive, just use onTransmit(MB8, callback) (mailbox specific) or onTransmit(callback) (global). The demo canSniff function can be used as a callback for onTransmit to show what has been sent on the bus and with current timestamp.
    Oh wow, thank you that's incredibly useful. Had to drop this for the moment, but I'll be back on it eventually!

  24. #424
    So, I finally got around to making a Teensy 4.1 based design. I put CAN-FD transceivers on there for all three channels even though not all three can actually do CAN-FD. But, it doesn't really hurt anything to use them and it's fun to keep the design nice and neat. Anyway, just got to testing and am testing sending messages between the first two CAN buses on the T4.1. I can reliably get some odd behavior. Using Arduino IDE 1.8.13 and TeensyDuino 1.53

    I took the beta_sample sketch and modified it just slightly to make it possible to send messages on both CAN buses. The buses are looped to each other. Yes, I did terminate them.

    Code:
    #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;
    CAN_message_t txMsg;
    
    void setup(void) {
      can1.begin();
      can1.setBaudRate(250000);
      can2.begin();
      can2.setBaudRate(250000);
    
      txMsg.id = 0x34A;
      txMsg.len = 8;
      txMsg.buf[0] = 0x10;
      txMsg.buf[1] = 0xAC;
      txMsg.buf[2] = 0x54;
      txMsg.buf[3] = 0x31;
      txMsg.buf[4] = 0xA6;
      txMsg.buf[5] = 0xF4;
      txMsg.buf[6] = 0x7E;
      txMsg.buf[7] = 0x37;
    }
    
    void loop() {
      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: ");
        for ( uint8_t i = 0; i < 8; i++ ) {
          Serial.print(msg.buf[i]); Serial.print(" ");
        }
        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: ");
        for ( uint8_t i = 0; i < 8; i++ ) {
          Serial.print(msg.buf[i]); Serial.print(" ");
        }
        Serial.print("  TS: "); Serial.println(msg.timestamp);
      }
      if (Serial.available())
      {
        int c = Serial.read();
        if (c == 'c') can1.write(txMsg);
        if (c == 'd') can2.write(txMsg);
      }
    }
    So, basically you can send c or d and it sends out of the CAN buses to the other one. This works. But, here are the oddities:

    1. If I send more than 8 letters in a row then only 8 messages actually show up as received. For instance. cccc works and 4 messages are received. cccccccccc will show that 8-9 messages were received but not 10. I'm using the Serial Monitor in the IDE so when I send this traffic it gets sent all at once. The question is, why can't it successfully send and receive 10, 12, 20 messages? It reliably stops after either 8 or 9.
    2. If I mix the two commands so that messages are sent from both buses then it almost always quits responding. cd might work but cdcd will just make the whole sketch quit working.

    I did try playing with the optimization setting a bit; didn't help. It seems like a weird result but I wonder if the version in TD1.53 is old or if I'm doing something wrong? I really never did use Teensy boards very much so maybe there's something obvious I'm doing that's dumb but I don't see it. This example sketch is really very basic.

  25. #425
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,515
    what does mailboxStatus() show? if it assigned 8 transmit mailboxes that probably explains why. I forgot what the default is (hopefully 16?) where 4 are STD, 4 EXT, and 8 TX. If so you can increase the mailboxes (up to 64). On flexcan, if the last RX mailbox gets full (all of them), the overflow happens on the last mailbox. If you have 4 standard RX like i mentioned, writing 12 transmits will fill all 4 and the 4th would overflow constantly with the last received updated frame. Thats why you should read em so they can be available to fill again. I would suggest to try interrupt mode instead of polling if frame loss happens if not read fast enough. remember you have 8 transmitting with 4 receiving (thats if i assume you're mailbox limit is 16). use setMaxMB(64) if you want more reception mailboxes, 16 will be automatically set as Standard, 16 for Extended, and 32 transmits. You may reconfigure them later using setMB() if you want to assign X amount of STD,EXT,or TX of your own choice

    I use all 3 CAN channels in interrupt mode in my setup, TeensyCAN uses interrupt mode as well last i tested 1 meg payload transferred without loss, crc validated too

Posting Permissions

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