AVR stub setup with atmega328p

avr-stub uses a GDB server implementation on the microcontroller side which transports the data via the primary serial. So it’s all via serial, no Atmel ICE used here. However, other people have gotten debugging to work with just the Atmel ICE and avarice over the debugWire protocol (which needs a fuse modification) (CLion / Avarice debug not working)

I was trying your solutions 1 by 1 and it seems like removing C:\Users\<user>\.platformio\packages\toolchain-atmelavr caused a problem. I did reinstal the atmel avr platform from pio home, so I dont understand what is missing. I get an include error from arduino.h and I get these errors when trying to build


Or in characters:

LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 16 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <avr-debugger> 1.1.0
Building in release mode
Compiling .pio\build\uno\src\main.cpp.o
Compiling .pio\build\uno\libcc6\avr-debugger\app_api.c.o
Compiling .pio\build\uno\libcc6\avr-debugger\avr8-stub.c.o
Compiling .pio\build\uno\FrameworkArduino\CDC.cpp.o
avr-g++: error: device-specs/specs-atmega328p: No such file or directory
Compiling .pio\build\uno\FrameworkArduino\HardwareSerial.cpp.o
avr-gcc: error: device-specs/specs-atmega328p: No such file or directory
Compiling .pio\build\uno\FrameworkArduino\HardwareSerial0.cpp.o
Compiling .pio\build\uno\FrameworkArduino\HardwareSerial1.cpp.o
avr-gcc: error: device-specs/specs-atmega328p: No such file or directory
Compiling .pio\build\uno\FrameworkArduino\HardwareSerial2.cpp.o
*** [.pio\build\uno\src\main.cpp.o] Error 1
avr-g++: error: device-specs/specs-atmega328p: No such file or directory
*** [.pio\build\uno\libcc6\avr-debugger\app_api.c.o] Error 1
*** [.pio\build\uno\libcc6\avr-debugger\avr8-stub.c.o] Error 1
*** [.pio\build\uno\FrameworkArduino\CDC.cpp.o] Error 1
avr-g++: error: device-specs/specs-atmega328p: No such file or directory
avr-g++: error: device-specs/specs-atmega328p: No such file or directory
*** [.pio\build\uno\FrameworkArduino\HardwareSerial0.cpp.o] Error 1
*** [.pio\build\uno\FrameworkArduino\HardwareSerial.cpp.o] Error 1
avr-g++: error: device-specs/specs-atmega328p: No such file or directory
avr-g++: error: device-specs/specs-atmega328p: No such file or directory
*** [.pio\build\uno\FrameworkArduino\HardwareSerial2.cpp.o] Error 1
*** [.pio\build\uno\FrameworkArduino\HardwareSerial1.cpp.o] Error 1

The compiler is not fully downloaded or extracted yet. Try and remove the toolchain-atmelavr folder again, and then “Build” the project in VSCode once to confirm the compiler is good.

Ok, I managed to make the debugger work on an Uno with a simple sketch.

I then tried adding the libraries used in the program I am actually trying to debug, and it failed. One of the libraries is causing a undefined reference to ‘regs’, which prevents build. I tried masking the libraries to remove them from build, but the problem persists. how do I get rid of the commented libraries in the image below, so that I can find which one is actually causing the build issue?
Thanks a lot! can’t wait to start debugging!!

The undefined reference to regs is a known error caused by a compiler bug in avr-gcc. You need to upgrade the compiler, see this.

1 Like

Thanks, I can now build and upload.
But I am not out of the woods just yet. PIO seems stuck on an endless loop when I lunch the debugger.

I see the led 13 is flashing sometimes, so there seems to be a communication… but I have placed breakpoints and it never reaches them, is this another known issue with the debugger?

Thank you, each post brings me closer to glory!

Once you start the debugging, it should stop at the first breakpoint you set (or inside the function that you set the breakpoint at). If I e.g. set a breakpoint in my referenced setup post at delay(), after pressing “Debug”, VSCode looks like

Which is inside delay() where I set the original breakpoint

If I use your main.cpp code with no manually set breakpoints, it will stop at the breakpoint() function

So I can’t reproduce the “infinite loading” issue.

Also, the bebavior I see on my Uno when starting “Debug” is:

  • Code is built
  • Code is uploaded (lights “TX” and “RX” stay on for a bit)
  • TX light stays on for a bit
  • All lights off
  • Code is uploaded again (TX / RX LEDs on)
  • VSCode loading stops and the breakpoint is hit

What is the action that you’re seeing?

There could be just general setup errors e.g. regarding VSCode (conflicting extensions installed), or the extension itself. (I use the beta version at PlatformIO IDE for VSCode 2.3 Beta – Help us with testing!, has a few bugs regarding the “General” project task dropdown but is ok otherwise). Or a problem with board upload.

Ok, I tried with a blinky

Started with no debugging

#include <Arduino.h>

#include "avr8-stub.h"

void setup() {

  // put your setup code here, to run once:

      // debug_init();

      pinMode(LED_BUILTIN, OUTPUT);

}

void loop() { 

  // breakpoint();

  digitalWrite(LED_BUILTIN, HIGH);

  delay(300);

  digitalWrite(LED_BUILTIN, LOW);

  delay(100);

}

ctrl+alt+u → the code uploads, the led blinks, all good

Then I uncomment the init and breakpoint function, I do not add red dot breakpoints
Then I click PIO debug
my debug console uploads the sketch, and then nothing happens. No blink. It seems as though the sketch is locked in the breakpoint, but I cannot control it, the PIO debug is still loading and the only debug controls available are restart and stop

I see the same actions, except the the breakpoint never hits.
It did work at first, then I added the libraries I needed for my project to debug in the .ini, it started having the regs issues, then I solved this with the toolchain-atmelavr@>=1.70300.0 package line in the .ini, this solved the regs issues but the breakpoints do not hit anymore…

Also, is there a difference between debug and skip pre-debug in that case? Should I choose one over the other?

thanks!

That is weird. With what exact platformio.ini are you where it doesn’t work?

; 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

; ###############################################################
; Debug Environment
; ###############################################################

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

debug_tool = avr-stub
debug_port = \\.\COM11


; ###############################################################
; Release Environment
; ###############################################################

; [env:ATmega328P]
; platform = atmelavr
; board = ATmega328P
; framework = arduino
; upload_protocol = atmelice_isp
; upload_flags = -e
; upload_port = usb

; ; do not forget to run burn fuse task if changing values, or open a terminal ctrl+shift+p and type: C:\pathto\pio.exe run -t fuses
; ; The path to pio.exe can be found when running a pio command (ex build), it will print in the terminal
; board_fuses.hfuse = 0xDF    ;smallest flash section size 256 since not using a bootloader with ICE programmer
; board_fuses.lfuse = 0xE2    ;int 8MHz crystal (16MHz not working with less than 5V PSU)
; board_fuses.efuse = 0xFE    ;BOD at 1.8V, perfect for low power

; ###############################################################
; Project Libraries
; ###############################################################

platform_packages =
  ; use GCC AVR 7.3.0+ prevents the 'regs' build error when using "avr8-stub.h"
  toolchain-atmelavr@>=1.70300.0

lib_deps = 
          jdolinay/avr-debugger @ ~1.1        ; Comment out if not using debug env
	  ; adafruit/Adafruit ST7735 and ST7789 Library@^1.6.0
	  ; adafruit/Adafruit BusIO@^1.5.0
          ; adafruit/Adafruit SSD1306@^2.4.0
          ; olkal/HX711_ADC@^1.2.1

Aha, I get the same behavior when using your platformio.ini.

It seems that the new compiler version has fixed compilation errors but has made the debugging library non-working. As soon as I remove the platform_packages instruction, it works again. Then it again looks like

to me.

Can you reproduce that?

Also, in the original post said that in some cases also lib_archive = false helps prevent the compile error. So maybe your project with those libraries will compile with that option and the older compiler and debugging still works.

Otherwise it’s a mystery to me how the newer compiler version breaks the code. Probably needs some deeper analysis. I’ll raise an issue in the original repo. (Edit: Raised per Incompatibility with new toolchain · Issue #24 · jdolinay/avr_debug · GitHub)

Yeah I can make it work as soon as I move the instruction!

Is there a version that would solve the regs issue while letting the debug lib work??
big thanks

**I also tried the lib_archive = false instruction but didnt change anything (i tried it without adding the libraries, just litterly added this line to the problematic.ini you just seen)

I have an unrelated issue but will help me out to find a solution, if I add the libraries to the .ini of my test project, I get an error no such file in directory. (They are installed on my real project and compile fine however). Shouldnt these libraries install locally in my test project if I place them in my .ini file? I dont get the reason why this happens

Your main.cpp must do a #include <Adafruit_GFX.h> otherwise PIO will not be able to scan the dependency chain correctly and doesn’t recognize Wire as a dependency.

1 Like

I had the includes, but oddly enough I had to add wire.h and subheaders of the adafruit_GFX library

Now I get the regs error with this .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

; ###############################################################
; Debug Environment
; ###############################################################

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

debug_tool = avr-stub
debug_port = \\.\COM11


; ###############################################################
; Release Environment
; ###############################################################

; [env:ATmega328P]
; platform = atmelavr
; board = ATmega328P
; framework = arduino
; upload_protocol = atmelice_isp
; upload_flags = -e
; upload_port = usb

; ; do not forget to run burn fuse task if changing values, or open a terminal ctrl+shift+p and type: C:\pathto\pio.exe run -t fuses
; ; The path to pio.exe can be found when running a pio command (ex build), it will print in the terminal
; board_fuses.hfuse = 0xDF    ;smallest flash section size 256 since not using a bootloader with ICE programmer
; board_fuses.lfuse = 0xE2    ;int 8MHz crystal (16MHz not working with less than 5V PSU)
; board_fuses.efuse = 0xFE    ;BOD at 1.8V, perfect for low power

; ###############################################################
; Project Libraries
; ###############################################################

; platform_packages =
;   ; use GCC AVR 7.3.0+ prevents the 'regs' build error when using "avr8-stub.h"
;   toolchain-atmelavr@>=1.70300.0
lib_archive = false 
lib_deps = 
jdolinay/avr-debugger @ ~1.1        ; Comment out if not using debug env
	  adafruit/Adafruit ST7735 and ST7789 Library@^1.6.0
  	; adafruit/Adafruit BusIO@^1.5.0
; adafruit/Adafruit SSD1306@^2.4.0
olkal/HX711_ADC@^1.2.1

And these library-related includes

#include <Adafruit_GFX.h>
#include <HX711_ADC.h>
#include <Adafruit_ST7789.h>    // Includes Adafruit_GFX.h

#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include "avr8-stub.h"

What the hell!?! :’( It seemed to work at first with this setup with my test project. I only got the regs error once I copied this .ini in my real project. Maybe something else is causing it… I will test with diffrent libraries and do a couple more tests and keep you posted

Sadly that’s a cat-mouse game right then: If the undefined reference to regs error can’t be solved with lib_archive = false with the set of libraries you are using, then the only other way to fix it is by upgrading the compiler which causes debugging to non-functional – I hope the library author can figure something out why the new toolchain is breaking it.

Per issue we’ve found the actual culprit – PlatformIO has multiple toolchain-atmelavr packages for the GCC 7.3.0 version, but the very latest one does not contain the avr-gdb.exe, for whatever reason. It’s just missing. The exact version of that package is 2.70300.201015.

The other 7.3.0 GCC package version, 1.70300.191015 does contain the executable and immediately works in my test.

Could you again try your project with your libraries but this time with the pacakge

platform_packages =
  toolchain-atmelavr@1.70300.191015

and see if both the linking error is gone and debugging works?

I’m catching the same debugger issue, using the latest PIO and toolchain-atmelavr under CLion IDE for macOS. It seems to me, a buggy version of the toolchain (1.70300.191015 ) has become the default. That is why the previous workaround does not work, at least for me.

So, defining the previous toolchain (1.50400.190710) is solving the issue.

platform_packages =
    toolchain-atmelavr @1.50400.190710

I managed to make it work on 1 laptop a few weeks ago, and I am not exactly sure what I have done, but I could not make it work on another. I will try with the info you gave me and get back to you asap. thanks!

I described in details my quest to make this work here. Credit is due to Max as always for helping so much in making PIO more accessible Setting up Arduino Uno as a debug environment for Atmel-ICE PlatformIO projects – Cyan Sensors

*this post is a work in progress, so if you see any improvements, tell me! I want to make atmel ICE usage on PIO easier for everyone!!