Using STM32CubeMX and PlatformIO

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!

I suspect you missed the

Now copy the headers and source files under the ā€œsrcā€ folder in the Platformio IDE and compile.

step :wink:

It looks like he created a project in VSCode/PlatformIO first and then copied the STM32CubeMX generated files into the /src project source folder. Since youā€™ve got the project, but donā€™t have it configured for platformio, you can initialise the project folder by using the VSCode/PlatformIO terminal (PlatformIO alien head icon on the sidebar, look for new terminal down the bottom), changing to the directory you have the project installed in, and then running

platformio init --ide vscode -b genericSTM32F103CB

with the correct board specified after the -b parameter. If youā€™re not sure what to put there, look it up here and get the board id on the documentation page for the chip/board youā€™re using.

Technically, you may even only need to create the platformio.ini and put the correct framework and board settings in it for it to workā€¦

Hello,
I am sorry, I have kind of given up on the STM32 cube way. It is generating way too much of a bloated binary with the simplest of code. I have chosen to move to libopencm3.

I am sorry I am unable to be of much help to you beyond this. :frowning:

Thank you very much, the script works for me with this config:

[platformio]
src_dir = Src

[env:disco_f407vg]
platform = ststm32
board = disco_f407vg
framework = stm32cube
extra_scripts = pre:copyMiddlewares.py
build_flags = -I Inc

Note that build_flags = -I Inc must be used instead of include_dir = Inc, otherwise building of middleware module from lib folder fails if it needs C headers from Inc

1 Like

Another way is to use config library.json. Then you donā€™t need any prescript. You only set all ā€œIncā€ subfolders in library.json.

platformio.ini:

[platformio]
src_dir = Src
lib_dir = Middlewares/ST

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

Create library.json in Middlewares/ST/STM32_USB_Host_Library and set this content

{
    "name": "STM32_USB_Host_Library",
    "version": "0.0.0",
    "build": {
        "flags": [
            "-I $PROJECT_DIR/Inc",
            "-I Core/Inc",
            "-I Class/CDC/Inc"
        ]
    }
}

I checked that if I regenerate code in STM32CubeMX, library.json isnā€™t lost.

1 Like

With all the wonderful hints from this discussion, Iā€™ve come up with something that works pretty well for me until PlatformIO can natively support CubeMX Middlewares. Here are the steps I do at the momentā€¦

PlatformIO with CubeMX middleware

  1. Start CubeMX project with required target device, adjust and add middleware, Makefile toolchain (anything else works too), Copy only the necessary library files, then Generate Code - see Images (1), (2)

  2. VSCode : Open folder, F1 > PlatformIO : New terminal

  3. platformio init --board nucleo_f103rb --project-option "framework=stm32cube"

  4. Close folder, open same folder (to get around PlatformIO not recognising project)

  5. Delete PlatformIO generated include folder, and CubeMX Drivers folder

  6. Adjust platformio.ini and add custom prebuild-include.py (only required if you are are using Middlewares)

platform.ini

[platformio]
src_dir = ./
include_dir = Inc/

[env]
src_filter = +<Src/>
extra_scripts = pre:prebuild-include.py
prebuild_include = Middlewares

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

prebuild-include.py

from os import path, walk
import SCons.Errors

Import('env')
try:
    import configparser
except ImportError:
    import ConfigParser as configparser

config = configparser.ConfigParser()
config.read('platformio.ini')
prebuild_include = config.get('env', 'prebuild_include').split()

source_paths = set()
header_paths = set()
for included in prebuild_include:
    if not path.exists(included):
        raise SCons.Errors.UserError(
            "prebuild-include.py could not find path '%s'" % included)
    for root, dirs, files in walk(included):
        for file in files:
            if file.endswith(('.c', '.cpp', '.s')):
                source_paths.add(root.replace('\\', '/'))
            if file.endswith(('.h', '.hpp')):
                header_paths.add(root.replace('\\', '/'))

if not 'SRC_FILTER' in env:
    env['SRC_FILTER'] = ['+<Src/>']
env['SRC_FILTER'] += ['+<%s/>' % source_path for source_path in source_paths]

if not 'BUILD_FLAGS' in env:
    env['BUILD_FLAGS'] = []
env['BUILD_FLAGS'] += ['-I' + header_path for header_path in header_paths]

ā€œIt works for meā€, but I hope this helps out anyone else in the same situation, or at the least provide some additional clues until the day PlatformIO automatically detects and uses CubeMX Middleware. :wink:

Aside: Out of date PlatformIO CubeMX drivers

The framework-stm32cube CubeMX LL and HAL drivers are very out of date now for a lot of the devices (last updated Nov 30, 2018). Pity this canā€™t be automatically generated via some PlatformIO build/release pipeline every time ST releases updates, or at least on a button click for PlatformIO project maintainers. I wish I could help here, but I couldnā€™t even find the source for it - and I tried very hard :frowning: .

Currently am forced to do the following (not recommended - absolutely dirty flaky hack)ā€¦

  1. Find device firmware location in CubeMX (might need to generate code to trigger the download first) - see Images (3)
  2. Delete all the existing files in PlatformIO <packages_dir>\framework-stm32cube\<Fx>\* (see platformio.ini :: platformio)
  3. Copy Drivers, Utilities, package.xml, Release_Notes.html from CubeMX location to PlatformIO location - see Images (4)

Note: This might not work as Iā€™m wilfully ignoring the existing PlatformIO <packages_dir>\framework-stm32cube\platformio\ldscripts, and have only tried it with the STM32F103 (but, will be trying STM32F401 today).

Images

Sorry about this, but had to combine into one image since I cannot post more than one for the first post!


Matthew

2 Likes

Hey guys.

Iā€™m having trouble using the code generated by CubeMX inside PIO, because of STM32Cube version mismatch. The one included in STSTM32 platform in PIO is outdated.

@rwx has a way to ā€œhackā€ the new drivers inside PIO, but that was 4 months ago. Is this still the best way to do this now?

Thanks! :slight_smile:

1 Like

Hi,

based on the script by rwx I created a script that collects the necessary information automatically from the STM32CubeIDE .cproject file and as a backup a manual script that relies heavily on the script by rwx. Looking forward to comments. Here is the link:

Cheers, Joachim

1 Like

Hi @jbaumann
Excellent. If I understand correctly, with your script, we will be able to convert all applications and samples coming with SMT32CubeMX to plaformio project domain and do everything we can do with platformio (for example, transfer to another IDE or transfer to another board, etc.) Is it true?