Sonntag, 5. Dezember 2010

HOWTO compile arduino programms without its slow IDE...

As i wrote in my first post i am currently trying to get rid of the Arduino IDE. Don't get me wrong, it's a great IDE for small & quick projects. No hassle with header files and so on. Just type your few lines of code and you're ready to go. But eventually the day where you need more editing power will come. From my experience Arduino IDE is fine as long as your lines of code are not more than 1, say 2 pages and you don't have extern files. It's possible thou to add new tabs (= new files) in Arduino IDE but there are some important options missing: you can't collapse text levels, e.g. 'close' functions you actually don't need to save view space and the code highlighting lacks some functionality too. Plus its slow, big in memory and full of odd bugs. Try to close a tab in a just created sketch and you'll see what i mean.

So i started editing my code files in an extern editor while still using ArduinoIDE to compile and upload. That didn't work so good either, mainly because the process takes so long. All arduino core files are compiled into a file named core.a every time you compile something. Why is that? So i decided to use my own compile & upload feature via the command line..

The script is a win batch file. I will do a Linux one the next days, as i use both plattforms i also need both versions. To get the script running you first of all need the core.a file containing all arduino specific libraries. To get this file, open the arduinoIDE and compile a sketch. Before your actual sketch will be compiled the core.a file will get created. Find it in the tmp folder or simply search for it. To look at the compiler output edit the arduino preferences.txt file and in line 132 set build.verbose=true

Do that editing while ArduinoIDE is closed. Then restart and compile a sketch, you can now see the command line output. If you'll look closer you can identify the tmp folder in the first lines.
Copy the core.a file into a folder of your choice and update all variables in the script. progName is the name you want the resulting files to have as prefix.

You have to rename your *.pde file to a *.cpp file and eventually have to write a header file for it. I didn't have to, just make sure you don't call functions before the are defined. Also there is now a little difference when including libraries. You have to include them like this, otherwise it won't work!
#include <libFolderName\libName.h> instead of just <libName.h>
If anyone has a better approach on this, please let me know.

Here is the file, copy it to a file with.bat extension inside your current arduino program directory. Please make sure you delete the word wraps in between the lines. I had to add them for better code view..
UPDATE: download core.a, build.bat and buildandupload.bat as zip package.

@echo off
set arduinoDir=E:\coding\arduino-0021\
set coreFile=C:\DOKUME~1\locomat\Eigene~1\Arduino\core\core.a
set sketchDir=C:\DOKUME~1\locomat\Eigene~1\Arduino\superNode\
set progName=myProg
set comport=COM3

FOR %%G IN (*.cpp) DO %arduinoDir%hardware\tools\avr\bin\avr-g++
-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections
-mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=21
-I%arduinoDir%hardware\arduino\cores\arduino -I%arduinoDir%libraries
%sketchDir%%%G -o%sketchDir%%%G.o

%arduinoDir%hardware\tools\avr\bin\avr-gcc -Os -Wl,--gc-sections
-mmcu=atmega168 -o %sketchDir%%progName%.cpp.elf %sketchDir%*.o %coreFile%

%arduinoDir%hardware\tools\avr\bin\avr-objcopy -O ihex -j .eeprom
--set-section-flags=.eeprom=alloc,load --no-change-warnings
--change-section-lma .eeprom=0 %sketchDir%%progName%.cpp.elf

%arduinoDir%hardware\tools\avr\bin\avr-objcopy -O ihex -R
.eeprom %sketchDir%%progName%.cpp.elf %sketchDir%%progName%.cpp.hex

avr-size --mcu=atmega168 -C %progName%.cpp.elf

The script takes all *.cpp files of the actual directory and subdirectories and compiles them to a hex file you can then upload with the batch command.

avrdude -p m168 -b 19200 -c stk500v1 -F -P \\.\%comport%
-V -U flash:w:superNode.cpp.hex

Please notice that the -V option specifies NOT to download and recheck the code from the avr which saves some extra seconds.Check all options here.

I have two batch files in my working dir, 'build.bat' which builds and 'buildandupload.bat' which builds and upload code. I write code in Notepadd++ and now use my scripts to compile and upload.

This really simplified everything and helps to save a lot of time!

1 Kommentar:

  1. this is actually exactly what I'm looking for!
    I tried your solution but it seems to have some problems.
    1. is that "%sketchDir%*.o" cannot run (I just skipped that and deleted that part of command)
    2. the core.a file seems to contain absolute path references that do not work after I copied that to another folder

    do you have a simple solution on that?

    Ideal solution would be to have a view lines of batchfile commands that compile and upload the code to my arduino