How to debug on Arduino mega 2560

And downgrading the PlatformIO extension to version 2.1.0 or 2.0.1 in VSCode isn’t working as well?

I get the same PackageItem problems when trying to install a previous version.
I don’t think this is going anywhere…
Clearly the install/uninstall logic is not quite there yet.
I guess I have to live without debugging a while longer…

Thanks for your help - appreciate it.

Okay then the previous VSCode extension is just not compatible with the current core. In previous topics it helped to downgrade that. Sadly you’ll have to do a reinstall again :sweat_smile:.

Please file an issue at Issues · platformio/platformio-vscode-ide · GitHub when using the newest extension + core version – this should work out of the box as I’ve shown, and I have no idea why that’s not working for you.

Hi,

For me, debugging the “blink” example is not working neither. I get the following in the debug console after launching a debug session:

avrdude done.  Thank you.

================= [SUCCESS] Took 3.77 seconds =========================

Reading symbols from c:\Users\...\PIO Test2\.pio\build\megaatmega2560\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target...
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
2
Ignoring packet error, continuing...
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout

I think, I did all the config in platformio.ini correct. Uploading and executing works fine. Any idea what I could try to make it work?

Thanx,
Marco

Show the full platformio.ini and code so that we can judge that.

Hi,
Thank you for the quick reply!
Please find the project files below.

On avr-stub.html / [Debugger limitations] is written:

  • One external interrupt pin must be reserved for the debugger

What does that mean? My board is a Industrial Shields M-Duino 58+ with Arduino Mega 2560 Board / ATmega2560 MCU. It is connected through USB virtual COM-Port (COM4) with the PC. No other connections.

Project files:

platformio.ini:

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

debug_tool = avr-stub
debug_port = COM4        ;also tried\\.\COM4

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

main.cpp:

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

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


void loop() {
  //breakpoint(); //also tried
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

whole output from DEBUG CONSOLE:

Processing megaatmega2560 (platform: atmelavr; board: megaatmega2560; framework: arduino)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/megaatmega2560.html
PLATFORM: Atmel AVR (4.0.0) > Arduino Mega or Mega 2560 ATmega2560 (Mega 2560)
HARDWARE: ATMEGA2560 16MHz, 8KB RAM, 248KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES: 
 - framework-arduino-avr @ 5.1.0 
 - tool-avrdude @ 1.60300.200527 (6.3.0) 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- avr-debugger @ 1.4.0
Building in debug mode
Compiling .pio\build\megaatmega2560\lib782\avr-debugger\app_api.c.o
Compiling .pio\build\megaatmega2560\lib782\avr-debugger\avr8-stub.c.o
Archiving .pio\build\megaatmega2560\lib782\libavr-debugger.a
Linking .pio\build\megaatmega2560\firmware.elf
Checking size .pio\build\megaatmega2560\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   4.1% (used 339 bytes from 8192 bytes)
Flash: [          ]   2.7% (used 6840 bytes from 253952 bytes)
Building .pio\build\megaatmega2560\firmware.hex
Configuring upload protocol...
AVAILABLE: wiring
CURRENT: upload_protocol = wiring
Looking for upload port...
Auto-detected: COM4
Uploading .pio\build\megaatmega2560\firmware.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file ".pio\build\megaatmega2560\firmware.hex"
avrdude: writing flash (6840 bytes):

Writing | ################################################## | 100% 1.10s

avrdude: 6840 bytes of flash written
avrdude: verifying flash memory against .pio\build\megaatmega2560\firmware.hex:
avrdude: load data flash data from input file .pio\build\megaatmega2560\firmware.hex:
avrdude: input file .pio\build\megaatmega2560\firmware.hex contains 6840 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.88s

avrdude: verifying ...
avrdude: 6840 bytes of flash verified

avrdude done.  Thank you.

========================= [SUCCESS] Took 4.01 seconds =========================
Reading symbols from c:\Users\marco\Documents\PlatformIO\Projects\PIO Test5\.pio\build\megaatmega2560\firmware.elf...
done.
PlatformIO Unified Debugger -> https://bit.ly/pio-debug
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target...
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
2
Ignoring packet error, continuing...
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout

Can you try adding

build_flags =  -DAVR8_BREAKPOINT_MODE=2

or

build_flags =  -DAVR8_BREAKPOINT_MODE=1

to the platformio.ini and retry?

sorry no. same error messages.

there is also a VS Code Error:

I see. I will have the hardware tomorrow to cross-check.

I have the hardware now but I can’t reproduce your problem. Both with breakpoint() and without it, I get a proper debug connection, with your original platformio.ini (except debug_port changed of course to \\.\COM21 for me).

Do you get regular serial output from the Mega with e.g. the code

#include "Arduino.h"

void setup() { Serial.begin(9600); }

void loop() { Serial.println("Test"); delay(500); }

and the Upload + Monitor task?

Hi,
thank you so much for your engagement!

Yes, the serial output works fine, also input, I tried with a “echo” code.

I re-installed the Arduino Mega 2560 USB to COM driver. It seems to work correctly

Also run VS Code from the Admin account.
Always the same result.

Seems to be a tricky case…

Can you open a CLI and execute pio upgrade --dev and pio platform update atmelavr and retry?

Still the same…
I rolled back my computer to before installing vs code & platformio and did everything again from scratch.

Ok:

PS C:\Users\Administrator> pio upgrade --dev
Please wait while upgrading PlatformIO ...
PlatformIO has been successfully upgraded to 6.1.0b1
Release notes: https://docs.platformio.org/en/latest/history.html

But: (I have a Intel Core I7 , not AMD)

PS C:\Users\Administrator> pio platform update atmelavr
WARNING: This command is deprecated and will be removed in the next releases.
Please use `pio pkg update` instead.
Error: Could not find the package with 'atmelavr' requirements for your system 'windows_amd64'

pio pkg update atmelavr does not work (?)

Ah, it should be pio pkg update -g -p atmelavr.

gives:

PS C:\Users\Administrator\Documents\PlatformIO\Projects\PIO Test7> pio pkg update -g -p atmelavr
Platform Manager: atmelavr@4.0.0 is already up-to-date
Tool Manager: framework-arduino-avr@5.1.0 is already up-to-date
Tool Manager: tool-avrdude@1.60300.200527 is already up-to-date
Tool Manager: toolchain-atmelavr@1.70300.191015 is already up-to-date

May there be something wrong with the COM settings? Is there a config somewhere?
On the other hand, uploading the firmware works, serial example too, so communication seems ok

I will try on an other machine and let you know next week.

Have a nice weekend & thanks again!

Hi,

I set up the toolchain on an other machine but with the same results, unfortunately.
I ordered a Arduino Mega 2560 board to crosscheck, maybe there is something wrong with the M-Duino, it should arrive tomorrow.

Meanhwhile I modified the loop to

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  bk++;
  if (bk == 5) breakpoint();
}

what makes the LED flash 5 times and then stop. So the breakpoint() seems to work

I read something about a OCDEN fuse that might be set to use the onchip-debugging (here: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-ICE_UserGuide.pdf)
Is this something to care about? How to read/set this fuses? When uploading, I see this:
avrdude: safemode: Fuses OK (E:FD, H:D8, L:FF).

Ok, the debugging works with a new out-of-the-box Arduino Mega2560 R3!
There must be an issue with the one in the Industrialshields M-Duino 58+.
I contacted the vendor to figure this out.

The Mega I bought required 0 fuse modifications – got the Elegoo one, connected it up with the delivered USB cable and used your above code and platformio.ini and it worked straight away. Really strange, everything looked okay code-wise, but you seem to have found out it was about hardware…