Search code examples
visual-studio-codeavratmelplatformio

How to setup Atmel-ICE to program an ATmega328 on PlatformIO with VSCode on Windows 10


I am trying to use my atmel ICE with platformIO and VS Code instead of Atmel Studio, as this software is a pain when I want to use Arduino.h with libraries.

However I cannot upload with my programmer. I tried with a 32 pin ATmega328.

I first used PIO's documentation on how to use Atmel-ICE with it on this page: https://docs.platformio.org/en/latest/plus/debug-tools/atmel-ice.html

If you would like to use this tool for firmware uploading, please change upload 

protocol:
[env:myenv]
platform = ...
board = ...
debug_tool = atmel-ice
upload_protocol = atmel-ice

So this is what I wrote in my file:

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

And I get this message as I run the upload command on PIO:

DATA:    [          ]   0.4% (used 9 bytes from 2048 bytes)
PROGRAM: [          ]   1.4% (used 444 bytes from 32256 bytes)
Configuring upload protocol...
AVAILABLE: atmel-ice
CURRENT: upload_protocol = atmel-ice
Looking for upload port...
Error: Please specify `upload_port` for environment or use global `--upload-port` option.
For some development platforms it can be a USB flash drive (i.e. /media/<user>/<device name>)
*** [upload] Explicit exit, status 1

At that point I am not sure what to try. I tried a couple things described below but I am not sure I am going on the right track. The atmel ICE programmer, compared to an Arduino for example, doesn't have a USB PORT number associated with it, so I don't know how I am supposed to specify my upload_port argument in my .ini file.

There are some options described on the platformIO documentation https://docs.platformio.org/en/latest/projectconf/section_env_upload.html but I am completely lost. I feel like the message I get : For some development platforms it can be a USB flash drive (i.e. /media//) can be a good clue. Any Idea what should I change in my .ini file to make my atmel ICE work? thanks!


Other things I tried: I read on a post (I'm sorry I cannot find the link anymore) that adding

upload_port = usb

can solve the issue. When adding this line to my platformio.ini, I get a different error message:

avrdude: Can't find programmer id "atmel-ice"

Valid programmers are:
  2232HIO          = FT2232H based generic programmer
  4232h            = FT4232H based generic programmer
  arduino          = Arduino

And the list goes on..

I searched and found this post, which has the same error message!: https://community.platformio.org/t/atmega-328-internal-8mhz-oscillator-atmel-ice-upload-troubles/10416

The solution here was to add

upload_flags = -e 

But it didn't change anything for me.

He also mentioned he made changes to the 328p8m.json file, which would be ATmega328P.json in my case. I don't even know where to find it. Would that be relevant?


Solution

  • Paste this in platformio.ini

    [env:ATmega328P]
    platform = atmelavr
    board = ATmega328P
    framework = arduino
    upload_protocol = atmelice_isp
    upload_flags = -e
    upload_port = usb
    
    board_fuses.hfuse = 0xDF    ;smallest flash section size 256 since not using a BSL w/ Atmel-ICE 
    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