Embedded Development with Xcode
by Jim Schimpf01/21/2005
I'm constantly amazed and envious of those who can write complicated GUI applications. But being an embedded developer in real life, what really gets me excited is to turn on an LED or flip a relay. Until Mac OS X, there never was much interest or support for embedded development on the Mac. Now, with the UNIX underpinnings of Mac OS X, we can benefit from the huge open source effort supporting small microprocessors.
What I also want is to be able to use Xcode to for this development. I've been doing my professional embedded systems with an IDE and would very much like this luxury in my amateur efforts. Putting all of the code into a project and having the IDE treat your code as a database so you can find structures, functions, and definitions with just a click is almost a necessity once the project grows beyond a few files.
Using the Mac to do this work requires three things, which I'm going to explore in this article. First, the tool chain. This is the compiler/assembler/linker/loader generator for your particular microprocessor. Second, some hardware plus software to load the generated program into the microprocessor. And third, a look at how to possibly manage the project in Xcode.
Tool Chain
Tool chains are specific for a microprocessor or family of processors. In my case, I'm working with the ATMEL AVR line and specifically, the 90s8535.
This is part of a whole family of RISC 8-bit micros that can be programmed in-circuit and are relatively cheap. My choice was made when I acquired a device with one of these in it already and wanted to program it myself.
Enter the GNU organization. They have developed a
very portable C compiler (gcc) that is already used by Apple. The
source code for it is available and is designed to be re-targeted for
almost any hardware. Beyond the compiler, you
also need a collection of tools to manipulate the output files to
create the actual file to load the micro. And most importantly,
you need a runtime library specifically built for this processor.
I found a page from Stanford, AVR Tool Chain, that details how to get the tool chain and install it on your machine. (These instructions would work on most any UNIX box.)
Go to this page and download the following four parts:
binutils: These are utilities for manipulating the ELF and binary files generated.gcc: This is the C compiler, targeted for the AVR.libc: This is the C runtime library targeted for the AVR.usip: This is a chip programmer designed for x86 machines (optional).
Since we're working on a Mac, things are a little simpler. You can use Stuffit Expander and a double-click to expand all the archives--no need for command-line operations. The configure/make/install operation for each of the four, though, is exactly the same, and while lengthy, works just fine on the Mac.
When all of this is done you have the AVR tools in
/usr/local/bin and the other parts in /usr/local/avr. The tools
include avr-gcc, the C compiler; the avr-objcopy code extractor; and
avr-objdump, a disassembler/dumper for the output files. The other
parts in /usr/local/avr include the special include files for the AVR
processors and the C runtime library. You then set /usr/local/bin in
the PATH environment variable, and you're off to the races.
At this point, you can compile code and build ROM
images to be downloaded into the processor. I've included a
simple example here that
flashes an LED attached to the
processor. It uses a makefile to build the parts and extract the
ROM code. This is example code from the MIT Solar Electric Vehicle Team,
written by Michael Seeman, slightly changed to support the latest
tools. If your tool chain is correctly installed, you should
be able to cd to the sample code directory and type make, and then have the
sample.rom loadable binary built.
The makefile in the above example is very valuable in showing exactly what command-line values are needed to compile an application and the link flags needed to build a loadable image. Also, it shows the commands needed after the ELF file is built to extract the binary image.
Programming the Chip
Here is where the Mac always seems to have a problem. We have to move the ROM file generated by the tool chain into the chip and need some kind of hardware connection from our computer to the micro. On x86 type PCs, there is the parallel port, which on most of those machines is as useful as an appendix, but can be used as uncommitted digital I/O to program a chip. Macs never had a parallel port or any other kind of port that could be used to do the kind of digital I/O we need.
Hmmm, looks like I'm getting a little ahead of myself. ATMEL chips can be programmed in circuit by wiggling the reset line and then sending in programming commands over the Serial Peripheral Interface (SPI). On x86 machines, they program the parallel port to do this. (See the code of UISP (Universal In System Programmer) in your tool chain.)
On the Mac, we can plug digital I/O devices into the USB bus and use those devices like the x86 machines use the parallel port. I have a Delcom Engineering USB Development board, which gives me eight digital bits in and eight bits out and supporting software for OS X. While it's not really fast, it does do the job and wiring it up to the AVR chip is as follows:
PORT 0 (Bit 0 Input) <--- MISO on AVR
PORT 1 (Bit 0 Output) ---> MOSI on AVR
PORT 1 (Bit 1 Output) ---> SCK on AVR
PORT 1 (Bit 7 Output) ---> ~RESET on AVR
Programming the AVR Chips
The AVR manual (Serial Programming) gives the steps necessary to program both the program ROM and the chip EEPROM. I've written a program to support this. It reads the ROM file generated by the tool chain and will then program the AVR chip via the Delcom board. The source and Xcode project are here.
The code for USIP was very valuable in showing me exactly how to send out the data via SPI. The manual for the chip shows the SPI waveforms as:

Reading the code in USIP (see TDAPA::SendRecv(int
b) in DAPA.C) showed that the clock was inverted compared to the above
figure. As my original code did not work, I then inverted the
clock as in USIP and it has worked since.
Xcode Use
At this point I have a system that will compile C code into ROM images and a tool that will put that binary file into a chip. Next, I wanted to have the code managed by Xcode so that it would jump to error lines in code and allow easy point-and-click access to program functions.
I found this article by Dr. Rolf Jansen on using FreePascal with Xcode. It showed me how to define scripts that are executed when Xcode compiles and links the code. Also I found this Apple document that lists the script variables available.
Setting Up Xcode for Build
- Create a new Standard Tool Project in your microprocessor code area.
- Delete the main.c file and bring in your microprocessor files.
- Pick the project and Get Info, Pick Styles and scroll down and
turn off ZeroLink.

- Pick the Target and Get Info. Pick the Rules Tab and change the C
source files to use a script. We use the command line from the
makefile in the sample code with the variables mentioned in the Apple
document. This will put the .o files into the build
directory. This isn't quite right, but will work for now. The
output file is specified to be the .o file and ROM file; this is like a
make line in that if the thing isn't built yet, do what you can to get
to the next stage.

- Add a new rule for the *.o files to do the link. This will
use the command lines from the make file to link all of the objects into
the ELF file and then build the ROM file. This is not exactly the
right way, but I could not find any script variable that had the names
of all of the .o files or the build phase where to place this. So we
sort of cheat and do the whole operation in a single script.
Suggestions here would be welcome. Note we specify the ROM file
as the output so the script will terminate when the ROM is created.

Using Xcode
At this point, the project will be almost like any regular Mac project. You can build the code. If you get errors, clicking on the error line will take you to the offending line of code. Command-double-clicking with a highlighted function or constant will jump to where that code is defined.
You can then use this project as the basis for other projects. Note you must change the .o script for each project, it has to have the names of all the .o files to be linked. (This is where someone out there is going to tell me the right way to do this.)
Clean doesn't seem to work, but that is because I put the .o files in the wrong place. In a standard project, they are in build/<name>.build/<name>.build/Objects-Normal/ppc. Wasn't quite sure how to build that!
Future Work
It would be nice if the debug worked, but that would need a micro that would support remote debugging and download. Someday I hope the Mac will support USB JTAG/BDM pods so we could develop/download/debug on 32-bit processors like embedded PPCs, ColdFires, or various DSPs.
The good news is, with its UNIX and open source support, Mac OS X already has the tool chains for most processors. The only things missing from making the Mac's tools work for embedded systems are download hardware/software and Xcode support.
Appreciation
There is a wealth of material out there on programming and using the AVR micros, and I thank the various keepers of these sites for making this stuff available. The folks who build and maintain the tools for this and other processors deserve our thanks for their huge effort making it so easy for us. Finally, I thank the writers of the papers mentioned above for taking the time to explain how to get from point A to point B, giving me a clue on how to get this stuff working.
Jim Schimpf does embedded software development for telecommunications test equipment and develops Mac freeware in his free time, while not doing his other job of tractor repair/general grunt work for his 200-acre farm.
Return to the Mac DevCenter
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 14 of 14.
-
AVR MacPack
2008-07-21 20:35:24 craiglandrum [Reply | View]
Found a terrific free downloadable package with pre-built fat binaries for avr-gcc and everything you need here:
http://www.obdev.at/products/avrmacpack/index.html
This site also has tips on how to set it up for use with XCode. I also recommend AVRFuses for determining the best fuse settings and downloading your code using a Mac GUI interface. It uses the avrdude app to do the communication, whihc worked great with my stk500 Atmel board and a keyspan USB to serial dongle. You can get AVRFuses here:
http://www.vonnieda.org/software/avrfuses
-
Process missing New Devices.
2006-06-07 12:23:57 feurig [Reply | View]
The the gcc chain moves slowly. The avr comunity does not. If you follow this process the devices atmel released (about 20 new processors) will be missing.
At a minimum these two patches should be applied to the source before building.
http://www.freebsd.org/cgi/cvsweb.cgi/ports/devel/avr-gcc/files/patch-newdevices
http://www.freebsd.org/cgi/cvsweb.cgi/ports/devel/avr-binutils/files/patch-newdevices
Cheers.
-
Process missing New Devices.
2006-06-07 12:40:37 feurig [Reply | View]
See Wuenches comments in this thread.
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=36051&highlight=osx+avrgcc (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=36051&highlight=osx+avrgcc)
Also I am getting to be a big fan of compiling once and packaging the resulfs.
http://www.digithink.com/embedded/Avr/OSXToolChain
(http://www.digithink.com/embedded/Avr/OSXToolChain)
This is a great article btw.
-
One Small Change
2005-05-06 07:51:15 dshoemake [Reply | View]
I would change the lines from build/aser.rom to
build/$(PRODUCT_NAME).rom
This will result in a rom file with the same name as the project
This works great if you're working with a single c file, when you start adding files then you have to link the resulting files outside of Xcode.
-
uisp
2005-01-27 21:03:27 JakeMcGuire [Reply | View]
I had really good luck using uisp in the past with a Keyspan USB-to-Serial adapter and the STK500 development board. I also had good luck with the same adapter, avarice, and the JTAGICE, but I've heard that the new JTAGICE has a USB interface that isn't supported yet.
If I had an amazon associates account I'd put a link to the Keyspan doohickey here, but if you search their site for it you'll find it no problem, or you can go to CompUSA or Fry's or some such and get one.
-
Fantastic Article
2005-01-25 05:14:21 MattMass [Reply | View]
I just wanted to write in to say that this article is absolutely fantastic. I have been a long time embedded developer because of work with, coincidentally, the McGill Solar Vehicle Project. We use PicMicro MCUs, and I've always been frustrated with the dev tools available.
I've actually worked on developing a USB digital I/O and In-Circuit programmer board for OS X, along with a Cocoa framework for using it, and for using serial I/O easily within a Cocoa program. You've inspired me to take another look at this project. Thanks very much.
-
E Mail responses
2005-01-24 09:58:53 jimschimpf [Reply | View]
Hi,
I have received e-mail from various people about other tool chains and where to get them. I'll list them here:
Jeff Kinross - Freescale Coldfire
http://sourceforge.net/docman/display_doc.php?docid=12275&group_id=39827
Rob Barris 68HC11/HC12
http://m68hc11.serveftp.org/m68hc11_port.php
Plus the items listed so far in this thread. Thanks to all.
--jim
-
Corrections!
2005-01-24 08:52:11 EW [Reply | View]
In the bulleted list there are some mistakes:
- libc should be: avr-libc
- usip should be: uisp
The author pointed out how difficult it is to use uisp on the Mac.
The author should have gotten avrdude instead:
http://savannah.nongnu.org/projects/avrdude
avrdude is known to work under Linux, FreeBSD, Windows, and Mac OS X. And it's licensed under the GPL.
-
Corrections!
2005-01-24 09:53:58 jimschimpf [Reply | View]
Thanks missed those things.
I hadn't seen avrdude. It seems very complete with all the bells and whistles. The only problem is it requires a parallel port. I had some very slight acquaintance with a USB <-> Parallel port device but never could get it working on the Mac. Perhaps others would work. I ended up rolling my own download program since I had the Delcom board. Not as nice as avrdude but pretty natural as I do this kind of thing in my (paid) embedded work.
I will look into adapting the Delcom device into the avrdude program. I just did a build of it on OS X and found it does need a bunch of _ppi_xxxx routines which I guess is where you insert your parallel port drive routines.
--jim -
Corrections!
2005-04-25 21:47:32 EW [Reply | View]
Read the avrdude user manual.
- avrdude will use a serial port to use an STK500 or AVRISP programmer. It is known that you can use a USB->serial port convertor successfully on Mac (IIRC, Keyspan works).
- On other platforms with using a parallel port, you should not have to change the avrdude source code. You can add a programmer to a configuration file.
Also, for all things AVR, you should visit the AVR Freaks website at www.avrfreaks.net (no affiliation). It has an incredibly active community with several online forums including an AVR GCC Forum.
Eric
-
Other MacOS X solutions
2005-01-22 09:39:26 kcoble [Reply | View]
Thanks for the article.
Other solutions for embeddeded controller programming with MacOS X exist as well. I am the author of one (http://www.MacRobotics.com/MacrocASM/MacrocASM.html), and others are discussed at the MacTRON Macintosh electronics hobbyist LISTSERV (www.wildrice.com).
-
Embedded Development under OS X
2005-01-22 08:37:11 DaleChayes [Reply | View]
Great opening! For some time it has felt to me that OS X provides a very good platform for embedded development and all the associated stuff that goes with organizing, documenting and supporting such projects, particularly in small (tiny) work groups.
We took a run at working with the gnu tool chain for the TI MSP430 family and got fairly far but found the debugging interface awkward at best (had to use a Linux box for the gdb-proxy....)
One of the first order issues in my mind is to find a place to focus the discussion about embedded development under OS X so that we can work together to create and demonstrate a critical mass.
-Dale Chayes <dale at ldeo.columbia.edu>
-
Embedded Development under OS X
2006-08-08 12:11:17 wayneContello [Reply | View]
I am an embedded developer who is very interested in bring tools to OS X. I am somewhat frustrated with:
- what is available for OS X
- the procedures for compiling for OS X
- the interface adapters (jtag, serial, parallel) for OS X
I am willing to participate in further the mac as a development platform.
(wayneatcontellodotnet)
Wayne





I have been using xcode for about 3 months so im very new to all of this c++ malarky its awesome!
I am using an Atmel 168 chip which is programmed and can be programmed through terminal. I have also established a constant comms programme with the chip.
The next part of my project is to not use terminal at all and write the communications process within my programme. www.kamtrak.blogspot.com for more information. But what i wanna do is to send voltages out of the chip which is controlled by my face tracking software. I have managed to get the debugger to display the correct values needed to move a camera rig in the direction of a persons face but now i need to establish comms through this programme.
I have bought the chip and programmer from www.nerdkits.com and they seem to have a lot of support for a lot of things its just i cant seem to find any existing code for communicating between the mac laptop via usb to serial and into the chip.
Any help on this would be much appreciated. cheers