AVR stub setup with atmega328p

Hi all,

I am trying to implement the AVR stub to debug a project, following avr-stub — PlatformIO latest documentation
I setup my .ini file as required, but I cannot use avr-stub as a debug_tool argument in .ini, it prevents me from building, even after installing the library. Here is the error message I get, I dont see why :

DebugInvalidOptionsError: Unknown debug tool `avr-stub`. Please use one of `simavr` or `custom`:
  File "C:\Users\Jean-ChristopheSicot\.platformio\penv\lib\site-packages\platformio\builder\main.py", line 177:
    env.SConscript("$BUILD_SCRIPT")
  File "C:\Users\Jean-ChristopheSicot\.platformio\packages\tool-scons\scons-local-4.1.0\SCons\Script\SConscript.py", line 591:
    return _SConscript(self.fs, *files, **subst_kw)
  File "C:\Users\Jean-ChristopheSicot\.platformio\packages\tool-scons\scons-local-4.1.0\SCons\Script\SConscript.py", line 280:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "C:\Users\Jean-ChristopheSicot\.platformio\platforms\atmelavr\builder\main.py", line 155:
    target_elf = env.BuildProgram()
  File "C:\Users\Jean-ChristopheSicot\.platformio\packages\tool-scons\scons-local-4.1.0\SCons\Util.py", line 658:       
    return self.method(*nargs, **kwargs)
  File "C:\Users\Jean-ChristopheSicot\.platformio\penv\lib\site-packages\platformio\builder\tools\platformio.py", line 61:
    env.ProcessProgramDeps()
  File "C:\Users\Jean-ChristopheSicot\.platformio\packages\tool-scons\scons-local-4.1.0\SCons\Util.py", line 658:
    return self.method(*nargs, **kwargs)
  File "C:\Users\Jean-ChristopheSicot\.platformio\penv\lib\site-packages\platformio\builder\tools\platformio.py", line 113:
    env.PrintConfiguration()
  File "C:\Users\Jean-ChristopheSicot\.platformio\packages\tool-scons\scons-local-4.1.0\SCons\Util.py", line 658:       
    return self.method(*nargs, **kwargs)
  File "C:\Users\Jean-ChristopheSicot\.platformio\penv\lib\site-packages\platformio\builder\tools\pioplatform.py", line 
233:
    _get_debug_data(),
  File "C:\Users\Jean-ChristopheSicot\.platformio\penv\lib\site-packages\platformio\builder\tools\pioplatform.py", line 
197:
    % board_config.get_debug_tool_name(env.GetProjectOption("debug_tool")),
  File "c:\users\jean-christophesicot\.platformio\penv\lib\site-packages\platformio\platform\board.py", line 141:       
    % (tool_name, ", ".join(sorted(list(debug_tools))))

and my .ini

[env:ATmega328P]

platform = atmelavr

board = ATmega328P

framework = arduino

upload_protocol = atmelice_isp

upload_flags = -e

upload_port = usb

;debug_tool = simavr

debug_tool = avr-stub

debug_port = SERIAL_PORT

; 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

lib_deps = 

    jdolinay/avr-debugger @ ~1.1

    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

Thanks!

Per

the ATMega328P target is supported by avr-stub.

I also get no error when trying your platformio.ini on my Uno (with the upload via Atmel ICE commented out and the

being substituted for my actual serial port).

So I can just suggest updating the Atmel AVR platform from the PIO Home → Platforms screen.

1 Like

Hi Max, I managed to build and upload with the updated AVR platform and a Uno.
But I get this error when I try debuggiong with VS code, and its not clear on the web what I should do with it


Any idea?

And also, I thought it was possible to use avr stub debugging with atmel ICE based on what I have read from other posts. Am I wrong? thanks!

Can you try and reproduce the exact setup from here (How to debug on Arduino mega 2560 - #2 by maxgerhardt)? On my Windows machine, the COM port has to be given in some special way to make (avr-)GDB happy.

Otherwise if it says ENONET/EPIPE there, you can try some generic stuff:

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.