cancel
Showing results for 
Search instead for 
Did you mean: 

Writing STM32CubeIDE libraries. What is the best Practice?

m12lrpv
Associate III

I'm just starting to play with the STM32 chips and like the Cube environment for it's ability to configure the chips functionality.

I want to be able to write my own class libraries for obvious reasons. Reusable Code is the name of the game.

What is the best way to integrate a source code library into a workspace such that all projects in that workspace can use that library.

1 ACCEPTED SOLUTION

Accepted Solutions
mattias norlander
ST Employee

The answer is probably as always it depends... 😉

Your decision path looks like this:

  1. If you target dual-core then there is at this point (CubeIDE <=1.4.2) no support for static libraries for dual-core, TrustZone or MPU devices. Only single core STM32 devices... So for these more complex devices you have to hack your way...
  2. Consider if the library will target only one Cortex-M core or be shared between let's say M0 and M3... Because this will have implications of your setup.
    1. If library is only targeting ONE core
      1. you can use the STM32 project wizard
        1. Select C or C++
        2. Targeted Binary Type = Static library
        3. Project type STM32Cube or Empty. With STM32Cube you dont get any ioc-file for this --> No MX support. So Empty may be just as good and more clean...
      2. Once project is created, you may want to setup different build configs for slightly different library builds... But be aware there is a bug in Eclipse/CDT leading to project references not working properly --> triggers unnecesary builds.
      3. once project is created, read the User Manual on how to include a library into your application project(s)
        1. https://www.st.com/resource/en/user_manual/dm00629856-description-of-the-integrated-development-environment-for-stm32-products-stmicroelectronics.pdf, see chapter 2.5.0 - Include libraries. Assumes UM2609 rev1...
    2. If library is targeting multiple different Cortex-M cores. Truely shared code. Then the problem is that CubeIDE only supports one Core per project. So you need to separate your own code into Core specific code and code shared between different cores.
      1. Create a static library like in bullet 2.1.1. But make one such project for each core you want to build the library for.
      2. Create a File > New > Project > General > Project. This project will just serve as a container for shared code. The project itself is not even C/C++ project. Just pure container.
        1. In this project add folders for code File > New > Folder … <-- place your share code here.
      3. In each library project link in the code folder from the container project.
        1. In the library project right-click on the linked folder and go to resource configuration to make sure files are NOT excluded from build.
        2. The include paths of the library projects must probably point to the shared code folder in the container project.

If you only target one Cortex-M core it is quite simple. If multiple cores. Then there is no official good support. So the above is just an experimental approach that probably could work...

I imagine the Use Case with "shared code targeting multiple cores" to be common, nevertheless we do not get so many requests about this...

View solution in original post

10 REPLIES 10
mattias norlander
ST Employee

The answer is probably as always it depends... 😉

Your decision path looks like this:

  1. If you target dual-core then there is at this point (CubeIDE <=1.4.2) no support for static libraries for dual-core, TrustZone or MPU devices. Only single core STM32 devices... So for these more complex devices you have to hack your way...
  2. Consider if the library will target only one Cortex-M core or be shared between let's say M0 and M3... Because this will have implications of your setup.
    1. If library is only targeting ONE core
      1. you can use the STM32 project wizard
        1. Select C or C++
        2. Targeted Binary Type = Static library
        3. Project type STM32Cube or Empty. With STM32Cube you dont get any ioc-file for this --> No MX support. So Empty may be just as good and more clean...
      2. Once project is created, you may want to setup different build configs for slightly different library builds... But be aware there is a bug in Eclipse/CDT leading to project references not working properly --> triggers unnecesary builds.
      3. once project is created, read the User Manual on how to include a library into your application project(s)
        1. https://www.st.com/resource/en/user_manual/dm00629856-description-of-the-integrated-development-environment-for-stm32-products-stmicroelectronics.pdf, see chapter 2.5.0 - Include libraries. Assumes UM2609 rev1...
    2. If library is targeting multiple different Cortex-M cores. Truely shared code. Then the problem is that CubeIDE only supports one Core per project. So you need to separate your own code into Core specific code and code shared between different cores.
      1. Create a static library like in bullet 2.1.1. But make one such project for each core you want to build the library for.
      2. Create a File > New > Project > General > Project. This project will just serve as a container for shared code. The project itself is not even C/C++ project. Just pure container.
        1. In this project add folders for code File > New > Folder … <-- place your share code here.
      3. In each library project link in the code folder from the container project.
        1. In the library project right-click on the linked folder and go to resource configuration to make sure files are NOT excluded from build.
        2. The include paths of the library projects must probably point to the shared code folder in the container project.

If you only target one Cortex-M core it is quite simple. If multiple cores. Then there is no official good support. So the above is just an experimental approach that probably could work...

I imagine the Use Case with "shared code targeting multiple cores" to be common, nevertheless we do not get so many requests about this...

Thanks. Just what I was after. I very much appreciate the detailed response. 👍

I had a go at setting something up yesterday and it aligns with what you've suggested and sort of works so I wasn't far off.

I'll redo it as per your suggestion.

I'm only looking at the Single core devices but am definitely looking at supporting multiple Cortex-M cores. That shouldn't be too much of a problem once i'm more familar with the IDE.

MCUpwr
Associate III

Where can be downloaded this user manual UM2609 rev1? I can't find it anywhere on the ST site or web.

MCUpwr
Associate III

Thanks m12lrpv, but I need rev1, here is only rev4.

Why do you think you need rev 1? Rev 4 is more current and won't contain as many errors as rev 1

MCUpwr
Associate III

Because it doesn't have this chapter "see chapter 2.5.0 - Include libraries. Assumes UM2609 rev1..."

Chapters in the UM2609 will have new numbers when we add new chapters preceding the one you are looking for. Have a look at chapter 2.5.8 - Include libraries in the latest manual version. Which CubeIDE version are you relying on? Probably the latest version of the UM2609 will be OK also for a number of older CubeIDE versions...

As per Mattias' answer... it's moved to 2.5.8