Sunday, March 8, 2009

Windows XP Build Script for Arduino

If you use an Arduino board with Windows XP...

If you like to code in C/C++ and have total control of compilation...

If you dislike GUI IDEs and prefer coding in <your favorite text editor> with a makefile...

A Windows XP Build Script is all you need!!!


Arduino (Show/Hide)

If you want to read more about Arduino, unhide this section.


The Arduino IDE and build process

The Arduino IDE seems to be tailored to smaller programs in which all codes can fit into one file. In my case, I'd like to have multiple files to keep things organized. I'd also want to have codes shared by two or more projects, so I have files in separate folders. The IDE seems to get confused by the file paths and it was not long until I found it too bothersome to get the projects to compile and link properly. I love the legendary makefile and command line tools after all.

The entire Arduino platform is actually a frontend of the WinAVR tools, which is essentially an ATmega128 extension of the GNU collection. So all I needed was to write a batch file that executes the proper commands to compile, link and download the program. Of course, this could be a difficult task, but it turned out that I wasn't the first one, as I found exactly what I wanted at the Arduino Playground.

The Windows command line build was written by Don Cross and is a build script that works seamlessly. Though, this tool comes in three batch file and was a little bit difficult to digest/customize at the first glance. Also, the tool assumes that all your code comes in one single file. These batch files weren't exactly what I wanted, but they did provide a good starting point!

THE Arduino build script

After some poking around, I wrote up a makefile-like build script. The script, along with an example project, can be downloaded here:

ArduinoBuildScript.zip

Below is a description of the build.bat build script:

1 Configs (Show/Hide)

2 Parse command line option (Show/Hide)

3 Prepare environment variables (Show/Hide)

4 Clean output files (Show/Hide)

5 Compile source codes (Show/Hide)

6 Link to elf file (Show/Hide)

7 Download to microcontroller (Show/Hide)


The Example project and Further customization

To run the example, simply hook up your Arduino board, edit the COM port in the script and execute buildExample.bat. The example demonstrates compiling and loading a multi-file project. Note that I made it include a 3rd party library called SoftwareSerial. The project doesn't really need it but I included it to demonstrate how you can simply provide a space delimited list to tell the build script to compile and link the 3rd party libraries.

NOTE: It is important that you execute the init() function at the beginning of your main() function. The init() function initializes the hardware of the board etc. Also, remember to include the WProgram.h header file whenever you use an Arduino function call (like Serial and digitalWrite).

This build script hopefully provides a good basis of a customized makefile for everyone. My original intention was to also code other features into the script, but I decided that there are too many possible features you can add and it's down to your choice of functionality. Without modifying any codes this script supports compiling and loading multiple source files in multiple folders.

With a little modification, you can also customize this script to do the following:

  • support more than 1 config with the same script (like a DEBUG preprocessor flag): You can add a command line option, then use an if statement to set the variables to respective values.
  • support more than 1 project: Similar to above, but use a command line option and an if statement to switch between sets of source files and output name. The project outputs will locate in its own folder so you can compile multiple projects without affecting each other.
  • save the build process console output to a file: change the BUILDLOG variable

... and lots of other features! Once you get familiar with the script, you should be able to tweak it according to your own need. So enjoy!

1 comment:

  1. Any idea what happened here?

    *************
    ARDUINO BUILD
    *************

    [Arduino] Cleaning output files...
    [Arduino] Compiling C:\arduino-0017\ArduinoBuildScript\example\src\main.cpp...
    [Arduino] Compiling C:\arduino-0017\ArduinoBuildScript\example\src\tools.cpp...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\HardwareSerial.cpp...

    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\pins_arduino.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\Print.cpp...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\WInterrupts.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\wiring.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\wiring_analog.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\wiring_digital.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\wiring_pulse.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\wiring_shift.c...
    [Arduino] Compiling C:\arduino-0017\hardware\cores\arduino\WMath.cpp...
    [Arduino] Compiling C:\arduino-0017\hardware\libraries\SoftwareSerial\SoftwareSe
    rial.cpp...
    [Arduino] Archiving to .\example\out\ArduinoLibraries\runtime.a...
    [Arduino] Linking to .\example\out\projectA.elf...
    [Arduino] Converting to .\example\out\projectA.rom...
    [Arduino] Converting to .\example\out\projectA.hex...
    text data bss dec hex filename
    0 1416 0 1416 588 .\example\out\projectA.hex
    [Arduino]
    [Arduino] Downloading firmware...
    avrdude: stk500_getsync(): not in sync: resp=0x06
    avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x06

    avrdude done. Thank you.

    [Arduino] Build FAILED.
    Press any key to continue . . .

    ReplyDelete