Command line Arduino

From Wikiid
Revision as of 20:37, 25 December 2008 by SteveBaker (Talk | contribs) (Upload:)

Jump to: navigation, search

See also Arduino

Here's how you build programs for Arduino from the Linux command line.

Source code considerations:

Program sources must include:

 #include "WProgram.h"

...and must call 'init()' before anything else.

Compile C & C++ file to make '.o':

 avr-gcc or avr-g++

Use all of the usual options - plus:

 -mmcu=atmega128            == Set the instruction set.
                               (use atmega8 on old arduino's)
 -DF_CPU=16000000           == Define F_CPU to be the clock rate.
                               This is 16000000 for everything except the LilyPad, set it to 8000000.
 -I ...                     == Location of WProgram.h, etc.
 -I ...                     == Location of interrupt.h, etc. (use /opt/cross/avr/include/... under OpenSuse)

Build a ".a" file from all of your library ".o" files:

 avr-ar rcs library.a *.o

Where:

   r     == replace
   c     == don't warn if has to be created
   s     == create an archive index
   *.o   == the results of compiling:
      pins_arduino.c     wiring.c
      wiring_analog.c    wiring_digital.c
      wiring_pulse.c     wiring_serial.c
      wiring_shift.c     WInterrupts.c
      HardwareSerial.cpp WMath.cpp

Link (to produce an Elf file):

 avr-gcc -mmcu=atmega128 \
         -I. \
         -gstabs \
         -DF_CPU=16000000 \
         -I ... \
         -Os \
         -Wall -Wstrict-prototypes \
         -std=gnu99 \
         -o blah.elf \
         blah.cpp \
         -L. \
         library.a \
         -lm

Where:

 -mmcu=atmega128            == Set the instruction set.
                               (use atmega8 on old arduino's)
 -I.                        == Look for header files here.
 -gstabs                    == Use STABS format for debugging.
 -DF_CPU=16000000           == Define F_CPU to be the clock rate.
                               This is 16000000 for everything except the LilyPad, set it to 8000000.
 -I ...                     == Location of WProgram.h, etc.
 -I ...                     == Location of interrupt.h, etc.
                               (use /opt/cross/avr/include/... under
                               OpenSuse)
 -Os                        == Optimize for minimum code size.
 -Wall                      == Turn on all standard warnings.
 -Wstrict-prototypes        == Demand proper function prototypes.
 -std=gnu99                 == Require C99 plus Gnu extensions.
 -o blah.elf                == Set the output file.
 blah.cpp                   == The source to compile.
 -L.                        == Look in the current directory for libraries.
 library.a                  == ...and other obj files.
 -lm                        == Grab the standard math library.

Elf to Hex:

 avr-objcopy -O ihex    \
             -R .eeprom \
             blah.elf   \
             blah.hex

Where:

 -O ihex     == Create output in ihex format.
 -R .eeprom  == Remove the .eeprom section from the output.
 blah.elf    == Input file in 'elf' format
 blah.hex    == Output file in 'ihex' format.

Upload:

avrdude -V \
        -F \
        -C $(INSTALL_DIR)/hardware/tools/avr/etc/avrdude.conf \
        -p $(MCU)  \
        -P $(AVRDUDE_PORT)  \
        -c $(AVRDUDE_PROGRAMMER) \
        -b $(UPLOAD_RATE) \
        -U flash:w:applet/$(TARGET).hex

Parameters:

 -V                           == Do not verify
 -F                           == Override signature check.
 -C avrdude.conf              == Set config file
 -p atmega128                 == Choose board processor type
                                 (use atmega8 on old arduino's)
 -P /dev/tty.USB0             == USB port. (/dev/ttyUSB0 in OpenSuSE)
 -c stk500v1                  == Choose programmer type
 -b 19200                     == Upload baud rate
 -U flash:w:applet/myfile.hex == Memory operation specification.