Using STM32CubeMX and PlatformIO


Is there a tutorial or writeup on how to use STM32cubeMX to generate skeleton configuration code and import it into platformio?

More specifically, how to configure CubeMX for pio compatible files, toolchain selection etc.

I am lost beyond creating a new project and twiddling my thumbs on how to progress further.

It might be obvious to most but I just fail to see it. :-I

Thank you in advance


I dug around a little.
And this is how I got it to work.

  1. Create a new project in stm32cubeMX app.

    I am choosing the STM32F030R8 for my demo and click on “start project”
    2.Under Project > Settings
  2. Select Toolchain / IDE as “Other toolchain (GPDSC)” under the Project tab
  3. Under Code Generator tab, select the “Copy only the necessary library files” and “Generate peripheral initialization as a pair of .c / .h files per peripheral”

    Apply the settings, customize the pinouts, peripherals as per your need. When you are done initializing the MCU, click on the “Generate Code” option.

This is how the project output looks like. A bunch of headers and C files under the “Inc” and “Src” folders.

Now copy the headers and source files under the “src” folder in the Platformio IDE and compile.
Thats all! :slight_smile: It should compile without any errors or issues.
(Well, atleast it did so for me)

Hope this helps for anyone else looking for a guide.


Thanks a lot that shared this guide with us! :blush:

Just a hint, you can use inc folder in pair with src. PIO automatically adds inc to CPPPATH. I think we should document this option.

Thank you for the hint about the “inc” folder @ivankravets. PIO has been incredibly intuitive and easy to use. Most of the time I dont read the documentation unless I have a specific need or purpose. :stuck_out_tongue:
( I really should take the time and read it… )

Where is the “inc” folder? Do we have to create it? (on same level as lib and src?)

The “inc” folder is created by the cubemx software when it creates the requisite header and source files.
Hope this clears up your doubt. :slight_smile:

Yes, just create it on the same level as src folder. I’ve just opened an issue for that and will update our docs:

Hi @Krishna_Chaitanya, @ivankravets : thanks for sharing this!

I’ve one additional question regarding the project folders generated by STM32CubeMX. For some platforms like STM32 F7x, enabling LwIP and FreeRTOS generates their sources under Middlewares/ and are not in a flat-directory structure. A part of that directory tree for LwIP library is attached below.

I felt that converting them into a flat-directory i.e. placing all of their headers/source files inside lib/ to be a tedious task since that would involve changing their source files’ include statements too. Is there any better way to include/configure such libraries using platformio.ini commands? lib_extra_dirs kind of statements didn’t help much.


Hello @ashish
Can you try to compile with the “lib_extra_dirs” option and there specify the source to lwip and freertos directories?

Hi @Krishna_Chaitanya, thanks for your reply. I meant lib_extra_dirs in my previous comment.
Following was my configuration and error received while building. Would I need any additional configuration file, like package.json?

Error message.

Can you post a complete directory tree structure of your entire project.
From what I am able to see and understand, your errors are because of improper paths in the include directives and the lib_extra_dirs directives.

Sure. Below given is the project-tree (3 level depth).

As you say, I understand that the headers/sources from third-party libraries are not included/linked during compilation. Would like to see how lib_extra_dirs directives may solve for that. More like a CPPPATH inclusion issue.

As @ivankravets mentioned in an earlier post, the ïnc"folder is automatically included in the directory list.

Trying specifying the include path wrt to the ïnc" folder for the middleware libraries.

Not sure whether I understood it right, correct me if I’ve understood it wrong. I set
lib_extra_dirs = …/Middlewares/Third_Party/LwIP, …/Middlewares/Third_Party/FreeRTOS
which is relative to Inc/ folder. Didn’t work as well.

Actually from your earlier posts, you referenced the extra libs search path to your home directory, not the inc folder, not even wrt your project folder.

the ‘~’ in your lib_extra_dirs path references wrt your user home folder.

Secondly, I meant when you include the freertos.h and the opt.h files in your source, give their path with respect to the inc folder.
#include “Middlewares/Third_Party/Freertos/freertos.h”
#include “Middlewares/Third_Party/lwip/opt.h”

that is assuming the files are in those respective folders. If they are in a sub folder, reference as such.
Hope this makes it clear. :slight_smile:

I’m bit confused as to how to use lib_extra_dirs properly. Have tried /Middlewares/Third_Party/LwIP and Middlewares/Third_Party/LwIP combinations in vain. Might be better to look at how Platformio includes/builds external sources.

My guess is that the overall build system expects a flat-directory i.e. where all .c and .h files are placed in the parent library directory which the user wants to compile and get linked to actual project sources.

Also if I do as you suggest, I may have to modify all relevant includes to Middlewares/Third_Party/LwIP/… etc., as their folder structure isn’t flat. Have attached below the structure inside Middlewares/Thiurd_Party/LwIP/src. Editing the header-includes would be bit tedious, isn’t it?

Currently Eclipse includes the following directories in its makefile and links them properly. Something similar must be available for Platformio too.

-include Middlewares/Third_Party/LwIP/system/OS/
-include Middlewares/Third_Party/LwIP/src/netif/ppp/polarssl/
-include Middlewares/Third_Party/LwIP/src/netif/ppp/
-include Middlewares/Third_Party/LwIP/src/netif/
-include Middlewares/Third_Party/LwIP/src/core/ipv6/
-include Middlewares/Third_Party/LwIP/src/core/ipv4/
-include Middlewares/Third_Party/LwIP/src/core/
-include Middlewares/Third_Party/LwIP/src/apps/tftp/
-include Middlewares/Third_Party/LwIP/src/apps/sntp/
-include Middlewares/Third_Party/LwIP/src/apps/snmp/
-include Middlewares/Third_Party/LwIP/src/apps/netbiosns/
-include Middlewares/Third_Party/LwIP/src/apps/mqtt/
-include Middlewares/Third_Party/LwIP/src/apps/mdns/
-include Middlewares/Third_Party/LwIP/src/apps/lwiperf/
-include Middlewares/Third_Party/LwIP/src/apps/httpd/
-include Middlewares/Third_Party/LwIP/src/api/
-include Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/
-include Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1/
-include Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS/
-include Middlewares/Third_Party/FreeRTOS/Source/

Set “lib_extra_dirs=inc/”

Then in your application source files, where the freertos and the lwip headers are included, modify them to
#include "Middlewares/Third_Party/…"to the respective paths for the lwip.h and freertos.h files.

Can you try this and tell please.

That doesn’t work, reports file not found error.

See Redirecting...

It should look like

build_flags = -I Inc

Please note that path should be specified from a ROOT of a project. In the example above, it means that folder Inc is located on the same level as platformio.ini.

1 Like