Microchip PIC Development Using Mac OS X or Linux

July 19th, 2008

Developing code for Microchip 8-bit PIC microcontrollers using any alternative to Microsoft Windows used to be a frustrating and often futile proposition. Fortunately this situation has since changed, and everyone can now jump on the PIC bandwagon regardless of their operating system of choice.

This article is not a comprehensive survey of every Microchip PIC development option for Mac and Linux, just a summary of the particular hardware and a few software choices I’ve found that most reliably meet my own criteria.



Important Update:

This article is now enormously obsolete…and I couldn’t be happier! Microchip has begun supporting cross-platform development in earnest. Their MPLAB X development environment runs equally well on Windows, Mac and Linux and supports the full range of PIC chips and programmers/debuggers. Though still in a beta state, this tool is working quite well and continues to improve with every release. I’ll be deleting this article in the future; just keeping it around in the interim should the information still be of use to anyone.


The Issues:

  • Many inexpensive or homebrew PIC programmers have been based on legacy I/O ports (serial or parallel), but all modern Macintosh systems (and increasingly PCs, especially netbooks and small form-factor systems) are “legacy free,” eschewing these older ports in favor of USB.
  • Adapters for USB to serial or parallel are available but introduce their own issues, as does emulation or virtualization software.
  • Low-cost USB PIC programmers are on the rise, but support for non-Windows systems has been spotty.
  • Many existing PIC-on-Mac (or Linux) sites contain obsolete information or dead links.

The Desired Outcome:

An affordable hardware and software toolchain closest to the native physical interfaces and operating system, minimizing or eliminating the use of adapters, emulators and other stopgap measures that add cost, bulk, complexity and compromise the stability of the development process.


Hardware

There are many devices and methods of getting code onto a PIC. Later articles might detail some of these options, but at present, for the hobbyist or casual developer, Microchip’s PICkit 2 — their entry-level programmer — seems the most generically useful choice:

Device: Microchip PICkit™ 2 Development Programmer/Debugger.
Pros:
  • USB native; no serial or parallel adapter is required.
  • Affordable ($35 USD and up).
  • Physically small; travels well.
Cons:
  • Support for newly-released chips often lags behind Microchip’s professional device programmers.
  • No native support for debugging or logic tool functionality on non-Windows systems; this is a device programmer only.

This is available in a variety of bundles:

  • PICkit 2 (Microchip part number PG164120, $35) – this is the standalone programmer with no additional parts; bring your own PIC microcontroller.
  • PICkit 2 Starter Kit (part number DV164120, $50) – this bundles the PICkit 2 programmer with Microchip’s Low Pin Count Demo Board containing a socketed 20-pin PIC16F690, four LEDs and one switch and potentiometer. Other 8-, 14- and 20-pin PICs can be used in this socket.
  • PICkit 2 Debug Express (DV164121, $50) – bundles a 44-pin Demo Board containing a nonremovable PIC16F887, eight LEDs and the same switch and potentiometer as above. Don’t be misled by the “Debug Express” name of this particular kit. All versions of the PICkit 2 are equally capable of debugging with most PIC microcontrollers (though currently only when used with Windows systems).
  • PIC18F4xK20 Starter Kit (DV164124, $100) – this bundle includes an elaborate demo board containing a more capable PIC18 microcontroller, a small OLED display, analog audio in and out jacks, a serial EEPROM, daughterboard connector and other doodads. Wow!

A demo board is not a requirement — any PICkit 2 can program PIC microcontrollers on a breadboard or in-circuit — but the starter kits are reasonably priced and the included projects are a good way for beginners to learn the ropes.

A number of “compatible” third-party programmers have since come to market, some undercutting Microchip’s product by a few dollars…but I’ve read reports of unreliable behavior from some of the cheap imported models. Given the modest cost of the official starter kits, it makes sense to simply buy directly from the source or their distributors (DigiKey, Mouser, Newark, etc.).

Microchip has since released a PICkit 3, but this is not backwardly-compatible with the PICkit 2 and will not work on non-Windows systems. The PICkit 2 is currently the only official programmer with solid open source support.


Software

If using a Windows-based system, this is a trivial matter: install the development software included with the PICkit 2 and get started. For alternative operating systems, things are not so cut-and-dried, and one must choose an approach most suited to their own development and usage patterns:

Option 1: Virtualization or Dual-Booting

MPLAB IDE — Microchip’s Windows-based development environment — works reasonably well under virtualization software such as VMware Workstation or Fusion, Parallels Desktop or the fantastic open-source VirtualBox…or alternately by “dual booting” a system for native Windows operation. Of course this is only an option for x86-based systems (not PowerPC), and is not without its share of issues.

Software: Microchip MPLAB IDE (running Microsoft Windows via virtualization or by dual-booting).
Pros:
  • Extensive graphical IDE is included free with PICkit 2 or can be downloaded from Microchip’s web site.
  • Most Microchip tutorials and sample code are written with this compiler and environment in mind.
  • Straightforward installation (assuming Windows is already loaded).
  • Frequent updates for new devices and features, including 16- and 32-bit microcontrollers.
Cons:
  • Added expense of acquiring Windows operating system and/or virtualization software.
  • Considerable system resources consumed by virtualization, or — if dual-booting — no concurrent access to preferred applications in primary operating system.
  • Productivity lost to booting and maintaining a second OS.
  • PowerPC users are out of luck.

If you choose the virtualization route, something to watch out for is that downloading code to chips using the PICkit 2 directly from within MPLAB IDE is horribly unreliable. The solution is to run both MPLAB IDE and Microchip’s small PICkit 2 utility concurrently, using the former program to compile the code and the latter to burn it to the chip (an “Auto Import” mode in the latter makes this easy).

So virtualization does let us develop PIC code on Mac or Linux systems, but it falls short of an optimal “shortest path” solution.

Option 2: HI-TECH C Compiler

Third-party commercial compilers produced by HI-TECH Software have been available for a number of years now. While they claimed Mac and Linux support, for the longest time this was rather dodgy — broken links, crashing software, a confusing product lineup among other things. HI-TECH has since been acquired by Microchip, and the outlook for this software has improved considerably.

The compilers are available for Windows, Linux and Mac OS X and offer different licensing options:

  • The “Lite” mode is free (even for commercial use) but optimization options are seriously curtailed. Still, it’s perfectly adequate for training and student use and is not impaired in any other way (code size, etc.).
  • A “Pro” evaluation install is fully functional — optimization and all — for a period of 45 days.
  • The full-on “Pro” license — which, at $1195, is clearly intended for actual professional developers — remains fully functional and includes a year of priority technical support.

The HI-TECH compilers include their own IDE which is based on Eclipse. Personally I’m not especially fond of IDEs, and Eclipse is among the most elephantine of the genre…but if you’re familiar with this environment from prior development work, well hey, you’re all set. If text-based development is more your style, the HI-TECH compilers can also be invoked from the command line.

Software: HI-TECH C compilers.
Pros:
  • “Lite” mode is free (as in beer).
  • Same tools are available on Windows, Linux and Mac OS X (PowerPC or Intel) — native and without virtualization — great for classrooms and other “mixed use” environments.
  • Includes Eclipse-based IDE, or can be used from command line if that’s more your style.
Cons:
  • The full “Pro” license is costly, and the PIC18 compiler is a separate product from the PIC12/14/16 — professional development for both will cost twice as much.
  • Does not interface directly with the PICkit 2 for downloading code to the microcontroller. Additional software and steps are required (see next section).
  • Certain features, such as processor configuration, are handled differently from Microchip’s original compilers; source code brought over will likely need some modification.

Option 3: Open Source Tools

Community-developed tools for PIC development likewise started out quite rough but have since crossed a threshold of viability. A complete development toolchain consists of three parts:

  • GPUTILS (GNU PIC Utilities) includes an assembler (gpsasm) that aims to be fully source compatible with Microchip’s own tools, and my own experiences have borne this out; code taken directly from MPLAB projects works with gpasm without modification. The project page does mention a handful of esoteric incompatibilities and bugs but I have never encountered these in regular use.
  • SDCC (Small Device C Compiler) supports a variety of microcontroller platforms; 8-bit PIC support is a recent addition and still a work in progress. I’ve tested simple student-level programs against it with no trouble at all, but at present there’s the risk it may be unsuited to more complex tasks.
  • PK2CMD is Microchip’s open-source tool for downloading code to a microcontroller using the PICkit 2.

These tools are all command line operated at present; there is no GUI or IDE. Savvy users might be able to integrate them with a development environment such as Eclipse; I’ve not figured this out because I’m an old fart and like developing on the command line. Uphill. In the snow.

Software: Open Source Command Line Tools
Pros:
  • Free (as in speech) — no crippled features or restrictions on use.
  • Lightweight and fast, even on vintage PowerPC Macs, small netbooks, etc.
  • Assembler is fully compatible with Microchip’s own; no additional porting effort.
  • Supports a few other microcontroller architectures as well.
Cons:
  • PIC support in SDCC is a work in progress with some known bugs. Perfectly adequate for training and hobbyist use, but complex projects could conceivably run into issues.
  • No technical support — if trouble arises, you’re on your own (though community support tends to be pretty good).
  • No GUI/IDE, if you’re inclined that way.

Installing GPUTILS

Windows: An .exe installer is available from the project’s download page.
Linux: You can install via Synaptic Package Manager (you may need to enable third-party repositories) — search for gputils. This can also be done from the command line using apt-get:

sudo apt-get install gputils

Alternately, if you have gcc installed, retrieve the .tar.gz file from the download page and compile using the directions below.

Mac OS X: There is no precompiled package available; you will need to build from source. This requires the gcc compiler, which is part of Xcode — an optional installer on your original Mac OS X DVD-ROM or can be downloaded from Apple’s developer site. Retrieve the GPUTILS source code .tar.gz file from the project’s download page. Once this archive is extracted, open Terminal.app, cd to the source directory and it’s a simple three step process to build:

./configure

make

sudo make install

This takes just a few minutes to complete, and the resulting executable will be installed in /usr/local/bin. Make sure this directory is added to your PATH environment variable.

Installing SDCC

Couldn’t be simpler — pre-built installers for all the major operating systems are available on the project’s download page. It’s also available in package format for Linux users; use Synaptic or apt-get to install the package sdcc. If for some reason you need to build this from the source code, the procedure to compile and install this uses the exact sequence of commands previously shown for GPUTILS.

Installing PK2CMD

This is also available as a precompiled installer for various operating systems, or can be built from source. The links for these are buried amidst the PICkit 2 product page (the “Download” box second from the bottom).

If compiling from source, the build process for PK2CMD is slightly different than the other tools. After extracting the source archive, the ./configure step isn’t necessary. Instead, just type the appropriate make command for your operating system:

Mac OS X 10.5: make mac105
Mac OS X 10.4: make mac104
Linux: make linux
Linux (older 2.4 kernel): make linux-old

Next type:

sudo make install

…and then make sure both /usr/local/bin and /usr/share/pk2 are in your PATH environment variable. e.g. in ~/.profile, add this line:

export PATH=$PATH:/usr/local/bin:/usr/share/pk2

Using the Open Source Tools

One aspect of SDCC initially had me confused: the documentation refers to the “pic14” and “pic16” ports of the compiler, but it turns out these do not correspond to Microchip’s part naming conventions. Rather, each refers to the instruction word size (in bits) on the midrange and high performance microcontrollers; in other words, you compile for the PIC16 using the pic14 option, and PIC18 using the pic16 option. Once that was cleared up, everything proceeded smoothly.

In addition to the documentation included with each, all three tools offer command-line help:

sdcc –help
gpasm –help
pk2cmd -?

In order to avoid a lot of repetitive typing (and having to remember all of the fiddly options for pk2cmd), I’ll usually group all of the files related to a single project into one folder and then create a Makefile that includes common operations such as “make write” or “make on”, like this:

# Example makefile for open source PIC toolchain
# Specific microcontroller type:
DEVICE=pic18f2620
# C compiler, assembler, device programmer:
CC=sdcc -mpic16
AS=gpasm -p$(DEVICE)
PK2=pk2cmd -P$(DEVICE)

All: foo.hex

foo.hex: foo.c foo.h
    $(CC) foo.c

write:
    $(PK2) -M -Ffoo.hex

on:
    $(PK2) -T

off:
    $(PK2) -W

erase:
    $(PK2) -E

clean:
    rm -f *.o *.cod *.hex *.lst *.err


15 Responses to “Microchip PIC Development Using Mac OS X or Linux”:

  1. Kristine says:

    What I understand you have expertise in Embedded design with PIC. Well… I am from a PCB Design background. I am trying to get into embedded designs.
    Could you please guide me which microcotroller I should choose for learning and also applying that learning to further designing embedded solutions.
    OR should I choose FPGA sounds funny at this stage – but then someone has directed me to a ALTIUM SITE – http://nb3000.altium.com/intro.html – [NANO BOARD]

    Please try to find out some of your valuable time to suggest me.

    Thank you in advance

  2. Andrew says:

    Thank you for this well written and explained atricle.

  3. Jamis says:

    Awesome article! Thank you for the clean and clear overview of the available options. This is, by far, the best article I’ve found on the topic. Thanks!

  4. Adeel says:

    I’m using first time PIC 16f676 and in burned it, process.
    Problem is that i heard that if the PIC burned is erased after
    programming, For second time it internal software contants are lost
    and PIC is useless, rather if we not install the on-chip software
    properly. Help me in such issue.

  5. funlw65 says:

    Have you seen Jalv2 language? Is the best open-source option for PIC from 10F/12F/16F/18F series under Linux/Mac OS X/Windows (better than SDCC). With a great and constantly growing library (jallib). The MAC OS X binary executable is not available but can be compiled from sources.

    http://casadeyork.com/jalv2 – jalv2 compiler and executables
    http://code.google.com/p/jallib – jallib project – libraries for Jalv2 which include also libraries for USB HID and CDC – see the featured downloads
    http://groups.google.com/group/jallib – discussion group related to jal libraries

  6. Sergio says:

    Hey your explanation is great, but when I use pk2cmd to turn on my PicKit2 I get different outcomes to when I turn it on in Windows, same program.

  7. Dacav says:

    Thanks for this article, very useful!

  8. Patrick says:

    Thanks for the info — it’s clear you’ve put a lot of work into this, and it seems well-organized and thorough. Is it still up to date, though? Specifically, if I’m looking to jump into PIC programming, are the kits and USB programmer you’ve listed still your recommendation?

    Thanks!

  9. Justin says:

    Is any one aware of any debugging tools for the Linux platform, that can be used with the Pickit 2 for real time debugging of devices.

    Thanks

  10. nardev says:

    What about PICLab?

    have you tried to do something with it?

  11. Paul says:

    Is there any support for a PICkit 3 programmer? Will PK2cmd work with a PICkit 3 programmer? I’m looking for documentation or someone who has had some success with this on a MAC.

    Thanks

  12. Daniel says:

    Where did you get the bread board for that Low Pin Count demo board?

  13. Brad says:

    Fortunately for all Linux/Mac (and Windows, for that matter) users, with the release of MPLABX from Microchip, combined with the latest versions of their compilers, all these difficult and complicated work-arounds are suddenly not needed.

    Simply download the NetBeans-based MPLABX and the free versions of the C Compilers for 8, 16 & 32 bit PICs, and go to town!

    I still like the UBW 32, though.

    HTH,

  14. SanUSB says:

    You can also make via USB microcontroller programming simply and directly in Mac OSX. You must have installed the XCode. More information ‪ http://www.youtube.com/watch?v=h6Lw2qeWhlM

  15. nardev says:

    Old article. Was helpful…

    There is MPLAB-X and compilers for linux available from microchip now.

Leave a Reply:

Copyright © 2014 Paint Your Dragon. All Rights Reserved.