user4478: I was using Arduino-Makefile to upload code to a Leonardo via its standard make upload. However, it became "stuck" and unresponsive, as Leonardo's sometimes do. I tried power cycling it, and unplugging the USB cable, but nothing worked. So I tried re-burning the bootloader via an AVRISP MkII programmer and the Arduino 1.8.2 IDE, but that only made things worse.
It somehow uploaded a simple "blink" program, but now the it's completely unable to be programmed via USB. When I run make upload, or use the Arduino IDE's "upload" button, the Leonoardo's status LED begins blinking rapidly and its /dev/ttyACM0 device disappears permanently. I have to power-cycle it to make the device reappear, but the upload still fails, with the error:
/usr/share/arduino/bin/ard-reset-arduino --caterina  /dev/ttyACM0
make[1]: Leaving directory '/usr/local/myproject/main'
make do_upload
make[1]: Entering directory '/usr/local/myproject/main'
/usr/share/arduino/hardware/tools/avr/bin/avrdude -v -p atmega32u4 -C /usr/share/arduino/hardware/tools/avr/etc/avrdude.conf -D -c avr109 -b 57600 -P /dev/ttyACM0 \
        -U flash:w:build-leonardo/main.hex:i

avrdude: Version 6.3, compiled on Jan 17 2017 at 11:00:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/usr/share/arduino/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/chris/.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: .avrdude: butterfly_recv(): programmer is not responding

avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
Found programmer: Id = ""; type = 
    Software Version = .; Hardware Version = �.
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: buffered memory access not supported. Maybe it isn't
a butterfly/AVR109 but a AVR910 device?
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: butterfly_recv(): programmer is not responding
avrdude: error: programmer did not respond to command: leave prog mode
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: programmer did not respond to command: exit bootloader

If I manually run:
/usr/share/arduino/bin/ard-reset-arduino --caterina  /dev/ttyACM0

or upload via the Arduino IDE, it makes the Leonardo disappear from USB, and blink rapidly. Pressing the reset button does nothing. I have to manually unplug its USB cable to fix it.
I can program it still using the AVRISP MkII programmer...but obviously that's a huge inconvenience since it takes about 5 minutes just to upload the sample blink sketch, and even then it requires that I first power off the Leonardo, disconnect the programmer, and then power on. For development, where I might need to make dozens of revisions, this turnout is far too slow.
Why am I unable to program the Leonardo via avrdude and USB?

user4478: Using some examples and similar questions, such as this one and this one, I fixed udev rules for the AVRISP MkII programmer, then I manually reflashed the Leonardo's bootloader with with the MkII by running:
avrdude -c avrispmkii -p m32u4 -P usb -B 8 -u -e -U lock:w:0x3F:m -v
avrdude -c avrispmkii -p m32u4 -P usb -v
avrdude -c avrispmkii -p m32u4 -P usb -u -U efuse:w:0xcb:m -v
avrdude -c avrispmkii -p m32u4 -P usb -u -U hfuse:w:0xd8:m -v 
avrdude -c avrispmkii -p m32u4 -P usb -u -U lfuse:w:0xFF:m -v 
avrdude -c avrispmkii -p m32u4 -P usb -U flash:w:/usr/share/arduino/hardware/arduino/avr/bootloaders/caterina/Leonardo-prod-firmware-2012-12-10.hex -v
avrdude -c avrispmkii -p m32u4 -P usb -U lock:w:0x0F:m -v

I'm not sure if the first and last command to write the lock bytes were necessary, since they returned the verification error; content mismatch error, but they didn't seem to break anything either.
Oddly, this ran in seconds, whereas the Arduino IDE took almost 5 minutes to burn the bootloader. So I suspect there's a bug in the Arduino IDE that burns an incorrect or corrupted bootloader.
After running this, ard-reset-arduino works correctly, and make upload can successfully upload a sketch again.
On a side note, I'm not 100% sure if Leonardo-prod-firmware-2012-12-10.hex was the correct bootloader to use. Again, I just made an educated guess based on naming. I'd read that the Leonardo uses the "caterina" bootloader, and the directory /usr/share/arduino/hardware/arduino/avr/bootloaders/caterina contained several .hex files, only three of which contained "Leonardo" in the name:
Caterina-Leonardo.hex
Leonardo-prod-firmware-2012-04-26.hex
Leonardo-prod-firmware-2012-12-10.hex

It seemed logical to not use Leonardo-prod-firmware-2012-04-26.hex, since that was likely an old and obsolete bootloader based on the date. That left Caterina-Leonardo.hex or Leonardo-prod-firmware-2012-12-10.hex. Unfortunately, checking the commit logs shows they're exactly the same age. In my experience, prod tends to mean the final or production product, so I decided to try that one first and it worked. If anyone knows what Caterina-Leonardo.hex is for and how it differs from the others, please tell me. I don't want to risk breaking my Leonardo again experimenting with this.
The only issue I'm having now is that after I upload my sketch, with uses ROS and about 49% of the Arduino's memory, I again can no longer upload new sketches to it. I'm assuming the sketch's use of the Serial port is somehow stopping ard-reset-arduino from properly resetting it`. However, I the Arduino IDE is still able to upload sketches, so as a clumsy workaround, I can use that to upload the "blink" sketch, afterwhich then I can upload my own revised sketch.

4478: <p>I was using Arduino-Makefile to upload code to a Leonardo via its standard <code>make upload</code>. However, it became "stuck" and unresponsive, as Leonardo's sometimes do. I tried power cycling it, and unplugging the USB cable, but nothing worked. So I tried re-burning the bootloader via an AVRISP MkII programmer and the Arduino 1.8.2 IDE, but that only made things worse.</p>

<p>It somehow uploaded a simple "blink" program, but now the it's completely unable to be programmed via USB. When I run <code>make upload</code>, or use the Arduino IDE's "upload" button, the Leonoardo's status LED begins blinking rapidly and its <code>/dev/ttyACM0</code> device disappears permanently. I have to power-cycle it to make the device reappear, but the upload still fails, with the error:</p>

<pre><code>/usr/share/arduino/bin/ard-reset-arduino --caterina  /dev/ttyACM0
make[1]: Leaving directory '/usr/local/myproject/main'
make do_upload
make[1]: Entering directory '/usr/local/myproject/main'
/usr/share/arduino/hardware/tools/avr/bin/avrdude -v -p atmega32u4 -C /usr/share/arduino/hardware/tools/avr/etc/avrdude.conf -D -c avr109 -b 57600 -P /dev/ttyACM0 \
        -U flash:w:build-leonardo/main.hex:i

avrdude: Version 6.3, compiled on Jan 17 2017 at 11:00:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/usr/share/arduino/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/chris/.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: .avrdude: butterfly_recv(): programmer is not responding

avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
Found programmer: Id = ""; type = 
    Software Version = .; Hardware Version = �.
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: buffered memory access not supported. Maybe it isn't
a butterfly/AVR109 but a AVR910 device?
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: butterfly_recv(): programmer is not responding
avrdude: error: programmer did not respond to command: leave prog mode
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: programmer did not respond to command: exit bootloader
</code></pre>

<p>If I manually run:</p>

<pre><code>/usr/share/arduino/bin/ard-reset-arduino --caterina  /dev/ttyACM0
</code></pre>

<p>or upload via the Arduino IDE, it makes the Leonardo disappear from USB, and blink rapidly. Pressing the reset button does nothing. I have to manually unplug its USB cable to fix it.</p>

<p>I can program it still using the AVRISP MkII programmer...but obviously that's a huge inconvenience since it takes about 5 minutes just to upload the sample blink sketch, and even then it requires that I first power off the Leonardo, disconnect the programmer, and then power on. For development, where I might need to make dozens of revisions, this turnout is far too slow.</p>

<p>Why am I unable to program the Leonardo via avrdude and USB?</p>
 4478: <p>Using some examples and similar questions, <a href="https://arduino.stackexchange.com/questions/8564/unable-to-flash-bootloader-on-atmega32u4-with-avr-dragon">such as this one</a> and <a href="https://stackoverflow.com/a/5414566/247542">this one</a>, I fixed udev rules for the AVRISP MkII programmer, then I manually reflashed the Leonardo's bootloader with with the MkII by running:</p>

<pre><code>avrdude -c avrispmkii -p m32u4 -P usb -B 8 -u -e -U lock:w:0x3F:m -v
avrdude -c avrispmkii -p m32u4 -P usb -v
avrdude -c avrispmkii -p m32u4 -P usb -u -U efuse:w:0xcb:m -v
avrdude -c avrispmkii -p m32u4 -P usb -u -U hfuse:w:0xd8:m -v 
avrdude -c avrispmkii -p m32u4 -P usb -u -U lfuse:w:0xFF:m -v 
avrdude -c avrispmkii -p m32u4 -P usb -U flash:w:/usr/share/arduino/hardware/arduino/avr/bootloaders/caterina/Leonardo-prod-firmware-2012-12-10.hex -v
avrdude -c avrispmkii -p m32u4 -P usb -U lock:w:0x0F:m -v
</code></pre>

<p>I'm not sure if the first and last command to write the lock bytes were necessary, since they returned the <code>verification error; content mismatch</code> error, but they didn't seem to break anything either.</p>

<p>Oddly, this ran in seconds, whereas the Arduino IDE took almost 5 minutes to burn the bootloader. So I suspect there's a bug in the Arduino IDE that burns an <strong>incorrect or corrupted bootloader</strong>.</p>

<p>After running this, <code>ard-reset-arduino</code> works correctly, and <code>make upload</code> can successfully upload a sketch again.</p>

<p>On a side note, I'm not 100% sure if <code>Leonardo-prod-firmware-2012-12-10.hex</code> was the correct bootloader to use. Again, I just made an educated guess based on naming. I'd read that the Leonardo uses the "caterina" bootloader, and the directory <code>/usr/share/arduino/hardware/arduino/avr/bootloaders/caterina</code> contained several .hex files, only three of which contained "Leonardo" in the name:</p>

<pre><code>Caterina-Leonardo.hex
Leonardo-prod-firmware-2012-04-26.hex
Leonardo-prod-firmware-2012-12-10.hex
</code></pre>

<p>It seemed logical to not use <code>Leonardo-prod-firmware-2012-04-26.hex</code>, since that was likely an old and obsolete bootloader based on the date. That left <code>Caterina-Leonardo.hex</code> or <code>Leonardo-prod-firmware-2012-12-10.hex</code>. Unfortunately, checking the <a href="https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/bootloaders/caterina" rel="nofollow noreferrer">commit logs</a> shows they're exactly the same age. In my experience, <code>prod</code> tends to mean the final or production product, so I decided to try that one first and it worked. If anyone knows what <code>Caterina-Leonardo.hex</code> is for and how it differs from the others, please tell me. I don't want to risk breaking my Leonardo again experimenting with this.</p>

<p>The only issue I'm having now is that after I upload my sketch, with uses ROS and about 49% of the Arduino's memory, I again can no longer upload new sketches to it. I'm assuming the sketch's use of the Serial port is somehow stopping <code>ard-reset-arduino</code> from properly resetting it`. However, I the Arduino IDE is still able to upload sketches, so as a clumsy workaround, I can use that to upload the "blink" sketch, afterwhich then I can upload my own revised sketch.</p>
