Using the STM32 Primer with Mac OS X
August 4, 2008 — Phillip Burgess — http://www.PaintYourDragon.com/uc/
Update 10/16/08: Compiler has been updated to G++ Lite 2008q3 (gcc 4.3.2-based) and a newer firmware library is now available.
Update 8/28/08: Big changes! “Soft” floating-point math support now works properly, the build process is much simplified, and a C++ compiler has been added to the mix. Most importantly, pre-built binaries for the compilers are now available (you will still need to download and build certain bits, but the hardest part is taken care of now).
The STM32 Primer is a nifty microcontroller evaluation/development unit
that can serve as the basis of all manner of clever DIY electronics projects.
The device is quite affordable (under $40), similar in cost to an
Arduino…but it’s a very different animal, packed with bonus
doohickeys that geeks will really enjoy playing with:
With its 3-inch round transparent case and single button, the STM32 Primer bears a passing resemblance to the old iMac “hockey puck” mouse. The case is designed to pop open easily to access and modify the internal hardware (there are a number of accessible solder pads on the back of the board).
As is to be expected, the included development tools are Windows based. While the IDE and compiler can work under virtualization environments such as Parallels Desktop or VMware, the USB programming functionality unfortunately does not — it demands Boot Camp or an actual PC.
The good news is that there’s a way to get a cross-compiler toolchain working natively on the Macintosh, and a utility available on the STM32 Primer side that allows us to download compiled programs. A similar sequence will likely work for Linux, though I have not tested this combination yet.
With a broadband net connection and a middle-of-the-road Mac (basic DSL and a MacBook here), it should be possible to complete this process in a couple of hours. You’ll need to have the Apple developer tools (Xcode) installed, several hundred megabytes of disk space (there’s about 25 megabytes to download — 85 megs if retrieving the all the source code — but additional space is needed to extract and build the software — a full gigabyte if installing Xcode), and one stage of the process absolutely requires access to either a real PC or a Mac running Boot Camp (virtualization software will not cut it). The latter is only an interim step — once complete, all work can then be done entirely on the Mac.
Also, before proceeding, let me mention some caveats: these are strictly command-line tools; there is no graphical IDE (integrated development environment) unless you run the Windows tools. And there is no debug support, programming only.
Acquiring the HardwareDue to the uncertain complexity of acquiring and installing the software components, you might want to verify first that you can complete the subsequent steps before buying one of these units, lest you end up with a costly paperweight (albeit one that can play Pac Man). Once you’re ready, Primer units can usually be found at Mouser Electronics, Digi-Key, and a few other sources listed on the STMicroelectronics web site. One source missing there is Rapid Electronics in the UK. Prices range from about $34 to $49 (plus tax and shipping) depending on the source.
The kit includes the Primer unit with neck lanyard (such a medallion serving as an irresistable chick magnet, no doubt), a USB A to Mini B cable, and a 3-inch CD-ROM that will likely send your slot-loading Mac on a trip to the Genius Bar, so don’t use it — all the software and documentation you need can be downloaded online.
The STM32 toolchain — the combination of compiler, assember, linker, etc. that work together to convert C code into executable programs — is legitimately derived from a commercial open-source product, Sourcery G++ Lite, in accordance with the license terms for that software. Please note however that this build does not constitute a CodeSourcery™ product, and they must not be burdened with any support issues that result. Bring any problems to my attention first. The source code contains specific license information regarding distribution requirements, restrictions and legal disclaimers.
The toolchain is available here in several formats depending on your needs:
Regardless whether you’re using the source or the pre-built binaries, you’ll still need to compile some other prerequisite code, and for that you’ll need:
Because the remaining pieces are ambiguous about licensing issues, no pre-built version is available at this time. It’s necessary to download each piece from its respective source, then build the tools with the instructions that follow.
Building the ToolchainIf you downloaded the pre-built toolchain, use these steps:
Open a Terminal window and type these two commands:
cd ~/Desktop/stm32/stm32-bin (or whatever directory contains the extracted files)
sudo tar xjPvf stm32.tar.bz2 (this will request your Administrator password)
Add the following line to ~/.profile
export PATH=$PATH:/usr/local/stm32/bin
Enter the same line of text on the command line, or close and open a new Terminal window before issuing further commands.
Or, if you chose to download the source instead, use these steps:
Open a Terminal window and type these two commands:
The entire build process may take 30 minutes or more.cd ~/Desktop/stm32/stm32-src (or whatever directory contains the extracted files)
sudo sh ./build.sh (this will request your Administrator password)
Add the following line to ~/.profile
export PATH=$PATH:/usr/local/stm32/bin
Enter the same line of text on the command line, or close and open a new Terminal window before issuing further commands.
(You can disregard this section if you opted for the source package; libusb is already included with that, and the build process is automated.)
cd ~/Desktop/stm32/libusb-0.1.12
./configure
make all-am
sudo make install-am
cd ~/Desktop/stm32/fatfryer-1.0/fatfryer-1.0
testfile=stm32lib_contents.htmto this:
testfile=stm32f10xfwlib_contents.htm
./configure --with-circle-os=`pwd`/../../CircleOS --with-stm32-fw=`pwd`/../../um0427
(Yes, the back-quoted `pwd` is necessary; normal relative paths won’t work in this case.)
One line in the FatFryer source must be changed to work with the Mac. Edit the file tools/fatfryer-flash.c, changing this line:
// usb_close (usbDevice);
To this:
usb_close (usbDevice);
(This is line #172 in the version 1.0 source.)
make (fairly quick, and OK to ignore the warnings)
sudo make install
To regurgitate a prior point, this sequence of steps absolutely must be performed either using Boot Camp or a separate Windows PC. It will not work under any virtualization software at this time.
If you’re using Boot Camp, have a slot-loading optical drive and are concerned about the 3" CD-ROM getting stuck, a disc image containing the current software can be downloaded from the STM32circle web site. The usual disclaimer applies of requiring a site account to access files. You should already have downloaded the current CircleOS and the FatFryer project files in a prior step. We’ll be needing those files again, so either transfer them to the PC or download them again via the previously mentioned links.
The README file included with FatFryer is quite thorough, but for posterity, here’s the walkthrough:
Source code and applications generated with this toolchain and FatFryer are entirely compatible with the Raisonance development tools in Windows, and the inverse is true as well. Only the build process is different. It should be possible to update CircleOS and FatFryer without returning to the PC; this is explained in the FatFryer README file. Another nicety of working with FatFryer is that no special USB driver is required; everything works in user space and, with some tweaking, the entire development kit could conceivably be stored on a USB thumb drive for easy portability.
A tip: you’ll need to unplug/reconnect the USB cable each time you use FatFryer; it’s just one of those quirks of the STM32 Primer.
FatFryer is the work of Andrew Bardsley. Binutils, Newlib and GCC represent the efforts of countless thousands; STM32 support therein was developed by CodeSourcery, and this project is descended directly from their source. Bogdan Marinescu’s tutorial on Building GCC for Cortex, though I was working from different source packages, provided essential insights that eventually got this working on Mac OS X.