Today I'm releasing a library that I developed for the STM32 microcontrollers (currently there is support for the F1, F2 and F4 families). This library is written in C++0x, and addresses some issues I run into when I used other free peripheral C libraries.
The library is named libstm32pp, and it's available in this repository. libstm32pp makes heavy use of template metaprogramming, enumerators, static assertions, templates and namespaces. (Most of these are C++ exclusive features)
All these features allow a memory efficient implementation, compile-time error checking, a clean global namespace, highly organized functions in classes, and a overall enjoyable programming experience.
I use this library along with bareCortexM, an Eclipse-based bare metal development enviroment, but you can use any other IDE, toolchain, etc. Although, I recommend having a auto-complete/content-assist feature, to get the most of this library.
Now, I'll go into some details of why this C++ library is better than other C peripheral libraries.
Enumerators instead of MACROS for constant values
Rationale: Enumerators can be used as function arguments, MACROS can't. Enumerators don't waste memory at run-time, like MACROS, in contrast with "const" variables. Enumerators are the best friends of template metaprogramming.
Hierarchical organization using namespaces and classes
Rationale: Nobody wants to call the content-assist/auto-complete and get hundreds of suggestions of enumerators and functions, a hierarchical organization using namespaces makes easier to find what your are looking for. Functions are stored inside classes as static members.
Verbose configuration, minimalistic implementation
Rationale: Template metaprogramming, allows the creation of advanced functions, that can get resolved at compile time, reducing the number of instructions at run time (which means faster execution and smaller memory footprint).
Highly configurable clock initialization
Rationale: Selecting between internal clock, external clock or PLL, and setting the bus prescalers is as easy as commenting a macro or filling an enumerator.
Rationale: Kills the bugs at compile time. Eliminates the chance of making impossible configurations. Template arguments are the best friends of static assertions.
Rationale: Get more done at compile-time, run less instructions at run-time, save power and memory. My favorite is the compile-time print, you can see values at compile time. (They appear as errors though)
Easy aliasing of peripherals
Rationale: The default name of the peripherals are nice, but sometimes giving them an alias name makes the program clearer.
That sums it all, I hope this library will be useful for all the STM32 hackers out there. I'll love to get feedback on this library. See you around.
- Federico Terraneo, his post inspired the creation of this library.
- I figured how to activate optimization without destroying the microcontroller exceptions and interrupts.
- The library now contains various demos to help you get started.
- Added system calls, now the new / delete operators can be used. printf is also supported.
How to use
Check this post, which offers a detailed tutorial on how to use this library alongside bareCortexM.