Building Microchip’s C30 Compiler v3.20 on Ubuntu 9.10
by matt on Jan.03, 2010, under How To
NOTE: This page is now outdated, please go to http://www.electricrock.co.nz/blog/microchip-c30/
This an update to my previous tutorial for building Microchip’s C Compiler for PIC24 MCUs and dsPIC DSCs (herein referred to as C30). This update covers building v3.20 of the compiler (as opposed to v3.12) under Ubuntu 9.10. Again, this is mostly gleaned from http://embeddedfreak.wordpress.com/2008/10/10/compiling-mplab-c30-v311b-under-linux, but adapted for the latest version of C30.
These instructions also work for v3.22 and are reported to work with v3.23.
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). I used the following steps to install the latest version (you can just install the one in the Ubuntu repositories if you aren’t worried about being bleeding edge):
sudo add-apt-repository ppa:ubuntu-wine/ppa sudo aptitude install wine
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.
- From Microchip’s website download the sources for the compiler and binutils. They can be found on this page: http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023073. You will need to login to download. You want to get:
- GCC Code for MPLAB ASM30 for dsPIC/PIC24 v3.20
- MPLAB C30 v3.20 GCC Source
- Download the academic or evaluation version of the proprietary compiler from the C30 website (http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010065)
- Download the patches. I have adapted these from those provided by John Steele Scott (see my previous post) to work with v3.20.
Building Binutils
- Unzip the sources.
mkdir -p $C30BUILD/binutils cd $C30BUILD/binutils tar -zxvf $DOWNLOADDIR/mplabalc30v3_20.tar.gz tar -jxvf $DOWNLOADDIR/pic30-binutils-3.20.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 the 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.20/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.20-Debian" ./configure --prefix=$C30INSTALL --target=pic30-coff
- Before we can make it we need to touch some files otherwise it doesn’t get built properly.
find . -name "*.y" -o -name "*.l" -exec touch '{}' ';' - Make it.
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_20.tar.gz tar -jxvf $DOWNLOADDIR/pic30-gcc-3.20.tar.bz2 find . -type f -exec dos2unix '{}' ';' for i in pic30-gcc-3.20/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.20-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
Next Steps
The rest of the steps are just the same as for building v3.12. I won’t repeat myself here, just check out my previous tutorial and follow the steps from Setup the non-free part onward.
7 Comments for this entry
1 Trackback or Pingback for this entry
-
Installing Microchip’s C Compiler for PIC24 MCUs and dsPIC DSCs (C30) on Ubuntu 9.04 - ElectricRock Blog
January 8th, 2010 on 8:53 am[...] EDIT: An updated version of the compilation instructions, for C30 v3.20 can be found here: http://www.electricrock.co.nz/blog/2010/01/building-c30-v32/. [...]
January 9th, 2010 on 8:12 am
Hey thanks for the tutorial, very useful… wanted to give it a try it on the latest version (3.22) but your link for the gcc patches is broken… Could you fix it please ?
Thanks
Billy
January 9th, 2010 on 10:09 am
Whoops, I didn’t have the right url in there. It’s fixed now.
Let us know how it goes with v3.22.
Cheers,
Matt
January 9th, 2010 on 1:38 pm
Haven’t done any extensive testing but it compiles with the 3.20 patches (nor did I setup the non-free part)…
Tried to compile a small testcase but gcc couldn’t find cc1 nor as :
“pic30-coff-gcc: installation problem, cannot exec ‘pic30-cc1′: No such file or directory” (same with pic30-as)
ln -s $C30INSTALL/libexec/gcc/pic30-coff/4.0.3/pic30-coff-cc1 $C30INSTALL/libexec/gcc/pic30-coff/4.0.3/pic30-cc1 (same for as) did the trick…
January 9th, 2010 on 4:57 pm
Thanks for the feedback Billy.
There was a bit of a delay between when I actually built the compiler and when I wrote the tutorial, and in that time I forgot about one of the files I had patched. It was config.gcc which is responsible for the program prefix (e.g. pic30-coff- or pic30- ). For some reason in the v3.20 sources they had hardcoded the prefix to pic30-. I have fixed this so that it will be pic30-coff- (or whatever binary type you choose). I have updated pic30-gcc-3.20.tar.bz2 with this patch now.
Cheers
February 18th, 2010 on 5:32 am
Build process confirmed to work for 3.23. The names for files on the Microchip website are slightly different now, but it’s fairly easy to spot what you need.
March 15th, 2010 on 6:28 am
I wanted to thank you for this tutorial. Even though it’s taken me over 12 hours to get the 3.23 toolchain working, I don’t think I could have managed it without you!
I also had the “pic30-coff-ld:built in linker script:6: syntax error” error for a while, and can verify that the
“find . -name “*.y” -o -name “*.l” -exec touch ‘{}’ ‘;’” fix is necessary, and should probably be part of your setup steps for binutils. (especially if the comments box smart-formats the quotes!)
I’m trying to get it all working purely from the command line for the moment, so some other things took a while to figure out; for example “pic30-coff-ld: cannot find -lpic30-coff” errors are caused when you don’t have the “pic30-nonfree/lib” directory (copied from the academic/evaluation release) added to the libraries path, either globally through environment variables, or with a “-L{$C30INSTALL}/pic30-nonfree/lib”-ish parameter to GCC. (Obvious in retrospect, but not at the time.)
I also get “pic30-coff-gcc-4.0.3: installation problem, cannot exec ‘pic30-coff-as’: No such file or directory” errors (even though I’ve linked it from “libexec/gcc/pic30-coff/4.0.3/” as you suggest) unless I add the binaries directory to my $PATH.
I’m now onto more normal errors preventing simple test compiles, such as “Link Error: PC Relative branch to ‘.L2′ is out of range. Suggest large-code model.” (even though I’m setting “-mlarge-code”) which I hope will be solved by a more thorough read of the documentation, or providing explicit linker files. I hope.
I just wish Microchip allowed redistribution of binaries, to prevent others from having to go through this whole grinding, unforgiving process. This is the third time I’ve tried to build the toolchain, and my first actual success.
So it seems this process does work for v3.23, on a 64-bit Xeon SMP ‘Dreamhost’ web-hosting server account. I’m at least compiling .c to .o, even if I’m not linking properly yet.
Thanks!
ps: [three hours later] I still can’t get past the “Suggest large-code model.” error, even when compiling the MPLAB examples. I can get “output.s” to assemble to “output.o”, but the linker refuses to work. My current best try is “pic30-coff-ld -o output.cof output.o –script ../../support/dsPIC30F/gld/p30f3014.gld –heap=0 -L../../lib/dsPIC30F” but it always fails with the same problem:
output.o(.text+0×8):output.s: Link Error: PC Relative branch to ‘wait’ is out of range. Suggest large-code model.
output.o(.text+0xe):output.s: Link Error: PC Relative branch to ‘print_string’ is out of range. Suggest large-code model.
output.o(.text+0×20):output.s: Link Error: PC Relative branch to ‘print_string’ is out of range. Suggest large-code model.
output.o(.text+0×24):output.s: Link Error: PC Relative branch to ‘exit’ is out of range. Suggest large-code model.
Any ideas?
March 21st, 2010 on 12:24 pm
Hi Jeremy,
I’ve added the find . -name “*.y” -o -name “*.l” -exec touch ‘{}’ line to the tutorial now. I spent the last few days puzzling over the Link Error problem and just as I was writing this comment remembered that someone else had previously posted a patch for it (it is due to integer sizes on 64bit machines.) I will update my patches, look for a new blog post today.
Cheers