Build_type = debug with Wire breaks programm on Teensy4.0

After a long investigation if found that, when I use ‘build_type = debug’ in Platformio.ini and only just include Wire.h, the compilation and programming works but the processor freezes.

As Teensy to my knowledge does not support debugging, this is i think an edge case, and would only happen if somebody has a cross-platform project where build type is set for all configurations.

Is is useful to have set build_type to debug if you are not running a debugger? i think i have set this for ESP32 to get detailed crash reports, but I’m not sure anymore

What’s the platformio.ini? Are you using the stable 1.57 Teensyduino core or a 1.58 beta version?

HI max,

here the info:

PLATFORM: Teensy (4.17.0) > Teensy 4.0 
 - framework-arduinoteensy @ 1.157.220801 (1.57)
 - tool-teensy @ 1.157.0 (1.57)
 - toolchain-gccarmnoneeabi @ 1.50401.190816 (5.4.1)


framework = arduino
monitor_speed = 115200
build_type = debug ; DO NOT USE WITH TEENSY - if I2C is used -> code breaks

; --------------------------- PLATFORM SETTINGS ---------------------------------

; basic configuration
build_flags = 

; platform Teensy config
[cfg_teensy] ;
lib_deps =
	WDT_T4= ;
lib_ignore = jSdFat
build_flags = ${dev_config.build_flags}

platform = teensy
board = teensy40 ;
lib_deps = ${cfg_teensy.lib_deps}
lib_ignore = ${cfg_teensy.lib_ignore}
build_flags = ${cfg_teensy.build_flags}

This code reproduces the problem

#include "Wire.h"

void setup() {

void loop() {

Too many variables for me.

When you change the platformio.ini to just

platform = teensy
board = teensy40
framework = arduino
monitor_speed = 115200
build_type = debug

does it still fail with the code you posted above?

yes. right after programming, the Serial Connection disappears and the MCU seems to be frozen

Do you have any external hardware connected to the teensy? If yes, disconnect it and retry.

some. i only have a bare Teensy connected to a laptop.
Its a very distinct behaviour: enable debug mode → teensy does not work.

i can try another laptop if needed, but I’m pretty sure its not something only happening on my mashine

And when you remove this line it works in both debug + release mode?

yes, it works if i remove Wire.begin()

Wire.begin() causing a crash was already reported in Wire library and Serail1 conflict on Teensy 4.0 · Issue #565 · PaulStoffregen/cores · GitHub but that was in conjuction with Serial1 not (USB) Serial. Was fixed in a “fix startup” commit.

Most recently there again was a “Fix startup” commit. Let me check if I can’t get the very last git version in PlatformIO…

top thx!
its not time critical for me, as i don’t need to use debug mode on teensy, but would be cool to have this fixed to avoid others running into this

I dont know if that fix is about the same thing, but lets see.

I found this bug with a code where it was enough to just reference Wire without calling begin. i have a I2C Wrapper for Wire like this:

class I2CWrapper
TwoWire* _i2c;
I2CWrapper(TwoWire* _thisi2c)
_i2c = _thisi2c;

I2CWrapper t(&Wire);

but i can not reproduce this with a short code sniped.

also why would the debug/release switch make a difference if it is a code bug?

do you know what debug / release actually changes ? is it compiler flags ?

Exactly, usually -Os optimization vs -Og or -O0 optimization.

You should also be able to reproduce this problem by building in “release” mode but changing the flags accodingly. Can you test

platform = teensy
board = teensy40
framework = arduino
monitor_speed = 115200
build_unflags = -Os
build_flags = -Og

then replace -Og with -O0, -O1, -O2, -O3, -Ofast and report which ones fail and which don’t.

okay thx.

but unfortunatly only changing the compiler flags does not produce the same error. it that really the only thing build type debug does ?

this is what i tested:


platform = teensy

board = teensy40

framework = arduino

monitor_speed = 115200

;build_type = debug

build_unflags = -Os

build_flags = -Og

code breaks if i remove the ;

i can confirm that this problem also happens on another mashine with a fresh installation of PIO. Same behaviour. only when build-type=debug is used, the COM port disapiert.