OMG, I did the dumb thing. I was getting frustrated with some keyboard emulation issues and dashed off a "quick, simple" sketch to test one particular continuous keypress issue...
Problem is, I was in a hurry and stupidly forgot that essential feature, a Stop switch.
Now I have this sketch which does nothing but emit keystrokes, and I can't download another sketch! Is there a standard recovery path for unbricking a Leonardo that has been accidentally turned into a USB keystroke spraygun? I tried the "hold down reset, start upload, release reset" trick many times, but it never worked; the loaded sketch immediately took over the usb link and the UL failed every time.
I really did know better than to do that. Embarrassing! Can I use avr "somehow" (I'm way out of my comfort zone here, but I guess it's time to learn some new tricks) and reflash a factory Leonardo setup? I read something about a procedure involving a 2nd Arduino and SPI and... got out of my depth fast, didn't understand most of what I was reading.
If you have another Arduino or an ISP programmer you can always burn the bootloader, which will erase the problem sketch from the Leonardo and thus recover it back to working again. I can provide instructions if you want to try that, just let me know what you're going to be using as the programmer.
which seems to explain it all, even for a bear of very little brain such as myself.
I have ordered a 2nd Leonardo. When it arrives I will follow the instructions in the link above. It sounds like anyone who has a spare working Leo can recover from this nightmare pretty quickly.
I believe that other avenues are closed to me, as I am working with OSX. I don't think the Atmel programmer works with OSX, so using a Leo to fix a Leo seems to be the way to go. I have a Uno but doubt that its default bootloader would be correct for a Leo (different chip), so I'll play it safe and use my new Leo when it arrives.
That really was stupid. I can't believe I did that.
The Uno will work fine and actually may be easier to work with because the last I checked it was a bit more complicated to use an ATmega32U4 based board such as Leonardo for Arduino as ISP, though it certainly can be done. I'm pretty sure nothing has changed since then.
The Uno is used only as an ISP Programmer, you will still be burning the Leonardo bootloader to the Leonardo
You will need either male to female jumper cables or female to female jumper cables. Do you have these on hand, and if so which ones? The wiring instructions are a bit different depending on this.
@pert I have every kind of jumper cable, F/F F/M M/M... thought I would go direct from icsp header to icsp header using F/F cables. I do have an Uno. but I'm a bit puzzled about how I tell the IDE (or the Uno) how to send a Leonardo bootloader to the target Leo. being such a n00b I am scared to get it wrong and maybe brick the Uno as well.
I think I've understood the wiring part -- connect the icsp pins appropriately between the 2 duinos, plug the Uno (or healthy Leo) into USB port on host, and "somehow" tell the IDE that I want the Uno (or healthy Leo) to flash the insane Leo. I'm not quite clear on the IDE interaction, have to read the instructions again very closely.
From the instructional post I found, I thought the ISP programmer was replicating its own bootloader onto the target so it had to be the same flavour of duino as the target. But what you're saying is that I have everything I need already (one sane Uno, one crazy Leo, one working IDE host) and could fix my problem right now if I can just get the steps right. That's encouraging... (takes deep breath) think I'll give it a try.
@spycatcher2k -- I gave that a try but I guess I'm not fast enough, couldn't get it to work.
@pert -- YES! I have recovered my Leonardo and I'm really chuffed. I wired it as shown in the thread I quoted above, then did my best to follow your advice plus those instructions, and shazzam -- my Uno did indeed work as a programmer and burn the Leo default bootloader onto the afflicted board. it started out in Blinky sketch (I guess that is factory default) but I am able to load any of my sketches onto it now, all is restored to normal.
[update] oops, not quite. my Leo has lost its USB HID functionality. I fear it has become a Uno! it can no longer emit keystrokes. I did have "Leonardo" selected when I did the "Burn bootloader" operation... guess I will try the whole procedure again.
so I'm back to a fundamental question: do I need a Leo to reflash a Leo?
[update] tried again, careful step by step. I definitely had "Arduino Leonardo" selected as board type, and the reflash seemed to work OK (busy tx/rx on the Uno, then Blinky behaviour on the Leo). but the brainwashed Leo now has no USB HID capability. Keyboard.write does not work. Keyboard.press does not work. I think it has become an Uno! As soon as I receive my new Leo, I'll try again using the factory fresh Leo as the programmer, and see if that restores the HID features.
I can't imagine what would cause that. I just tried it using Arduino IDE 1.8.2 on my Leonardo to make sure nothing has changed since I tried it last and burning the Leonardo bootloader using an Uno as Arduino as ISP worked fine and I am still able to use the Keyboard library functions.
I connected all ICSP header pins from one board to another except for the reset pin. The reset pin on the Leonardo was connected to pin 10 on the Uno.
Thanks pert, I am realising now that it the problem is with my own test sketch (memo to self: always revert to the Examples library for sanity testing, never to my own code!) But the misbehaviour of my test sketch is very bizarre; call to analogRead of A5 seemed to disable HID. Too crazy -- it had to be a memory stomp of some kind, and I did eventually find it (coming from years with loosely typed languages, I find it hard to wrap my head around the stringent typing of cpp -- I committed a run-time array indexing overrun not detectible at compile time).
Glad to hear you found the problem and your Leonardo is restored! Now you can feel more confident playing around with the HID functions because it will be easy enough to recover from this problem if it happens again. If you are going to be using an ISP programmer frequently I recommend buying a 6 pin USBasp. You can get them for <$3 USD with free shipping on eBay and it's really convenient to be able to just plug the thing right into the ICSP header instead of having to set up an Arduino as ISP every time. It doesn't even faze me when I mess up my Leonardo or Pro Micro, I just grab the USBasp that lives on my desk and a minute later it's good as new.
@pert thanks for the cable recommendation, ordered a couple. I do feel much more confident now knowing that I can reflash my duinos any time. Most errors (ones that don't involve blue smoke, that is) are now recoverable. Many thanks for your help!