How to use Teensy 4.1 Debug Mod with Platform.io

I purchased and installed the Teensy 4.1 Debug PCB on a Teensy 4.1 and connected it to a Teensy 4.1 Debug Adapter both designed by SpenceV1

When I plug the modded Teensy 4.1 into my PC via the micro USB cable, I get the 2 blink error code which means "NXP JTAG Not Responding" (link). I think this is the expected behavior since the JTAG lines are cut to install the mod.

I connect the Debug Adapter to a JLINK debugger. Using the JLINK Commander terminal I am able to connect to the Teensy 4.1, so I think my connections are all good.

What are the next step(s) to use this setup to debug code inside Platform.io? I am specifically looking forward to being able to set breakpoints and examine variables and/or registers.

Related Thread: Debug Mod Project
 
I have tried both PlatformIO using the config posted by sp33 and elphil as well as trying to use Cortex-Debug.

I have been unable to consistently step through code or hit breakpoints. When doing a simple blinky sketch the debugger jumps around to seemingly "random" memory locations in random files like HardwareSerial libraries when I am not using any serial ports, just a simple blink sketch.

I have tried using MCUXpresso and debugging seems to work fine there, so I don't think it's a hardware problem. However, I prefer using VS Code (or Cursor) for editing files.

Any more thoughts or ideas?
 
I have tried both PlatformIO using the config posted by sp33 and elphil as well as trying to use Cortex-Debug.

I have been unable to consistently step through code or hit breakpoints. When doing a simple blinky sketch the debugger jumps around to seemingly "random" memory locations in random files like HardwareSerial libraries when I am not using any serial ports, just a simple blink sketch.

I have tried using MCUXpresso and debugging seems to work fine there, so I don't think it's a hardware problem. However, I prefer using VS Code (or Cursor) for editing files.

Any more thoughts or ideas?
Yes, this is an indication that you are not building using "debug" optimization. Code is being optimized out which causes the debugger to land on lines which it shouldn't. Try this.

Use elphil's config here
https://community.platformio.org/t/teensy-4-1-uploading-with-jlink/18423/23
add this to the end
Code:
build_type = debug
This will build your code without optimizations.
If you have a need to switch between debug or release code you can do something like this.
Code:
[env:release]
build_type = release

[env:debug]
build_type = debug

Let me know how it goes
 
First a quick note. One must install the tool-jlink at least once by putting the following line in the platformio.ini file. (I tried this on a different PC and had to set it up again).
Code:
platform_packages = platformio/tool-jlink@^1.81206.0

Still having issues. It looks like the code is being optimized. I put a breakpoint on line 20. When debugging it stops on the next line and when I hover over result it says optimized out.
1759427798792.png


It does seem like I can often step over the delay and digitalWrite functions, however often I get pulled into random methods like HardwareSerial and I can't seem to get back to the main loop even when I click "continue". Do you have any other ideas or feedback?

A side question, should I be able to step into and out of Arduino functions like digitalWrite()?

C++:
#include <Arduino.h>

// put function declarations here:
int myFunction(int, int);

void setup() {
  // put your setup code here, to run once:
  int result = myFunction(2, 3);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop() {
  static uint loopCounter = 0;
  // put your main code here, to run repeatedly:
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  int result = myFunction(2, 5);
  loopCounter++;
}

// put function definitions here:
int myFunction(int x, int y) {
  return x + y;
}

INI:
[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
lib_extra_dirs = ~/Documents/Arduino/libraries
upload_protocol = custom
upload_flags =
   -device
   MIMXRT1062xxx6A
   -speed
   4000
   -if
   jtag
   -jtagconf
   -1,-1
   -autoconnect
   1
   -NoGui
   1
   -CommanderScript
   "upload.jlink"
upload_command = $PROJECT_PACKAGES_DIR\tool-jlink\JLink.exe $UPLOAD_FLAGS
;debug_tool = custom
debug_tool = jlink
debug_server =
   ${platformio.packages_dir}\tool-jlink\JLinkGDBServerCL.exe ; edited as suggested by Ivan
   -singlerun
   -if
   jtag
   -select
   USB
   -device
   MIMXRT1062xxx6A
   -port
   2331
   -speed
   4000
debug_port = localhost:2331
build_type = debug
 
Back
Top