PlatformIO Community

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: https://docs.platformio.org/page/boards/ststm32/genericSTM32F103C8.html
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 -> http://bit.ly/configure-pio-ldf
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



#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