ElectricRock Blog

Installing Microchip’s C Compiler for PIC24 MCUs and dsPIC DSCs (C30) on Ubuntu 9.04

by matt on Aug.16, 2009, under How To

NOTE: This page is now outdated, please go to http://www.electricrock.co.nz/blog/microchip-c30/

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.  However, the documentation for getting a working setup seems to be sparse.  This blog entry details the steps I followed to install it under Ubuntu 9.04, mostly gleaned from  http://embeddedfreak.wordpress.com/2008/10/10/compiling-mplab-c30-v311b-under-linux. There are some small changes from the embeddedfreak version, I am using v3.12 of the compiler (the most recent version at the time of writing) and I will provide details of how to setup the non-free part.

Code lines in blue are to be executed at the terminal, sorry that they look so ugly at the moment–I will pretty up the formatting at some point.

Disclaimer: These instructions are provided as a guide only, you use them at your own risk–I accept no responsibility if something goes wrong :) .

Installation

Install prerequisite software

  • Install packages from the Ubuntu repositories:
    sudo aptitude install build-essential bison flex tofrodos
  • Install wine (instructions available from the Wine website: http://www.winehq.org/download/deb).

Setup paths

The exports below can be varied depending on where you prefer things to be put. At the risk of stating the obvious I’ll outline what each of the exports is for:

  • C30BUILD is the temporary directory where the C30 tool suite is built.
  • C30INSTALL is  where it will be installed to.
  • DOWNLOADDIR is where you save files to when downloading.
  • WINEPREFIX is where you want to install the proprietary tool suite.

export C30BUILD=$HOME/c30-build
export C30INSTALL=$HOME/Apps/pic30
export DOWNLOADDIR=$HOME/Downloads
export WINEPREFIX=$HOME/Apps/pic30-wine

Download sources, etc.

Building Binutils

  • Unzip the sources.

    mkdir -p $C30BUILD/binutils
    cd $C30BUILD/binutils
    tar -zxvf $DOWNLOADDIR/mplabalc30v3_12.tar.gz
    tar -jxvf $DOWNLOADDIR/pic30-binutils-3.11b.tar.bz2
  • The microchip sources have been edited on windows, so we need to convert files to unix line endings so that they can be patched.
    find . -type f -exec dos2unix '{}' ';'
  • Apply John Steele Scott’s patches.  So far as I can tell, these fix a few compile issues with the microchip compiler and make it play nicer under Linux.
    for i in pic30-binutils-3.11b/patches/*.diff; do echo "Applying patch $i..."; patch -p0 < $i; done
  • Build it, the version number in DMHCP_VERSION needs to match the version of the non-free compiler you install.
    cd acme/
    CFLAGS=-DMCHP_VERSION="v3.12-Debian" ./configure --prefix=$C30INSTALL --target=pic30-coff
    make
  • Install, if you install to a location that you don’t have write permission to (e.g. /usr/local) you will need to use sudo.
    make install

Building GCC

  • Similar process to binutils above for building GCC.
    mkdir -p $C30BUILD/gcc
    cd $C30BUILD/gcc
    tar -zxvf $DOWNLOADDIR/mplabc30v3_12.tar.gz
    tar -jxvf $DOWNLOADDIR/pic30-gcc-3.11b.tar.bz2
    find . -type f -exec dos2unix '{}' ';'
    for i in pic30-gcc-3.11b/patches/*.diff; do echo "Applying patch $i..."; patch -p0 < $i; done
  • We take a slight deviation from binutils here as we create a separate directory for GCC to build in, apparently it likes it that way.
    mkdir build
    cd build
    CFLAGS=-DMCHP_VERSION="v3.12-Debian" ../gcc-4.0.2/gcc-4.0.2/configure --prefix=$C30INSTALL --target=pic30-coff --enable-languages=c
  • Then we have to touch c-parse.y cause (according to embeddedfreak) it doesn’t get generated correctly, so we need it to be recreated. I just followed the instructions and it worked.
    touch ../gcc-4.0.2/gcc-4.0.2/gcc/c-parse.y
  • Build it and install it (again, use sudo for make install if you have to).
    make
    make install
  • Link the binutils into GCC’s tool path so it can find them, and create a link to GCC itself so we don’t have to type the version number every time:
    ln -s $C30INSTALL/bin/pic30-coff-as $C30INSTALL/libexec/gcc/pic30-coff/4.0.3/pic30-coff-as
    ln -s $C30INSTALL/bin/pic30-coff-ld $C30INSTALL/libexec/gcc/pic30-coff/4.0.3/pic30-coff-ld
    ln -s $C30INSTALL/bin/pic30-coff-gcc-4.0.3 $C30INSTALL/bin/pic30-coff-gcc

Setup the non-free part

  • Install the downloaded non-free version of the compiler using WINE. Make sure you run this from the terminal after setting the WINEPREFIX environment variable (as opposed to e.g. double clicking the EXE icon).
    wine {path to installer.exe}
  • Copy required directories and files, it would probably be tidier to fix the search path for the info files in the source, but instead I just make some links where necessary.
    cp $WINEPREFIX/drive_c/Program\ Files/Microchip/MPLAB\ C30/bin/c30_device.info $C30INSTALL/info
    ln -s $C30INSTALL/info/c30_device.info $C30INSTALL/c30_device.info
    ln -s $C30INSTALL/info/c30_device.info $C30INSTALL/libexec/gcc/pic30-coff/c30_device.info
    mkdir $C30INSTALL/pic30-nonfree
    cp -r $WINEPREFIX/drive_c/Program\ Files/Microchip/MPLAB\ C30/support $C30INSTALL/pic30-nonfree
    cp -r $WINEPREFIX/drive_c/Program\ Files/Microchip/MPLAB\ C30/include $C30INSTALL/pic30-nonfree
    cp -r $WINEPREFIX/drive_c/Program\ Files/Microchip/MPLAB\ C30/lib $C30INSTALL/pic30-nonfree

Troubleshooting

Provide Resource File

When running the compiler you may find that it baulks when running a command such as:
$C30INSTALL/bin/pic30-coff-gcc -mcpu=30F4013  -o test test.c
with the error
pic30-coff-cc1: warning: Provide a resource file
pic30-coff-cc1: error: Invalid -mcpu option.  CPU 30F4013 not recognized.

To solve this add -mresource=$C30INSTALL/info/c30_device.info to your GCC command line.

Unknown Processor

If you get the error:
Unknown processor 30F4013.
Then you haven’t created the links to the info file correctly. I found from running gcc with the -v (verbose) flag that this error is actually generated by the assembler, which is being called from under the libexec directory (hence the need for the info file to be linked there also.)

Next Step

Now that you have built a working toolchain, it’s time to setup an IDE to use with it (if you are that way inclined).  I recommend Piklab. You can find instructions for using the C30 toolchain with Piklab in this post.

References

You should now have a working C30 compiler installed in $C30INSTALL.  Thanks to KUNILKUDA Embedded Freaks for the tutorial which I followed  and from which these instructions are mostly derived.

:, , ,

3 Comments for this entry

  • László Meskó

    Thank you the install guide!
    To make it better, in the troubleshooting, it worth to mention the following:
    If you create a test2.c sample code with the following:
    int main(){ while(1); }
    you can get an error.
    Because this empty loop contains a negative jump, you can get an error message on 64 bit systems:
    Link Error: PC Relative branch to ‘.L4′ is out of range. Suggest large-code model.
    The problem is the negative integer size on 64 bit. The solution is to modify 2 lines in the binutils ource, in acme/bfd/elf32-pic30.c and acme/bfd/coff-pic30.c:
    (source is:
    http://old.nabble.com/dsPIC-toolchain-%28GNU-Linux%29-td23349948.html
    )
    I’ve modified the source code manually, it seems to work for me. (The website doesn’t mention the modification of coff-pic30.c, but is is required also)

    — acme/bfd/elf32-pic30.c.orig 2009-05-04 15:39:05.000000000 +0100
    +++ acme/bfd/elf32-pic30.c 2009-05-04 15:35:43.000000000 +0100
    @@ -1973,7 +1973,7 @@
    case R_PIC30_PCREL_BRANCH:
    case R_PIC30_BRANCH_ABSOLUTE:
    /* valid range is [-32768..32767] and not [-2, -1, 0] */
    - if ((relocation > 0×7FFF) && ~(relocation | 0xC0007FFF))
    + if ((relocation > 0×7FFF) && ~(relocation | 0xFFFFFFFFC0007FFFull))
    {
    *error_msg = (char *) malloc(BUFSIZ);
    sprintf(*error_msg,
    @@ -1986,7 +1986,7 @@
    case R_PIC30_PCREL_DO:
    case R_PIC30_DO_ABSOLUTE:
    /* valid range is [-32768..32767] and not [-2, -1, 0] */
    - if ((relocation > 0×7FFF) && ~(relocation | 0xC0007FFF))
    + if ((relocation > 0×7FFF) && ~(relocation | 0xFFFFFFFFC0007FFFull))
    {
    *error_msg = (char *) malloc(BUFSIZ);
    sprintf(*error_msg,

  • Marc Michels

    Followed your guide and succesfully installed V3.22 on Gentoo. Great write-up!!

    Ran into the “Provide a recource file” error and now I’m puzzled. What do you mean by:

    To solve this add -mresource=$C30INSTALL/info/c30_device.info to your GCC command line.

    How do you add to a GCC command line, especially in a GUI?

    I’m quite sure I linked everything correctly so I doubt it’s that.

  • matt

    Hi Marc. I think you can solve this by creating a link to c30_device.info in your $C30INSTALL/bin directory. If this doesn’t work for you, then you can try adding the -mresource argument on the commandline. If you were running gcc directly from the commandline (e.g. running $C30INSTALL/bin/pic30-coff-gcc -mcpu=30F4013 -o test test.c) you would just add in the -mresource argument, giving you ‘$C30INSTALL/bin/pic30-coff-gcc -mcpu=30F4013 -mresource=$C30INSTALL/info/c30_device.info -o test test.c’.

    If you are using a GUI, how you add this is dependent on the application you’re using. If you’re using Piklab look here: http://www.electricrock.co.nz/blog/2009/08/using-c30-with-piklab

2 Trackbacks / Pingbacks for this entry

Leave a Reply