[SOLVED] Arduino keeps overwriting bootloader

I'm using a Atmega32u4 in standalone mode. Chip is soldered to a PCB with a 16Mhz xtal and I've programmed the Sparkfun Caterina-promicro16.hex bootloader.

I previously tried the Caterina-Leonardo.hex from Arduino but when I upload sketches I have to keep resetting the processor before the upload would start (very tricky timing and a pain when writing / debugging code with lots of uploads)

It seems I don't have this issue with the Sparkfun version.

Programming bootloader:
avrdude -c usbtiny -p m32u4 -U flash:w:Caterina-promicro16.hex

The Sparkfun bootloader uploads fine. I then plug the device in the PC and can see it on USB:

[371282.346575] usb 6-2: new full-speed USB device number 20 using uhci_hcd
[371282.760661] usb 6-2: New USB device found, idVendor=1b4f, idProduct=9205, bcdDevice= 0.01
[371282.760664] usb 6-2: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[371282.760666] usb 6-2: Product: Pro Micro 5V
[371282.760668] usb 6-2: Manufacturer: SparkFun Electronics

The problem I'm having now is when I upload a sketch using Arduino IDE 1.8.13 (Same issue with PlatformIO) is the sketch uploads fine but the bootloader is also overwritten and I"m back to the reset problem.
After upload of the sketch and replugging into USB:

[371521.089317] usb 6-2: New USB device found, idVendor=2341, idProduct=8036, bcdDevice= 1.00
[371521.089320] usb 6-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[371521.089322] usb 6-2: Product: Arduino Leonardo
[371521.089324] usb 6-2: Manufacturer: Arduino LLC

How can I stop the bootloader from being overwritten and keep the Sparkfun bootloader?

Debug of the upload on Arduino if it helps:

Sketch uses 6118 bytes (21%) of program storage space. Maximum is 28672 bytes.
Global variables use 485 bytes (18%) of dynamic memory, leaving 2075 bytes for local variables. Maximum is 2560 bytes.
Forcing reset using 1200bps open/close on port /dev/ttyACM0
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
PORTS {/dev/ttyACM0, /dev/ttyS0, } / {/dev/ttyACM0, /dev/ttyS0, } => {}
Uploading using selected port: /dev/ttyACM0
/home/greg/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/bin/avrdude -C/home/greg/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf -v -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:/tmp/arduino_build_198866/i2c_scanner.ino.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 "/home/greg/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17/etc/avrdude.conf"
         User configuration file is "/home/greg/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyACM0
         Using Programmer              : avr109
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         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  9000  9000 0x00 0x00
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 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 : butterfly
         Description     : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: devcode selected: 0x44
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: reading input file "/tmp/arduino_build_198866/i2c_scanner.ino.hex"
avrdude: writing flash (6118 bytes):

Writing | ################################################## | 100% 0.53s

avrdude: 6118 bytes of flash written
avrdude: verifying flash memory against /tmp/arduino_build_198866/i2c_scanner.ino.hex:
avrdude: load data flash data from input file /tmp/arduino_build_198866/i2c_scanner.ino.hex:
avrdude: input file /tmp/arduino_build_198866/i2c_scanner.ino.hex contains 6118 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.10s

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

avrdude done.  Thank you.

Always seems to happen, I post a question and then I find the answer :slight_smile:

I reprogrammed the bootloader using the Arduino IDE (Using a usbtiny) and its fixed the issue. A quick read up looks like it was a fuse issue. Initially I was just programming the .hex file using arvdude and not setting any fuses.

Arduino IDE takes care of the fuses and its also fixed my issue with having to reset before uploading. Working perfectly with the standard bootloader now :slight_smile: