Microchip PIC32 Development on Mac OS X and Linux

October 22, 2008 — Phillip Burgess — http://www.PaintYourDragon.com/uc/

I’ll be perfectly frank about this: if you’re serious about PIC microcontroller development and have the resources to throw at it, you’re better off simply acquiring an extra Windows PC to dedicate to the task. Even a slightly dated secondhand system is adequate for using the standard development tools in their native habitat.

But maybe you’re a student or a hobbyist just dabbling in this stuff and have a Mac or Linux system, lacking the funds or space to dedicate to a second system for development. Or you find working in Microchip’s Windows IDE just a bit too alien from your accustomed workflow. Or maybe you just enjoy technical challenges and fiddling around with gadgets in unexpected ways!

It might seem that virtualization or emulation software would fit the bill, or perhaps “dual-booting” into Windows when needed, but unfortunately this is not the case. These approaches have their share of problems, among them:

As with my prior 8-bit Microchip PIC article, the goal here is a fully native, shortest-path solution to PIC32 development in Mac OS X or Linux. The result is a command-line toolchain that’s source code-compatible with Microchip’s own PIC32 software development tools. My preference is for command-line coding anyway and I’ve left it at that, but if you’re adept with the Eclipse IDE then I imagine it should be straightforward to get this all working within that framework.

There are two phases to making this work: getting the software running, and acquiring the necessary hardware. I’m presenting things and hope you’ll follow along in that order, as I can’t guarantee the toolchain will build and work on every system out there, and it would be unfortunate in that case to have invested in hardware that’s then not usable.

I must point out also that there’s no hardware debugging support with this approach. For this reason and others, you might still find it occasionally helpful to dual-boot and use the native Windows tools.

Software

The pivotal thing that makes this entire project possible is that Microchip’s C32 compiler and most of the related libraries are based on the open source GCC (GNU Compiler Collection), which obliges them to freely make available their own revisions to this code. With only a few changes, the software can be coerced into compiling on Mac OS X or Linux.

But producing a working executable requires more than just a compiler. There are additional parts — certain libraries, headers, scripts, etc. — that are not bound by open source obligations and thus have not been similarly posted. What we do then is use a program called Wine, essentially a Windows compatibility layer for other operating systems, to install those components, then direct our compiler to reference those files in place of its own. Not entirely unlike Jeff Goldblum in The Fly, though hopefully without all that vomiting or body parts falling off.

A crucial point is that I am not selling, re-distributing or otherwise offering any of the required code or software here…everything is acquired through the legitimate channels and in whole…I’m only providing instructions and a few short scripts. Absolutely no proprietary files are copied, but rather are installed normally and referenced in situ. One step of this process involves explicit agreement (by button-click) to Microchip’s software license terms. I’ve read this over repeatedly and it seems okay if done this way, but I Am Not A Lawyer™ and cannot say with authority whether following these directions is compatible with that license, nor what the consequences would be should that turn out not to be the case, assuming they give the proverbial rat’s ass in the first place, especially as the subsequent hardware acquisition entails every interested user dropping a couple hundred bucks in their online store. Once you click that “I Agree” button, the matter, if there is one, is entirely between you and Microchip.

Finally, I think it goes without saying, but for posterity: the human/fly hybrid that emerges from this experiment is not a sanctioned Microchip product, and they have absolutely no reason or obligation to provide technical support. If you have a legitimate programming or hardware query that would apply even when using the official tools, that’s a different matter and the Microchip Technology User Forums are an excellent resource. But if something unique to this particular toolchain build is causing you trouble, talk to me first. This work stands a better chance of being quietly tolerated if we avoid creating a support burden.

Prerequisites

For Macintosh Users: For Linux Users:

  • An Intel x86-based Mac (will not work with PowerPC).
  • Xcode — Apple’s developer tools — included as an optional installer on your original OS X disc, or available as a free download from Apple’s web site.
  • Darwine, a Mac-compatible port of the Wine Windows compatibility package.

Install the following four packages using the Synaptic Package Manager or apt-get from the commnd line:

  • build-essential
  • bison
  • flex
  • wine
For Both

For either operating system, you will then need to register for an account (if you don’t already have one) on Microchip’s web site and download three files:

And from this site: the build scripts (.tar.gz, 5KB).

Building the Software

  1. Run the MPLAB PIC32 Windows installer:

    Mac users: install Darwine, run the WineHelper application, select “Open…” from the Application menu, and select the previously-downloaded MPLABC32_v1_04.exe file.

    Linux users: Wine seems to be better integrated with some distributions more than others. With Ubuntu for instance, once Wine is installed, simply double-click MPLABC32_v1_04.exe — Wine should launch automatically (be patient, don’t run two instances of the installer). Other distributions (e.g. Debian) may require command-line invocation; type in a terminal window: wine ./MPLABC32_v1_04.exe (you’ll likely see all manner of error messages in the terminal during installation, but these can be safely ignored; the software will install all the same).

    Regardless of operating system, install C32 in the default location. This will be in a hidden folder (.wine) in your home directory; you can’t see it in the Finder or file manager, but it will be visible via ls -a in a terminal window.

  2. Expand the previously-downloaded c32-build.tar.gz file, which should result in a directory called c32-build containing a few files.
  3. Expand the file pic32-microchip-release-1-04-20080908-source.zip, which should result in a directory called microchip-release-1.04-freeze. Move this directory (not its contents) into the c32-build directory from the previous step.
  4. In a terminal window, cd to the location of the c32-build directory (e.g. cd ~/Downloads/c32-build) and run the automated build script:
    sh ./build.sh
    The build process takes about ten minutes, and you will occasionally see some compiler warning messages but should not hit any hard errors.
  5. If the prior step completes with a “Success” message, continue with the installer script, which requires root access:
    sudo sh ./install.sh
    You can optionally provide an absolute path to the location of the MPLAB C32 folder if you installed it somewhere other than the default location. Be sure to enclose the full path in quotes to accommodate any spaces in the name.

    There is a corresponding uninstall.sh script if you wish to remove the software later.

  6. Expand the file pk2cmdv1.12.0LinuxMacSource.tar.gz, which should result in a directory called pk2cmdv1.12.0LinuxMacSource. cd to this directory and build the program using an appropriate make target for your system:
    make mac105for Mac OS X 10.5.
    make mac104for Mac OS X 10.4.
    make linuxfor recent Linux distributions using a 2.6 kernel (e.g. Ubuntu 8.04).
    make linux-oldfor older Linux distributions using a 2.4 kernel.
  7. If the prior step completes successfully, install pk2cmd as root, e.g.:
    sudo make install
    And edit your PATH environment variable to include the location of the new software, e.g. in .profile or .bashrc in your home directory, add the line:
    export PATH=$PATH:/usr/local/bin:/usr/share/pk2
    Issue the above line in any open terminal windows as well, or simply close these windows and open new ones to have the new PATH take effect.

A Quick Test

Inside the c32-build directory is a subdirectory called example. Go here (e.g. cd c32-build/example) and issue a make command to test the compiler. If successful, this should result in two new files: test.elf and test.hex. This is not an exhaustive diagnostic but does let us know if all the components of the toolchain are properly in place and working.

If everything runs to your satisfaction, you can delete the c32-build directory and its contents at this point, but you may want to keep the example directory for future reference and testing, and the uninstall.sh script should you decide to remove the software later.

Hardware

This project is based around Microchip’s PIC32 Starter Kit, though the compiler you just made should work just as well with any other PIC32 development boards and many of the same interfacing concepts will apply.

Although the PIC32 Starter Kit has an onboard USB programmer/debugger already built in, the code and protocols for using this interface are proprietary and undocumented — supported only in Microchip’s Windows software — so we’ll need alternate means of transferring our code to the PIC32 chip. Fortunately the PIC32 can use the same in-circuit serial programming (ICSP) methodology as their 8- and 16-bit microcontrollers, so most tools that can program those devices can also program a PIC32. Your best bet at present is the PICkit 2: it’s compact, affordable, and USB-based so there’s no additional serial or parallel port adapter involved. Most importantly, Microchip has generously made Mac and Linux software available for this device. And if you’ve done any work with their 8-bit chips in the recent past, you probably already have this part; just make sure you have the lateset software and device file installed to ensure compatibility with the PIC32. One downside is that the PICkit 2 doesn’t presently support PIC32 debugging, and even if it did, there’s no Mac or Linux software for this at present anyway.

As there’s no ICSP header directly on the Starter Kit (header J3 on the board provides ICSP for the PIC18 debugger, not the PIC32), one interfacing option would be to “blue wire” your own connector to the board, but this is exceedingly challenging at the scale of these surface-mount devices. A better solution is to use any of several breakout boards that connect to the 120-pin Hirose connector on the bottom of the Starter Kit and add, among other things, a 6-pin RJ11 ICSP connector that can be used with the PICkit 2 with a simple adapter. None of these boards are inexpensive, but they do have the advantage of additional connectors and interfaces that open up a lot of options for the future; you would probably end up adding one to your development arsenal at some point anyway.

Let’s Go Shopping!

All of the necessary hardware can be acquired directly from the source at the microchipDIRECT web site or through most major electronics distributors such as Digi-Key, Mouser Electronics, etc. An exhaustive list of distributors can be found through Microchip’s Global Sales & Distribution page. Prices seem to be the same regardless of source, so there’s little point in wasting a lot of time shopping around.

My example hardware setup is comprised of four parts:

  1. The PIC32 Starter Kit (Microchip Part Number DM320001, $49.99) contains the microcontroller itself, a few LEDs and pushbuttons to play with, a 120-pin expansion connector and the integrated USB programmer/debugger that unfortunately we can’t make use of (Windows only). Alternately, for a few dollars more, the PIC32 USB Starter Board (P/N DM320003, $55.00) adds ports for USB host and device development. The latter uses a slightly different processor model and you’ll need to edit the example Makefile to match, but otherwise should work similarly.
  2. The PIC32 I/O Expansion Board (DM320002, $72.00) attaches to the Starter Kit’s expansion connector to provide sockets for ICSP programming and optional I/O and device boards.
  3. The PICkit 2 Development Programmer/Debugger is the magic sauce that transfers our code from the computer to the microcontroller. The bare unit (PG164120, $34.99) is sufficient for this project, but if you were considering dabbling in Microchip’s 8-bit microcontroller lines as well, consider either the PICkit 2 Starter Kit (DV164120) or the PICkit 2 Debug Express (DV164121), either of which (at $49.99) bundles the same programmer with a small evaluation/prototyping board containing a different 8-bit MCU.
  4. Finally an RJ-11 to ICSP Adapter (AC164110, $9.99) is used to connect the PICkit 2 to the I/O Expansion Board. (The photo at the top of this page shows a home-brewed cable, but the “official” adapter is pretty reasonable if you don’t already have the parts on-hand.)

All told, that’s about $167 plus applicable tax and shipping for the least expensive options.

An alternate arrangement, which works fine with the previously-built compiler but not the example program, is based on the company’s Explorer 16 Development Board:

  1. Start with the Explorer 16 Development Board (DM240001, $129.99)
  2. Add the PIC32MX Plug-In Module (MA320001, $25.00), or if you want to do USB development, substitute the PIC32MX USB PIM (MA320002, $25.00) plus USB PICtail Plus Daughter Board (AC164131, $60.00), or the PIC32 Starter Board to Explorer 16 PIM Adapter (AC320002, $30.00) plus the PIC32 USB Starter Board (DM320003, $55.00). Either way, USB development is an $85 premium on the Explorer 16, versus just $5 extra if starting with the PIC32 USB Starter Board and PIC32 I/O Expansion Board.
  3. The same PICkit 2 programmer is required as above, though there is a crafty possibility here. More on that in a moment…
  4. The Explorer 16 requires an external DC power supply. If you don’t already have one with the right voltage, polarity and connector type, Microchip of course offers something suitable (AC162039, $24.00). But again, hold off a moment…

The Explorer 16 route is generally more spendy, but this does come with some additional bells and whistles: a two-line alphanumeric LCD, more buttons and LEDs, a temperature sensor and a serial port. Disadvantages are that there’s only a single PICtail slot (a second can optionally be soldered on) and no easily-accessible headers for all of the MCU lines. So it works out to about $225 for the basic (non-USB) Explorer 16 setup with a PICkit 2 and power supply…however, if you already have or know somebody with a Microchip programmer, and don’t mind a little warranty-voiding soldering, this article explains some modifications that reduce one’s needs to just the Explorer 16 board and PIC32 PIM, at a more manageable $155 total.

What’s more, the Explorer 16 also comes bundled with two different 16-bit microcontroller PIMs that can be swapped out for the PIC32. At present these are just fancy paperweights unless you’re using the Windows MPLAB IDE software, but the 16-bit compiler is likewise open source and I’m currently working on getting that ported to Mac and Linux as well (it’s actually much more difficult than the 32-bit compiler, go figure).

Making it Go

Putting the pieces together is fairly obvious: the PIC32 Starter Kit mounts to the corresponding space on the I/O Expansion Board, the RJ-11 adapter connects the Expansion Board to the PICkit 2, and a USB Type-A-to-mini-B cable connects the PICkit 2 to your computer (a suitable cable is included with both the PIC32 Starter Kit and PICkit 2, but I’m very fond of the retractable type).

There’s one last thing though: it’s necessary to feed power to the circuit! The PICkit 2 can provide only about 100 milliamps, barely enough to run the PIC32 Starter Kit and a lightly-populated I/O Expansion Board, and entirely inadequate when it comes time to actually program the PIC32. There’s a barrel jack connector on the Expansion Board that accepts 9 to 15 volts DC…Microchip will gladly sell you a suitable 9 volt adapter for twenty four fricken’ dollars! I rummaged through my power brick collection and found one from an external hard drive with a compatible plug and polarity. But an even simpler approach, unless your circuit needs a whole lot of current, is just to plug a second USB cable in between the Starter Kit board and a powered USB hub or port. Even though we can’t make use of the onboard USB debugger, that connection can still be used to provide enough power to program the PIC32 and drive a modest circuit.

If your hardware setup is the same as first described (rather than the Explorer 16-based combination) you can test everything with the example program included with the build scripts. With the hardware all connected as described above, open a terminal window, cd to the location of the example program (e.g. cd c32-build/example), then issue these two commands:

make

make write

If successful, the three LEDs on the Starter Kit should slowly fade in sequence.

If using the PIC32 USB Starter Board rather than the regular Starter Kit, one line in the Makefile needs to be changed: DEVICE = 32MX360F512L should be replaced by DEVICE = 32MX460F512L

Other targets in the Makefile, such as clean and erase, should be pretty obvious. Vitally important, notice that the pk2cmd program in the Makefile is always invoked with the -R flag; this releases the MCU from the reset state and starts our program running, otherwise you’ll see no response from the chip. You may want to use this Makefile as a template when creating your own.

Aside from a few lights and buttons, there’s not a whole lot to see and do on the PIC32 Starter Kit. This is where the I/O Expansion Board (or the Explorer 16) comes into play. Microchip produces a number of different modules: color graphic LCD, speech and audio processing, SD memory card storage and wireless mesh networking among others…even blank prototyping boards for applications yet to be envisioned. Go envision some!

Early in my programming career, the MIPS R4000 processor (from which the PIC32 instruction set is descended) represented a bleeding-edge technology inside computers that would have cost three years’ salary and filled an entire corner of an air-conditioned server room. Though well acclimated to the reality of Moore’s Law, I can’t help but occasionally boggle that this technology has trickled down to an almost entirely self-contained, power-sipping chip costing less than a burger and fries. And although the lack of a debugger in this project might limit the sophistication of what can be developed, I’m excited to be able to travel with my regular laptop of preference — not what’s dictated— and start hammering out code any time and place.

Back to Phil’s Microcontroller Page