PlatformIO for stm32 and CubeMX include error

Hello! Please help me to understand how to configure the project. I am creating a project for STM32 in STM32CubeMX, have 4 folders: “Drivers”, “Inc”, " Src " and “Middlewares”. Next, I create a new project in PlatformIO for stm32 and CubeMX. Added folders:".pio",".vs code", “include”, “lib”. I’m trying to do a Build, but it doesn’t work. “fatal error: the main.h: No such file or directory”. I tried deleting the folder .“vscode” but it does not produce a result. I intuitively understand that you need to somehow configure the folder paths Drivers, Inc, Src and Middleware but how to do it correctly and did not understand. Please help solve this problem!

For simple projects, there is a simple approach:

  1. Copy the files from the Inc directory into the include directory of your PlatformIO project.
  2. Copy the files from the Src directory into the src directory of your PlatformIO project.
  3. Build your project

In other words: Ignore the Drivers and the Middlewares directories. They are usually not needed as they are not specific to your project and as the same files are automatically available in PlatformIO.

I can’t tell you if it always work even if you use RTOS, USB etc. Also note that PlatformIO currently does not include the latest version of these files; they are about a year old.

2 Likes

I thank you for your answer! But I have a project that uses USB and I need to somehow connect these libraries. Please tell me how this can be done.

So I doesn’t work with the simple approach? Have you tried?

I did exactly as you said:

  1. Created new project in PlatformIO
  2. Copied the files from the Inc directory (CudeMX) into the include directory of my PlatformIO project.
  3. Copied the files from the Src directory (CudeMX) into the src directory of my PlatformIO project.
  4. Build and have result:

Executing task: C:\Users\Peter.platformio\penv\Scripts\platformio.exe run <

Processing genericSTM32F103C8 (platform: ststm32; board: genericSTM32F103C8; framework: stm32cube)

Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: Redirecting...
PLATFORM: ST STM32 5.7.0 > STM32F103C8 (20k RAM. 64k Flash)
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (blackmagic) External (blackmagic, jlink, stlink)
PACKAGES: toolchain-gccarmnoneeabi 1.70201.0 (7.2.1), framework-stm32cube 2.0.181130
Warning! Cannot find a linker script for the required board! Firmware will be linked with a default linker script!
LDF: Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 11 compatible libraries
Scanning dependencies…
No dependencies
Building in release mode
Compiling .pio\build\genericSTM32F103C8\src\main.o
Compiling .pio\build\genericSTM32F103C8\src\stm32f1xx_hal_msp.o
In file included from src\main.c:23:0:
include/usb_device.h:33:10: fatal error: usbd_def.h: No such file or directory


  • Looking for usbd_def.h dependency? Check our library registry!
  • CLI > platformio lib search “header:usbd_def.h”
  • Web > PlatformIO Registry

#include “usbd_def.h”
^~~~~~~~~~~~
compilation terminated.
*** [.pio\build\genericSTM32F103C8\src\main.o] Error 1

Where is this file? In some middleware folder for USB functionality? Have you copied its sources and includes, too?

Here’s an alternative, more involved approach:

  1. In STM32CubeMX, configure Project settings:

    • Toolchain / IDE: Other Toolchains (GPDSC)
  2. In STM32CubeMX, configure Code Generator settings:

    • STM32Cube MCU packages and embedded software packs: Copy only the necessary files
  3. Generate the code

  4. Put a platformio.ini file into the directory with the generated files (similar to the one shown below).

  5. Open the directory from PlatformIO and build it

You likely have a different board and you might have additional Middlewares. So adapt it as needed.

Note:

  • I haven’t fully tested it. It successfully builds with USB initialization code. But it might still contain problems.
  • The Drivers directory is generated by CubeMX but not used by the PlatformIO project. Instead, these files are taken from the PlatformIO installation.
[env:stm32]
platform = ststm32
framework = stm32cube
board = blackpill_f103c8
build_flags =
    -I Inc
    -I Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc
    -I Middlewares/ST/STM32_USB_Device_Library/Core/Inc
lib_extra_dirs =
    Middlewares/ST/STM32_USB_Device_Library/Class
    Middlewares/ST/STM32_USB_Device_Library
lib_deps =
    Class
    Core
2 Likes

I did as you recommend and everything worked! I am very grateful to you!

1 Like

You can also use stm32pio to automate these tasks.

1 Like

I tried to use stm32pio, but I couldn’t get it to work. The instructions are pretty obscure, but I got as far as generating a stm32pio.ini file. Then I tried to do a “stm32pio new”, but it complained because there was no board specified. I couldn’t see how to specify a board to the program (I had already generated the .ioc file with CubeMx using that board) so I went into the ini file, having found out from the website that the board id was “nucleo_f401re” and put that into the line starting "board = ". Tried the “new” function again but it got the same error, and when I went back to the stm32pio.ini file again, the board id had been deleted. Tried several times, but each time it just deleted the board id, and then complained that there was no board id. Not sure what I’ve done wrong…
I use Manjaro Linux. Just installed latest version of STM32CubeMX and all pio files are up to date.
:-\

You can see help at any time by entering stm32pio -h. Currently, you need to always specify the board for new command, like this:

stm32pio new -d path/ -b nucleo_f401re

In this case you don’t need to run init command first. But I see the problem now, this need to be fixed, thanks for a feedback.