PlatformIO Community

How to debug on Arduino mega 2560

Hello, I have just installed Platformio and vscode. It’s really nice and thank you to all who have conceived and worked on this, it’s a really nice environment. I am trying to get the debug feature working and although I have been programming arduino to drive stepper motors and worked with various sensors etc, I have no real experience of debugging in the arduino context. Previiously I used the arduino IDE for development work and had a brief foray into Visual Micro and its debug environment.

Having read a few of the debugging tutorials and tried to debug a simple sketch in vscode, I have a few questions - thanks very much for help with this:

What are the key steps I need to undertake before attempting to debug? (e.g. install/ configure something)
Can I use the same serial port as I use to upload code to the arduino or do I need to use a different hardware serial?
When I run the debug feature, I get an error dialog “The preLaunchTask ‘undefined’ terminated with exit code 1.” What des this mean please?
Do I need to download any debug environment for the mega2560?

Thanks very much,
Paul Kirk

The main documentation for the avr-stub debugging tool, which is the only real-hw debugging tool implemented right now, is at https://docs.platformio.org/en/latest/plus/debug-tools/avr-stub.html.

PlatformIO will install everything as needed, and as instructed per the platformio.ini. Configuration of that can be seen above.

Per docs

  • Any part of your application that uses the UART module (e.g. Arduino Serial class) cannot be used in your program together with the debugger.

So you need to move your normal Serial output to another hardware serial (Serial2?) on the Mega.

As a start I’d suggest you debug just a blink sketch to verify the setup.

So a platformio.ini of

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino

debug_tool = avr-stub
debug_port = SERIAL_PORT

; GDB stub implementation
lib_deps =
    jdolinay/avr-debugger @ ~1.1

Whereas you have to replace SERIAL_PORT with the actual serial port name of the Mega on your computer, e.g. COM3. (see Windows device manager).

EDIT: In my test I had to say debug_port = \\.\COM14 when using COM14 so that GDB is happy.

And as code / src\main.cpp

#include "Arduino.h"
#include "avr8-stub.h"

void setup()
{
  // initialize GDB stub
  debug_init();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

Works on my Uno with a similiar platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
debug_tool = avr-stub
debug_port = \\.\COM14
lib_deps =
    jdolinay/avr-debugger @ ~1.1

grafik

thanks very much for a comprehensive reply - much appreciated. I’ll give it a go.
Regards,
Paul

yes worked fine,
thanks for help,
Paul

1 Like

Do you need to install anything additionally? (GDB?)
It’s not working for me - I’m sure I’m doing something wrong, but can’t see what it is.
Marc

No, not specifically. If you can upload to the board (via serial), everything should be fine.

Are testing with the exact code and platformio.ini above? Have you adadpted the COM port number? What’s the error message you’re getting?

Failed to launch GDB: Error: write EPIPE

Have a blinky project with debug_init() in setup()
Compiles and uploads fine.

And my platformio.ini

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
upload_port = COM8

debug_tool = avr-stub
debug_port = COM8

lib_deps = 
    jdolinay/avr-debugger @ ~1.1

PIO 2.2.1
VSCode 1.52.1
Win10

Have you tried replacing that with \\.\COM8 per edit above?

Yes - no change…
(20-chars)

In the debug console tab there should be more output than that – an you post it?

Could not start debugger process > Error: spawn platformio ENOENT

Could it be a %PATH% problem?

Is there no more output? There should be something like

Reading symbols from C:\Users\Max\Documents\PlatformIO\Projects\attiny\.pio\build\uno\firmware.elf...
done.
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target...
0x0000033a in micros () at C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino/wiring.c:103

and possible error messages.

My example still works fine for me, so I can’t reproduce the error you’re getting.

grafik

You could also try the suggestion in Cannot debug STM32: "Failed to launch GDB: Error: write EPIPE" - #4 by maxgerhardt or blatantly re-installing PlatformIO (remove all system-installed PlatformIO cores ever installed, especially if done globally via pip, remove C:\Users\<user>\.platformio\ completely, remove VSCode PlatformIO extension, then reinstall it and retry).

Also sometimes antivirus software can interfer with it, you might want to temporarily disable it.

No, there is nothing more in the debug tab…

I de-installed platformIO and removed the .platformio user folder.
Reinstalled the extension in vscode and now its broken…

Platform Manager: atmelavr @ 3.1.0 is already installed
Error: Unknown development platform ‘PackageItem <path=C:\Users\marc.platformio\platforms\atmelavr metadata=PackageMetaData <type=platform name=atmelavr version=3.1.0 spec={‘owner’: ‘platformio’, ‘id’: 8006, ‘name’: ‘atmelavr’, ‘requirements’: None, ‘url’: None}’

Remove the VSCode PlatformIO extension, then close VSCode, then remove the .platformio again fully, then reopen VSCode, then reinstall the PlatformIO extension.

Nope same problem. I even deleted C:\Users\<user>\.vscode\extensions\platformio.platformio-ide-2.2.1

I also see Unable to resolve configuration with compilerPath "C:/Users/<user>/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe". Using "cl.exe" instead.

The toolchain folder does not exist…

Reboot before reinstall fixed it. Pfew!

Debugging still not working.
image

In the upper left corner I can see that you’ve selected “PIO Debug (sk…”, presumably skip-predebug. Can you click on that config and make sure that you’ve select the right configuration for the right project, and also the normal variant, not the skip-predebug variant?

Yeah - I’ve tried them both - made a screen shot at the end with the one I’ve tried last.
The launch.json file is unchanged from how it was generated for the project.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug",
            "executable": "c:/Users/marc/Documents/PlatformIO/Projects/blinky/.pio/build/megaatmega2560/firmware.elf",
            "projectEnvName": "megaatmega2560",
            "toolchainBinDir": "C:/Users/marc/.platformio/packages/toolchain-atmelavr/bin",
            "preLaunchTask": {
                "type": "PlatformIO",
                "task": "Pre-Debug"
            },
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "type": "platformio-debug",
            "request": "launch",
            "name": "PIO Debug (skip Pre-Debug)",
            "executable": "c:/Users/marc/Documents/PlatformIO/Projects/blinky/.pio/build/megaatmega2560/firmware.elf",
            "projectEnvName": "megaatmega2560",
            "toolchainBinDir": "C:/Users/marc/.platformio/packages/toolchain-atmelavr/bin",
            "internalConsoleOptions": "openOnSessionStart"
        }
    ]
}

Are you still able to build the firmware and upload normally?

Yeah - build and upload both are working (again)