Arduino Leonardo - Bad sketch inhibits booting

When it works

For an upload to start the Leonardo USB driver normally detects when USB port opened using 1200 baud. This makes Leonardo drop the USB port connection and instead present itself as another device - a boat loader.

The boot loader activates on another port and lives for 8 seconds or as long as it takes once an upload is started. When completed it presents itself as the initial USB port again.

When it fails
I uploaded a sketch initiating ADC interrupts but forgot to declare an ISR routine. The setup(); enabled ADC interrupt and kicked off the first reading. After that the device is stuck. It boats to this bad code and the USB “soft reset” is no longer working.

The only way “went out” the bad code out is to upload a healthy sketch by manually pulling the reset pin low just before the IDE/Studio tries to upload.

(Adafruit and similar “Pro Micro” devices require a fast double reset to make their unusually short bootloading timeout longer.)

Is there a way to avoid this hangup in a future USB driver?

I do not believe there is a way to avoid this anomaly when using a USB bootloader on the Leonardo-type boards. You can't do anything about it from a driver perspective, because the driver is on the PC and the problem is caused by malfunctioning code on the board. So you will always need to intervene with a manual reset.

USB-to-serial functionality is not offloaded to a separate chip like on the Uno. If something is not quite working right on the main processor of the Uno, it is not a problem because the separate USB-to-serial chip on the Uno resets the main processor. If something is not quite working right on the main processor of the Leonardo, you are in a predicament.

You can give yourself a more Uno-like experience by using a separate USB-to-serial adapter and put the Optiboot bootloader on the Leonardo. That will also give you more flash memory since Optiboot takes up less room than Caterina. If you would like to do that:

In the case with a missing ISR the device is not dead. It displays as a USB port. I think the 1200 baud reset should disable interrupts and reset the ones it does need for itself.

Anyone knows what steps are made to gain control and overwrite a running sketch?