Forum Rule: Always post complete source code & details to reproduce any issue!
Page 4 of 4 FirstFirst ... 2 3 4
Results 76 to 87 of 87

Thread: FlexCAN_T4 - FlexCAN for Teensy 4

  1. #76
    Junior Member
    Join Date
    Nov 2019
    Posts
    1
    @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();
    }
    Click image for larger version. 

Name:	20191122_144838-min.jpg 
Views:	12 
Size:	165.3 KB 
ID:	18245

  2. #77
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,133
    Try enableFIFOInterrupt()?

    MB version will ignore your request because MB0 doesnt exist in FIFO mode

  3. #78
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Hi tonton81,
    there is an unexpected behaviour if I want to send a message with the CanFD Object with a edl value of 0.
    Even if i set the bit to 0 in the CANFD_message_t on the sender side the bit is always 1 on the receiver side.

    I traced that behaviour down to the function FCTPFD_OPT::writeTxMailbox in File FlexCAN_T4FD.tpp line 257.

    Adding an extra if statement resolves that issue:
    Code:
    FCTPFD_FUNC void FCTPFD_OPT::writeTxMailbox(uint8_t mb_num, const CANFD_message_t &frame) {
      CANFD_message_t msg = frame;
      writeIFLAGBit(mb_num);
      uint8_t mbsize = 0;
      uint32_t code = 0;
      volatile uint32_t *mbxAddr = &(*(volatile uint32_t*)(mailbox_offset(mb_num, mbsize)));
      mbxAddr[0] = FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_INACTIVE);
      mbxAddr[1] = (( msg.flags.extended ) ? ( msg.id & FLEXCAN_MB_ID_EXT_MASK ) : FLEXCAN_MB_ID_IDSTD(msg.id));
      if ( msg.flags.extended ) code |= (3UL << 21);
      for ( uint8_t i = 0; i < (mbsize >> 2); i++ ) mbxAddr[2 + i] = (msg.buf[0 + i * 4] << 24) | (msg.buf[1 + i * 4] << 16) | (msg.buf[2 + i * 4] << 8) | msg.buf[3 + i * 4];
      if ( msg.len > mbsize ) msg.len = mbsize;
      code |= len_to_dlc(msg.len) << 16;
      if ( msg.brs ) code |= (1UL << 30); // BRS
      /* Editded: */if ( msg.edl ) code |= (1UL << 31); // EDL
      mbxAddr[0] = code | FLEXCAN_MB_CS_CODE(FLEXCAN_MB_CODE_TX_ONCE);
    }
    Cheers,
    Daniel Stümke

  4. #79
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,133
    Thanks for spotting that
    Updated the fix on github:
    https://github.com/tonton81/FlexCAN_...9f5d068575cd1a

    ::events() now updated as well to return 12 bits each for TX/RX queue sizes. (used by TeensyCAN)

  5. #80
    Junior Member
    Join Date
    Nov 2019
    Posts
    2
    Does FD work reliably on the T4 with this library?
    I'm considering doing a custom PCB for work using a T4 and a MCP2542FD transceiver chip.

    I have no idea how to route out CAN3 from the pads below the T4 yet though.
    It's a shame they chose to hide the single most interesting feature of the T4 on the bottom and only comfortably route out the lame CAN2.0 channels.

  6. #81
    You can use SMD connector like this one:
    https://www.digikey.co.uk/product-de...100/WM17459-ND

    Soldering the connector on can be quite tricky.

  7. #82
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,133
    Define reliably? TeensyCAN is using the FD library to distribute 640 byte data with 64byte frames in about 1.5ms constistantly for days. If there is any issues in the FD library I usually respond to fix it within the same day, as dastit03 pointed out, a minor bug for transmitting CAN2 frames on a FD bus was updated few hours after the post

    I don't only look at the reliability, but the multiple features not available on any other library for flexcan. One example is, aside from the SDK not supporting both memory regions, and using an exponential search algorythm of divisions to find dynamically placed mailboxes in memory block 0 only, FlexCAN_T4 allows both memory accesses with direct mailbox access without any use of divisions. Features like automatic filtering and distributions you will not find on any libraries (except IFCT), and up to 3 raw data outputs for 3rd party libraries (TeensyCAN uses one). That and the advanced bitrate selection in CANFD mode allows even the novice users to pick the exact timings parameters based on a list printout to fine-tune for an error-free bus based on sampling points of different busses. The TDC values for tranceiver are fully automated based on the timings results and done without user intervention/requests. It has also, on CAN2.0 side, gave backwards compatibility for Teensy 3.x, and in a way, an upgraded more performance, replacement for IFCT.

    Useful additions are always considered and implemented, and bugs, even though stable and without at the moment, are fixed promptly.
    I agree the pads are not very user friendly but you have to understand that the focus was based on form factor and backwards compatibility as much as possible, with teensy 3.2 boards. It may change in the future T4.x but the form factor will still be maybe like a T3.5/6 but as I said backwards compatibility is always a factor in the design decision, and not all pins offer a MUX to the peripheral we want, so certain placements can hinder functionalities accross backwards compatibility.

  8. #83
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Hello,
    I need to set the speed of CANBus at an unusual speed of 307.2kbps.
    How can I make this possible? I didn't try (because I don't have the Teensy 4 yet), but reading the library source code seems to refuse such speed.
    Can someone help me?
    Thanks

  9. #84
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,133
    CAN2.0 or FD mode?
    If CAN2.0 you can use 307200 for the speed

  10. #85
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Quote Originally Posted by tonton81 View Post
    CAN2.0 or FD mode?
    If CAN2.0 you can use 307200 for the speed
    I've to use CAN 2.0.
    Have I use a specific clock? How can the 307200 speed pass the bestError limits (492 bestError achieved is more than 300 admitted) on setBaudRate function with a clock of 24MHz (the default)?
    There is also an error in the code at line 455: the error value can be negative, there is no check like on line 465.

  11. #86
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,133
    The clock can be changed using setClock, default for Teensy 3.x is 16MHz, you can try up to 60MHz on T4

    If there is an error in the setBaudRate function it's been there since the original teachop/pawelsky's FlexCAN_Library forks, till here, but on errors the function usually exits. I havn't rewritten it from scratch but I did combine it to be more clean and centralized in the function itself. Try a different flexcan clock and see if your error rates are reduced, because your baudrate isn't common it may need specific clock
    Last edited by tonton81; 12-05-2019 at 05:40 PM.

  12. #87
    Junior Member
    Join Date
    Dec 2019
    Posts
    3
    Quote Originally Posted by tonton81 View Post
    The clock can be changed using setClock, default for Teensy 3.x is 16MHz, you can try up to 60MHz on T4

    If there is an error in the setBaudRate function it's been there since the original teachop/pawelsky's FlexCAN_Library forks, till here, but on errors the function usually exits. I havn't rewritten it from scratch but I did combine it to be more clean and centralized in the function itself. Try a different flexcan clock and see if your error rates are reduced, because your baudrate isn't common it may need specific clock
    I tried with all 3 possible clock sources and all 64 dividers, I can't reach the speed with an error below 300.
    The unusual speed I think is related to the usage of power of 2 oscillators, like 3.072 MHz, 30.72 MHz or 307.2 MHz.

Posting Permissions

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