platformio / platform-atmelmegaavr

Atmel megaAVR: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelmegaavr
Apache License 2.0
29 stars 22 forks source link

Fuse programming doesn't work #34

Closed maxgerhardt closed 2 years ago

maxgerhardt commented 3 years ago

Per https://community.platformio.org/t/set-fuses-on-atmega4809/24681.

When using the standard platformio.ini

[env:nano_every]
platform = atmelmegaavr
board = nano_every
framework = arduino

with a Nano Every board, normal uploading works:

>pio run -t upload -v
[...]
Auto-detected: COM11
Forcing reset using 1200bps open/close on port COM11
avrdude -v -p atmega4809 -C C:\Users\Max\.platformio\packages\tool-avrdude-megaavr@1.60300.191015\avrdude.conf -c jtag2updi -b 115200 -V -e -D -P "COM11" -U flash:w:.pio\build\nano_every\firmware.hex:i
avrdude: Version 6.3-20190619
         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-megaavr@1.60300.191015\avrdude.conf"

         Using Port                    : COM11
         Using Programmer              : jtag2updi
         Overriding Baud Rate          : 115200
JTAG ICE mkII sign-on message:
Communications protocol version: 1
M_MCU:
  boot-loader FW version:        1
  firmware version:              6.00
  hardware version:              1
S_MCU:
  boot-loader FW version:        1
  firmware version:              6.00
  hardware version:              1
Serial number:                   00:00:00:00:00:00
Device ID:                       JTAGICE mkII
[..]
avrdude: 254 bytes of flash written

avrdude: safemode: Fuses OK (E:FF, H:FF, L:FF)

avrdude done.  Thank you.

============================================= [SUCCESS] Took 3.44 seconds =============================================

However, programming fuses per documentation, it does not work

>pio run -t fuses -v
[...]
Selected fuses:
------------------------
[fuse2 / OSCCFG  = 0x01]
[fuse5 / SYSCFG0 = 0xC9]
[fuse8 / BOOTEND = 0x00]
[lfuse / LOCKBIT = 0xC5]
------------------------

avrdude -p atmega4809 -C C:\Users\Max\.platformio\packages\tool-avrdude-megaavr@1.60300.191015\avrdude.conf -v -P usb -c jtag2updi -Ufuse2:w:0x01:m -Ufuse5:w:0xC9:m -Ufuse8:w:0x00:m -Ulock:w:0xC5:m

avrdude: Version 6.3-20190619
         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-megaavr@1.60300.191015\avrdude.conf"

         Using Port                    : usb
         Using Programmer              : jtag2updi
avrdude: usbdev_open(): did not find any USB device "usb" (0x03eb:0x2103)

avrdude done.  Thank you.

*** [fuses] Error 1
============================================== [FAILED] Took 1.12 seconds ==============================================

The firmware upload used a 1200bps reset and the avrdude invocation has -P "COM11" in it. The fuses setting does not do such a reset and uses -P usb.

I think it should be generally possible to set the fuses of the underlying ATMega4809 on the Nano Every using the on-board programmer (jtag2updi), but PlatformIO invokes avrdude in a wrong way.

Additionally, in the non-verbose mode (pio run -t fuses), the critical error message did not find any USB device does not appear.

Setting fuses...

avrdude done.  Thank you.

*** [fuses] Error 1
============================================== [FAILED] Took 1.14 seconds ==============================================

Leaving the user extremely confused why the task failed although avrdude says avrdude done. Thank you..