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.
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.
- 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.
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.|
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.
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).|
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.|
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|
|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:
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:
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.
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.
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 (older 2.4 kernel):||make linux-old|
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:
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:
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:
# C compiler, assembler, device programmer:
foo.hex: foo.c foo.h
$(PK2) -M -Ffoo.hex
rm -f *.o *.cod *.hex *.lst *.err