Can't debug arduino Uno in C (avr-stub)

Here’s my platformio.ini

; 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
; https://docs.platformio.org/page/projectconf.html

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

debug_tool = avr-stub
debug_port = COM6

lib_deps =
    jdolinay/avr-debugger

and the code that works

#include <Arduino.h>
#include <avr8-stub.h>
#include "app_api.h"

void setup() {
      debug_init();
      pinMode(LED_BUILTIN, OUTPUT);
  // put your setup code here, to run once:
}

void loop() {
   digitalWrite(LED_BUILTIN, HIGH);
   delay(500);
   digitalWrite(LED_BUILTIN, LOW);
   delay(500);
  // put your main code here, to run repeatedly:
}

When I renamed the file to .C extension, and its content to

/**
 * Copyright (C) PlatformIO <contact@platformio.org>
 * See LICENSE for details.
 */

#include <avr/io.h>
#include <util/delay.h>
#include <avr8-stub.h>
#include "app_api.h"

int main(void)
{
    debug_init();
    // make the LED pin an output for PORTB5
    DDRB = 1 << 5;

    while (1)
    {
        _delay_ms(500);

        // toggle the LED
        PORTB ^= 1 << 5;
    }

    return 0;
}

It fails to start debugging, with following message:

avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.67s

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

avrdude done.  Thank you.

========================= [SUCCESS] Took 3.88 seconds =========================
Reading symbols from D:\embedded\test\.pio\build\uno\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

Related thread

I didn’t not change clock rate as author did, but I’ve tried to set BAUD rate in platformini.io to 115200 and it did not help.

Remove that line to disable compilation of the Arduino core if you’re going to do baremetal / avr-libc programming.

Can you then test the canonical example

with the include path fixed up?

It worked, looks like

breakpoint();

was required to be added. Ty

Would debug_message work on an arduino nano ?
is it somehow possible to debug an avr target (without usb serial connection) from say an arduino nano host ? possibly something that you can upload to run on the target avr ?

If it’s not working it’s a bug.

Well, if you can upload a sketch that is avr-stub enabled to your target AVR microcontroller, then the tooling doesn’t care how you get the UART communication between your PC and the target AVR microcontroller working. You could use a an Arduino Nano as a serial-to-USB bridge. Just like a more approriate serial-to-USB converter like an FT232 or CH340 etc.

The other way to go (that’s also way more professional) is to build a AVR debugger probe that connects to the target AVR microcontroller via debugWire on the RESET pin. Newer AVRs use other debugging protocols like PDI or UPDI. See https://pyavrocd.io/supported-debuggers/.

1 Like