My ATMEGA328P has a deathwish!!

I'm really struggling with the bootloader on my ATMEGA328P overwriting itself like it has a deathwish! I've tried for weeks now to stop it. Any help or advice would save me from milling this chip down into a fine powder... :wink:

What I can do:

  • I can successfully programme the chip using avrdude and an Arduino UNO as an ISP i.e. I can re-write the bootloader
  • I can successfully upload sketches from Arduino IDE v1.6.4 to the chip using a serial FTDI cable and the optiboot bootloader

Here's where it breaks...everytime I upload a sketch, it overwrites the bootloader. How do I know this? Post upload, the reset button just begins the sketch, no more 'three flashes' when you press the reset button and the IDE can no longer upload sketches over FTDI.

What I've tried:

  • Modified board.txt to decrease the flash available for application programming, from 32,256 (512 bytes for bootloader) to 30,270 (2048 bytes for bootloader)
  • Modified hfuse to increase Boot flash section size from 512 words (hfuse=DF) to 2048 words (hfuse=D9)
  • Manually unlocked lock fuse (lock=3F) pre-flash, then manually locked post flash (lock=0F)
  • Upgraded to the new optiboot v6.2

All the usual teething problems have been resolved (reset capacitor, power decoupling, baud rates for programming and uploading etc)

Arduino IDE config:

Board: ATmega328P on a breadboard (8 MHz internal clock) .......see below for parameters
Port: COM6 (FTDI cable)
Programmer: AVR ISP

Verbose upload output uploading the standard 'blink' sketch using Arduino IDE v1.6.4 follows:

Sketch uses 1,030 bytes (3%) of program storage space. Maximum is 32,256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for local variables. Maximum is 2,048 bytes.
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM6 -b57600 -D -Uflash:w:C:\Users\David\AppData\Local\Temp\build7596630527965589035.tmp/Blink.cpp.hex:i 

avrdude: Version 6.1, compiled on Mar 13 2014 at 00:09:49
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM6
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 6.2
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f
avrdude: reading input file "C:\Users\David\AppData\Local\Temp\build7596630527965589035.tmp/Blink.cpp.hex"
avrdude: writing flash (1030 bytes):

Writing | ################################################## | 100% 0.57s

avrdude: 1030 bytes of flash written
avrdude: verifying flash memory against C:\Users\David\AppData\Local\Temp\build7596630527965589035.tmp/Blink.cpp.hex:
avrdude: load data flash data from input file C:\Users\David\AppData\Local\Temp\build7596630527965589035.tmp/Blink.cpp.hex:
avrdude: input file C:\Users\David\AppData\Local\Temp\build7596630527965589035.tmp/Blink.cpp.hex contains 1030 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.48s

avrdude: verifying ...
avrdude: 1030 bytes of flash verified

avrdude done.  Thank you.

Boards.txt entry follows:
(note: restored the changes described above because they had no effect):

##############################################################

atmega328bb.name=ATmega328P on a breadboard (8 MHz internal clock)

atmega328bb.upload.tool=arduino:avrdude
atmega328bb.upload.protocol=arduino
atmega328bb.upload.maximum_size=32256
atmega328bb.upload.maximum_data_size=2048
atmega328bb.upload.speed=57600

atmega328bb.bootloader.tool=arduino:avrdude
atmega328bb.bootloader.low_fuses=0xE2
atmega328bb.bootloader.high_fuses=0xDF
atmega328bb.bootloader.extended_fuses=0x05
atmega328bb.bootloader.unlock_bits=0x3F
atmega328bb.bootloader.lock_bits=0x0F
atmega328bb.bootloader.file=arduino:optiboot_atmega328.hex

atmega328bb.build.mcu=atmega328p
atmega328bb.build.f_cpu=8000000L
atmega328bb.build.board=AVR_ATMEGA328P
atmega328bb.build.core=arduino:arduino
atmega328bb.build.variant=arduino:standard

HFuse is wrong - should be 0xDE. DF (specifically the unprogrammed BOOTRST bit, bit 0) tells it there's no bootloader, so it doesn't jump to the bootloader on reset, and only enters the bootloader if there's nothing else on the chip.

It is not clear what you are getting at here.

If you upload using a programmer including "Arduino UNO as an ISP" then it always erases the chip, including the bootloader. If you want to prevent this you have to prepare a special HEX file which includes the bootloader and upload that as one unit.

If uploading a sketch through the bootloader using the serial interface erases the bootloader, then that would be very peculiar indeed.

When the sketch was uploaded using serial, the bootloader was not erased. Both the sketch and the bootloader were then on the chip. But the reset vector was pointing to the sketch so the bootloader never launched after the first time. Normally the reset vector points at the bootloader and when the bootloader find nothing to do, it runs the sketch.