STM32CubeMX for make and gcc

Using the STM32CubeMX code generator with gcc and make the quick and dirty way


Project motivation

The manufacturer of my favourite microcontrollers (ST micro) offers a new code generation tool: the STM32CubeMX. This is a nice tool to generate the initialization code for the STM32F microcontrollers.

The tool generates code and projects for some commercial IDE, if you do not use them, you're left alone with a huge amount of C Code (depends on the project configuration) and no instructions how to use it.

Since my favourite method to compile embedded C Code is to use Makefiles, I've created a template that can be used to compile the generated C Code. I'm using a "arm-none-eabi" toolchain, they are available at various places in the internet. The Makefiles should work on Linux and Cygwin. I've never tried them on Windows.

I've used Atollic Studio Lite (available free, but has a 32k size limit) to open the generated project and look up the necessary compiler settings and include paths. Then I've transferred these into a set of Makefiles that I'm using for my STM32 projects. These Makefiles build the libraries (HAL etc.) as own archives (.a), and these are linked to the main code. Dependencies are build for the main code, but not for the libraries. If you change any library configuration options, type "make libclean; make" to rebuild the libraries. In case that you regenerate the code with STM32cube, type "make clean libclean; make" to rebuild the whole project.

And yes, I'm using Windows (as a necessary evil) to run STM32cube, but then I move to my Linux box and continue there (using Eclipse, make, gcc, openocd etc). Since this workflow is rather annoying, I've recently tried to run STM32cube on Linux using wine. It works, but you'll have to install the Java Runtime for Windows into wine, which was quite easy (just run the Java installer with wine). After having installed Java, I ran STM32cube from the windows partition, where it was previously installed under windows. I've tested this as described on a recent Ubuntu 14.04 (Trusty Tahr) 64Bit setup.

There's also a native way to run STM32cube on Linux (Taken from here): First install the Oracle JRE (Oracle JRE for Ubuntu), then use java to install STM32cube: "java -jar SetupSTM32CubeMX-4.5.0.exe". Skip the installation of Desktop Icons and shortcuts and start STM32cube by java (cd to install dir, then): "java -jar STM32CubeMX.exe"

Result

Template Makefiles for STM32F4 based projects
Sample project for the Olimex STM32-E407 board

This is how to use the template:

  • Create your project using the STM32CubeMX software
  • Choose "TrueSTUDIO" as Toolchain
  • Set "Copy only the necessary library files" option
  • Generate the Code
  • Copy the Makefiles from the template over the created directory structure
  • type "Make" at the top level directory of the project

  • OK,life isn't that easy, you'll have to customize the Makefiles according to the MCU type and family and the used libraries. The example project uses all of the available third party libraries and also the USB libs. The project doesn't do anything useful but compiling successfully. I've never tested the binary on the hardware. Use it with caution and only as a starting point. Especially the ethernet driver and maybe LWIP will need modifications for the PHY, I haven't done this yet.
    The option "Copy only the necessary library files" leaves out all source codes that are not required in your processor configuration. If you plan to use more HAL drivers later, you'll either have to regenerate the code, manually copy the HAL library files to your project or set the option "Copy all library files".
    As stated above, for some of the provided libraries (HAL) it might be useful to use the option "copy all library files". If you do this, some of the non-HAL libraries will contain files that will cause compilation errors. You'll have to locate and remove these files (unused codepages, templates etc.).

    Conclusion

    At the moment I do not continue using STM32cube for code generation of anything more complex than the basic initialization of the controller (pin mux and clock configuration). The generated code for the provided middlewares (FreeRTOS, LwIP, ...) appears to work not reliable and requires debugging and fine tuning.

    The STM32cubeMX is still a nice tool to handle the peripheral to pin mapping and also the basic clock initialization.

    The new STM HAL libraries provided with the STM32cube look interesting, but at the moment I continue to use the older Standard Firmware Libraries together with third party software like LwIP and chibios.

    Look here for my multiple project makefile framework using the STM standard peripheral library: Makefile framework for STM32.

    I also do not continue to use tools like Atollic, Keil, IAR etc. since they usually put annoying restrictions on their free of cost versions and use proprietary project files. I prefer using real free (and preferably OSS) tools and plain Makefiles.

    Links and derived work

    Hrvoje Mihaldinec used this template as a starting point for another STM32 makefile template. The readme.md there contains a quick start guide for STM32 development on Ubuntu 14.04 covering the common pitfalls.

    Look here: The STM32 files for my other STM32 related pages.
    Table of contents ... und ein Zaehlpixel hab ich auch :-)