Teensy 4.1 hangs at CanBus.begin() with FlexCAN_T4

lucas14145

New member
Hello,

I am having an issue where my Teensy 4.1 hangs when CanBus.begin() is called from the FlexCAN_T4 library. The loop() function is never reached.

I have performed extensive debugging:

  • The issue occurs on two different Teensy 4.1 boards.
  • The issue occurs on two different computers, one with a completely fresh install of the latest Arduino IDE and Teensyduino.
  • The issue occurs with a minimal internal loopback test sketch, with no external hardware connected.
  • The issue persists with a polling-based loopback sketch, removing interrupts as a variable.
Here is the simplest code that demonstrates the hang. The serial monitor only ever prints "Teensy 4.1 - Simplified Internal Loopback Test".
Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> CanBus;

void setup() {
  Serial.begin(115200);
  while (!Serial) { }
  delay(1000);
  Serial.println("Teensy 4.1 - Simplified Internal Loopback Test");

  CanBus.setBaudRate(500000);
  CanBus.enableLoopBack(true);
 
  CanBus.begin(); // Program hangs here
}

void loop() {
  Serial.println("This message is never printed.");
  delay(2000);
}

Given that this fails across multiple boards and multiple computers, this seems to be a potential bug in the core library or Teensyduino core for the T4.1. I have also tried different USB cables.

Any ideas would be greatly appreciated.
Lucas
 
Hello everyone,

I have found the solution to my problem.

The issue was the initialization order. For the FlexCAN_T4 library, it is critical to call CanBus.begin() before calling other configuration functions like setBaudRate() or enableLoopBack().

This code fails (hangs at begin()):
Code:
// FAILS
CanBus.setBaudRate(500000);
CanBus.enableLoopBack(true);
CanBus.begin();

This code works perfectly:
Code:
// WORKS
CanBus.begin();
CanBus.setBaudRate(500000);
CanBus.enableLoopBack(true);


Thank you for this great library!
 
Back
Top