Embedded Freaks..

October 10, 2008

Compiling MPLAB C30 v3.11(b) Source Code under Linux

Filed under: pic — Tags: — kunilkuda @ 7:47 pm

Here’s my experiment with the MPLAB’s C30 source code. The original site for the patches (for C30 v3.00) can be viewed from Peter-Jan Randewijk page and piklab wiki.

System Prequisities

You will need GCC, Bison, Flex, and Tofrodos/Dos2unix utility. For Ubuntu users, just do:

# sudo apt-get install build-essential bison flex tofrodos

The autoconf of the MPLAB package seems like doesn’t check the bison and flex availability, causing the compilation failure at certain point. If the compilation failure due to *.y and/or *.l file, make sure your bison (yacc) and flex in on the system path.

Getting The Source Code

Download:

You need to register yourself before you can download it from Microchip.

Compiling PIC30-Binutils

Untar mplabalc30_v3_11B.tar.gz

# tar xvzf mplabalc30_v3_11B.tar.gz

You’ll find two directories extracted: acme/ and c30_resource/. Since Microchip is using Windows, you need to convert the files into Unix files (ie. replace Windows newline with Unix’s).

# find . -type f -exec dos2unix '{}' ';'

Then apply modified John Steele Scott’s patch (here)

# unzip pic30-binutils-3.11b.zip
# patch -p0 < pic30-binutils-3.11b/patches/allow-empty-device-info-file.diff
# patch -p0 < pic30-binutils-3.11b/patches/binutils-makefile_in.diff
# patch -p0 < pic30-binutils-3.11b/patches/search-path-fix.diff

Configure the package, make, and make install

# cd acme/
# CFLAGS=-DMCHP_VERSION="v3.11b-Debian" ./configure --prefix=/usr/local/pic --target=pic30-coff
# make
# sudo make install

The binary that you’ve just compiled can only produces/modify COFF executables. You might need the ELF one too.

# make distclean
# CFLAGS=-DMCHP_VERSION="v3.11b-Debian" ./configure --prefix=/usr/local/pic --target=pic30-elf
# make
# sudo make install

And that’s the end for the PIC30-Binutils. Note: Under Ubuntu 8.10, if the compilation complaints about “ylwrap : line xx -d not found“, install bison first (eg. “sudo apt-get install bison”), before you configure the package.

Compiling PIC30-GCC

Untar mplabc30_v3_11B.tar.gz

# tar xvzf mplabc30_v3_11B.tar.gz

It will extract two subdirs: gcc-4.0.2/ and c30_resource/. The next step is similar with PIC30-Binutils, replace the Windows newline with Unix’s.

# find . -type f -exec dos2unix '{}' ';'

Then apply John Steele Scott’s patch and mine into the GCC source (here)

# unzip pic30-gcc-3.11b.zip
# patch -p0 < pic30-gcc-3.11b/patches/libiberty-testsuite.diff
# patch -p0 < pic30-gcc-3.11b/patches/pic30-standard-prefix.diff
# patch -p0 < pic30-gcc-3.11b/patches/search-path-fix.diff
# patch -p0 < pic30-gcc-3.11b/patches/make-relative-prefix.diff
# patch -p0 < pic30-gcc-3.11b/patches/resource-path.diff
# patch -p0 < pic30-gcc-3.11b/patches/t-pic30.diff

Now, the PIC30-GCC doesn’t like you to build the source code inside the directory itself. Create different directory to configure and compile the PIC30-GCC, then install it.

# mkdir gcc-build
# cd gcc-build
# CFLAGS=-DMCHP_VERSION="v3.11b-Debian" ../gcc-4.0.2/gcc-4.0.2/configure --prefix=/usr/local/pic --target=pic30-coff --enable-languages=c

Additional note from Michal Demin:

The semantic analyzer in gcc package (c-parse.c file) is not generated correctly. Therefore the pic30-gcc will not recognize standard C keywords (void, return..etc). You have to force the Makefile to regenerate it by

touch ../gcc-4.0.2/gcc-4.0.2/gcc/c-parse.y

before executing the make.

# make
# sudo make install

Note: Under Ubuntu 8.10, install flex if the compilation is error due to “gcc: gengtype-lex.c: No such file or directory”.

Now, due to my time limitation, I cannot fix the default path for the binutils directory (I hope someday someone will..It’s pretty easy). Therefore, you will need to copy binutils’ assembler (pic30-coff-as) and linker (pic30-coff-ld) into PIC30-GCC’s default program path ($PREFIX/libexec/gcc/pic30-coff/$GCC-VERSION). Like this:

# sudo cp /usr/local/pic/bin/pic30-coff-as /usr/local/pic/libexec/gcc/pic30-coff/4.0.3/
# sudo cp /usr/local/pic/bin/pic30-coff-ld /usr/local/pic/libexec/gcc/pic30-coff/4.0.3/

That’s it. Your PIC30-GCC is ready. But suppose you want to build ELF executable (instead of COFF), do it again with –target=pic30-elf

# cd .. && rm -fdr gcc-build && mkdir gcc-build && cd gcc-build
# CFLAGS=-DMCHP_VERSION="v3.11b-Debian" ../gcc-4.0.2/gcc-4.0.2/configure --prefix=/usr/local/pic --target=pic30-elf --enable-languages=c
# touch ../gcc-4.0.2/gcc-4.0.2/gcc/c-parse.y
# make
# sudo make install

Don’t forget to copy the pic30-elf-as and pic30-elf-ld.

# sudo cp /usr/local/pic/bin/pic30-elf-as /usr/local/pic/libexec/gcc/pic30-elf/4.0.3/
# sudo cp /usr/local/pic/bin/pic30-elf-ld /usr/local/pic/libexec/gcc/pic30-elf/4.0.3/

Simple Test for The Compiler

Here’s some simple test for the newly built compiler:

# echo "int main(void) { return 0; }" > test.c
# /usr/local/pic/bin/pic30-coff-gcc-4.0.3 -c test.c

You cannot link the program without the linker files (*.gld files from Microchip’s libc). But you can check the compilation result by:

# /usr/local/pic/bin/pic30-coff-objdump -S test.o
test.o:     file format coff-pic30

Disassembly of section .text:

00000000 :
   0:	00 00 fa    	lnk       #0x0
   2:	00 00 eb    	clr.w     w0
   4:	00 80 fa    	ulnk
   6:	00 00 06    	return

Adding Microchip’s Library
Here’s the hint. Issue this command to get the PIC30-GCC’s default library path:

# /usr/local/bin/pic30-coff-gcc-4.0.3 -print-search-dirs

By looking the default library path, you should know which directory in Microchip’s “Program Files/Microchip” that needs to be copied. Also, you will know where to put it (Hint: $prefix/pic30-nonfree or /usr/local/pic/pic30-nonfree if you follow this guide).

Does It Realy Work ?

You tell me. I’m newbies in PIC MCUs, and I haven’t got any Microchip’s libc so far. Microchip said that the libc (and C start-up anf linker files) are belong to them. So, they are not the part of the GPL-ed code.

Anyway, if you find this build guide works, please post me the sample code that you are using. I can use it to learn PIC24 :)

About these ads

28 Comments »

  1. I’m confused: either
    a) Microchip are failing to provide the source for C30, a product derived from GCC, which is in violation of the GNU license. FSF lawyers please take note.
    b) Microchip provide the source for C30 so you can build it yourself and the Microchip libc is licenced for use with C30. Another way to look at it would be that Microchip helpfully provide the re-compiled binaries for C30 but you’re welcome to roll your own.

    The bit that I don’t think is in doubt is that if you derive something from the C30 source code then that derivative is not licensed for use with Microchip libc. However, you’re not talking about modifying C30 – just compiling it (albeit for a different platform)

    Just my $0.032 worth

    Comment by Chris St John — November 7, 2008 @ 10:57 pm

  2. Hi Cris,

    My case is (B), they provide the source code for pic30-gcc. I just recompiled it to Linux. However, the libc for pic30-gcc belongs to Microchip.

    So, I can compile {for (delay=0; delay<0xFFFF; delay++);}, but I lack of strlen(), memcpy(), printf(), puts(), malloc(), free(), the linker files and C-startup files.

    So, the compiler is usable, but need more works before it’s usable.

    My personal opinion: I don’t think that Microchip has violate GPL (since the software/pic30-gcc works, and we have the pic30-gcc source code).

    But I believe that Microchip is stingy. Those libc implementation can be ported from newlib, glib, or avr-libc, while making linker files and C-startup files usually just take 1-2 hours (for someone that’s familiar with the architecture).

    Thanks for $0.032 anyway =)

    Comment by kunilkuda — November 8, 2008 @ 10:08 am

  3. I applied the patchs and built both binutils and gcc. The gcc patch to c3_resource failed.

    My previous builds ran gcc ok and segfaulted on as. Now they segfault on as & gcc.

    Environment is Fedora 8.

    George

    Comment by George Gallant — December 21, 2008 @ 12:10 pm

  4. Thanks for the info George.

    My suggestion is to build both binutils and gcc in different directory, since both have the same c30_resource/ directory which may override each other. So, you may end up patching the already patched file, or patching the windows line-ended file, since the original source code from Microchip has Windows newline (ie. CRLF instead of LF).

    Please note that the patch only works for Unix line-ended file (ends with LF, instead of CRLF Windows-style).

    Comment by kunilkuda — December 22, 2008 @ 10:40 am

  5. [...] 先日のMPLABのコンパイルですが、検索すれば似たようなページ(英語)がありますね…。 日本語の情報源はうちだけのようなので、費やした時間が無駄にならないものと信じます。 [...]

    Pingback by “MPLAB gcc”でググってみたら… | PIC+R workbench — December 23, 2008 @ 4:19 am

  6. Tried the separate source trees for binutils and gcc. Builds without error.

    No longer getting segfaults, now complaining about not finding the cpu type

    pic30-coff-cc1: error: Invalid -mcpu option. CPU 24HJ32GP202 not recognized.

    Comment by George Gallant — December 26, 2008 @ 10:20 pm

  7. I have hacked a version of as & gcc to work with Fedora. Most of the problems deal with the default directory paths and c30_devices.info. In particular:

    1. make a link in libexec/gcc/pic30-coff/4.0.3 to the pic30-coff-as.
    2. have multiple copies of c3_devices.info

    Now am stuck on pic30-coff-ld. Does anyone have a demo project to share?

    George

    Comment by George Gallant — December 28, 2008 @ 2:49 am

  8. This looks great and I’m excited to try it out!

    Maybe I missed it but I didn’t see a link to “pic30-gcc-3.11b.tar.bz2″?

    searching for it in the 4shared website led to:
    http://www.4shared.com/file/76025702/9eb06aad/pic30-gcc-311btar.html

    Thanks for putting this together!

    Comment by Ed Damos — January 8, 2009 @ 3:25 am

  9. Thanks for the information. I have compiled the compiler and binutils following your instructions. The problem is, that I can’t compile the test-program. It gives:

    test.c:1: error: syntax error before ‘)’ token
    test.c: In function ‘main’:
    test.c:3: error: syntax error before numeric constant

    #cat test.c
    int main(void)
    {
    return 0;
    }

    Regular gcc doesn’t give the error. What have I missed?

    Comment by Jesper Andersen — January 18, 2009 @ 2:36 am

  10. Hi Andersen,

    Thanks for the info.

    Honestly, I don’t know what’s your problem. I’ve rechecked my pic30-elf-pic30-elf-gcc and pic30-coff-pic30-coff-gcc. The test.c that you’ve mentioned can be compiled perfectly.

    kunil@cerpelai:~/Temp$ cat test.c
    int main(void)
    {
    return 0;
    }

    kunil@cerpelai:~/Temp$ /usr/local/pic/bin/pic30-coff-pic30-coff-gcc -c test.c -o test.o
    kunil@cerpelai:~/Temp$ /usr/local/pic/bin/pic30-elf-pic30-elf-gcc -c test.c -o test.o

    Maybe there’s some problem with the native compiler. I’m using GCC 4.3.2 to compile the PIC30-GCC

    kunil@cerpelai:~/Temp$ gcc –version
    gcc (Ubuntu 4.3.2-1ubuntu11) 4.3.2
    Copyright (C) 2008 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Comment by kunilkuda — January 19, 2009 @ 9:27 am

  11. Well, heres my gcc version :)

    data@gandalf:~$ gcc –version
    gcc (Ubuntu 4.3.2-1ubuntu11) 4.3.2
    Copyright (C) 2008 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    So no, it’s not that…

    But thanks anyway :)

    Comment by Jesper Andersen — January 19, 2009 @ 2:46 pm

  12. Tried separating the gcc and binutils to two separate directories and recompiling with no luck. I’ll try again tomorrow. I’m tired of being stuck on a windows platform for my PIC development…

    Comment by Jesper Andersen — February 1, 2009 @ 7:47 am

  13. Im having the same issue with the toolchain. Could you please post version of utilities like bison, flex, gcc
    and md5sums of the mplab archives as well ?

    Mine are
    mplabalc30_v3_11B.tar.gz
    f5a20b25659221f8f65f4e11d577292d

    mplabc30_v3_11B.tar.gz
    e04959d8b7002b4960590557e08876b4

    flex 2.5.33
    bison (GNU Bison) 2.3

    Thanks.

    Comment by Michal Demin — February 3, 2009 @ 5:44 am

  14. so the important step is to
    touch gcc-4.0.2/gcc/c-parse.y

    Prolly the parser from Mchip is incorrectly generated – needs to be redone ;)

    Comment by Michal Demin — February 3, 2009 @ 8:00 am

  15. @Jesper: I think your source code is not complete or something wrong about it. I guess you are running on Ubuntu Intrepid as I do, so your utilities should be similar as mine

    @Michal: Here’s the utilities version that I used:

    kunil@cerpelai:~$ bison –version
    bison (GNU Bison) 2.3
    Written by Robert Corbett and Richard Stallman.

    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    kunil@cerpelai:~$ flex –version
    flex 2.5.35

    kunil@cerpelai:~$ gcc –version
    gcc (Ubuntu 4.3.2-1ubuntu12) 4.3.2
    Copyright (C) 2008 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Comment by kunilkuda — February 3, 2009 @ 10:10 am

  16. @kunikuda:
    His sources are not wrong, i had the same problem. The problem is in semantic analyser, the one that is already in package (c-parse.c file) is not generated correctly. You have to force the Makefile to regenerate it by
    touch ../gcc-4.0.2/gcc-4.0.2/gcc/c-parse.y
    before executing the make.
    I have sucessfuly compiled the gcc after this step.

    Another thing to notice is that you should not compile it on x86_64, the gcc can generate simple code (eg test.c as on this site), but more complex programs fail to compile (wrong .o files and link problem)
    You can compile it on x86 (32bit) and then use it on x86_64 (many linux distros support this)

    i also had a small problem with the resource file and gas. I had to modify the binutils and hardwire it inside, but YMMV.

    Comment by Michal Demin — February 3, 2009 @ 6:38 pm

  17. YES! Thankyou! It works!

    Well, at least the compile/disassemble of test.c works. Now I have to install the Microchip libraries and test on some real code.

    /Jesper

    Comment by Jesper Andersen — February 4, 2009 @ 4:12 am

  18. @Michal: Thanks a lot Michal. I’ll update my post.
    @Jesper: Thanks for pointing out this problem.

    Comment by kunilkuda — February 4, 2009 @ 9:27 am

  19. Hi guys!

    I working on my university with the e-puck robot which uses a dsPIC processor.
    This web-page is really helpful for compiling its own free C30 compiler.
    But now Microchip do not offer any more the sources for Binutils and GCC of the version 3.11b (mplabalc30_v3_11B.tar.gz & mplabc30_v3_11B.tar.gz).
    They offer now only the actual sources of the version 3.12.
    At first I tried to compile the older versions (v3.00 & v3.01) like above. But I was not able to compile both of them under Ubuntu 8.10 and 9.04.
    So I tried to use the sources of the version 3.12 and patch them with the patches of the version 3.11b (I didn’t found a more actual patch).
    To patch and compile the PIC30-Binutils with the patch “pic30-binutils-3.11b.tar.bz2″ works very well (only some line positions in the patches are not matching to the actual source code – but this is not the problem ;) ).
    After installing the Binutils I’ve tried to patch and compile the GCC compiler with the other patch (pic30-gcc-3.11b.tar.bz2). This works well, but during the compilation process of the GCC compiler I get following error:

    java/parse.o java/class.o java/decl.o java/expr.o java/constants.o java/lang.o java/typeck.o java/except.o java/verify.o java/verify-glue.o java/verify-impl.o java/zextract.o java/jcf-io.o java/win32-host.o java/jcf-parse.o java/mangle.o java/mangle_name.o java/builtins.o java/resource.o java/jcf-write.o java/buffer.o java/check-init.o java/jcf-depend.o java/jcf-path.o java/xref.o java/boehm.o java/java-gimplify.o main.o libbackend.a ../libcpp/libcpp.a -L../zlib -lz ../libcpp/libcpp.a ../libiberty/libiberty.a
    libbackend.a(pic30.o): In function `validate_target_id’:
    pic30.c:(.text+0xaf5): undefined reference to `flag_preprocess_only’
    libbackend.a(pic30.o): In function `pic30_handle_section_pragma’:
    pic30.c:(.text+0×15dfd): undefined reference to `c_lex’
    pic30.c:(.text+0×15e43): undefined reference to `c_lex’
    pic30.c:(.text+0×15eea): undefined reference to `c_lex’
    libbackend.a(pic30.o): In function `pic30_handle_interrupt_pragma’:
    pic30.c:(.text+0×160a1): undefined reference to `c_lex’
    pic30.c:(.text+0×160d8): undefined reference to `c_lex’
    libbackend.a(pic30.o):pic30.c:(.text+0×16169): more undefined references to `c_lex’ follow
    libbackend.a(pic30.o): In function `pic30_handle_dbl_conversion’:
    pic30.c:(.text+0×1cf69): undefined reference to `flag_short_double’
    libbackend.a(pic30.o): In function `pic30_system_include_paths’:
    pic30.c:(.text+0×1dff6): undefined reference to `add_path’
    collect2: ld returned 1 exit status
    make[1]: *** [jc1] Error 1
    make[1]: Leaving directory `/home/martin/Desktop/PIC/v3_12/mplabc30v3_12/gcc-build/gcc’
    make: *** [all-gcc] Error 2

    Does somebody know what I must do, to get rid of these errors? I think there is not missing much, but the patch “pic30-gcc-3.11b.tar.bz2″ must be adapted for the newest version 3.12. Does somebody have an actual patch file for the C30 GCC compiler of version 3.12 or knows a solution to this error?
    If not, I would be glad if someone can add the sources “mplabalc30_v3_11B.tar.gz” and “mplabc30_v3_11B.tar.gz” somewhere in the web for download. They do not exist in the archive of Microchip.

    Thank’s.

    Martin.

    Comment by Martin — June 9, 2009 @ 9:17 am

  20. [...] 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 [...]

    Pingback by Installing Microchip’s C Compiler for PIC24 MCUs and dsPIC DSCs (C30) on Ubuntu 9.04 - ElectricRock Blog — August 18, 2009 @ 5:11 am

  21. [...] 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 [...]

    Pingback by Building Microchip’s C30 Compiler v3.20 on Ubuntu 9.10 - ElectricRock Blog — January 3, 2010 @ 2:06 pm

  22. Hi. Just wondering if you could help with this problem. Building the toolchain and compiling my PIC code with it is working, but it seems that the linker fails.

    /home/kemp/pic30/bin/pic30-coff-ld:/home/kemp/pic30/pic30-nonfree/support/PIC24F/gld/p24FJ64GA002.gld:9: syntax error

    This occurs with the linker script quoted above and also with the default linker script. People have also reported this with other PIC-specific scripts. I’ve confirmed that the correct line endings are present, but beyond that I’m stumped, and Google doesn’t have any suggestions that I can see.

    Comment by Kemp — February 18, 2010 @ 2:56 am

  23. here is a script to automate this (WIP). tested on gentoo, fixes from comments have been added.

    contains MD5sums of all files, easily resumable on errors, mirror for patches.

    http://david.lutolf.net/dt/pic_microchip/build_c30_linux

    comments welcome

    Comment by David — November 16, 2010 @ 8:35 pm

  24. Thanks David =)

    Comment by kunilkuda — November 17, 2010 @ 10:08 pm

  25. Tracking the subject at http://pic30.googlecode.com/

    Contributors welcome

    Comment by John Pritchard — January 22, 2011 @ 9:15 pm

  26. while trying to get a compiler on a new install I fell back on my post – which I had totally forgotten. after a few server upgrades it appears the files got lost.

    this has been fixedm everything is back online.

    Comment by David — October 23, 2011 @ 2:17 am

  27. I suppose all data on this page is more or less obsolete, as microchip released a linux version of its compiler, available here:

    http://ww1.microchip.com/downloads/mplab/X_Beta/installer.html

    I got it to work with piklab, here’s my config (in piklab, settings > configure toolchains > pic30 toolchain)

    executable directory: /opt/microchip/mplabc30/v3.30c/bin/bin/
    header directory: /opt/microchip/mplabc30/v3.30c/support/
    linker script directory: /opt/microchip/mplabc30/v3.30c/src/
    library directory: /opt/microchip/mplabc30/v3.30c/errata-lib/

    be careful, header files have mixed-case filenames, piklab sample code includes them with lower case. it might need to be fixed.

    compiling a project fails with

    built in linker script:688: undefined symbol `__reset’ referenced in expression

    I’m checking out on this right now…

    Comment by David — October 23, 2011 @ 2:58 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: