Arduino Boot-Cloner

The boot-cloner is a program compiled and burnt with the Arduino IDE, that copies it's boot flash memory onto a new IC, using ISP.

This is an idea I've had for a while. Instead of burning the bootloader onto new/extra ATmega8's with a separate device, use the Arduino (that has a bootloader already).
The need for this stems from college; the instructors want extra ATmega8's, and this would be the fastest way to get dozens of new ICs ready.

To read the boot flash memory, I'm using the header pgmspace.h function pgm_read_byte_near(), which seems to be working well.
The other part is understanding and implementing the ISP protocol, which I've had trouble finding detailed information on.

At the moment, I've stumbled across an unexpected problem.
On a fresh ATmega8, ISP behaves normally and MISO returns the bytes I've written. I've read Device Codes and everything is fine.
When I tried communicating thru ISP with an ATmega8 that already had the bootloader, I get nothing but 0xFF. I figure the lock bits are set by default, by whoever burned the bootloader at Sparkfun.

Any suggestions on where to find detailed ISP information would be helpful :slight_smile:

"When I tried communicating thru ISP with an ATmega8 that already had the bootloader, I get nothing but 0xFF."
The AVR being programmed through ISP still requires a clock source, unless the fuse bits are set to use the internal 1MHz oscillator. Because xtal1/2 weren't connected, the chip was frozen; not locked.

Wow, I finally realized the ISP programming details are in the ATmega8 documentation. That makes life so much easier. (doc2486)

really nice idea!

i wonder if would be usable to burn other hex files than the bootloader. sometimes i'd like to get rid of the bootloader and have my programs start at power-on. this could finally be a convenient solution.

good luck.

kuk

"i wonder if would be usable to burn other hex files than the bootloader."
It could be modified to do that, but it'd be easier for most people to buy/build a parallel port or serial port ISP programmer.

The boot cloner just copies it's own flash memory onto another microcontroller; no computer required.

However, if the Arduino programmer's bootloader was modified to write incoming data to another microcontroller (instead of itself) then you could still use the Arduino IDE to burn programs, without the bootloader taking up any memory. That'd be a similar but separate project; piggy-backing code to a second microcontroller.

It's finished. Burning a bootloader takes about 2 seconds. No computer is required once it's setup.
The running program indicates it's status two ways, Idle/Run/Error LEDs and serial messages.
Instead of constantly resetting the Arduino, I've incorporated a simple push button to start the programming process.

Anybody want the simple schematic and commented source code?

Would Arduino's website host an odd project like this? How should I go about doing that?

Anybody want the simple schematic and commented source code?

Yes!

Would Arduino's website host an odd project like this? How should I go about doing that?

This is a perfect thing for the Arduino Playground. It is a wiki for anyone to put stuff like this:

http://www.arduino.cc/playground/

You need to create a userid/password. Most people make it the same userid as you use here, but it is a separate system from this message board. I have written a couple of things in it; the syntax is very easy to learn. You can always look at source code for other pages to see how things are done. I can help out if you get stuck... just let me know.

Very interesting!

You can put it online yourself on the arduino playground... anybody can sign up and post content!

send us a link when you're done

massimo

OMG! An arduino virus! :smiley:

I'm glad there's people interested in it.

Here it is: an explanation, the schematic and source code.
http://www.arduino.cc/playground/BootCloner/BootCloner

I didn't add pictures of it on a breadboard; those parts are now being turned into a proper device, with an enclosure.
I'll probably amend a picture of the final device when I get that built.

There's other projects I'm either working on, or I've got done, that might also be suitable for the playground. Let me know which would be the best one to start on next, or if any shouldn't be added. Some are kind of redundant, or dependant on hardware that can't be purchased anymore.

  1. seven-segment display multiplexed clock(s) -- several variations (illustrates ways of using fewer IO pins)
  2. controller for a multiplexed 7x96 LED Moving Message Display (a complex design using sram to accelerate refreshing; it was a rebuilt device, the sign can't be bought anymore)
  3. "nokia knock-off" 128x128 lcd controller optimized for speed (PORTB galore); also demonstrates a fast 12 bit color filling trick and a way to get handy binary constants, with enum.
  4. my college capstone project: an inexpensive, built from scratch pda, with pseudo-wireless ethernet (to reduce cost, it's not true WiFi), dedicated to controlling an automated home (currently a work in progress, lots of potential for other projects)
  5. OLED 128x128 18 bit color display SPI controller (a work in progress)
  6. ATmega8 instruction set emulator - on an ATmega8 ~ for executing external code from ram, without constantly reburning the program flash ~ this'd be necessary for a pda's processor. (I haven't done it yet; largely it's inline assembly & some protocol for loading the code)
  7. handheld movie player, derived from the "built from scratch pda" with minimal hardware (something I'd really like to start making, after the pda is finished)
  8. using FPGA's as coprocessors (I'll be doing this in the next few months)
  9. if tied in with another Arduino project, because it's a bit off-topic, I've got a useful technique for surface mount etching extremely fine traces by hand. It's easy and I might be the only person that's figured this out. Ideal for surface mount leads and ribbon cable connectors. I've never seen an SMD part too small to make an adapter for, with this. Only requires basic etching supplies, needle or fine pointed tool, sharp eyes or magnifying glass, and maybe a tweezers.

Hi,

I would definitely be interested in your code/example for the "nokia knock-off" 128x128 lcd sold at Sparkfun.com. I got the unit working with BasisStamp, but I would much prefer to use it with Arduino, especially with the new Bluetooth board.

Thanks in advance. If you don't have time to setup a full tutorial, would it be possible to share hints for getting it to work your with Arduino. Is this tutorial (http://www.arduino.cc/en/Tutorial/SPIEEPROM) a good start?

Camille

It couldn't hurt, but keep in mind SPI is a generic description of a two or three wire data transfer connection. There are a lot of device specific things that have to be done.

I'll start making a tutorial for it, today.

Hi Amp,

thanks a lot for the info and link (via PM). It is really appreciated.

Camille

Hi Amp!

Your boot-cloner is damn interesting! I just have a question : you can clone the bootloader from an ATmega8 to another, but can you do it from an ATmega168 to another ATmega168 with a NG board?

Thanks a lot

melka

The boot cloner doesn't read the bootstrap from the device. It has the bootstrap code compiled into the sketch. You can stick in any bootstrap code you like with a handy program to format it into C source.

I didn't find anything in the boot cloner to do it, but this little bit of code will take a .hex file and format it nicely for inclusion in the bootcloner sketch.

#include <stdio.h>
#include <string.h>

main()
{
    char buf[4096];
    int n = 0;

    while( fgets(buf, sizeof(buf)-1, stdin)) {
        int len = strlen(buf);
        char *b;

        if ( len < 9) continue;
        buf[len-4] = 0;

        for ( b = buf+9; *b != 0; b += 2) {
            printf("0x%c%c, ", b[0], b[1]);
            n++;
        }
        printf("\n");
    }
    printf("/* %d bytes */\n", n);
}

Jims answered the previous question, and gave the code necessary to use different bootstraps.
If the ATmega168's bootloader is still around 1K, you could even use an ATmega8 to write it to the 168. (and other atmel microcontrollers)
You'll probably need to change the bootloader size, and memory location constants in the sketch.

David Cuartielles requested an explanation of the procedure used for cloning, so I've added that to the playground BootCloner article... I think I understood what he meant by procedure, in retrospect he might have wanted to source code desribed better. ::slight_smile:

The original bootloader table data in that sketch was extracted from the hex file by a small basic program. It was a quick and dirty program that I threw away afterwards.

I think this'll be a recurring issue, so here's what I plan to do in the next couple weeks.

Plan A: An elegant solution would be to modify the sketch so the plain text contents of a hex file can be pasted into the bootloader table. It might take too much program memory to do it this way. (1K vs 2.4K and the flash write code would be bigger)

If Plan A doesn't work;
Plan B: (in addition to jims program) Add Mac/Win/Linux applications for ihex data extraction; just to quickly get it compiled for multiple platforms with a friendly GUI. I'd write it in RealBasic: OS X Carbon, Win32 (95/98/2000/XP), and Linux (I forget which linux, exactly). I could make a VisualBasic 2005 (Win32 only) project, too - if somebody wants that. RB and VB are nearly identical, except RB can compile a project for multiple platforms. (I'm guilty of falling back on languages I've got the most experience with)

...Plan Z: Whip up another new project aptly named Fanout, which allows an Arduino to completely program any atmel microcontroller. It would use the same technique as the BootCloner, but instead of storing just the bootloader in flash memory it'd write a lot more than 2K, by listening to the serial port and programming the target avr with data it receives.
With the Fanout program on the Arduino's ATmega8, nice things like target automatic reset and programming a target with no bootloader could also be done.
Since I started out with the Arduino, this is just kind of a natural evolution for me to programming other microcontrollers. And it's just what I'll end up doing... But I'll admit it's gonna a paradox for a lot of other people.
The ideal hardware for this thing might be an Arduino with just an SMD ATtiny, instead of an ATmega8 - which is used to program other microcontrollers held in a zif socket. I don't know if this would be a benefit for the Arduino community though, since it could be more for beginners to learn, more complex IDE/libraries and code in the playground wouldn't be as universal.
I'll probably do this between May~July, after doing Plan A or B and finishing that LCD article in the playground.

Thanks for the answer amp, i'll go check the playgournd! ^^
And i'm really looking forward the plan Z from outer space!

melka

Fanout sounds wonderful. Massimo's blog has a post on using the Arduino as an AVR-ISP (http://tinker.it/now/2007/02/19/make-an-arduino-ng-into-an-avr-isp-programmer-for-good/), but it requires changing the crystal on the board. It would be great to have a way to use an unmodified Arduino as an AVR-ISP. If you got that working, I'd be very happy (and so would many other people). STK500 would be the ideal protocol for talking to the programmer, since it's what the current bootloaders use, but anything supported by uisp/avrdude would be fine.

Beautiful! After a couple of days struggling with the Arduino AVR ISP, I found the "bootcloner" and in less than half an hour I had another Arduino! Many thanks for this simple and elegant project.