ElectricRock Blog

Microchop C30 Compiler for Linux

Microchip’s C Compiler for PIC24 MCUs and dsPIC DSCs (herein referred to as C30) is a GCC derivative and therefore the sources are freely available to build it on any platform. This page provides information and tools for building and installing C30 on Linux (specifically Ubuntu, but should be compatible with other distros).

Installation

At this stage I am not providing C30 binaries; however, the source code is freely available, so it is possible to build it yourself.  In order to simplify the process I have written a bash script to automate most of this task. The build script will unzip the sources, apply patches, then build and install C30′s  GCC and binutils.  It will also extract from the evaluation version of C30 to get the non-free component and install the required elements in the correct places. The current version of the script (v 0.1) is tested with C30 version 3.20 and 3.23.

The following instructions detail how to install C30 using this script.

Install prerequisites

On Ubuntu this means will need to install build-essential bison flex tofrodos wine checkinstall.  Other platforms may have different requirements, but basically you need a GCC environment with flex and bison, the dos2unix tool, and wine.  Please not that wine version 1.1.41 appears to cause problems with the installer, so for now stick with an earlier version (e.g. v1.0.1 in the Ubuntu 9.10 repos.) Checkinstall is optional, but makes installation neater if your platform supports it.

Download required sources

Tailor Build Script

Once you have the build script it may require some editing to tailor it to your specific setup.  The main things you will need to consider are:

  • The location in which you want to the compiler to be built (default is /tmp/c30/build)
  • The temporary location where the non-free elements will be extracted (default is /tmp/c30/non-free)
  • The version of compiler to be installed (default is currently 3.23)
  • Path to the downloaded files (default is in $HOME/Downloads)
  • Prefix to install to (default is /usr/local)
  • Command to install (by default checkinstall is used, however this could be replaced with make install for platforms it does not support.)

Aside from the install command, these settings can be changed by editing variables near the start of the script.  Some documentation is provided in the script itself.  I highly recommend reading the script through before using it because your setup may differ from mine.  I have tested it under Ubuntu 10.04 amd64, Ubuntu 9.10 i386 and amd64.

Run It

Running the build script is simply a matter of

chmod +x build-c30.sh
./build-c30.sh

The directory from which you run it is unimportant as it will change to the directory specified in the script. If you specify -f on the commandline the script will not prompt for user input (aside from sudo password if required) and will automatically overwrite the temporary directory.

That’s it. If it worked you now have C30 installed (and hopefully working.) If you have any issues, suggestions or other feedback please leave a comment.

Patches

In order to build a working C30 compiler under Linux a bit of patching is required. I have adapted patches from John Steele Scott and other sources, and provide them here for you. I have changed the version numbering scheme from my previous blog posts, as it was no longer matching up with the version of the sources being patched. Now I will just number the patch versions sequentially as I release them.

The current version is v01 and is tested with compiler v3.23. It can be downloaded from:

Using the Compiler

Once the compiler is installed, the various tools can be executed by running pic30-coff-<toolname> (e.g. pic30-coff-gcc).  Here is an example to build a very, very basic program for a dsPIC30F4013.

test.c

#include <p30f4013.h>

int main()
{
  while(1);
}

Commands

All-in-one GCC:

pic30-coff-gcc -o test.coff -mcpu=30f4013 test.c

Step by step:

pic30-coff-gcc-4.0.3 -S -o test.s -mcpu=30f4013 test.c
pic30-coff-as -o test.o test.s
pic30-coff-ld -o test.coff test.o -lpic30 -L/usr/local/share/pic30-nonfree/lib

As you can see from the last line, a little bit more tweaking of the patches is required to get ld to pick up the library path automatically, but it isn’t too hard to specify it manually.


Sample Makefile

I have created a sample Makefile that should simplify the build process. Download it (here), open it in a text editor and tweak the variables as necessary (some comments provided to help), rename to ‘Makefile’, run make and with any luck it will build successfully. By default it assumes all your C and H files are in the same directory as the Makefile, but it is easy enough to add extra paths, as shown in the file itself. If you have any questions, leave a comment.