Forum Rule: Always post complete source code & details to reproduce any issue!
Page 15 of 15 FirstFirst ... 5 13 14 15
Results 351 to 372 of 372

Thread: FlexCAN_T4 - FlexCAN for Teensy 4

  1. #351
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,416
    have you tried starting the I2C before and after flexcan begin?
    do you have a sketch?
    have you tried different I2C clockspeeds?

    did you try modifying the flexcan clock?
    Canx.setClock(CLK_60MHz) - check source for different values
    Last edited by tonton81; 08-11-2020 at 02:01 PM.

  2. #352
    Junior Member
    Join Date
    Apr 2020
    Posts
    17
    Ok, so tried to move the <Wire.h> and <FlexCAN_T4> include statements before and after each other. Tried beginning the wire library before and after the can libraries and tried all available Wire.setClock(); speeds including; 10000, 100000, 400000, 1000000 and 3400000. For everything that i changed, the only thing that displays the correct data is if i don't call the can read subroutines

  3. #353
    Junior Member
    Join Date
    Apr 2020
    Posts
    17
    And the can clock speeds are already set to 60MHZ.

    #include <FastTouch.h> // capacitive touch library
    #include <EEPROM.h> // eeprom access memory
    #include <FlexCAN_T4.h> // flexcan library for teensy
    #include <Wire.h> // I2c wire library
    //#include <ds3231.h> // RTC library
    #include <stdio.h> // 10 DOF GY-87
    #include <math.h> // 10 DOF GY-87
    #include "I2Cdev.h"
    #include "HMC5883L.h"
    #include <nRF24L01.h> // Radio comms library (MUST BE AFTER FLEXCAN)
    #include <RF24.h> // Radio comms library
    #include <SPI.h> // SPI comms library

    void setup()
    {
    Serial.begin(115200);
    Serial.println("5 inch NEXTION DISPLAY");

    Serial.println("I2C begin"); // begin i2c
    Wire.setSDA(18); // declare SDA pin
    Wire.setSCL(19); // declare SCL pin
    Wire.begin(); //start i2c (required for connection) to REAL TIME CLOCK
    Wire.setClock(400000);

    Serial.println("CAN2 begin"); // can2 begin
    Can0.begin();
    Can0.setBaudRate(500000); // can2 set baud
    Can0.setClock(CLK_60MHz); // can2 clock speed
    Can0.enableFIFO(); // can2 fifo
    // Can0.enableFIFOInterrupt(1);
    // Can0.mailboxStatus();

    Serial.println("CAN1 begin"); // can1 begin
    Can1.begin();
    Can1.setBaudRate(500000); // can 1 set baud 500kb/s
    Can1.setClock(CLK_60MHz); // can 1 clock speed
    Can1.enableFIFO(); // can 1 fifo
    // Can1.enableFIFOInterrupt(1);
    // Can1.mailboxStatus();

  4. #354
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,416
    which specific subroutine causes it to not work? (you said read one..?which)

  5. #355
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Hi tonton81,

    You were right, finally I have read through the Flexcan part of the IMXRT1062 manual, and it helped a lot to understand the process.
    https://www.pjrc.com/teensy/IMXRT1060RM_rev2.pdf

    Laszlo

  6. #356
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,416
    found the problem? I don't think the I2C has any influence on flexcan it should work fine, the triple CAN board from skpang uses an I2C display addon.

  7. #357
    Junior Member
    Join Date
    Apr 2020
    Posts
    17
    Hey tonton81,

    here is the loop and sub routines;
    if i comment out both CAN_READ(CANREP); and CAN1_READ(CANREP); so that can is not read by the subroutine at all, the i2c data is fine, as soon as can is read, the i2c data changes

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP //

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    void loop() {

    op_timer = micros();

    if ((op_timer - op_gy87) > 20)
    {
    GY87_READ_SENSORS(); // request I2C data from GY-87
    if(SHOW_GY87 == 1){GY87_DISPLAY_DATA();} //display GY-87 data on serial if enabled
    op_gy87 = micros();
    }

    CAN_READ(CANREP); // read Can2 (repeat CANREP number of times)
    CAN1_READ(CANREP); // read Can2 (repeat CANREP number of times)
    SERIAL_READ_RH(5); // read serial from right screen 5 times
    SERIAL_READ_LH(5); // read serial from left screen 5 times
    READ_TOUCH();



    LED_WARN();
    CONSUMPTION(); // updates consumption data from flow meter data

    if ((op_timer - op_nextion) > 1000)
    {
    UPDATE_NEXTION(); // update Nextion screens
    op_nextion = micros();
    }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // CAN2 READ CAN2 READ CAN2 READ CAN2 READ CAN2 READ CAN2 READ CAN2 READ CAN2 READ //

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    void CAN_READ(int rep) {
    //Serial.println();
    //Serial.println("READ CANBUS 2");
    for ( int i = 0 ; i < rep ; i++)
    {

    if (Can0.read(rxmsg))
    {
    len = rxmsg.len;
    CANRXID = rxmsg.id;

    if (SHOW_CAN == true)
    {
    Serial.println(); Serial.print("RAW ID> "); Serial.print(CANRXID); Serial.print(" RAW LEN> "); Serial.print(len);
    }

    CONVERT(); // switch case populate can data into variables and conversion calculations

    sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", CANRXID, len);
    if (SHOW_CAN == true) {
    Serial.print(msgString);
    }

    if (SHOW_CAN == true)
    {
    for (byte i = 0; i < len; i++)
    {
    sprintf(msgString, " 0x%.2X", rxmsg.buf[i]);
    Serial.print(msgString);
    int NUMb = rxmsg.buf[i];
    Serial.print(" ("); Serial.print(NUMb); Serial.print(")");
    }
    Serial.print(" MICROS "); Serial.print('\t'); Serial.println(micros());
    }
    }

    // Can0.mailboxStatus();
    }
    UPDATE_VARIABLES(); // update variable database
    delayMicroseconds(CANDELAY);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // CAN1 READ CAN1 READ CAN1 READ CAN1 READ CAN1 READ CAN1 READ CAN1 READ CAN1 READ //

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    void CAN1_READ(int rep) {
    // Serial.println("READ CANBUS 1");
    for ( int i = 0 ; i < rep ; i++)
    {

    if (Can1.read(rxmsg))
    {
    len = rxmsg.len;
    CANRXID = rxmsg.id;

    if (SHOW_CAN == true)
    {
    Serial.println(); Serial.print("RAW ID> "); Serial.print(CANRXID); Serial.print(" RAW LEN> "); Serial.print(len);
    }

    CONVERT(); // switch case populate can data into variables and conversion calculations

    sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", CANRXID, len);
    if (SHOW_CAN == true) {
    Serial.print(msgString);
    }


    if (SHOW_CAN == true)
    {
    for (byte i = 0; i < len; i++)
    {
    sprintf(msgString, " 0x%.2X", rxmsg.buf[i]);
    Serial.print(msgString);
    int NUMb = rxmsg.buf[i];
    Serial.print(" ("); Serial.print(NUMb); Serial.print(")");
    }
    Serial.print(" MICROS "); Serial.print('\t'); Serial.println(micros());
    }
    }

    // Can1.mailboxStatus();

    }

    delayMicroseconds(CANDELAY);
    UPDATE_VARIABLES(); // update variable database
    if (SHOW_DATA == true)
    {
    DISPLAYDATA();
    }
    }

  8. #358
    Junior Member
    Join Date
    Apr 2020
    Posts
    17
    the subroutine reads the canbus and sends the data to CONVERT(); a conversion routine which bitshifts, does some math and populates a data array for ecu values based on the canrx id. So the CAN side of things works fine but as soon as it is executed it corrupts the incoming data from I2C

  9. #359
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,416
    very odd, there is no trickery in the read (polling) function, and it doesn't play with timers, interrupts, or other hardware, just reads the memory of the mailbox, what happens if you comment out all those functions CAN_READ and just put a dummy Can0.read(msg) in the loop without handling it?

  10. #360
    Junior Member
    Join Date
    Apr 2020
    Posts
    17
    I'll give it a try and let you know

  11. #361
    Junior Member
    Join Date
    Feb 2020
    Posts
    6
    Just wanted to say thanks for this library. Was able to test all 3 can channels concurrently on T4.1. Test was listen only, for now, 3 separate buses.

    Can anyone confirm that can2 & can3 alternate pins are not available on the T4.1?

  12. #362
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,416
    the only difference between 4.0 and 4.1 is that CAN3 has been routed to pins 30 and 31 at edge of board, the other CAN1 and CAN2 have same regular and alternate pins. It's already updated in the library

  13. #363
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Hello,

    Can anybody help me to understand, how to use the Waveshare can transceiver SN65HVD230 chip? I want to close the canbus ends with 2pcs separated 120 ohm resistor, so I dont need resistor on the transceiver board. Can I turn it off somehow? There is a jumper on the board, but if I close it, it make a short between the canH and canL, what is not good I think (what is the function of this jumper?). I have removed the resistor from the board, and now I can measure 70ohm between canH and canL. I hoped there will be a brake between them. So now I am lost.

    Thanks

  14. #364
    Looking at pictures of that board, the "jumper" on the board is probably not meant to be a jumper. More likely its just a 2pin 0.1inch header for connecting to CAN_H/CAN_L. You can connect via this header and/or via the screw terminals. I have no guess as to why you are measuring 70ohms between CAN_H and CAN_L with no termination resistors connected.

  15. #365
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Ah, you are right. It must be a connector.
    I have sent the question to the Waveshare support.

  16. #366
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    Hi guys. I'm hoping someone can advise on why I can't seem to read the CAN stream on my car.

    I'm using a Teensy 4.0, latest FlexCAN_T4 and a SN65HVD230 CAN transceiver.

    I am using the basic barebones code below and using the wiring as shown in the image, unfortunately I am not getting and CAN id's dumped to the Serial monitor and I was wondering if I have done something wrong.

    Code:
    #include <FlexCAN_T4.h>
    
    FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> canBus;
    static CAN_message_t dashMsg;
    
    void setup()
    {
      // Init Serial
      Serial.begin(9600);
      // while (!Serial) ; // wait for Arduino Serial Monitor
      Serial.println("Teensy 4.0 CAN Example test");
    
      // Init CAN
      canBus.begin();
      Serial.println("begin");
      canBus.setBaudRate(1000000);
        Serial.println("setbaud");
    
      // Turn LED on
      pinMode(13, OUTPUT);
      digitalWrite(13, HIGH);
    }
    
    
    void loop()
    
    {
      // Get CAN Messages & Update values
      if (canBus.read(dashMsg) ) {
        // Flash onboard LED
        digitalWrite(13, LOW);
        Serial.println("read");
        // Get ID
        String id = String(dashMsg.id, HEX);
        Serial.println(id);
        // Flash onboard LED
        digitalWrite(13, HIGH);
      }
    }
    the board is wired as below
    Click image for larger version. 

Name:	IMG_20200911_151343_copy_800x600.jpg 
Views:	6 
Size:	144.7 KB 
ID:	21699


    Orange CanL
    Yellow CanH

    I've added a few print stages just so I could see where it is getting to and it appears to be failing when it gets to the "if (canBus.read(dashMsg) )" line.

    I've tried it on the other CAN ports on the Teensy, it's just on pin 14&15 (CAN3) for this last test. I was grasping at straws and tried 0,1 & 2 first.


    At this stage I'm lost and will try anything. If anyone has a sniffer code or anything else they suggest I try I'll happily give it a go !

  17. #367
    Senior Member
    Join Date
    Jan 2015
    Location
    UK
    Posts
    130
    How are you connecting it to your car ?

    Are you sure the baud rate is 1000000 on your car ?

  18. #368
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    Quote Originally Posted by skpang View Post
    How are you connecting it to your car ?

    Are you sure the baud rate is 1000000 on your car ?
    Apologies, I didn`t add that. I`m running DTA S80Pro standalone ECU, connected to the CanH & L wires.

    From DTA spec.
    "CAN bus Baud Rate 1 MBd
    Identifiers All 29Bit
    6 Data Packets All 8 Bytes Ea.
    Send Frequency 10 - 50Hz
    All Data Values 16 Bit Signed Sent LSB First (Little Endian)"

  19. #369
    Senior Member
    Join Date
    Jan 2015
    Location
    UK
    Posts
    130
    Pin 14&15 is not CAN3.

    Try and use
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;

    On pin

    CRX1 pin 23
    CTX1 pin 22.

  20. #370
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    Quote Originally Posted by skpang View Post
    Pin 14&15 is not CAN3.

    Try and use
    FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;

    On pin

    CRX1 pin 23
    CTX1 pin 22.
    OK, I tried that, replaced all the "canBus" references with can1 to match your line.

    That seems to read something ! I can't believe this all it was Thank you for the prompt and helpful reply.


    I may be back if I get stuck again

  21. #371
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    I have got the answer from the support, I can remove the 120 ohm resistance. I hope it will work. It would be very simple to make a jumper or dip-switch for this function.

  22. #372
    Senior Member
    Join Date
    Dec 2016
    Location
    Detroit, Michigan, USA
    Posts
    104
    Tonton,

    Man. This is glorious. Very easy to set up and use. My 4.1 is humming along using this library. Very well done!

Posting Permissions

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