Arduino-Leonardo raw programming

I hate the Arduino’s highest level of abstraction, aka the IDE and the Wiring language; but at the same time I cannot stop loving its hundreds of libraries.

Two years ago, while I was working on a commercial project, I realized that Wiring didn’t allow me for multi-file compilation, so that broke my heart (and the project as well, indeed). However I had spent a lot of time and resources in such a project, so I needed to find  the way to keep moving on it.

Then I was able to compile the project from the command line, dropping out the IDE and Wiring, and using .cpp files and multi-file compilation. Wee!!. The bad news is that I didn’t document what I did 😦

Now I’m in the same place as before, so I made it work again, but this time I want to document the steps toward a raw compilation from the command line.


  • Download the packages

We need to download and install the following packages. Best if doing so from the distro admin packager.

  1. arduino-core
  2. Arduino.mk
  • Modify the Arduino.h file

This file calls the pins_arduino.h file, but it’s not the behavior we want because that file doesn’t correspond to the one that Leonardo needs. So we must tweak this file. It is located at:

/usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h

Open it with root privileges with your favorite text editor and find this line. At the time of this writing this is the last line in the file:

#include "pins_arduino.h"

and change it for these lines:

#ifdef USB_VID
#include "../leonardo/pins_arduino.h"
#else
#include "pins_arduino.h"
#endif

Save it and close it.

  • Create your own Makefile

Now we need to create a Makefile of our own in the root folder of our project. I mean, every project that we want to compile from the command line will need this Makefile:

ARDUINO_DIR = /usr/share/arduino
BOARD_TAG    = leonardo
ARDUINO_PORT = /dev/ttyACM*
ARDUINO_LIBS =
include /usr/share/arduino/Arduino.mk
  • main.cpp

I wrote this mini main program to test the compilation process. I haven’t had tested uploading the code to the board:

#include <Arduino.h>

int main(void)
{
  return 0;
}

This file, the Makefile, and the others .cpp files your application will need, should be in the same folder (as this tutorial). The make command will compile all source files automatically.

  • Test it

In the command line, where the Makefile is, run the make command:

$ make <CR>

If everything was ok, the command will end with these next lines:

----------------
Device: atmega32u4
Program:     212 bytes (0.6% Full)
(.text + .data + .bootloader)
Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)

$

The Makefile also created a folder called build-leonard. So you want to try:

$ ls build-leonardo/ <CR>

And a list of object and executable files will be shown. In my testing project:

$ ls build-leonardo/
 CDC.d                   HardwareSerial.o  main.o    Tone.d           wiring_analog.o   wiring_shift.d
 CDC.o                   HID.d             new.d     Tone.o           wiring.d          wiring_shift.o
 CortinasArd.eep         HID.o             new.o     USBCore.d        wiring_digital.d  WMath.d
 CortinasArd.elf         IPAddress.d       Print.d   USBCore.o        wiring_digital.o  WMath.o
 CortinasArd.hex         IPAddress.o       Print.o   WInterrupts.d    wiring.o          WString.d
 CortinasArd.hex.sizeok  libcore.a         Stream.d  WInterrupts.o    wiring_pulse.d    WString.o
 HardwareSerial.d        main.d            Stream.o  wiring_analog.d  wiring_pulse.o
 $

(In this case I used CortinasArd.cpp instead of main.cpp, but the output will be the same.)


For now that’s all, but please let me know if this tutorial works for you. Or if you know something better or easier, again, please don’t hesitate and share it.

Greetings 🙂

PD: I cannot understand why this Arduino’s bug still exists after Leonardo’s board came to live several years ago. It seems that everyone that wants to try the command line needs to walk for this painfull process.

It’s worth to mention too that I tried several solutions that I found on the Internet, but all of them have dependencies and they all are not as easy as the method I’ve just shown.

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s