Skip to main content

Small cross-platform Python app that can create and update PlatformIO projects from STM32CubeMX .ioc files. It uses STM32CubeMX to generate a HAL-framework-based code and alongside creates PlatformIO project with compatible parameters to stick them both together

Project description

stm32pio

Small cross-platform Python app that can create and update PlatformIO projects from STM32CubeMX .ioc files.

It uses STM32CubeMX to generate a HAL-framework-based code and alongside creates PlatformIO project with compatible parameters to stick them both together.

The GUI version is available, too.

Logo

Table of contents

Features

  • Start the new complete project in a single directory using only an .ioc file
  • Update an existing project after changing hardware options in CubeMX
  • Clean-up the project
  • Get the status information
  • [optional] Automatically run your favorite editor in the end
  • [optional] Automatically make an initial build of the project
  • [optional] GUI version (beta) (see stm32pio-gui sub-folder for the dedicated README)

Requirements:

  • For this app:
    • Python 3.6 and above
  • For usage:
    • macOS, Linux, Windows
    • STM32CubeMX with desired downloaded frameworks (F0, F1, etc.)
    • Java CLI (JRE) (likely is already installed if the STM32CubeMX is working)
    • PlatformIO CLI (already presented if you have installed PlatformIO via some package manager or need to be installed as the "command line extension" from IDE)

A general recommendation there would be to test both CubeMX (code generation) and PlatformIO (project creation, building) at least once before using stm32pio to make sure that all tools work properly even without any "glue".

Installation

You can run the app in a portable way by downloading or cloning the snapshot of the repository and invoking the main script or Python module:

stm32pio-repo/ $   python3 stm32pio/app.py  # or
stm32pio-repo/ $   python3 -m stm32pio  # or
any-path/ $   python3 path/to/stm32pio-repo/stm32pio/app.py

(we assume python3 and pip3 hereinafter). It is possible to run the app like this from anywhere.

However, it's handier to install the utility to be able to run stm32pio from anywhere. Use

stm32pio-repo/ $   pip install wheel
stm32pio-repo/ $   python setup.py sdist bdist_wheel
stm32pio-repo/ $   pip install dist/stm32pio-X.XX-py3-none-any.whl

commands to launch the setup process. Now you can simply type stm32pio in the terminal to run the utility in any directory.

Finally, the PyPI distribution (starting from v0.95) is available:

$ pip install stm32pio

To uninstall in both cases run

$ pip uninstall stm32pio

Usage

Basically, you need to follow such a pattern:

  1. Create CubeMX project (.ioc file), set-up your hardware configuration, save with the compatible parameters
  2. Run the stm32pio that automatically invokes CubeMX to generate the code, creates PlatformIO project, patches a platformio.ini file and so on
  3. Work on the project in your editor as usual, compile/upload/debug etc.
  4. Edit the configuration in CubeMX when necessary, then run stm32pio to re-generate the code

Refer to Example section on more detailed steps. If you face off with some error try to enable a verbose output to get more information about a problem:

$ stm32pio -v [command] [options]

On the first run stm32pio will create a config file stm32pio.ini, syntax of which is similar to the platformio.ini. You can also create this config without any following operations by initializing the project:

$ stm32pio init -d path/to/project

It may be useful to tweak some parameters before proceeding. The structure of the config is separated in two sections: app and project. Options of the first one is related to the global settings such as commands to invoke different instruments though they can be adjusted on the per-project base while the second section contains of project-related parameters. See comments in the settings.py file for parameters description.

You can always run

$ python app.py --help

to see help on available commands. Find the copy of its output on the project wiki page, also.

GUI from CLI

You can start the GUI version using gui subcommand and pass some of the arguments to it:

$ stm32pio gui -d ./sample-project -b discovery_f4

Project patching

Note, that the patch operation (which takes the CubeMX code and PlatformIO project to the compliance) erases all the comments (lines starting with ;) inside the platformio.ini file. They are not required anyway, in general, but if you need them for some reason please consider to save the information somewhere else.

For those who want to modify the patch (default one is at settings.py, project one in a config file stm32pio.ini): it can has a general-form .INI content so it is possible to specify several sections and apply composite patches. This works totally fine for the most cases except, perhaps, some really big complex patches involving, say, the parameters interpolation feature. It is turned off for both platformio.ini and user's patch parsing by default. If there are some problems you've met due to a such behavior please modify the source code to match the parameters interpolation kind for the configs you need to. Seems like platformio.ini uses ExtendedInterpolation for its needs, by the way.

Embedding

You can also use stm32pio as an ordinary Python package and embed it in your own application. Find the minimal example at the examples to see some possible ways of implementing this. Basically, you need to import stm32pio.lib module (where the main Stm32pio class resides), (optionally) set up a logger and you are good to go. If you prefer higher-level API similar to the CLI version, use main() function in app.py passing the same CLI arguments to it (except the actual script name). Also, take a look at the CLI (app.py) or GUI versions.

Example

  1. Run CubeMX, choose MCU/board, do all necessary tweaking
  2. Select Project Manager -> Project tab, specify "Project Name", choose "Other Toolchains (GPDSC)". In Code Generator tab check "Copy only the necessary library files" and "Generate periphery initialization as a pair of '.c/.h' files per peripheral" options

Code Generator tab

  1. Back in the first tab (Project) copy the "Toolchain Folder Location" string (you maybe not be able to copy it in modern CubeMX versions so use terminal or file manager to do this). Save the project, close CubeMX

Project tab

  1. Use a copied string (project folder) as a -d argument for stm32pio (can be omitted if your current working directory is already a project directory).
  2. Run platformio boards (pio boards) or go to boards to list all supported devices. Pick one and use its ID as a -b argument (for example, nucleo_f031k6)
  3. All done! You can now run
    $ stm32pio new -d path/to/cubemx/project/ -b nucleo_f031k6 --start-editor=code --with-build
    
    to trigger the code generation, compile the project and start the VSCode editor with opened folder (last 2 options are given as an example and they are not required). Make sure you have all the tools in PATH (java (or set its path in stm32pio.ini), platformio, python, editor). You can use a slightly shorter form if you are already located in the project directory:
    path/to/cubemx/project/ $   stm32pio new -b nucleo_f031k6
    
  4. To get the information about the current state of the project use status command.
  5. If you will be in need to update hardware configuration in the future, make all the necessary stuff in CubeMX and run generate command in a similar way:
    $ stm32pio generate -d /path/to/cubemx/project
    
  6. To clean-up the folder and keep only the .ioc file run clean command.

Testing

There are some tests in file test.py (based on the unittest module). Run

stm32pio-repo/ $   python -m unittest -b -v

to test the app. It uses STM32F0 framework to generate and build a code from the test stm32pio-test-project.ioc project file. Please make sure that the test project folder is clean (i.e. contains only an .ioc file) before running the test otherwise it can lead to some cases failing. Tests automatically create temporary directory (using tempfile Python standard module) where all actions are performed.

For the specific test suite or case you can use

stm32pio-repo/ $   python -m unittest tests.test_integration.TestIntegration -b -v
stm32pio-repo/ $   python -m unittest tests.test_cli.TestCLI.test_verbosity -b -v

Restrictions

  • The tool doesn't check for different parameters compatibility, e.g. CPU frequency, memory sizes and so on. It simply eases your workflow with these 2 programs (PlatformIO and STM32CubeMX) a little bit.
  • CubeMX middlewares are not supported yet because it's hard to be prepared for every possible configuration. You need to manually adjust them to build appropriately. For example, FreeRTOS can be added via PlatformIO' lib feature or be directly compiled in its own directory using lib_extra_dirs option:
    lib_extra_dirs = Middlewares/Third_Party/FreeRTOS
    
    You also need to move all .c/.h files to the appropriate folders respectively. See PlatformIO documentation for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stm32pio-1.30.tar.gz (63.5 kB view hashes)

Uploaded Source

Built Distribution

stm32pio-1.30-py3-none-any.whl (54.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page