How to: Compiling the Arduino Bootloader…
A project we’ve been recently working on requires a custom Arduino boot loader. Working in an incremental fashion, the first step is getting the stock Arduino boot loader to compile with the latest WinAVR libraries and current Arduino 1.0 IDE. While this wasn’t terribly difficult, it did require a bit of searching, debugging, testing and then a bit more debugging before it worked as intended. Presented here for your convenience, are the fruits of this labor, outlined are the following steps:
- Download the latest Arduino IDE – 1.0
- Modify the boot loader source
- Modify the makefile
- Compile in command prompt
Step 1: Download the latest Arduino IDE – 1.0
The latest Arduino IDE, version 1.0 can be downloaded directly here: http://arduino.googlecode.com/files/arduino-1.0-windows.zip
The complete package is an 86MB zip that unpacks to about 240 MB.
You can also locate the specific files in the Github Arduino repository.
Once you’ve downloaded and unzipped it, navigate to the bootloaders folder at the following path in Windows: arduino-1.0hardwarearduinobootloaders. On the Mac, the path is /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/bootloaders/. For purposes of testing, I’ve made a new folder here called: atmega168test. And populated the new test folder with the two files ATmegaBOOT_168.c and Makefile from the folder atmega.
For programming under Windows, I prefer to use Programmers Notepad 2, an open source editor with syntax highlighting for many common languages (it makes the words pretty colors). You can download it here: http://www.pnotepad.org/. Windows Notepad or any other text editor will also suffice.
Step 2: Modify the boot loader source
The first (and only) compiler error we will tackle is due to the EEWE bit definition missing in versions 1.6.7 and newer of the file /avr/eeprom.h. The actual compiler error reads as:
ATmegaBOOT_168.c: In function ‘main’:
ATmegaBOOT_168.c:596:11: error: ‘EEWE’ undeclared (first use in this function)
ATmegaBOOT_168.c:596:11: note: each undeclared identifier is reported only once for each function it appears in
make: *** [ATmegaBOOT_168.o] Error 1
This EEWE issue (152) has been around for a while and was originally reported in 2009. Within the source file ATmegaBOOT_168.c, around line 584, we will comment out the old and add in the new:
// ?kraz? -fixed EEWE bit definition missing in versions 1.6.7 and newer of the file /avr/eeprom.h // According to this issue reported in 2009: http://code.google.com/p/arduino/issues/detail?id=152&q=eewe #if defined(EEPE) while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete #else while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete #endif /* #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete #else while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete #endif */
And don’t forget to save the file!
Step 3: Modify the makefile
Now that we have the bootloader fixed, it does compile without error, but it ends up larger than the 2k space allocation for the bootloader section of the MCUs flash. The easiest solution here is to bump the compiler optimization level up to -0s. This is in the makefile on line 52, beware the makefile isn’t C language, it uses the # operator to comment out a line:
# ?kraz? - changed opt level to fit under 2k OPTIMIZE = -Os # OPTIMIZE = -O2
Step 4: Compile in command prompt
The final step is to compile, under Windows we will be using the command prompt. The location of command prompt will depend on your version of Windows, in 7 it can be found by typing the first few letters of command in the search box of the start menu. Once it’s open, use the “CD” command navigate to the bootloader folder of your Arduino install as seen below in Windows, like this:
And on the Mac, like this:
We then compile with the command “make pro8”, which specifies the target board, in this case it’s an Mega168 clocked at a relaxing 8Mhz. Other board target definitions can be found listed in the makefile, such as diecimila, lilypad, ng, atmega328_pro8, mega, etc. If the hex already exists in the folder, you will get a message make: Nothing to be done for `pro8′. In this case just delete the hex and compile again with “make pro8”. If everything went alright, you should see the compiler report as in the first image at the top of this post. Good luck and happy compiling!