Microchip PIC32 Development on Mac OS X and Linux
October 22, 2008 — Phillip Burgess — http://www.PaintYourDragon.com/uc/
Update 2/19/09: Great news! Mac users interested in dabbling in PIC32 programming will find an inexpensive solution in this follow-up article. It doesn’t work with the Microchip expansion boards, but is great for students and tinkerers seeking an affordable introduction to the PIC32.
Update 4/24/09: Upgraded to C32 1.05, and tested with Ubuntu 9.04 (both the 32- and 64-bit versions).
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’ll be 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.
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.
| For Macintosh Users: | For Linux Users: |
|---|---|
|
Install the following five packages using the Synaptic Package Manager or apt-get from the commnd line:
|
| 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 my site: the build scripts (.tar.gz, 5KB). |
|
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-v105-Evaluation.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-v105-Evaluation.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-v105-Evaluation.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.
sh ./build.shThe build process takes about ten minutes, and you will occasionally see some compiler warning messages but should not hit any hard errors.
sudo sh ./install.shYou 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.
| make mac105 | for Mac OS X 10.5. |
| make mac104 | for Mac OS X 10.4. |
| make linux | for recent Linux distributions using a 2.6 kernel (e.g. Ubuntu 8.04). |
| make linux-old | for older Linux distributions using a 2.4 kernel. |
sudo make installAnd 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/pk2Issue 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.
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.
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.
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:
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:
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).
Update: If you missed the note up top, I’ve since written a second article that outlines a much less expensive option that may be adequate for many users.
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.