Skip to content

-D (disable auto‑erase) is unconditionally added to avrdude flags and breaks usbasp flash process #351

Description

@sstefanov

Summary

builder/main.py unconditionally appends -D to UPLOADERFLAGS for every AVR
upload, regardless of upload_protocol. This is correct for bootloader-based
uploads (Optiboot/stk500v1/v2) but silently corrupts flash when the upload
protocol is an ISP programmer (usbasp, usbtiny, arduino-as-isp,
stk500v2, avrispmkII, ...), because no chip‑erase happens and AVR flash
bits cannot transition from 0 back to 1 without an erase.

Location

builder/main.py:

env.Append(UPLOADERFLAGS=["-D"])

Reproduction

  1. Configure any AVR env with upload_protocol = usbasp (or any ISP).
  2. Flash firmware A.
  3. Flash firmware B that has any 1 bit at a flash address where A had a 0.
  4. avrdude reports verification error, first mismatch at byte 0x… with the
    read value showing extra zero bits.

Why it’s subtle

  • Successful first‑time flashes (or flashes after a manual chip‑erase) work fine.
  • Subsequent re‑flashes only fail at bytes whose new value introduces a 1
    where the old image had a 0. Failures appear random / intermittent and are
    often misattributed to USBasp signal noise.
  • Skipping verify (-V) hides the corruption — code is actually wrong on the chip.

Proposed fix

Detect ISP-style upload protocols and omit -D (or add -e to force chip
erase) for them. Roughly:

ISP_PROTOCOLS = {"usbasp", "usbtiny", "stk500v2", "avrispmkII",
                 "arduino-as-isp", "atmelice_isp", "dragon_isp",
                 "jtag2isp", "jtag3isp", "pickit4_isp", "snap_isp"}

protocol = env.subst("$UPLOAD_PROTOCOL")
if protocol in ISP_PROTOCOLS:
    env.Append(UPLOADERFLAGS=["-e"])         # explicit chip erase
else:
    env.Append(UPLOADERFLAGS=["-D"])         # bootloader path

Environment

  • platform-atmelavr: <version from pio platform show atmelavr>
  • PlatformIO Core: <pio --version>
  • avrdude: <avrdude -v first line>
  • OS: <e.g. Linux>
  • Programmer: USBasp clone, firmware bcdDevice 1.04
  • Target: ATmega1284P (Sanguinololu/Anet A8 mainboard)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions