Skip navigation.
Home
Tech for Linux Users

Installing and using SDCC on Linux

SDCC, the Small Device C Compiler, is an open source C compiler for a number of microcontrollers and small platforms. This document describes how to get SDCC installed and working under Linux, provides an example code and a description of how to compile it for the 16f877, 16f84 and 18f252.

Installing gputils

Before installing SDCC it is wise to first install gputils if you are going to be compiling for the PIC platform. This is because SDCC requires some of the files contained within the gputils package for PIC development. To retrieve the CVS version of gputils use the following commands (you will need to have CVS installed). The first command will ask for a password, since you are accessing the cvs anonymously this should be left blank:

	cvs -d:pserver:anonymous@gputils.cvs.sourceforge.net:/cvsroot/gputils login
	cvs -z3 -d:pserver:anonymous@gputils.cvs.sourceforge.net:/cvsroot/gputils co gputils

It is then configured and compiled in the normal manner:

	cd gputils
	./configure
	make

To install you will need to be root:

	make install

Installing SDCC

SDCC is always under active development, it is thus best to get hold of the source from CVS and compile it to be sure to have the latest developments. To download the source, change to the directory you wish to download the source into (a sub directory will be made below this to hold all the files) and run the following commands. As before, the first command will ask for a password, since you are accessing the cvs anonymously this should be left blank:

	cvs -d:pserver:anonymous@sdcc.cvs.sourceforge.net:/cvsroot/sdcc login
	cvs -z3 -d:pserver:anonymous@sdcc.cvs.sourceforge.net:/cvsroot/sdcc co sdcc

The code then needs to be configured and compiled (you will require GCC to be installed for this):

	cd sdcc
	./configure
	make

Then, as root, install it:

	make install

Setting up PIC header files

NOTE:This seems to be done already if packages are installed from source, as part of the installation procedure, however if SDCC has been installed from a binary distribution there is a good chance that it will not have been.

Rather than creating a set of headers for the PIC series of microcontrollers, the SDCC developers decided to create a script, called inc2h.pl, to parse the .inc (or include) files that have been created as part of the gputils effort. This file seems to be missing as part of the binary package available for Debian. This file can be obtained, either by downloading SDCC CVS as described above (in which case it can be found in "sdcc/support/scripts/") or by downloading it via the sourceforge cvs front end.

Once downloaded this script will allow the user to parse a .inc file, creating a stream in a .h file format, which is sent to the standard output. On Debian the header files are stored in "/usr/share/sdcc/include/" and the gputil's .inc are stored in "/usr/share/gputils/header/", this may vary from distribution to distribution. With this in mind, to create header files for all available .inc files, as root:

	cd /usr/share/sdcc
	cp /<path to>/inc2h.pl ./
	cd include
	mkdir pic
	cd pic
	for file in `ls /usr/share/gputils/header/p*.inc`
	do export picstring=`echo $file | sed "s/\/usr\/share\/gputils\/header\/p//; s/.inc//"`
	../../inc2h.pl $picstring /usr/share/gputils > pic$picstring.h
	done

Installing PIC16 Libraries

SDCC includes a number of libraries which have been written for the PIC16 family of microcontroller, this family of PICs are generally labeled PIC18fxxx or PIC18fxxxx. These libraries include functions for initialising the PIC, utilising I2C, math operations and even some functions for printing to the stdout (where ever that may be). These libraries are not compiled and installed as part of the main install and thus must be done separately as not all users of SDCC will be creating PIC16 projects. To build the libraries:

	cd /<path to sdcc source>/device/lib/pic16/
	./configure
	make all

The build for I/O libraries is also separated out as these are required to be built for each supported device individually and this can be time consuming. It is possible to limit the devices that the make process builds for by editing the "pics.build" file. The following command makes the I/O Libraries:

	make lib-io

The library build then needs to be completed and the libraries then need to be copied to there correct locations:

	cd ..
	make model-pic16
	make install

Lastly the header files must be copied to there correct locations:

	cd ../include/
	make install

PIC16F877 Example

The following C program, count.c, should compile with the following command:

	sdcc -mpic14 -p16f877 count.c

This should create a hex file (count.hex) which can then be transfered to the pic using your favourite programmer. A description of how to use the picp driver for the PicStart Plus can be found on "Using PicStart Plus under Linux". When running correctly this program should create sucessively bigger square waves on each pin of port A, the frequencies will depend on clock speed.

PIC16F84 Example

The following C program, test.c, should compile with the following command:

	sdcc -mpic14 -p16f84 test.c

This should create a hex file (test.hex) which can then be transfered to the pic using your favourite programmer. A description of how to use the picp driver for the PicStart Plus can be found on "Using PicStart Plus under Linux". When running correctly this program should create sucessively bigger square waves on each pin of port A, the frequencies will depend on clock speed.

PIC18F252 Example

The following C program, wave.c, should compile with the following command:

	sdcc -mpic16 -p18f252 wave.c

This should create a hex file (wave.hex) which can then be transfered to the pic using your favourite programmer. A description of how to use the picp driver for the PicStart Plus can be found on "Using PicStart Plus under Linux". When running correctly this program should create sucessively longer square waves on each pin of port A, the frequencies will depend on clock speed.

Serial Example for PIC18F252

The following C program, serial.c, should compile with the following command:

	sdcc -mpic16 -p18f252 serial.c

This should create a hex file (serial.hex) which can then be transfered to the pic using your favourite programmer. A description of how to use the picp driver for the PicStart Plus can be found on "Using PicStart Plus under Linux". This is a more advanced example and it shows how to achieve serial transmission using the onboard usart. When interfaced correctly to a PC, via a serial line driver such as the Maxim MAX232, the ring array should be transmitted. This simple example does not use the printf library, though the putchar function provided here should be capable of allowing the printf function to be used.

This follow up example, printf.c (the hex file printf.hex), has the printf_tiny function working.