Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode to develop for the Arduino

Please read this well to make sure you understand what I want to do.

  • I DO want Xcode to be able to compile, but only so I can debug in Xcode.
  • I do NOT want to use Xcode to compile or upload the code to the Arduino board. I will use the Arduino IDE in "Use external editor" mode instead.

What I have done (also as a future reference for people who might want to do the same thing):

  • In the project settings (click on the project file in the left pane)
  • I have changed the compiler to GCC to avoid many errors.
  • I have added the following paths to the Header Search Paths and Library Search Paths:

    • /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/lib/gcc/avr/4.3.2/include
    • /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include
    • /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino

(If you have installed Arduino.app somewhere other than the Applications folder, you will need to adjust the paths accordingly.)

In main.cpp, I have included <WProgram.h>, but it wasn't enough. I was getting undefined identifier errors (for SPCR, SPE, MSTR, SPR1, SPR0) due to not being able to pass the -mmcu=somechipname as a flag to the compiler, which caused no device to be defined and avr/io.h to be unable to include the file that defined those symbols. I got around it by manually including <avr/iom328p.h> which is the appropriate header file for my chip.

That's how far I got.

Now I get these errors:

Undefined symbols for architecture i386:
  "_init", referenced from:
      _main in main.o
  "_setup", referenced from:
      _main in main.o
  "_loop", referenced from:
      _main in main.o
  "_pinMode", referenced from:
      SBSetup()    in main.o
  "_digitalWrite", referenced from:
      SBSetup()    in main.o

The whole main.cpp including the evil offending code is this:

#include <WProgram.h>
#include <avr/iom328p.h>    // Getting around warning "device type not defined"
#define NumLEDs 25
#define clockpin 13 // CI
#define enablepin 10 // EI
#define latchpin 9 // LI
#define datapin 11 // DI

int LEDChannels[NumLEDs][3] = {0};
int SB_CommandMode;
int SB_RedCommand;
int SB_GreenCommand;
int SB_BlueCommand;

void SBSetup(void) {
    pinMode(datapin, OUTPUT);
    pinMode(latchpin, OUTPUT);
    pinMode(enablepin, OUTPUT);
    pinMode(clockpin, OUTPUT);
    SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
    digitalWrite(latchpin, LOW);
    digitalWrite(enablepin, LOW);
}

int main(void)
{
    init();
    setup();
    for (;;)
        loop();
    return 0;
}

What do I about this?

like image 844
user922764 Avatar asked Oct 11 '22 03:10

user922764


2 Answers

These two great references let you set up XCode not only for compile, but also for upload:

  1. http://www.quietless.com/kitchen/setting-up-xcode-to-compile-upload-to-an-arduino-atmega328-duemilanove/
  2. https://github.com/timknapen/Arduino-With-XCode#readme

The first explains the additional tools you need, while the second provides a project template for XCode 4.2 (and previous versions).

I have used both and it works.

like image 183
henning77 Avatar answered Oct 20 '22 03:10

henning77


I think your best approach might be to forget about XCode, and just use the Arduino IDE.

For whatever it's worth, it doesn't even sound like you're building for the correct platform (i386?). Assuming you've got the AVR toolchain for GCC, you might have some luck looking at the default switches in the Arduino IDE's source:

src\Compiler.java

Good luck!

like image 1
paulsm4 Avatar answered Oct 20 '22 04:10

paulsm4