How To Build a new bootloader

I am looking for a good ‘how to’ page on building a bootloader for a new processor.

The details of doing this are disorganized. So, here is a list of questions that I would like to have answered, and I am sure that it would be helpful to future viewers if it were all documented in one place. If a lot of people contribute to this I’ll be glad to put it all together in one document and post it back.

  1. How to compile the boot loader from source code. Setting up the folders in the right place so that the compiler can find references to things such as avr/pgmspace.h and other includes. Which compiler to use? Can this be done with avr-gcc? How would that be done. I don’t understand makefiles, so how is this done from the command line?

  2. How to modify the boards.txt file to get the sketches to download to the new device. What values to use for the fuse bits. Can I check them with AVR Dragon or other ISP programmer and use those values in boards.txt? What about 8mHz, 16Mhz and 20mHz micros? How do I get the baud rates right for each of these? What else needs to change to make it work at the new frequency?

  3. When should I add a new build.core? Can’t everything be done with build.core=arduino? Why did Sanguino use a new core?

  4. Things that I have missed because I have never done this before.

A complete resource. Does anything like this already exist for the Arduino IDE?

I am looking for a good 'how to' page on building a bootloader for a new processor. : A complete resource. Does anything like this already exist for the Arduino IDE?

No. Building bootloaders is outside the scope of the Arduino project... Everything you need is there, in the best spirit of Open Source, but there has been essentially no "simplification" done, so like a lot of open source, this may not be very useful to most of the would-be users. (Whereas the Arduino itself is a much simplified "programming environment.")

I don't understand makefiles

You will first need to understand "makefiles", and the rest of a command-line based C development environment. Compilers, Libraries, executable search paths, include search paths, library search paths... In short, you first need to learn how to program the destination processor "for real" rather than in an abstracted environment like arduino. Consider than Optiboot has had three serious bugs found in it since it was first included with Arduino (timer, 30k, and known-zero.) (this in spite of having been done by someone with significant programming experience, and having been looked at by others, and used, prior to release...)

Fuse bits will depend on exactly which chip you are using. Read the datasheets for explanations. (and then ask on AVRFreaks, because the fuses are conFusing.)

The baud rate is one of the few things that should be relatively automatic, since its an exact formula based on the CPU clock rate, which is one of the things that gets specified in the Makefile that compiles the bootloader.

The "build.core" is entirely separate from the bootloader. Sanguino used a new core because the chip they were using was significantly different than the one Arduino was using, they didn't want to have to get their changes added to the official distribution, and the arduino code wasn't written in a way that made it easy to add additional chips (this is something that improved in 0022, BTW. Sanguino should re-evaluate.)

Things that I have missed

It would help to know what processor you are thinking of using, and what kind of development environment you have (mac, windows, linux?) Writing a bootloader for an ATmega48 (which has only 4k of memory and no "boot section") is a much different exercise than writing one for the mega2560 (which has a "minimum" bootloader size twice as big as the current "optiboot", so you have "lots of room.")

The size of the bootloader is important. If you are slightly too large and don't notice, you will probably get especially mysterious failures.