Low-Cost Microchip PIC Development on Mac OS X*
July 19, 2008 — Phillip Burgess — http://www.PaintYourDragon.com/uc/
This is not a comprehensive survey of all PIC development options on the Mac, just a summary of the particular hardware and software I’ve found that reliably meet my own criteria. A more extensive article might follow at some point, but in the meantime here’s the “In a Nutshell” version:
Many inexpensive or homebrew PIC programmers are based on standard legacy ports (serial or parallel), but all modern Macintosh systems (and increasingly PCs) are “legacy free,” lacking such interfaces, while port adapters introduce their own issues. Low-cost USB programmers are on the rise but software support for Mac OS X has been spotty. Many existing PIC-on-Mac sites contain obsolete information or dead links.
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.
| Device: | Microchip PICkit™ 2 Development Programmer/Debugger |
| Pros: |
|
| Cons: |
|
Microchip offers several PICkit 2 bundles. In addition to the standalone unit (about $35), there’s the PICkit 2 Starter Kit ($50) which includes a Low Pin Count Demo Board containing a socketed 20-pin PIC16F690, four LEDs, one switch and one potentiometer; the PICkit 2 Debug Express (also $50) includes a 44-Pin Demo Board with a nonremovable 44-pin PIC16F887 and similar assortment of devices, and the newly-released PIC18F4xK20 Starter Kit (about $100) featuring an elaborate demo board with a small OLED display, analog audio in and out, a serial EEPROM and other such doodads.
Don’t be misled by the “Debug Express” name of one kit. All versions of the PICkit 2 are equally capable of debugging with most PIC microcontrollers (but currently in Windows only).
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 (the least expensive is $32 including postage), available from various eBay sellers. I’ve yet to work with any of these alternatives and cannot vouch for their reliability. Given the low cost of the starter kits, my present recommendation is simply to buy directly from the source or their distributors (DigiKey, Mouser, Newark, etc.).
Microchip includes the free MPLAB IDE (Integrated Development Environment) and related software with the PICkit 2. These tools can work reasonably well under virtualization software such as VMware Fusion or Parallels Desktop, or of course by booting into Windows natively using Boot Camp.
| Software: | Microchip MPLAB IDE (Microsoft Windows via virtualization or Boot Camp) |
| Pros: |
|
| Cons: |
|
Attempting to program devices using the PICkit 2 directly from MPLAB IDE has been so unreliable under virtualization as to be futile. MPLAB often reads the PICkit 2 firmware version incorrectly and attempts to force an update, usually sending the device into an unresponsive state. The solution is to run both MPLAB IDE and the small PICkit 2 program concurrently, with “None” as the selected programmer in the former, and “Auto Import” mode enabled in the latter to detect object file changes and reprogram the PIC. It’s a non-issue using Boot Camp, but one then loses similtaneous access to their usual Mac apps. (If you’re wondering about Wine, CrossOver or VirtualBox compatibility: no, not yet.)
The bundled third-party “lite” C compiler could be a pro or a con. It’s included free, but device support is very limited and some artificial constraints are placed on code size. It's sufficient for learning, while the full-featured version of the compiler is a considerable investment that might appeal only to professionals. The same company also produces a native Mac OS X version of the compiler (both “lite” and full-cost commercial) including the Eclipse IDE, but this lacks a direct means of programming via the PICkit 2.
So virtualization does let us program PIC devices on the Macintosh, but it falls short of an optimal “shortest path” solution. And in addition to the above caveats, I’ve simply never been fond of IDEs in general, hence:
The development toolchain requires two or possibly three parts: either an assembler or C compiler (or both) and a device programmer compatible with the PICkit 2. Progress among open source tools on all three fronts is now crossing a threshold of viability.
GPUTILS (GNU PIC Utilities) includes an assembler 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 mentions a handful of esoteric incompatibilities and bugs yet to be resolved but I have not encountered these in regular use yet.
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.
The holdout has been a solid and straightforward device programmer. Not meaning to disparage the author’s hard efforts, but PK2, while adequate, has been a bit unsatisfactory in this regard — earlier versions compatible only with older PICkit 2 firmware (rendering the device incompatible with Microchip’s current Windows software releases, which I occasionally still use), and the new version being alpha quality by the author’s own admission. I had started development on my own streamlined application when something interesting happened: Microchip released as open source a Windows command-line program (PK2CMD), which Mr. Post and others graciously and quickly took initiative in porting to other operating systems. Precompiled binaries for Mac OS X and Linux are now directly downloadable from Microchip’s web site (though they do not offer technical support for these programs).
| Software: | Open Source Command-Line Tools |
| Pros: |
|
| Cons: |
|
There are no precompiled binaries currently available for GPUTILS; you will need to compile from the source, which requires having the Apple Developer Tools installed (an optional installer on your original OS X disc). The GPUTILS source can be acquired here. Once the archive is extracted, using Terminal.app cd to the source directory and it’s a simple three-step process to build:
./configure
make
sudo make install
SDCC can be downloaded in a precompiled Universal Binary format for Mac OS X, or built from the source exactly like GPUTILS. Either version can be downloaded from the project’s download page here.
PK2CMD is also available in both source and Universal Binary formats from Microchip’s PICkit 2 page here. The build process for PK2CMD is slightly different:
Then add the following line to ~/.profilemake mac105 (or make mac104 if running OS X 10.4)
sudo make install
export PATH=$PATH:/usr/local/bin:/usr/share/pk2
In addition to the documentation included with each, all three tools offer command-line help:
sdcc --help
gpasm --help pk2cmd -?
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 to not correspond to Microchip’s part naming conventions. Rather, each refers to the instruction word size (in bits) on the midrange (PIC12, PIC16) and high performance (PIC18) microcontrollers, respectively; 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 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 and processor into one folder and then create a Makefile that includes operations such as “make write” or “make on”, like this:
DEVICE=pic18f2620
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