Using STM32CubeMX and PlatformIO

Custom LD script could be set via Redirecting...

I am trying to do exactly this but am having some trouble getting it to work. Did you set up your MX Cube project as outlined in the first post? Would you mind posting your platformio.ini file? Did you get this to work with any middlewares such as FreeRTOS?

Not as in the first post as it was too cumbersome but in the reply a couple of posts above.
Basically these are the steps…

  1. Create a new project in CubeMX

  2. Change to the appropriate settings in the settings section.
    These are mine.

  3. Then setup the device as you choose to in the cubemx software and generate code.

  4. The initialise a pio project in the same folder as that of the cubemx project and add a main.cpp to the src folder.

  5. this is what my directory structure looks like after new pio project and adding the main.cpp file.

    .
    ├── Core
    │ └── Inc
    │ ├── gpio.h
    │ ├── main.h
    │ ├── stm32f0xx_hal_conf.h
    │ └── stm32f0xx_it.h
    ├── Drivers
    │ ├── CMSIS
    │ │ ├── DSP_Lib
    │ │ ├── Device
    │ │ ├── Include
    │ │ ├── Lib
    │ │ └── RTOS
    │ └── STM32F0xx_HAL_Driver
    │ ├── Inc
    │ └── Src
    ├── disco_030_test.gpdsc
    ├── disco_030_test.ioc
    ├── lib
    │ └── readme.txt
    ├── platformio.ini
    ├── platformio.sublime-project
    └── src
    └── main.cpp

And this is what my platformio.ini file looks like.

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html

 [env:disco_f030r8]
 platform = ststm32
board = disco_f030r8
framework = stm32cube
build_flags = -ICore/Inc

I had to add the build_flags section for the main.cpp to “see” the generated headers…

This is my barebones main.cpp

#include "main.h"
#include "gpio.h"

int main()
{

	return 0;

}

And thats it… I was able to compile the project just fine…

this is my build output.

[ Deviot 2.3.0.dev3 ] Starting…
[Tue Jun 19 20:10:50 2018] Processing disco_f030r8 (platform: ststm32; board: disco_f030r8; framework: stm32cube)

Verbose mode can be enabled via -v, --verbose option
PLATFORM: ST STM32 > ST STM32F0308DISCOVERY
SYSTEM: STM32F030R8T6 48MHz 8KB RAM (64KB Flash)
DEBUG: CURRENT(stlink) ON-BOARD(stlink) EXTERNAL(blackmagic, jlink)
Library Dependency Finder → Library Dependency Finder (LDF) — PlatformIO latest documentation
LDF MODES: FINDER(chain) COMPATIBILITY(soft)
Collected 0 compatible libraries
Scanning dependencies…
No dependencies
Compiling .pioenvs/disco_f030r8/src/main.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_can.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_cec.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_comp.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_cortex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_crc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_crc_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_dac.o
/Users/srikrishnachaitanyanarumanchi/.platformio/packages/framework-stm32cube/f0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc.c: In function ‘CRC_Handle_8’:
/Users/srikrishnachaitanyanarumanchi/.platformio/packages/framework-stm32cube/f0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc.c:475:8: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
(uint16_t volatile) (&hcrc->Instance->DR) = ((uint32_t)pBuffer[4i]<<8) | (uint32_t)pBuffer[4i+1];
^
/Users/srikrishnachaitanyanarumanchi/.platformio/packages/framework-stm32cube/f0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc.c:479:8: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
(uint16_t volatile) (&hcrc->Instance->DR) = ((uint32_t)pBuffer[4i]<<8) | (uint32_t)pBuffer[4i+1];
^
/Users/srikrishnachaitanyanarumanchi/.platformio/packages/framework-stm32cube/f0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc.c: In function ‘CRC_Handle_16’:
/Users/srikrishnachaitanyanarumanchi/.platformio/packages/framework-stm32cube/f0/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_crc.c:511:8: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
(uint16_t volatile) (&hcrc->Instance->DR) = pBuffer[2*i];
^
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_dac_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_dma.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_flash.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_flash_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_gpio.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_i2c.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_i2c_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_i2s.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_irda.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_iwdg.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_pcd.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_pcd_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_pwr.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_pwr_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_rcc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_rcc_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_rtc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_rtc_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_smartcard.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_smartcard_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_smbus.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_spi.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_spi_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_tim.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_tim_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_tsc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_uart.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_uart_ex.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_usart.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_hal_wwdg.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_adc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_comp.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_crc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_crs.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_dac.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_dma.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_exti.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_gpio.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_i2c.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_pwr.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_rcc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_rtc.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_spi.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_tim.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_usart.o
Compiling .pioenvs/disco_f030r8/FrameworkHALDriver/Src/stm32f0xx_ll_utils.o
Compiling .pioenvs/disco_f030r8/FrameworkCMSISDevice/gcc/startup_stm32f030x8.o
Compiling .pioenvs/disco_f030r8/FrameworkCMSISDevice/system_stm32f0xx.o
Archiving .pioenvs/disco_f030r8/libFrameworkHALDriver.a
Archiving .pioenvs/disco_f030r8/libFrameworkCMSISDevice.a
Indexing .pioenvs/disco_f030r8/libFrameworkHALDriver.a
Indexing .pioenvs/disco_f030r8/libFrameworkCMSISDevice.a
Linking .pioenvs/disco_f030r8/firmware.elf
Checking size .pioenvs/disco_f030r8/firmware.elf
Building .pioenvs/disco_f030r8/firmware.bin
Memory Usage → Redirecting...
DATA: [ ] 0.3% (used 28 bytes from 8192 bytes)
PROGRAM: [ ] 0.5% (used 352 bytes from 65536 bytes)
========================= [SUCCESS] Took 5.13 seconds =========================

The advantage of doing the project this way is that you can make any changes to the cubemx settings again and again… and yet not redo the entire process of platformio. If you place your code in the

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

section in the headers… you won’t lose your code when you regenerate the cubemx code headers…

PS: I forgot to add, I did not test this with any middlewares. I have not worked on any project that needed them.

Hope this helps… :slight_smile:

3 Likes

Can you tell me what version of CubeMx you are using? Thanks very much.

I am using the latest version V4.26.0.

PlatformIO seems to be great if you are using a board that is made by someone else. It is not obvious to me how to proceed if I have my own custom designed board. Seems like you would get alot more adoption of your tools if you could accommodate those who design their own boards. Specifically I use STM32 series and I use FreeRtos. I have been reading documentation and threads all day and have still not succeeded in setting up the tools for my custom board. Is there a guide you can point me to? Maybe the folks at PlatformIO could make a Youtube video or a short write up. All documentation I am finding assumes you are using some board already in the eco system.

It is pretty straightforward to add you own custom board in platformio.
Please go through this link.

http://docs.platformio.org/en/latest/platforms/creating_board.html

Irrespective of the processor in question, the frameworks, upload_protocols are pretty much the same.
You can specify additional parameters like RAM/ROM, build flags and/or the VID/PID of the programmer/device.

I hope this helps you create you own custom board. :slight_smile:

OK this looks promising. Thanks for sharing and helping. I will give it another try. I think the major point I was missing that is explained in your link is how to tell platformio about your new board file.

I created my custom board file. Put it in my home_dir with all the other board files. Things were fine until I made the mistake of running the Platformio: Update installed platforms command. It deleted my file :frowning:. Is there a way to store such a file in the project directory. To make the project portable and sharable with other team members it would be nice to keep it all in the project directory somehow. Thanks.

Try placing the file in the “boards_dir”. By default it should be the root of the project.
Or you can specify any other arbitrary path which is not overwritten by platformio.
The update command should keep your custom board intact.

Please refer to this section.
http://docs.platformio.org/en/latest/projectconf/section_platformio.html#projectconf-pio-boards-dir

OK, thanks again. I appreciate your help.

glad to be of some help! :slight_smile:

I am tinkering with platformio and cube too because I would like to use cube as a Firmware manager and code generator, and platformio for the rest.

I am able to generate code from cube, init a platformio project inside the root folder and compile some code, my .ini looks like this:

[platformio]
src_dir = Src/
include_dir = Inc/

[env:nucleo_f401re]
platform = ststm32
board = nucleo_f401re
framework = stm32cube

build_unflags = 
    -IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\CMSIS\Include 
    -IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\CMSIS\Device\ST\STM32F4xx\Include 
    -IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\STM32F4xx_HAL_Driver\Inc
build_flags = 
    -IDrivers/STM32F4xx_HAL_Driver/Inc 
    -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy 
    -IDrivers/CMSIS/Device/ST/STM32F4xx/Include 
    -IDrivers/CMSIS/Include 
    -std=c++14
; Serial Monitor options
monitor_speed = 115200 

and when I verbose build I obtain the following command (and many other similar):

CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/nucleo_f401re.html
PLATFORM: ST STM32 > ST Nucleo F401RE
HARDWARE: STM32F401RET6 84MHz 96KB RAM (512KB Flash)
DEBUG: CURRENT(stlink) ON-BOARD(stlink) EXTERNAL(blackmagic, jlink)
Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF MODES: FINDER(chain) COMPATIBILITY(soft)
Collected 0 compatible libraries
Scanning dependencies...
No dependencies
arm-none-eabi-gcc -o .pioenvs\nucleo_f401re\src\main.o -c -Os -ffunction-sections -fdata-sections -Wall -mthumb -mcpu=cortex-m4 -nostdlib -DPLATFORMIO=30602 -DSTM32F4 -DSTM32F401xE -DSTM32F40_41xxx -DUSE_HAL_DRIVER -DF_CPU=84000000L -ISrc -IInc -IDrivers\STM32F4xx_HAL_Driver\Inc -IDrivers\STM32F4xx_HAL_Driver\Inc\Legacy -IDrivers\CMSIS\Device\ST\STM32F4xx\Include -IDrivers\CMSIS\Include -IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\CMSIS\Include -IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\CMSIS\Device\ST\STM32F4xx\Include -IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\STM32F4xx_HAL_Driver\Inc Src\main.c

From what I see, the builder is including folders contained in the platformio installation; for example:

-IC:\users\andrea\.platformio\packages\framework-stm32cube\f4\Drivers\STM32F4xx_HAL_Driver\Inc

I tried to unset those flags from the .ini file but nothing happens. Any suggestions?

Moreover, a second question, If I want to change toolchain, where should I edit?

Thanks a lot

Hello,
My apologies for the delay in response.

I dont think there is a way to unset the default flags. Its a part of the pio build script. I found it much much easier (for specifically cubemx projects) to generate a makefile project (in the cubemx settings) and work from there…For most other platformios, platformio is more or less a single-solution-fits-all.

I am sorry I am not of much help for your current precadicament. Maybe someone more knowledgeable can pitch in with the correct solution.

1 Like

Thanks, I am interested in your solution. So basically you generate a Makefile project and then compile manually with make, right? Does platformio fits somewhere in this workflow?

I am trying to prepare a more modern (and pleasant) setup for embedded development respect to the existing solutions (ugly eclipse reskins), platormio fits my need in some sense. I would like to use VS Code as editor and some debugger, Makefile projects seems fine though.

FYI I just started embedded development, so anything I will say may be wrong or questionable :wink:

We all started there as well.
(I stil am a noob… :slight_smile: )
Platformio does not figure anywhere (but as I said only for the cubemx projects)

VScode would work just fine. Just ensure you have the gcc-none-eabi toolchain installed on your system and available in the system path.

running make should work just fine.
You might need to add some targets though if you would like to flash or debug the targets.

Thanks a lot, it starts making sense now. Last question, is there a way to integrate a debugger in VSCode? What do you use for debugging?

Thanks a lot

Platformio has an integrated debugger you can use. and it works pretty well in vscode.You should be able to see a debugger symbol in the IDE.

I am more comfortable with gdb. Afterall most of the debugging interfaces are also just a front-end for gdb.
(except in some commercial applications like keil or segger).

Hope this helps. :slight_smile:

2 Likes

It helps a lot, thanks! In the meanwhile via Bash for windows I tried the gcc-arm-none-eabi and I can compile makefile projects without any problem. I think I should write a rule for flashing, then I am good to go :wink:

Hello! Thank you very much for posting your STM32 CubeMX configuration steps. I followed the steps, and generated my projects; however when I tried to open the project in PlatformIO (by clicking “Open Project”), I am getting the following error:

How could I deal it with it from here?

Thank you!