Sunday, May 31, 2009

How to setup Xcode with SDCC


Step One: Download and install Xcode

In order to download the Developer's package you must become a member of the "Developer Connection", which provides you with access to enormous amounts of sample code, a plethora of API's, as well as the definitive reference for all of Apple's available technologies, including the workings of the IPhone. But it's free and easy.

You will need about 3 Gb of free space on a drive, and of course, that does take a while to download, but did I mention, it's free?

Download The Developer's package and Xcode here.


Step Two: Download and install SDCC

  • At the SDCC web site, navigate to the osx download page, make sure to choose the sdcc-macosx version.It will download as a bzipped tar ball, just double click to uncompress.

  • Place the uncompressed root folder in the root of your Developer folder like so: /Developer/sdcc
  • Build the software with these commands in the terminal

  • cd /Developer/sdcc
    ..sends the terminal into the sdcc folder

    ./configure
    ..the configure program will prepare a 'makefile'

    make
    ..the make program will compile sdcc, this will take some time

  • Test your installation with this command in the terminal

  • sdcc -v
    ..if all is well, you should see a return of

    SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.0 #5416 (Mar 22 2009)(Mac OS X ppc)

    This line indicates the installation was successful, it lists the target microcontroller chip libraries contained in this version.

    Ok, now you have a complete embedded C system on your machine. You can use SDCC within the terminal immediately, if you like, but I found the learning curve pretty steep, so, to make things a little more convenient I came up with the next step.


    Step Three: Download and install the Xcode SDCC-Build template

    Before you download, You'll need:

  • Leopard 10.5.0 or later,
  • Xcode 3.0 or later,
  • SDCC installed at /Developer/sdcc.


  • SDCC is a command-line program, actually a suite of them, that compiles, links, and assembles, files you specify into a sequence of interim files that are used to create the final product, a HEX file, or library. It also includes a debugger, and several utility programs to aid in the task. Xcode is designed to create Mac programs, and it normally uses an opensource program called GCC, for Gnu C Compiler. So what's so different? Is this only about convenience and polishing the Apple?

    Well, the issue is in the name, Small Device C Compiler. Microcontrollers have an extremely small memory space compared to a MacBook Pro. Each targeted chip, while sharing a base of features and commands derived from Intel's 8051, also have individual capabilities that must be coded for specifically. Types can be extremely puzzling due to the constraints of memory: int, long, char, XDATA, IDATA each has special meaning in microcontrollers, in general, but also in each chip. And the basic form of the main function is totally different, in a Mac, the main function comes to an end and returns a value, from which the machine can determine the status of the result, in micros, there is no other machine and the main function is intended to loop for ever.

    What it does

    So the trick is to combine the great convenience and organizing features of Xcode, with the detailed expertise of SDCC. The solution is to use Xcode's special build targets. Xcode offers these configurations as a way to accomplish a wide variety of tasks within Xcode, by utilizing Xcode's extensive battery of environment variables, and the scripting capabilities inherent in the BSD system.

    So within the template, there is a makefile configured to call SDCC as the compiler for the specified target file, in each build phase there are scripts which copy or move files so everything lands in the right place. The last script copies the final compiled output file, processes it with a SDCC utility, ipackhx, and delivers it to the loader,a seperate program, which downloads the program to the chip automatically.

    You'll find the template is configured to use the EZ-Loader example from the IOKit's USB sample code as the loader, which is specifically for theEZ-USB line of chips from Cypress Semiconductor but it can be easily changed to use any other loader, as long as that loader can be controlled via a bash script.