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 Hardware

Due 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.

Acquiring the Software

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 Toolchain

If you do not have Xcode installed, take care of that first. Note that the Xcode IDE is not actually used during this build process, just the command-line tools (via Terminal.app). Some editing of text files will be necessary, so I hope you’re reasonably comfortable with a terminal-based editor like pico, vi or emacs. Unpacking the downloaded archives, or deleting the interim results once installed, can be done using the Finder. I created a folder on my desktop called “stm32” and placed all of the previously-downloaded items into that common location.

Install or Build GCC (the ARM cross-compiler)

If you downloaded the pre-built toolchain, use these steps:

  1. Double-click the file stm32-bin.tar.gz. Two progress bars will appear as the archive is extracted. You should be left with a folder (stm32-bin) containing two items: a large bzip2 archive and a README file essentially containing these directions.
  2. 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)

  3. 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.

  4. If everything compiled and installed without incident, the uncompressed folder is no longer needed and can be removed. All of the compiler-related files are located in the directory /usr/local/stm32.

Or, if you chose to download the source instead, use these steps:

  1. Double-click the file stm32-src.tar.bz2.gz. Three progress bars will appear as the archive is extracted. You should be left with a folder (stm32-src) containing several items: a subdirectory (src) containing the original open-source components, and three files; build.sh, patchfile and README, the latter essentially containing these directions.
  2. Open a Terminal window and type these two commands:

    cd ~/Desktop/stm32/stm32-src    (or whatever directory contains the extracted files)

    sudo sh ./build.sh    (this will request your Administrator password)

    The entire build process may take 30 minutes or more.
  3. 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.

  4. If everything compiled and installed without incident, the uncompressed folder is no longer needed and can be removed. All of the compiler-related files are located in the directory /usr/local/stm32.

Build and Install libusb

(You can disregard this section if you opted for the source package; libusb is already included with that, and the build process is automated.)

  1. Extract the libusb source by double-clicking the file libusb-0.1.12.tar
  2. In a Terminal window, run these four commands:

    cd ~/Desktop/stm32/libusb-0.1.12

    ./configure

    make all-am

    sudo make install-am

  3. If this step completed successfully, the uncompressed folder is no longer needed and can be removed.

Edit, Build and Install FatFryer on the Mac

  1. Extract the FatFryer source and STM32 Primer-related files by double-clicking the files CircleOS-2.0.zip, fatfryer-1.0.zip and um0427.zip (the latter is the STM32 firmware library).
  2. In the Terminal:
    cd ~/Desktop/stm32/fatfryer-1.0/fatfryer-1.0
  3. Because FatFryer 1.0 expects an earlier version of the STM32 firmware library, we need to change the configuration script slightly to acknowledge a change of filename. Edit the “configure” file, changing this line:
    testfile=stm32lib_contents.htm
    to this:
    testfile=stm32f10xfwlib_contents.htm
  4. Provide the configuration command with paths to the CircleOS and STM32 firmware library:

    ./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.)

  5. 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.)

  6. Edit the file lib/Makefile.in, deleting the three lines that reference the files stm32f10x_tim1.c (and .o and .h — but not stm32f10x_tim.c, etc., just the “tim1” files), then proceed with the build and installation:

    make     (fairly quick, and OK to ignore the warnings)

    sudo make install

  7. If this completed without errors, you’re done with the toolchain side! The CircleOS, FatFryer source and STM32 firmware directories can be removed at this point. All the needed files have been installed in /usr/local/share and /usr/local/bin.

Install FatFryer on the STM32 Primer

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.

  1. Insert the STM32 Development Suite CD-ROM (or extract the disc image) and follow the installation directions. There are check boxes for the Ride7 IDE and RKitARM for RIDE7 — install both. There’s also a USB device driver in a separate folder that will need to be installed.
  2. While the software installs, and if you haven’t already done so, open the STM32 Primer and move the jumper on the back of the board to join the two contacts, then press the button on the front to turn it on. You can turn it off by holding down the button for several seconds.
  3. Power up the SMT32 Primer and connect a USB cable to the port labeled DEBUG (not the port under the power button).
  4. Update the operating system first. This will wipe out any demo applications on the Primer, but replacements (and many others) can be downloaded from the web site. After extracting the Zip file, inside the CircleOS folder is a batch file called Update_Circle_OS.bat. Double-click this file and follow the instructions. If there are any problems, the program offers some guidance for debugging the most common issues.
  5. Next, install FatFryer: after extracting the Zip file, inside the fatfryer-1.0 folder is a batch file called add_to_Circle.bat. Double-click this file and follow the directions.
  6. You’re done with the PC side now, and can put it away.

Using FatFryer

The README file included with FatFryer is quite thorough, but for posterity, here’s the walkthrough:

  1. Move the USB cable from the DEBUG USB port to the STM32 port (under the power button).
  2. Power up the STM32 Primer and run the FatFryer application. The display will show the text “FatFryer” in large letters, along with a couple of diagnostic values.
  3. Run one of the Mac-side FatFryer apps. For example, fatfryer-flash displays the contents of the STM32 flash memory and can be used to remove previously-installed applications.
  4. fatfryer-start outputs an example program source file (toggle_with_CircleOS.c) and associated Makefile. A subsequent make will then compile this program, and make program will download the application to the Primer. If everything is working correctly, when you run toggle from the Primer’s application menu, the red and green LEDs on the front of the unit should alternately flash.

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.

Acknowledgments

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.

Back to Phil’s Microcontroller Page