Unable to debug ATMEGE 2560 project

Hello,

maybe anybody can help?

I tried to debugf my ARDUINO project, after a while compiling with Platformio worked.
After strating the Debugger it stops with a errormessage.

Processing megaatmega2560 (platform: atmelavr; board: megaatmega2560; framework: arduino)
Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: Redirecting...
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 → Library Dependency Finder (LDF) — PlatformIO latest documentation
      LDF Modes: Finder ~ chain, Compatibility ~ soft
      Found 10 compatible libraries
      Scanning dependencies…
      Dependency Graph
      |-- avr-debugger @ 1.5.0
      |-- Adafruit GFX Library @ 1.11.1
      | |-- SPI @ 1.0
      |-- Adafruit SSD1306 @ 2.5.3
      | |-- Adafruit GFX Library @ 1.11.1
      | | |-- SPI @ 1.0
      | |-- SPI @ 1.0
      | |-- Wire @ 1.0
      |-- PWMServo @ 2.1
      |-- SPI @ 1.0
      |-- Wire @ 1.0
      |-- EEPROM @ 2.0
      Building in debug mode
      Compiling .pio\build\megaatmega2560\lib46e\avr-debugger\app_api.c.o
      Compiling .pio\build\megaatmega2560\lib46e\avr-debugger\avr8-stub.c.o
      Compiling .pio\build\megaatmega2560\lib351\Adafruit_GFX_Library\Adafruit_GFX.cpp.o
      Archiving .pio\build\megaatmega2560\lib46e\libavr-debugger.a
      Compiling .pio\build\megaatmega2560\lib351\Adafruit_GFX_Library\Adafruit_SPITFT.cpp.o
      Compiling .pio\build\megaatmega2560\lib351\Adafruit_GFX_Library\glcdfont.c.o
      Compiling .pio\build\megaatmega2560\lib1e9\Adafruit_SSD1306\Adafruit_SSD1306.cpp.o
      Archiving .pio\build\megaatmega2560\lib351\libAdafruit_GFX_Library.a
      Compiling .pio\build\megaatmega2560\libfe9\PWMServo\PWMServo.cpp.o
      Archiving .pio\build\megaatmega2560\lib1e9\libAdafruit_SSD1306.a
      Archiving .pio\build\megaatmega2560\libfe9\libPWMServo.a
      **** [.pio\build\megaatmega2560\firmware.elf] Implicit dependency C:\Users\User\.platformio\platforms\atmelavr\builder\90' not found, needed by target .pio\build\megaatmega2560\firmware.elf’.*
      ========================== [FAILED] Took 5.98 seconds ==========================

What went wrong, atached also the Platformio project settings

; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
debug_tool = avr-stub
debug_port = COM4
lib_deps = jdolinay/avr-debugger@^1.5
debug_build_flags = 90 -Og -g2 -DDEBUG -DAVR8_BREAKPOINT_MODE=1

I also get this message
Set up and build your CMake project before debugging.
Bad CMake executable: “”. Check to make sure it is installed or the value of the “cmake.cmakePath” setting contains the correct path

Kind regards
Maxl

And what’s the “90” doing in there?

I don´t know, I used a HOWTO and there was a “90” added.
Deleted but same issue, by pressing CMake in platformio toolbar the message
“Bad CMake executable: “”. Check to make sure it is installed or the value of the “cmake.cmakePath” setting contains the correct path”
appears.

Are you working in VSCode or CLion? VSCode + PlatformIO does not need CMake, if you have this extension installed, uninstall or deactivate it for your workspace (extensions sidebar).

Hello,

I tried to deaktivate ths plugin, but the error appears again. I use the blue marked buttons for compiling, maybe I am wrong with handling PLATFORMIO.

after deinstall of the mentioned plugins this buttons were not available anymore

trying with F5 it starts and stops with …

Reading symbols from d:\Eigene Dateien\Documents\PlatformIO\Projects\Paternoster.pio\build\megaatmega2560\firmware.elf…
done.
PlatformIO Unified Debugger → Redirecting...
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target…
Ignoring packet error, continuing…
warning: unrecognized item “timeout” in “qSupported” response
Ignoring packet error, continuing…
Ignoring packet error, continuing…
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout

CMake is still active. Please uninstall it (CMake Tools / CMake in the extension sidebar).

I don’t see the necessary setup code to make avr-stub work at all in your sketch? See docs.

Hello,

now deinstalled and after adding “debug_init();” to setup I received folloeing error:

Linking .pio\build\megaatmega2560\firmware.elf
HardwareSerial0.cpp.o (symbol from plugin): In function `Serial':
(.text+0x0): multiple definition of `__vector_25'
avr8-stub.c.o (symbol from plugin):(.text+0x0): first defined here
WInterrupts.c.o (symbol from plugin): In function `attachInterrupt':
(.text+0x0): multiple definition of `__vector_1'
avr8-stub.c.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\megaatmega2560\firmware.elf] Error 1
=============================================== [FAILED] Took 6.45 seconds ===============================================
 *  The terminal process "C:\Users\User\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

here the setup function, maybe you can tell me which interrupt is the problem, is it not allowed to use Serial.Print during debugging? I thought using another serial port is possible, for instance Serial3.

void setup()
{

  // initialize GDB stub
  debug_init();
  cli();

  // Pin change IRQ control register
  // X X X X X PCIE2 PCIE1 PCIE0
  PCICR |= 0b00000100;  
  // Pin Change Mask Register 0
  // PCINT7 PCINT6 PCINT5 PCINT4 PCINT3 PCINT2 PCINT1 PCINT0
  PCMSK0 |= 0b00000000;

  // Pin Change Mask Register 0
  // PCINT15 PCINT14 PCINT13 PCINT12 PCINT11 PCINT10 PCINT9 PCINT8
  PCMSK1 |= 0b00000000;

  // Pin Change Mask Register 0
  // PCINT23 PCINT22 PCINT21 PCINT20 PCINT19 PCINT18 PCINT17 PCINT16
  PCMSK2 |= 0b00001111;

  DDRK = 0x00;   // Port K as INPUT (1 = OUTPUT)
  PORTK = 0xFF;  // pullup enable

  //set timer0 interrupt at 2kHz
  TCCR0A = 0;// set entire TCCR2A register to 0
  TCNT0  = 0;//initialize counter value to 0
  // set compare match register for 2khz increments
  OCR0A = 0x10;// = (16*10^6) / (200*64) - 1 8Bit register !!!
  // turn on CTC mode
  TCCR0A |= (1 << WGM01);
  TIMSK0 |= (1 << OCIE0A);

  sei();//allow interrupts

  Serial3.begin(38400);

  rxBufLen = 0u;
  if(CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ) == CAN_OK)
    Serial3.println("MCP2515 Initialized Successfully!");
  else
    Serial3.println("Error Initializing MCP2515...");
 
  CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(RELAIS1,HIGH);
  digitalWrite(RELAIS2,HIGH);
  digitalWrite(RELAIS3,HIGH);
.....

  pinMode(DI_IRQ_MCP_CAN, INPUT);  // Configuring pin for IRQ input Pin2
  pinMode(DI_CheckFlimSwitch, INPUT); // Configuring pin
  pinMode(DI_Start, INPUT); // Configuring pin
  pinMode(DI_Stop, INPUT); // Configuring pin
  pinMode(DI_InPos1, INPUT); // Configuring pin
  pinMode(DI_BoltOK_IN, INPUT); // Configuring pin
  pinMode(DI_GearCount1, INPUT); // Configuring pin
  pinMode(DI_GearCount2, INPUT); // Configuring pin
....
  attachInterrupt(digitalPinToInterrupt(DI_IRQ_MCP_CAN), ext_IRQ0, FALLING);
  attachInterrupt(digitalPinToInterrupt(DI_InPos1), ext_IRQ1, RISING);
  attachInterrupt(digitalPinToInterrupt(DI_GearCount1), ext_IRQ4, RISING);
  attachInterrupt(digitalPinToInterrupt(DI_ReferenzOK), ext_IRQ5, RISING);

  pciSetup(7);
  pciSetup(8);
  pciSetup(9);
  pciSetup(A0);

  Serial3.println("MCP2515 Library Receive Example...");
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
   Serial3.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();
  myservo0.attach(9);  // attaches the servo on pin 9/10 to the servo object
  myservo0.write(0);   //0..180
  myservo1.attach(10); // attaches the servo on pin 9/10 to the servo object
  myservo1.write(0);   //0..180

  canbusRecv = 0;
  ptxBuf=txBuf;
  FYNcontr.Bitcountbyte = 8u;
  Serial3.println("Timer ready...");
  wdt_enable(WDTO_1S);
  Serial3.println("Watchdog ready...");
}

You cannot use the same serial object and thus interrupts that the avr-debugger library is using. This is stated in the documentation.

This means by default no Serial usage and no INT0_vect.

You can however change which serial it uses, since an ATMega2560 has multiple ones:

but if you change that, you would of course have to add an external USB-serial converter and connect it to those pins, and give PlatformIO that as the debug_port.

And of course also the interrupt for the software interrupt is redefinable, see here and

So you would e.g. use a platformio.ini like

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
debug_tool = avr-stub
; connected to (arduino perspective) Rx=PD2=D19, Tx=PD3=D18
debug_port = COM4
lib_deps = jdolinay/avr-debugger@^1.5
; use free software interrupt source,
; use serial1
build_flags =
   -DAVR8_SWINT_SOURCE=7
   -DAVR8_UART_NUMBER=1

I encourage you to step back from trying to debug your master project that uses 3000 interrupts and serials and instead try to debug the smallest possible project to verify that it’s generally working.

Hello,

now I truied a very small project, only with a while loop and a variable that was counted up.
It worked, great :slight_smile:
After that I took another sample project, MQTT Server with Ethernet (ATMEGA2560 and Ethernetcard)
Then I got following error message, now I am at the beginning :frowning:
The project itself run, but debugging is not possible, could you help again?

plarformio.io

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
; monitor_speed= 38400
build_flags = -DAVR8_BREAKPOINT_MODE=1
debug_tool = avr-stub
debug_port = \.\COM5
lib_deps = jdolinay/avr-debugger@^1.5

DEBUG CONSOLE

Processing megaatmega2560 (platform: atmelavr; board: megaatmega2560; framework: arduino)
--------------------------------------------------------------------------------
Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: Redirecting...
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 → Library Dependency Finder (LDF) — PlatformIO latest documentation
      LDF Modes: Finder ~ chain, Compatibility ~ soft
      Found 9 compatible libraries
      Scanning dependencies…
      Dependency Graph
      |-- avr-debugger @ 1.5.0
      |-- Ethernet @ 2.0.1
      | |-- SPI @ 1.0
      |-- MQTT @ 2.5.0
      Building in debug mode
      Compiling .pio\build\megaatmega2560\src\test.cpp.o
      Compiling .pio\build\megaatmega2560\lib2dd\avr-debugger\app_api.c.o
      Compiling .pio\build\megaatmega2560\lib2dd\avr-debugger\avr8-stub.c.o
      Compiling .pio\build\megaatmega2560\lib1fb\SPI\SPI.cpp.o
      Archiving .pio\build\megaatmega2560\lib1fb\libSPI.a
      Archiving .pio\build\megaatmega2560\lib2dd\libavr-debugger.a
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\Dhcp.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\Dns.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\Ethernet.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\EthernetClient.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\EthernetServer.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\EthernetUdp.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\socket.cpp.o
      Compiling .pio\build\megaatmega2560\libc06\Ethernet\utility\w5100.cpp.o
      Compiling .pio\build\megaatmega2560\libb53\arduino-mqtt-master\MQTTClient.cpp.o
      Archiving .pio\build\megaatmega2560\libc06\libEthernet.a
      Compiling .pio\build\megaatmega2560\libb53\arduino-mqtt-master\lwmqtt\client.c.o
      Compiling .pio\build\megaatmega2560\libb53\arduino-mqtt-master\lwmqtt\helpers.c.o
      Compiling .pio\build\megaatmega2560\libb53\arduino-mqtt-master\lwmqtt\packet.c.o
      Compiling .pio\build\megaatmega2560\libb53\arduino-mqtt-master\lwmqtt\string.c.o
      Archiving .pio\build\megaatmega2560\libb53\libarduino-mqtt-master.a
      Archiving .pio\build\megaatmega2560\libFrameworkArduinoVariant.a
      Compiling .pio\build\megaatmega2560\FrameworkArduino\CDC.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\HardwareSerial.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\HardwareSerial0.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\HardwareSerial1.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\HardwareSerial2.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\HardwareSerial3.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\IPAddress.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\PluggableUSB.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\Print.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\Stream.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\Tone.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\USBCore.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\WInterrupts.c.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\WMath.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\WString.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\abi.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\hooks.c.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\main.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\new.cpp.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\wiring.c.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\wiring_analog.c.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\wiring_digital.c.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\wiring_pulse.S.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\wiring_pulse.c.o
      Compiling .pio\build\megaatmega2560\FrameworkArduino\wiring_shift.c.o
      Archiving .pio\build\megaatmega2560\libFrameworkArduino.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: [= ] 5.5% (used 449 bytes from 8192 bytes)
      Flash: [= ] 10.6% (used 26904 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: COM5
      Uploading .pio\build\megaatmega2560\firmware.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

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

Writing | ################################################## | 100% 4.47s

avrdude: 26904 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 26904 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 3.70s

avrdude: verifying …
avrdude: 26904 bytes of flash verified

avrdude done. Thank you.

========================= [SUCCESS] Took 24.50 seconds =========================
Reading symbols from d:\Eigene Dateien\Documents\PlatformIO\Projects\Arduino Mega.pio\build\megaatmega2560\firmware.elf…
done.
PlatformIO Unified Debugger → Redirecting...
PlatformIO: debug_tool = avr-stub
PlatformIO: Initializing remote target…
Ignoring packet error, continuing…
warning: unrecognized item “timeout” in “qSupported” response
Ignoring packet error, continuing…
Ignoring packet error, continuing…
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout

What’s the full code of the project?

Here it is, everything that belongs to SERIAL is commented:

#include <Arduino.h>
#include <Ethernet.h>
#include <MQTT.h>
#include "..\.pio\libdeps\megaatmega2560\avr-debugger\avr8-stub\avr8-stub.h"
#include "..\.pio\libdeps\megaatmega2560\avr-debugger\avr8-stub\app_api.h"
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 1, 177};  // <- change to match your network

EthernetClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect() {
  //Serial.print("connecting...");
  while (!client.connect("arduino", "public", "public")) {
    //Serial.print(".");
    //delay(1000);
  }

  //Serial.println("\nconnected!");
  client.subscribe("/hello");
  // client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
  //Serial.println("incoming: " + topic + " - " + payload);
  // Note: Do not use the client in the callback to publish, subscribe or
  // unsubscribe as it may cause deadlocks when other things arrive while
  // sending and receiving acknowledgments. Instead, change a global variable,
  // or push to a queue and handle it in the loop after calling `client.loop()`.
}

void setup() {
  //Serial.begin(115200);
  Ethernet.begin(mac, ip);
  // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
  // by Arduino. You need to set the IP address directly.
  client.begin("192.168.1.120", net);
  client.onMessage(messageReceived);
  connect();
}

void loop() {
  client.loop();
  if (!client.connected()) {
    connect();
  }
  // publish a message roughly every second.
  if (millis() - lastMillis > 2000) {
    lastMillis = millis();
    client.publish("/hello", "world");
  }
}

Where’s debug_init() in this function like the documentation demands it?

Hi, sorry I forgot :joy: it works Thanks