sdcc for PIC HowTo

A simple walkthrough for getting started with C programming on PIC micros using the sdcc C compiler.

P.J.Onion

1 Introduction

Traditionally the bulk of development on PICs, both by professionals and enthusiasts, has been done in assembly language. In part this was due to performance requirements, and part due to the lack of any real alternatives. However, with the increase in the power of available PICs and the availability of several high level languages for PICs, it is becoming increasingly appropriate to consider HLLs for PIC projects.

While C compilers have been available to professional programmers for some time, their cost has limited their use by enthusiasts. Now, through the open source development model, free tools are becoming available.

The purpose of this HowTo is to provide help getting started with one such tool “sdcc” - the Small Device C Compiler.

1.1 What you should know from the start

This is NOT a tutorial on C programming! If you aren't already a reasonably competent C programmer, go away and learn C.

This is NOT a tutorial on PIC programming either, but this is less important.

This is NOT a tutorial on the various package management schemes and tools used by different Linux distributions, and aims to be distribution independent.

This WILL help you overcome any specific problems associated with installing sdcc and getting it working.

SDCC supports many small device families, and PICs are not the best supported but as with all open source projects improvements are only made if someone is willing to make the effort. [Add reference to the PIC developers here].

2 The Tool Chain

SDCC does not operate in isolation, and when targeted on PIC devices it makes use of the gputils tools to provide the back end of the compilation process. The simplest tool chain is shown below.

The starting point is the “C Source File”. You may create this with what ever editor you like. This is read by SDCC which produces a PIC assembly language file. The gputils tools take over at this point and gpasm is used to convert the “.asm” file to a “.o” file. If you were expecting gpasm to produce a “.hex” file directly, then you need to learn about using the gputils in “relocatable mode”, so go and have a look at

Craig Franklin's page about GPUTILS Relocatable Object HowTo. The next step is to use gplink to convert the “.o” file into a “.hex” file suitable for use with whatever device programmer you chose to use.

Note: There are a few simplifications in the above description, but these won't matter until we move onto programs more complex that the obligatory blink_a_led.c .

2.1 SDCC

2.1.1 Getting the sources.

If you've only ever installed pre-built binary packages on your Linux system before, then your in for something new because the first step is to download the sdcc source code and build sdcc from that. This will require you to have a working C development environment on your Linux system. If running “gcc -v” produces a few lines of information finishing with a version number you're probably OK.

The SDCC project has a home page on sourceforge. The sourceforge download page will allow you to download the latest released version, or if you want to be more “bleeding edge” you can download one of their nightly snapshots. You'll need a “SDCC Source Code (sdcc-src)” tar ball. Unpack the tar-ball into somewhere appropriate. (ED: Should this be /usr/local/src or some where else ?)The instructions that follow will build just the PIC compiler.

In the top level of the source tree (where you'll find a file called “ChangeLog”) run the following command:

./configure --disable-mcs51-port --disable-gbz80-port --disable-z80-port \
--disable-avr-port --disable-ds390-port --disable-ds400-port --disable-hc08-port \
--disable-xa51-port

You should see lots of output that starts off like this...

checking for gawk... gawk
checking version of the package... 2.5.1
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes

And ends like this....

config.status: creating gui.src/Makefile
config.status: creating gui.src/serio.src/Makefile
config.status: creating doc/Makefile
config.status: creating ddconfig.h



This has checked that all the “stuff” needed to build sdcc is present on your system, and has writen the “makefiles” that will automatically build sdcc from the source code. All that is needed is to run the commands

make

and assuming there are no errors, the next command needs to be run as root so that the files can be installed in the correct places.

su -c “make instal”

While were in the “building stuff” mood, the next few commands will build the libraries for the 16 bit pics. We'll be using them later on.



cd device/lib/pic16
./configure
make
cd ..
make model-pic16

Then as before

su -c “make instal”

Finally, there are a few more libraries to build....



cd pic16

make lib-io

These libraries arn't included in the makefiles yet, so they need to be put into the right places manually....

cp libio/usart/*.o /usr/local/share/sdcc/lib/pic16

And were done building sdcc.

2.2 gputils

The gputils package contains the tools that are needed to convert the assembly language files (.asm) produced by sdcc into object files (.o), and then to link them together with any required library code to make the final executable.

Tool

Inputs

Action

Outputs

gpasm

Assembly language file converted from C language file by sdcc

Convert “.asm” file to “.o” file

COFF format object fil

gplink

Object files “.o” & library files “.lib” & target configuration files “.lkr”

Place code from object and library files into PIC memory, allocate RAM to variables

Intel format hex files, listing file, map file.





2.3 Programmers

3 Building sdcc

4 Blink-a-LED

5 More Complex examples

6 Conclusions