Page 1 of 1

How to unlock a LeoStick with a hung sketch (and why)

Posted: Sun May 13, 2012 1:53 pm
by marc
EDIT: If you use the latest board profile the tone() function and other hung sketch culprits now work correctly on newer LeoStick V2.0 models (the Getting Started page shows you how to identify your model.)

If you have an older V1.0 LeoStick then it is possible to update to V2.0.

Scroll straight to the blue text for the quick procedure further below, if you'd like to skip the background

Some background: The LeoStick has two sections to the program areas in flash memory, the bootloader and the sketch.

The bootloader:
There is a small protected space in the flash memory that runs for around 7 seconds just after the board is powered up or reset, this is called the bootloader.
The bootloader's job is firstly to be there and ready for the very first sketch to ever be loaded. Most boards ship with the bootloader pre-programmed and ready to receive a sketch, and the sketch area of the flash memory is blank.
The second job of the bootloader is to be there in case the sketch hangs or freezes up, and give an opportunity for the bootloader to recover from a 'hung' sketch.

When the bootloader is running you will see the Red LED 'breathing' every second or so, slowly fading dimmer and brighter.

About 7 seconds after powerup or reset, *if* the bootloader sees a sketch is loaded into the sketch area of the memory, the bootloader will jump to the sketch to start it running. The bootloader doesn't do any kind of checks of the sketch being ok before running, it just sees something there in memory.

The sketch area:
For the LeoStick, both the bootloader and the sketch sections can talk on USB and receive a new sketch. On the LeoStick, because the USB is built into the microcontroller and not a separate device like large boards, the running program is also responsible for talking on USB and receiving a new sketch.
And unlike with the bootloader, it's possible to change some things in a sketch that can freeze up or upset the USB communications from the hidden part of the sketch that talks on USB and is ready to accept a program.

The startup and bootloader timing:
1. The board is powered up or reset. The 7 second window of the bootloader begins. The Red LED is fading in and out.
2. The bootloader starts up USB and its USB Serial Port is identified to the PC. You'll soon see "Freetronics LeoStick (COMn)" where n is its serial port number.
3. It takes about 3 seconds for Windows to identify the bootloader. If you have the Windows Device Manager open to the Ports (COM & LPT) entry you'll see it appear there. Take note of the COM port number.
4. 4 seconds later, the 7 second bootloader time is up, and the bootloader jumps to the sketch. If the sketch memory is blank then the bootloader will just sit at this step and wait for the first ever sketch to be loaded.
5. The sketch is run. If the sketch is alright, it will run as normal.
At this point the board is now running the USB communications from the Arduino sketch, and will identify as the sketch USB side and a different COM port number.

Recovering from a locked up sketch at this stage:
1. If the sketch has something in it from stage 5. above that locks up the USB communications part of the 32u4 microcontroller, then it's not possible to load a new sketch at this point, the USB is not fully functional.
We have to get into the bootloader to recover it. Don't worry, this is what the bootloader is there for!
Blink is a good sketch to use in the recover process, it's simple and you can see when it is loaded ok. (File > Examples > Digital > Blink).
2. Open the Arduino IDE and open File > Examples > Digital > Blink.
3. Select Tools > Board > Arduino Leonardo
4. Now here's the trick. You have to firstly set the IDE to the bootloader's COM port number, then you have to program the board in the 4 seconds available when the bootloader is available as a COM port, and before the sketch starts at the 7 second mark.
These next steps are described as used in the Arduino 1.0 IDE and Windows OS, please adjust for Mac or Linux. Mac's or Linux connect to the bootloader's COM port faster than Windows so you have an extra 1-2 seconds.
5. With the Windows Device Manager open to Ports (COM & LPT), plug the LeoStick into the PC. You'll see it appear "Freetronics LeoStick (COMn)", note down this COM port number!
6. Remove the LeoStick and get ready in the Arduino IDE to select this COM port number within a few seconds after you insert the LeoStick again. The IDE will remember it for the new sketch loading next.
7. Plug in the LeoStick, within the 3-7 second time window after inserting it, select Tools > Serial Port and select the COM port number that the bootloader just had.

8. Now the Arduino IDE is pre-set to the bootloader's COM port number, and so long as we time the upload of the new Blink sketch, we're fixed.
Press Upload in the Arduino IDE, and somewhere between 0-5 seconds *after* pressing Upload, plug in the LeoStick.
For me this was plugging in the LeoStick 1-2 seconds after pressing Upload, but the time may vary depending on the computer's speed and Windows version.

That's all! With a few insertion time vs. Upload time adjustments, you should have Blink running on your board and sketches running again.

More info: The Arduino 1.0 IDE spends some time and the first 90% of its progress bargraph compiling the sketch, and then does the upload near the end. (the upcoming 1.0.1 IDE will again be faster and won't recompile everything before every upload)
We want to line up the LeoStick's available bootloader time window of 3 to 7 seconds after insertion, to match up with the point at which the Upload progress bargraph hits 90% and the Upload sketch event starts. The Upload will then be going straight into the bootloader and not the hung sketch, and you can reflash your board any time regardless of the running state of the sketch.

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Thu May 17, 2012 12:28 pm
by Stephen
It took a bit of timing practice, but got it - certainly a trap for newbies like me. Thanks.

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Mon May 21, 2012 11:12 pm
by Fractal
Interesting, and something that I kinda figured out in a roundabout way after a failed attempt at using the tone() library!.

Do you have any idea how the increasingly close to release/already released Arduino Leonardo attempts to attenuate this problem?
It seems that they've slightly modified the environment, such that you need release the reset button when you see the 'uploading' text, rather than sightly before.

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Wed Aug 29, 2012 3:16 am
by nonentity
Ah, that also explains why it does upload (for the most part) without needing to press reset. =)

Is there any way of adding in the 'hidden part of the sketch' manually? i.e something like an interrupt on the rx/tx pin, and a soft reset after a particular signal?

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Fri Sep 21, 2012 11:47 am
by swordfishBob
Arduino 1.0.1 seems to compile a bit faster.
I have to do this every time, so far at least. Same story both under Win7-64bit and XP.

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Sun Jan 06, 2013 11:36 pm
by bemarc
So I understand the issues here but am having trouble now due to the bootloader and sketch part of the leostick having different interfaces (PIDs?) and driver to windows.

The bootloader is using the freetronics driver and would load up on one com port, while the sketches would change to the Arduino driver on a new com port.

My PC will only show the second comm port for the sketch because the 4-5 seconds of the bootloader is not long enough for windows to load the drivers and show up in the arduino IDE. As a result I can never catch the bootloader comm port to upload a new sketch to.

What are my options?

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Sat Jan 12, 2013 5:26 am
by bemarc
Anyone listening?

Is there the option to recompile the original bootloader to give a longer time before the sketch is run?

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Thu Jan 24, 2013 6:40 am
by chiefpad
I made a video of my successful use of this workaround.

I hope it gets addressed soon, and I can take down the video.

Re: How to unlock a LeoStick with a hung sketch (and why)

Posted: Thu May 09, 2013 11:53 pm
by angusgr
If you use the latest board profile the tone() function now works correctly on newer LeoStick V2.0.

If you have an older V1.0 LeoStick then it is possible to update to V2.0.