Project targeting AVR and Due

Hi all, this is a newbie question XD

Can I create a project with source shared between AVR and Due boards, and then recompile for one or the other?

I expect there will be some code specific to AVR or ARM, I would normally (with other IDEs) put that into a sub-folder, then reference it accordingly from the top level. But it seems Arduino only compiles source code in the top level folder, and ignores sub-folders.

How would people manage this?

If this is an FAQ, I apologise, I couldn't find it mentioned anywhere.

Cheers

@bobcousins

I guess #ifdef is your friend at the moment, in the usual C/C++ style of selective inclusion of different #defines etc. for different platforms.

Also, I may be wrong but I believe the following should work in a .h, .c or .cpp file in the source folder:

#ifdef DUE
    #include sam\myDefs.h
#else
    #include avr\myDefs.h
#endif

(The DUE constant is hypothetical and that's another story - see other threads...)

The above is closer to your use case, but you''ll have to edit the files outside of the Arduino IDE (but hopefully the folks at VisualMicro.com will soon support the Due...).

HTH
Jim

I tried a similar scheme. It didn't solve my particular problem but the approach works.

#if defined(SAM3X8E)
#include <sam/SomeDueThing.h> // For the Due
#else
#include <avr/SomeAVRThing.h> // For anything else
#endif

So if I have source code file, mydriver.c, I have to do

void driver_init ()
{
#if defined(SAM3X8E)
// For the Due
#else
// for anything else
#endif
}

but I can't have files avr/mydriver.c and sam3x/driver.c ?

Unless I include them in a top level file, I suppose. Still a bit messy.

I think you can have in mydriver.c:

#ifdef SAM (or whatever)
#include <path\mysamstuff.c>
#else
#include <path\myavrstuff.c>
#endif

?
Jim

I can't this to work. My sketch is:

#include "sub/mydriver.c"

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  myfunc(2);
}

sub/mydriver.c :

int  myfunc (int a)
{
	return a*a;
}

The compiler output is :

C:\Programs\arduino-1.5.2\hardware\tools\avr\bin\avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega1280 -DF_CPU=16000000L -DARDUINO=152 -IC:\Programs\arduino-1.5.2\hardware\arduino\avr\cores\arduino -IC:\Programs\arduino-1.5.2\hardware\arduino\avr\variants\mega C:\Users\bob\AppData\Local\Temp\build2805575431420087737.tmp\sketch_feb16b.cpp -o C:\Users\bob\AppData\Local\Temp\build2805575431420087737.tmp\sketch_feb16b.cpp.o 
sketch_feb16b.ino:2:26: error: sub/mydriver.c: No such file or directory
sketch_feb16b.ino: In function 'void loop()':
sketch_feb16b:11: error: 'myfunc' was not declared in this scope

Using Arduino 1.5.2. I am doing something dumb?

ETA: I though that using chevron quotes #include <sub/mydriver.c> was finding the file but not compiling it, but in fact the include is ignored completely, and doesn't complain about missing files.

ETA2: Yes, I realize the above is from the AVR compiler, but the same applies for the ARM compiler. See below :wink:

Ok, the answer is kinda obvious, the IDE copies source files from the sketch directory to a temp folder, and compiles there. So any subfolders from the sketch folder do not exist in the temp folder.

Seems like the only way to do something similar is with libraries, but that is not a satisfactory solution. I think that the Arduino IDE is probably not a suitable tool for me.

Hi Bob,

I'm sure you realize, but for the benefit of other readers, you can use the full path for the include files and then the following works with 1.5.2:

#define ARM_PLEASE

#ifdef ARM_PLEASE
  #include "F:\Docs\- DEV\Arduino 1\Test\Sub1\mydriver_ARM.c"
#else
  #include "F:\Docs\- DEV\Arduino 1\Test\Sub1\mydriver_AVR.c"
#endif

HTH someone,
Jim

Thanks Jim, that's worth noting.

Of course, I have "unstated requirements" (aren't there always? :)) I want to share my project via github, so can't make assumptions about where files are located.

For the moment, I will try to create a makefile project which uses the Arduino toolchain, and maybe figure out how to make it IDE compatible later.

Just as a little background, I am porting RepRap 3D printer firmware to Due. Currently most Repraps run on Arduino or AVR variant, and the existence of the Arduino platform has been a great help to Reprap. I am hoping that success will continue with Due.