Upload and Debug for ATmega328P with Atmel ICE

Hi there,

I working on a custom board with an ATmega238P using the Atmel-ICE debugger.
The source code is compliled using a Make file on Linux.

For debug pourposes, I want to use an IDE that provides debugging capabilities and since Atmel Studion is not working on Linux (+ I like VS Code and I used platformio on an ESP8266) I want to migrate my projet to Platformio.

My environment running inside an Xubuntu Virtual Machine.
With the command

lsusb

I verified that the debugger is connected to my VM

Bus 001 Device 002: ID 03eb:2141 Atmel Corp. ICE debugger
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 002 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

I create a new project and I can compile my projet with the Arduino framework.

This is my platformio.ini

[platformio]
src_dir = ../src

[env:ATmega328P]
platform = atmelavr
board = ATmega328P
framework = arduino
debug_tool = atmel-ice
upload_protocol = atmel-ice
upload_flags = -e

; Change MCU frequency
board_build.f_cpu = 16000000L

With this configuration I get this error:

DebugInvalidOptionsError: Unknown debug tool atmel-ice. Please use one of avr-stub, simavr or custom:

I also followed these instructions Setting up Atmel-ICE with PlatformIO using ATmega328p but they are for Windows environments.

Can anyone help me to solve this issue?

Thanks in advance!

Federico

The platformio.ini settings are OS-independent. Does

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

not work?

Debugging AVR chips with an Atmel-ICE is not supported out-of-the-box, upload is. The currently support debug methods for Atmel chips are by simulation (simavr) or by a serial GDB stub program loaded into the firmware (avr-stub).

The problem is the GDB server for an atmel-ice (and other chips), Atmel does not supply a program like that since they don’t use GDB under the hood. But, free alternative programs like AVaRICE exist (Debugging AVR / Arduino with ATMELICE / JTAGICE3 and avarice / avr-gdb) but seemed buggy at the point of testing. There’s also dwire (VSCode PIO debugger for AVR).

Hi @maxgerhardt,

thanks for the reply.

I tried your suggestion:

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

but when I click on “Upload” from Platformio, I get this error:

Building .pio/build/ATmega328P/firmware.hex
Configuring upload protocol…
AVAILABLE: atmelice_isp
CURRENT: upload_protocol = atmelice_isp
Looking for upload port…
Use manually specified: usb
*** [upload] could not open port usb: [Errno 2] No such file or directory: ‘usb’
==================================================================================== [FAILED] Took 4.07 seconds ====================================================================================
The terminal process “pio ‘run’, ‘–target’, ‘upload’, ‘–environment’, ‘ATmega328P’” terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

How can I solve it?

Thanks in advance!

That’s not what I see when uploading.

AVAILABLE: atmelice_isp
CURRENT: upload_protocol = atmelice_isp
BeforeUpload(["upload"], [".pio\build\ATmega328P\firmware.hex"])
Use manually specified: usb
avrdude -e -v -p atmega328p -C C:\Users\Max\.platformio\packages\tool-avrdude\avrdude.conf -c atmelice_isp -b 115200 -D -P "usb" -U flash:w:.pio\build\ATmega328P\firmware.hex:i

avrdude: Version 6.3, compiled on Sep 12 2016 at 17:24:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\Max\.platformio\packages\tool-avrdude\avrdude.conf"

         Using Port                    : usb
         Using Programmer              : atmelice_isp
         Overriding Baud Rate          : 115200
avrdude: jtag3_open_common(): Did not find any device matching VID 0x03eb and PID list: 0x2141

avrdude done.  Thank you.

Can you make sure your core and platforms are up to date? Open a CLI and do pio upgrade --dev and pio platform update atmelavr, then restart VSCode and retry.

Ok,

I upgraded

pio upgrade --dev
Please wait while upgrading PlatformIO …
PlatformIO has been successfully upgraded to 5.0.5b5

and

pio platform update atmelavr
Please wait while upgrading PlatformIO…
PlatformIO has been successfully upgraded to 5.0.5b5!


If you like PlatformIO, please:


Platform atmelavr

Updating platformio/atmelavr 3.1.0 [Up-to-date]
Updating platformio/toolchain-atmelavr 1.50400.190710 @ ~1.50400.0 [Up-to-date]
Updating platformio/framework-arduino-avr-minicore 2.0.9 @ ~2.0.7 [Up-to-date]
Updating platformio/tool-avrdude 1.60300.200527 @ ~1.60300.0 [Up-to-date]


We found 79.48KB of unnecessary PlatformIO system data (temporary files, unnecessary packages, etc.).
Use pio system prune --dry-run to list them or pio system prune to save disk space.

but I get the same error:

Configuring upload protocol…
AVAILABLE: atmelice_isp
CURRENT: upload_protocol = atmelice_isp
Looking for upload port…
Use manually specified: usb
*** [upload] could not open port usb: [Errno 2] No such file or directory: ‘usb’
==================================================================================== [FAILED] Took 5.63 seconds ====================================================================================
The terminal process “pio ‘run’, ‘–target’, ‘upload’, ‘–environment’, ‘ATmega328P’” terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

Should I install something related to “Atmel Ice”?

I’m using Xubuntu on VMware Player.

Thanks a lot!

Oh wow this really is operating-system specific regarding the upload_port when the value is usb. PlatformIO performs a sanity-check whether that “file” is there before uploading.

Please add

board_upload.require_upload_port = no

to the platformio.ini.

However then -Pusb doesn’t show up in the avrdude invocation anymore – but it still seems to look for the right thing. Worth a try. In the meantime I’ll raise an issue.

Now the error is changed and it seems related to the upload with avrdude

avrdude: usbdev_open(): WARNING: failed to set configuration 1: Device or resource busy

avrdude: stk500v2_command(): command failed
avrdude: bad response to AVR sign-on command: 0xa0
avrdude: Target prepared for ISP, signed off.
avrdude: Now retrying without power-cycling the target.
avrdude: stk500v2_command(): command failed
avrdude: bad response to AVR sign-on command: 0xa0
avrdude: Target prepared for ISP, signed off.
avrdude: Now retrying without power-cycling the target.
avrdude: stk500v2_command(): command failed
avrdude: bad response to AVR sign-on command: 0xa0
avrdude: Target prepared for ISP, signed off.
avrdude: Now retrying without power-cycling the target.
avrdude: stk500v2_command(): command failed
avrdude: bad response to AVR sign-on command: 0xa0
avrdude: Target prepared for ISP, signed off.
avrdude: Now retrying without power-cycling the target.
avrdude: stk500v2_command(): command failed
avrdude: bad response to AVR sign-on command: 0xa0
avrdude: Failed to return from debugWIRE to ISP.
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

avrdude done. Thank you.

*** [upload] Error 1

udev rules are installed and reloaded (or to be safe, pc reboot…)? Quick googling seems like the kernel or something else interferes here. Uno wifi rev2 - avrdude: usbdev_open(): WARNING: failed to set configuration 1 - Avrdude, stk500, Bootloader issues - Arduino Forum

Yes, udev rules are installed and to be sure I rebooted the VM.

Is ther another debugger that I can use?

If I use a VM with windows do you think I can resolve something?

Can you quickly test the original platformio.ini on Windows without the USB device being passed through? That would quickly rule out some error sources.

Yes, not so quickly but I just install VScode and Platformio.

I start right now.

Ok, I installed evrything and now I get this error:

Configuring upload protocol…
AVAILABLE: atmelice_isp
CURRENT: upload_protocol = atmelice_isp
Looking for upload port…
Uploading .pio\build\ATmega328P\firmware.hex
avrdude: jtag3_open_common(): Did not find any device matching VID 0x03eb and PID list: 0x2141

avrdude done. Thank you.

*** [upload] Error 1
================================================================================================== [FAILED] Took 3.06 seconds ==================================================================================================
The terminal process “C:\Users\f.platformio\penv\Scripts\pio.exe ‘run’, ‘–target’, ‘upload’, ‘–environment’, ‘ATmega328P’” terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

Something different.

avrdude can’t interface with Atmel-ICE when it’s loaded with the Atmel Studio drivers, it needs libusb drivers, per Atmel ice cannot upload (but debug works) - #12 by pfeerick. Also make sure the USB device wasn’t auto-forwarded to the VM.

Later edit: Zadig fixed the issue on windows with avrdude not detecting the atmel-ice - just replaced the “Atmel-ICE CMSIS-DAP” WinUSB driver with libusb-win32, and it works fine. Atmel studio doesn’t like it, but it will be just a matter of swapping the drivers back if I want to use it.

Not sure about the driver reinstall procedure though once the drivers are replaced using Zadig – might need just a re-insert or executing a driver install from Atmel Studio again to get the old drivers.

Yes, sure!
I read the topic and I can change the driver on Windows.

On Windows, can I debug my code? On linux this is not possible without other tool as you told me before. An I right?

Thanks

Well the same holds for Windows, no built-in support for the atmel-ice + AVR combination. AVARICE seems to be working better in Linux though. But a Windows-compiled version is also available and some docs.

I replaced the dirvers using Zadig tool and now the error is:

Configuring upload protocol…
AVAILABLE: atmelice_isp
CURRENT: upload_protocol = atmelice_isp
Looking for upload port…
Uploading .pio\build\ATmega328P\firmware.hex
avrdude: usbdev_send(): wrote -22 out of 7 bytes, err =
avrdude: jtag3_send(): failed to send command to serial port
avrdude: failed to sync with the JTAGICE3 in ISP mode

avrdude done. Thank you.

*** [upload] Error 1
================================================================================================== [FAILED] Took 1.48 seconds ==================================================================================================
The terminal process “C:\Users\fi.platformio\penv\Scripts\pio.exe ‘run’, ‘–target’, ‘upload’, ‘–environment’, ‘ATmega328P’” terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

Have you tried other LibUSB drivers or WinUSB in Zadig, and they give the same error?

You might also try to reduce the programming speed by removing the old upload_flags line and adding

upload_flags = 
  -e 
  -B10

where 10 means a bit-time of 10µS. Try higher values if needed.

You can also run the “Advanced → Verbose Upload” task in VSCode to see more information.

I tried using WinUsb from Zadig.

This is my platformio.ini

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

but I still get the error:

avrdude: usbdev_send(): wrote -22 out of 7 bytes, err =
avrdude: jtag3_send(): failed to send command to serial port
avrdude: failed to sync with the JTAGICE3 in ISP mode

avrdude done. Thank you.

I’ve looked through the google results for avrdude: usbdev_send(): wrote -22 out of 7 bytes, err = but couldn’t find a definitive answer. Some say their programmer is bad, others say the -B bitrate fixed it, others say the installed drivers from Atmel Studio still interfered. No idea why avrdude is throwing that, and since I don’t have the programmer hardware I also can’t verify if it usually works. You may submit a bug request to AVR Downloader/UploaDEr - Bugs: Browse Items [Savannah] but since this is obviously not working well at all I’d recommend to either try pure Linux without the VM to see if Windows drivers are the issue or really Atmel Studio.

Thanks for the explanation.

The idea to use a VM is due to easly handling PC swapping and to be compatible to the initial development process that uses makefile.

Is it possible to debug an ATmega328P with a probe different from Atmel Ice?

Do you have any suggestions?

BR,
Federico