Upload and Debug for ATmega328P with Atmel ICE

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

As said above, with the avr-stub method for example, this just requires a serial port device but in turn you can’t use the hardware UART in the debugged firmware / must disable it (How to debug on Arduino mega 2560 - #2 by maxgerhardt). dwire would be another thing for debugging, unsure if it’s for uploading too. Programming can be done with a multitude of devices, basically everything AVRdude supports, as see per docs Atmel AVR — PlatformIO latest documentation.

Hi @maxgerhardt,

after reading the link you posted, I understand that if I use the debug with avr-stub is not possible to use the USART module into the code. This could be a problem because my code uses the USART to communicate with another MCU and the communication must be in place in order to execute the code.

I’m sorry for my many questions but I always work on a different kind of MCU (usually STM32 with other IDE) and the debug is supported natively by the IDE and by the programmer.

In this particular project, I switched to ATmega238P but I looking to find a way to debug in a way similar to other MCU.

Unfortunately Atmel Studio works only on Windows.

So I think the only way to debug in a similar way is to change the programmer/debugger. Am I right?
Or there is not a programmer/debugger to debug that without using avr-stub?

UPDATE: I tried the debugging functionality on my Arduino UNO board (same ATmega328P) and everything works with ‘avr-stub’. And it was very simple.

I need to figure out how to debug my system in a simular way.

Thanks for your time!

Best regards.

It’s also about hardware USART here. When working with e.g. Arduino, the SoftwareSerial library is available (with some baud rate and format restrictions though), so one can move the original Serial output to a software serial pretty easily and thus enable hardware debugging – or, one could also think about moving the GDB serial interface to a software serial and freeing up the original software serial that way.

Hello @maxgerhardt,

now it is clear. I will try it.

Just an update (another): I tried to debug my project on ArduinoUNO board with ‘avr-dude’ but I get this error when I start a debug session:

immagine

I think the code inside my project (developed from another person) interfers with GBD.

Thank you.

If the blinky-code from the topic I’ve linked above works avr-gdb but your firmware code not, that should be the reason.

Yes, exactly. Your blink code works bwith avr-gdb, but my code not!

So since debugging with ‘avr-gdb’ doesn’t work the only thing I can do is to use Atmel Studio.

Thank you for your time and help!

BR,
Federico

Hi @maxgerhardt,

I try again to upload the firmware on Windows.

Now I get this error:

avrdude: jtag3_edbg_prepare(): unexpected response 0x81, 0x11
avrdude: jtag3_edbg_prepare(): unexpected response 0x02, 0x01
avrdude: jtag3_edbg_send(): Unexpected response 0x01, 0x00
avrdude: jtag3_edbg_recv(): Unexpected response 0x80
avrdude: failed to sync with the JTAGICE3 in ISP mode

I installed the WinUSB driver with Zading and tried to upload the firmware with this platformio.ini

[env:ATmega328P]
platform = atmelavr
board = ATmega328P
framework = arduino
upload_protocol = atmelice_isp
upload_port = usb
upload_flags = -e
board_upload.require_upload_port = no
board_build.f_cpu = 8000000L

Do you have any suggestions?

Thanks fro your time!

Best regards

I’ve seen the post AVR Debuggen met Atmel-ICE en MPLAB X IDE onder Linux which says this error can occur if debugWire has been enabled or is in use.

What does it say when you open a PIO CLI and do

C:\Users\<user>\.platformio\packages\tool-avrdude\avrdude -v  -C "C:\Users\<user>\.platformio\packages\tool-avrdude\avrdude.conf"  -c atmelice_isp  -p m328p

?

Other generic trouble-shooting methods would be:

Hi,

thanks for the reply.

The output is

avrdude.exe: 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\Federico\.platformio\packages\tool-avrdude\avrdude.conf"

         Using Port                    : usb
         Using Programmer              : atmelice_isp

avrdude.exe: usbdev_open(): Found Atmel-ICE CMSIS-DAP, serno: J41800043393
avrdude.exe: Found CMSIS-DAP compliant device, using EDBG protocol
avrdude.exe: jtag3_edbg_prepare(): unexpected response 0x81, 0x11
avrdude.exe: jtag3_edbg_prepare(): unexpected response 0x02, 0x01
avrdude.exe: jtag3_edbg_send(): Unexpected response 0x01, 0x00
avrdude.exe: jtag3_edbg_recv(): Unexpected response 0x80
avrdude.exe: failed to sync with the JTAGICE3 in ISP mode

avrdude.exe done. Thank you.

Now, I remember that my target could be in debugWire because yesterday I try to debug with Microchip studio.

Since I change the Atmel-ICE driver, so Microchip Studio did not recognize the programmer, can I disable the debugWire writing the DWEN fuse from platformio?

Thank you so much!