The Arduino build workflow using Arduino-Makefile

To program the Arduino the official Arduino IDE is not needed. One can use the arduino-mk Makefile package to used standard make commands for compiling, uploading and controlling the Arduino.

This workflow describes how to compile a simple program and upload it to the Arduino using Arduino-Makefile.

The build workflow of building a Arduino sketch when using the Arduino IDE is described on the Arduino website http://www.arduino.cc/en/Hacking/BuildProcess and in more detail here: http://openhardwareplatform.blogspot.co.at/2011/03/inside-arduino-build-process.html.

Generally, the standard Arduino build process is:

  1. Combine .ino files into the main sketch file.
  2. Transformation of the main sketch file: add the #include<Arduino.h> statement; create function declarations (prototypes) of all functions in the main sketch file; append the contents of the main.cxx file of the target to the main sketch file.
  3. Compile the code to object files.
  4. Link the object files to produces a .hex file ready for uploading it to the Arduino.

There are some slight differences between the Arduino standard build process and the build process using Arduino-Makefile:

  1. Only one .ino file is supported.
  2. Function declarations are not automatically created in the .ino file. The user has to take care of creating the correct function declarations.

Download the recent release fo arduino-mk from https://github.com/sudar/Arduino-Makefile/releases and unpack it in a local programs folder (e.g. /home/stefan/02_programs).

Download the recent Arduino sources from http://arduino.cc/en/Main/Software and upack it in a local programs folder (e.g. /home/stefan/02_programs).

As a short example I'm using the Blink example. Create a directory to store the arduino source code file. Create a file blink.ino in this directory and type the following source in this file:

blink.ino
// Blink
 
void setup(void) {
    pinMode(13, OUTPUT);
}
 
 
void loop() {
    digitalWrite(13, LOW);
    delay(1000);
    digitalWrite(13, HIGH);
    delay(1000);
}

The next step is to create the corresponding Makefile. Create a file named Makefile in the directory containing the blink.ino file. Enter the following text in the Makefile:

Makefile
BOARD_TAG = uno
ARDUINO_PORT = /dev/ttyACM0
ARDUINO_LIBS = 
ARDUINO_DIR = /home/stefan/02_programs/arduino-1.0.5
AVR_TOOLS_DIR = /usr
include /home/stefan/02_programs/Arduino-Makefile/arduino-mk/Arduino.mk
  • The BOARD_TAG is the name of the Arduino board on which the program should run. Use make show_boards to get a list of the boards supported by Arduino-Makefile.
  • The ARDUINO_PORT is the serial port to which the Arduino connects to the computer (use dmesg to check).
  • The ARDUINO_LIBS is a list of the libraries used by the code. Arduino-Makefile assumes these libraries are in $(ARDUINO_DIR)/hardware/libraries or your sketchbook's libraries directory.
  • The ARUINO_DIR is the directory in which the arduino code is located. In this example version 1.0.5 of Arduino was used. Change this entry according to the Arduino version used on your system.
  • The AVR_TOOLS_DIR is the directory containing the avr-tools code. If the AVR_TOOLS_DIR is not specified, arduino-mk trys to use the avr-tools bundled with the arduino code. I had problems with this option, because some of the programs were not located in the directories, that arduino-mk expected. One could create the correct locations or wait for a bugfix in arduino-mk, the problem already has been reported to the arduino-mk developers.

To compile the code in blink.ino type make.

To upload the compiled code to the Arduino board type make upload.

The options of the Makefile are:

  • make upload - upload the code to the Arduino
  • make depends - update dependencies
  • make reset - reset the Arduino by tickling DTR on the serial port
  • make raw_upload - upload without first resetting
  • make show_boards - list all the boards defined in boards.txt
  • make monitor - connect to the Arduino's serial port
  • make size - show the size of the compiled output (relative to resources, if you have a patched avr-size)
  • make disasm - generate a .lss file in build-cli that contains disassembly of the compiled file interspersed with your original source code.
  • make verify_size - Verify that the size of the final file is less than the capacity of the micro controller.
  • make eeprom - upload the eep file
  • make raw_eeprom - upload the eep file without first resetting