Flexcan_T4 on Teensy 3.2?

Status
Not open for further replies.

r5gordini

Member
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
 
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).
 
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...
 
use CAN0 in constructor for Teensy 3.2/3.5
Teensy 3.6 has CAN0 and CAN1....
 
Last edited:
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
 
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
 
Status
Not open for further replies.
Back
Top