Struggling to debug mega2560 using avr-stub

I’ve been trying to get my debugger working with my mega2560, and I could use some help. Whenever I run the debugger, I get the following error:

PlatformIO: Initializing remote target...
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
Ignoring packet error, continuing...
.pioinit:13: Error in sourced command file:
Bogus trace status reply from target: timeout

My platformio.ini file is as follows

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino
board_build.f_cpu = 2000000L

debug_tool = avr-stub
debug_port = COM7
lib_deps = jdolinay/avr-debugger @ ~1.4

I uninstalled VSCode and reinstalled it, and still couldn’t get it to work. I created a new project where I copy and pasted the code given on the configuration page

#include "avr8-stub.h"
#include "app_api.h" // only needed with flash breakpoints

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);
}

and when I ran the debugger, it works. This project was made in PlatformIOs default location, as opposed to my own project in which I set the location manually. I tried migrating my own code to this new project, and when I ran it, I got the same error shown above. I don’t know what’s causing the issue, and I would appreciate any help you can give me. Here’s the code I’m trying to debug (I’ve modified it a bit so you know what’s happening within functions):

#include "avr8-stub.h"
#include "app_api.h" // only needed with flash breakpoints

unsigned int curMS;

#define MAIN_ENABLE

#ifdef MAIN_ENABLE

int main(void){
    debug_init();
    //The code below is called from a function in a board_init library i wrote
    CLKPR = 0b10000000;     //raise prescaler change enable bit
    CLKPR = 0b00000011;     //set prescaler to 8 to run at 2MHz
    SREG |= (1<<7);         //enable global interrupt

    //the code below is called from a function in my timer library
    TCCR2A = 0b00000010; //clear timer on compare match
    TCCR2B = 0b00000010; //prescaler to 1:8
    TCNT2 = 0; //initialize timer
    OCR2A = 0xFA; //output compare to 250
    ASSR = 0; //clear asynch settings
    TIMSK2 = 0b00000010; //interrupt enable for T2A
    msCount = 0;


    // the onboard LED corresponds to PORT B, pin 7
    // set the DataDirectionRegister of PC3 as input
    DDRB = 0xFF; // set data direction to output
    DDRA = 0x07; // set columns to output
    PORTB = 0x00;
    flag = 2000;
    curMS = 0;

    while (1){
        //SETBIT(PORTB, 7);
        curMS = Get_Milliseconds();
        if (curMS > 0){
            SETBIT(PORTB, 7);
        }
    }
    return(0);
}

Quick note for understanding, my timer library contains an interrupt which triggers every millisecond. msCount increments each time the interrupt is triggered, and Get_Milliseconds() simply returns msCount.

Apologies if I’m missing any info, please ask about anything that you’re curious about. Thanks for the help.

Your chaning of the clock prescaler / clock freq will cause avr-stub to use a different baud-rate, namely it will be 9600 baud instead of 115200 baud

but, at the PlatformIO side, the baud is defaulted to

thus at the very least, you will want to add

board_debug.avr-stub.speed = 9600

to your platformio.ini. (Or for a test, change C:\Users\<user>\.platformio\platforms\atmelavr\boards\megaatmega2560.json directly)

Also, this is an uncondition setting of that register, so it will effect the entirety of port A and port B GPIO pins – if the original Serial pins happen to be on this port, this will be problematic.

I love you. I added

board_debug.avr-stub.speed = 9600

and it didn’t work so I changed the file directly and altered DDRB so I only set necessary pins, and now it works. Thanks so much.