Using PlatformIO with CubeMX
Background
In the previous article, we observed that PlatformIO offers a more elegant development experience compared to Keil. As we all know, in the world of STM32 development, the HAL library, in combination with the powerful tool CubeMX, is more convenient than the standard peripheral library. However, PlatformIO's official support for CubeMX is not entirely seamless, requiring an intermediary Python middleware for code conversion.
In this article, I will introduce a unique method to make PlatformIO work seamlessly with CubeMX for a delightful development experience.
Project Initialization
TL;DR: I've placed the project folder created using the following steps in this repository. You can clone it directly.
CubeMX Initialization Steps
- Create a new project.
- Choose the MCU model.
- Configure Pinout & Configuration.
- Configure RCC (select external/internal clock, as needed).
- Configure SYS (change DEBUG option from
No Debug
toSerial Wire
as necessary).
- Configure Clock Configuration.
- Configure Project Manager.
- Project Page 1. Enter the project name (e.g.,
Template_of_PlatformIO_with_CubeMX
). 2. Modify the project path (e.g.,D:/Desktop
). 3. Change the toolchain/IDE toOther Toolchains
. - Code Generator Page 1. Choose the software package option (STM32Cube Firmware Library Package) as
Copy only the necessary library files
. 2. Check the 'Generate peripheral initialization as a pair of '.c/.h' files per peripheral' in generated files options.
- Project Page 1. Enter the project name (e.g.,
With the configuration completed, let's click on 'Generate Code' in the top right corner to generate the code.
PlatformIO Initialization Steps
- Open the PlatformIO main page.
- Click on 'New Project' to create a new project.
- Enter the project name. Note: It must match the name configured in CubeMX (e.g.,
Template_of_PlatformIO_with_CubeMX
). - Select the board/MCU model. You can directly choose the MCU model (e.g., STM32F103C8) or the board type (e.g., BluePill F103C8). Note: It must match the configuration in CubeMX.
- Choose the code framework as
STM32Cube
. - Uncheck 'Use default location' under the 'Location' path, and set a custom path. Note: It must match the configuration in CubeMX (e.g.,
D:/Desktop
).
- Enter the project name. Note: It must match the name configured in CubeMX (e.g.,
-
Open the
platformio.ini
file in your project and add the following lines:This is because PlatformIO and CubeMX generate framework folders differently. To ensure compatibility, we align with CubeMX. 4. You can delete the 'include' folder in your project. Since Windows file naming is not case-sensitive, the 'src' folder naturally becomes 'Src'.
Enjoy your development journey!
In the project, .c
files are located in the Src
folder, while .h
files are in the Inc
folder.
Any code between /* USER CODE BEGIN */
and /* USER CODE END */
will be preserved throughout the subsequent generation process from CubeMX and will not be overwritten.
You can use the following shortcuts in PlatformIO: - Press Ctrl + Alt + B
to compile. - Use Ctrl + Alt + U
to compile and upload. - Press F5
to start debugging.
The next step in your exploration is to learn about the HAL library. To be continued ~
References and Acknowledgments
- STM32CubeMX Series Tutorial 03 - Creating and Generating Code Projects
- STM32CubeMX Series Tutorial 06 - Project Manager Detailed Explanation
- Using VS Code as an STM32 Development Platform (PlatformIO)
Original: https://wiki-power.com/ This post is protected by CC BY-NC-SA 4.0 agreement, should be reproduced with attribution.
This post is translated using ChatGPT, please feedback if any omissions.