Monday, July 2, 2012

Open Source: Template Peripheral Library for the STM32 microcontrollers


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.


Static assertions


Rationale: Kills the bugs at compile time. Eliminates the chance of making impossible configurations. Template arguments are the best friends of static assertions.


Compile-time functions


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.

Acknowledgements

  • Federico Terraneo, his post inspired the creation of this library.

Updates

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

8 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. @manoj
    How about actually commenting about the library, before posting links to your site?
    Thanks

    ReplyDelete
  3. Hola. Enhorabuena y gracias por la biblioteca.

    Hello. Good job!. I will test it soon. Since I read the Terraneo Federico's article on template metaprogramming the STM32 GPIO I was seeking for an extension of that concept to all peripherals. And there is your project!

    Thank you very much.

    ReplyDelete
    Replies
    1. Hola Francisco,

      Thanks for pointing out the identity of the creator of the GPIO template metaprogramming post, I've added a proper acknowledgment.

      I hope you find this library useful. Feedback and constructive criticism is welcome!

      Delete
  4. Hello Jorge !
    I am trying to use your library with an stm32F103RC
    I unused PLL USART works fine, but when I try to turn PLL the program either loops (PLL=2-7) or displays the port(PLL=8,9): üüà üà üà à à à üüüüà üà üà à à à üüüüà üà üà à à à üüüüà üà üà à à à üüüüà üà üà à à à üüü
    üà üà üà à à àü............
    Tell me where can be the problem?

    p.s. Sorry for my English, I'm from Russia. Thank you very much.

    ReplyDelete
    Replies
    1. Hello there,

      I haven't done much testing with the F1 microcontroller, and I don't have one at hand right now. I think you might have forgotten to:

      Uncomment the #define STM32F1XX under the device_select.h file

      or

      You are exceeding the device operating frequency. Check the datasheet and the cPrint command.

      Hope that helps

      Delete
  5. Hey,

    Have you used this library in the CoIDE? I'd guess you haven't because this IDE doesn't like your .hpp file extension but this is one of the most user friendly environments available. Are you able to any support because I have had no luck including your library thus far?

    ReplyDelete
  6. This post is written up with a height of striking and involves decent information. Great insights c# cheat sheet

    ReplyDelete