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.

    Toolchain Components

    The components of your toolchain are a code editor, a compiler, a linker, an assembler, a debugger, your reference documentation, a development board, and the means to communicate and download from your coding platform to your development board. If your primary concern in learning about microcontrollers is time and energy, then the professional tools available will be the most efficient way to enter the field. But if money is a concern there are alternatives that cost more in creative effort and time, but cost nothing in cash money.

    Tools such as Keil Uvision, from ARM provide an integrated and comprehensive way to develop microcontroller software. The downside is that it is an expensive solution and it is only available for Windows machines. Another non-Mac solution is AVRDude , but this tool is designed for only one manufacturer's chips, ATMEL, though these are extremely popular.

    But here, I'll concentrate on a toolchain that is completely compatible with Macs, and, with the possible exception of the development board, is totally free. You'll find in the software examples that come with Xcode our loader written as an example of USB transfer code for the IOKit, EZ-Loader. And I'll provide a template for a project that integrates SDCC with the rest of the tools of Xcode. Together these tools will take you quite a way along the path exploring microcontroller knowledge.

    Here's what you'll need:

  • A Mac with Leopard 10.5 and the Developers package
  • SDCC, the Small Device C Compiler
  • Familiarity with the C language. You should be familiar with the compiling, linking, and debugging processes.
  • Familiarity with the Mac Terminal program

  • Xcode

    Of course, since you're reading this, you've got a Mac, but I'd recommend an upgrade to Leopard even with a PowerPC machine( that's what I use .) Xcode is definitely worth the hassle of learning a new OS.
    Xcode icon image
    The code editor is brilliant, with multiple views, automated error labeling, and code folding, you're working with a tool that helps you organize your thoughts as well as your code. The Organizer let's you keep sample code, or libraries, or other projects, instantly available for reference. And there's a software reference assistant available to give you suggestions when you find yourself lost in the technical miasma.

    You become a member of the "Developer Connection", which provides you with access to enormous amounts 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.

    Download The Developer's package and Xcode here.

    Once Xcode is installed and working, use this Xcode SDCC-Build template to work with SDCC within Xcode. This template will only work if you move the root folder of your SDCC installation into your Developer folder: /Developer/sdcc.

    So it makes sense to download and install SDCC BEFORE you download this template.

    SDCC
    Xcode SDCC-Build template

    Download this Xcode project template to create SDCC compiled HEX programs in Xcode.

    The opensource Small Device C Compiler, SDCC is a suite of shell programs that create microcontroller applications from c code. It compiles, assembles and links your c code into HEX files, loadable programs that will function in your microcontroller. As an opensource project the authors have assembled the header files, source files, and tools, for virtually every 8051 derivative chip that's out there. It's an amazing group effort that deserves support, and in my experience functions beautifully.
    Here are the details for downloading and installing SDCC.

    I'll show you how to setup Xcode to control SDCC and build HEX files that you can download to your development board.

    Microcontroller Development Kits

    Manufacturers need to educate potential users about the features and methods of their chips, so they provide packages of tools and software that include tutorials, reference libraries, and sample code. Most assume that the developing platform is a Windows PC, but that is changing, and once you've become familiar with the peculiarities of coding for microcontrollers, it's easy to adapt the code to a Mac environment.

    Development Boards

    So here you have many choices, controlled primarily by the manufacturer of the chip you are interested in. The cost of these boards varies wildly depending on the processing power and ancillary technologies that are provided.
    The least expensive are chips you find in existing circuits that you can communicate with. This is the solution I chose. These cost nothing, but take time and effort to locate and research, and you should be willing to take things apart and do a little soldering.

    But there are many alternatives just a bit more expensive.

  • The Arduino, an opensource hardware project is by far the easiest and least expensive to get into. There is a large community of users who clearly have a lot of fun with their gizmos. And there are many support sites.

  • The PICStamp, is another very popular hobbyist board that is available in many different configurations, and at many different price points.

  • In the future I'll discuss ways to simplify these boards use with a Mac. There are quite a few others I'm not familiar with. If you want a look at what is readily available check out Digikey, they have an enormous selection and limited quantities are welcome. But, all the alternatives do have one major disadvantage—they are not free.

    And you're off!

    There you have it. Assembling these components provides a toolchain that you can use to explore the features of microcontrollers, and create your own gizmos, or robots, or whatever. And the price can't be beat.

    Tuesday, May 19, 2009

    Starting Up...

    Hey, if you're like me, you've seen a lot of crazy projects utilizing micro-controllers that look like they'd be a lot of fun to get into. They seem interesting, challenging, and, could teach a lot about electronics, programming, and a whole new world of creative control over the "machines."



    But.. I'm a Mac type of guy, and the sad truth is that , right now,  there aren't very many resources that allow someone with a Mac to get into micro-controllers easily. Almost all the documentation for 8051s,  8052s, the Arduino, etc. is written with the assumption that the user is sitting at a Windows PC or  Linux. Some companies boast of cross-platform technology, but they are only talking about Linux, and as soon as you get close to the machine layer that deals with hardware–as micros do very early on–that's not cross-platform enough.



    So here are some tips, links, and whatever I can scrounge up, about how to get up and running with a workable, and extremely affordable, tool-chain of development software that is focused on the Mac as the primary development platform, and guess what, it's a lot easier than you'd think.