Creating a flashable *.hex-file from a C-Source using the arduino tools

Hi,

(using Linux)

I want to experiment with my own C-sources to create a flashable program.
For this I want not to use Arduino-Libraries.
But I want to use the toolchain already installed with the Arduino-IDE.

I tried to understand the output of a compilation run from the iDE -- but without
any explanation AND the usage of Arduino related libs (which I dont want in this
case I am ...buffled.

Is there any documentation available, which explains the usage of the toolchain
soleil from the commandline for "hexifying" a C-Code into something flashable?

Thanks a lot for any help in advance!
Cheers
mcc

Arranging your code like this will skip the Arduino stuff.
DO NOT name the *.ino file main.

This will be compiled by the g++ compiler but for the most part
it will be no different than using the C compiler.

If you want to use the C compiler open a tab in the IDE and
put the code in that file and name it *.c, leave the *.ino
file empty.

At the top of the c file add:

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
   DDRB |= _BV( PB5 );    // PB5 (D13) is output

    while(1)
    {
      _delay_ms(250);     // busy wait, blocking
      PINB  = _BV( PB5 ); // toggle PB5;
    }
}

Sketch->Export compiled binary

Hi,

thanks for all the help -- but it is not what I has asked for.

I want to use the toolchain via the commandline. The IDE is installed and want to use the installation of avr-gcc and friends, which comes with it. But I dont want to use the IDE.

Where can I find instructions for that task?

Cheers
mcc

How much do you know about compiling non-arduino C code from the command line?

Hi,

hmmm...the question is difficult answer, since it is not clear, for what "Arduino C code" stands for..
Do you mean:
"...in opposite to compile Arduino C++ code (which it is actually)"
Arduino C-code in the sense of "any C-code to be run on an Arduino board"

So let me explain a little what I am able to do and what I have asked for:
I am experienced in compiling C-code into an executable application from the commandline using UNIX/Linux and - if needed - hand crafted makefiles.
I want to know how to map this experience into the non-standard world of Arduino "*.ino" sketches (I am not sure, why *.c become *.ino and C-code becomes sketch to get the whole thing running...but...)

I want to compile ".ino" into ".hex" from the commandline using the cross-compiling infrastructure of the Arduino IDE without using the IDE at all.

How can I accomplish this?

So, there are only a couple "extras" involved in using avr-gcc or avr-g++ to compile programs, compared to C or C++ for a desktop.

Point your path at the arduino binaries sub-directory (or install the avr tools using your favorite package manager - you'd need avr-gcc, avr-binutils, and avr-libc. Or download the "AVR toolchain for linux" from Atmel/Microchip. Using the arduino binaries gives you an extra level of compatibility and supresses some potential surprises...

Prepare your C and/or C++ files using EMACS in the usual manner :slight_smile:
Be sure to #include <avr/io.h> at the top of your program to get the per-chip symbol definitions. It is convention to define F_CPU as the clock speed the microcontroller is running at, for the various avr-libc functions that need that info (like _delay_ms) You can do that either in the source code, or on the compile line.

When compiling, you'll need to to specify which mcu you are using on the compile line with -mmcu option, and you'll want to be sure to use -g and -Os

avr-gcc -g -Os -mmcu=atmega328p -DF_CPU=16000000 blink.c -o blink.elf

You can compile individual .c or .cpp files and link them together, just like with desktop gcc. This will end up producing the usual .elf file, which still contains symbol and section info and a bunch of stuff that isn't pure .hex binary. Some programming software will accept the .elf as input and strip the extra info during programming, but normally there is an extra step using avr-objcopy:

avr-objcopy -oihex -R .eeprom blink.elf blink.hex

And that should be about it..

[img width=500 height=128]http://forum.arduino.cc/index.php?action=dlattach;topic=493004.0;attach=221628[/img]

Hi westfw,

OH YEAH! These are the instruction I have waited for so long! THANK YOU! THANK YOU SO MUCH!
:slight_smile: :slight_smile: :slight_smile:

Best regards,
mcc

PS: Small correction:
This
avr-objcopy -oihex -R .eeprom blink.elf blink.hex
must be
avr-objcopy -O ihex -R .eeprom blink.elf blink.hex

:wink: