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

Thread: Flexcan_T4 on Teensy 3.2?

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    15

    Flexcan_T4 on Teensy 3.2?

    Hi All,

    I did a search but couldn't find anything...

    I'm using Flexcan_T4 on a Teensy 4.1. It works great! I'd also like to use it with a Teensy 3.2. However, the Teensy just crashes. No output to the serial console at all from the below code. I'm using the standard example from the library:

    Code:
    #include <FlexCAN_T4.h>
    FlexCAN_T4<CAN1, 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();
      Serial.println("Starting");
    }
    
    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();
    
      static uint32_t timeout = millis();
      if ( millis() - timeout > 200 ) {
        CAN_message_t msg;
        msg.id = random(0x1,0x7FE);
        for ( uint8_t i = 0; i < 8; i++ ) msg.buf[i] = i + 1;
        Can0.write(msg);
        timeout = millis();
      }
    }
    According to the github page on the library, it should work with Teensy 3.x. Has this been tested?

    Any hints?

    Thanks!

    Andrew

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,011
    IIRC, the pins are different between Teensy 3.x and 4.x. On Teensy 4.x the first CAN device is on pins 22 (TX) and 23 (RX). On Teensy 3.x the first CAN device is on pins 3 (TX) and 4 (RX).

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    15
    Aha. Does the lib not adapt to the Teensy being used then? So automatically selecting 3 and 4 if it's being run on a 3.2? Sounds plausible...

  4. #4
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,661
    use CAN0 in constructor for Teensy 3.2/3.5
    Teensy 3.6 has CAN0 and CAN1....
    Last edited by tonton81; 03-02-2021 at 10:53 PM.

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,661
    when I added Collin's Object Oriented CAN callbacks to FlexCAN_T4, I also included compiler errors which you would have seen:

    Code:
    #if defined(__MK20DX256__) || defined(__MK64FX512__)
      static_assert(_bus == CAN0, "Only CAN0 works on Teensy 3.2/3.5");
    #endif
    #if defined(__MK66FX1M0__)
      static_assert(_bus == CAN0 || _bus == CAN1, "Only CAN0 & CAN1 works on Teensy 3.6");
    #endif
    #if defined(__IMXRT1062__)
      static_assert(_bus == CAN1 || _bus == CAN2 || _bus == CAN3, "Only CAN1 & CAN2 & CAN3 works on Teensy 4.0/4.1");
    #endif
    I believe the latest teensyduino has it already

  6. #6
    Junior Member
    Join Date
    Feb 2019
    Posts
    15
    PERFECT!

    Silly mistake/bad assumption on my part. THANK YOU all so much... I should post more - I'd solve my problems much faster :-)

    I'll post some stuff on what I'm up to for people's interest. My second custom Teensy - a customisable set of CAN bus modules for vehicle electronics.

    Thanks,

    Andrew

Posting Permissions

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