Writing C/C++ Code for Arduino

The Arduino IDE comes with the full source code for all basic libraries needed to build a large number of Arduino projects. However, it hides all of that code under the wrapper of the IDE.

You can find the code, though, with a little digging into the IDE installation.

Mac Arduino

On a Mac, the Arduino application is installed in the standard system Applications folder as Arduino.app. Using the Finder tool, right click on that file and select Show Package Contents.

  • Installation /APplications/Arduino.app
  • Source files: Contents/Java/hardware/arduino/avr/cores/arduino.

Windows

Assuming you installed the Arduino IDE normally, the program is located in:

  • Installation: C:\\Program FIles\Arduino
  • Source files: Java\hardware\arduino\avr\cores\arduino

Browse the Files

Once you find the source code folder, looking around at the files will turn up many clues as to how to construct your own C/C++ programs for the Arduino. The Makefile provided for AVR projects will build any combinarion of C/AVR Assembly code. All you need to do to add a C++ library file to the Makefile is add the missing Makefile code to locate all .cpp files, and convert those to object files. The linker will take care of the rest. (Use the Makefile pattern for .c iles and add additional lines for C++ files).

Main.cpp

The IDE ignores the main function needed to make the code an actual program. It adds it silently behind the scenes. Here is the code used:

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

        return 0;
}

Note

I stripped out some code not needed for our work.

The Init function sets up the processor, and we have seen the code needed to do that in examples in this class. Add tht code here.

The basic Arduino application simple cals the two user-provided functions in this code.

To include library code in your project, you will need to explore the header files needed, and decide which of those you need. I usually copy the headers and Arduino source code for those libraries to another folder, and strip out things not needed for my project when I play with Arduinos.

Grep

When you are browsing source code ina directory like this. it is very handy to be able to find where a name is located. In the Mac/Linux world, there is a great tool to assis in this: Grep!

Grep reads every file in your directory (or even on your entire system - which might take a while!) looking for a text pattern.

Working on the command line in the directory with the files you are syudying, here is an example run:

$ grep analogWrite *
Arduino.h.orig:void analogWrite(uint8_t, int);
wiring_analog.c:void analogWrite(uint8_t pin, int val)

This output tells me that the function prototype is located in Arduino.h, and the function itself is located in wiring_anaog.c. Pretty handy!

Writing your project code

To craft your own code for some device, my usual advice is to start off by building a pure Arduino project in the Arduino IDE that works with your device. These are suually easy to find for the devices I provide.

Once you have an Arduino project running, copy that code to another project folder, together with any library routines you need (from the Arduino installation). Add the standard Makefile we have been using for AVR projects in this class.

Warning

If you need a C++ library, you need ot add the build code to the Makefile. Use the C file lines as a guide ot do this, or see me.

Try to build your project from jusy this directory. It shoudl roduce a running version that works like it did in the IDE

Next identify the library code needed to talk to your selected device.

Note

Remember that you are free to use librries that do anything else to support your device, like commnicate with the host machine over serial.

Then work on stripping out all the unneeded code. You probably do not need the entire library, just the functions needed to talk to your device. Throw the other code away!

You will see a lot of “conditional compile” lines (#ifdef) and a reference to some chip by name. The libraries are designed to work with dozens of Arduino variants, and all we want is the code for our particular board.

When you have your device code reduced to the minimum needed (ant it still works), put all of the code to set up your device,a nd communicate with it in a simple function (if it is not alread there). Your Makefile will produce alisting file showing that code in assembly language. You can then rewrite that code in (cleaned up) assembly code, and put that cod ein separate files. Either rename or remove the old functions, then compile your project, now with assembly language code. If it runs, you are good. If not, see what you miseed (or see me if needed).