Skip to content
adam3141Public

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

stem

stem creates, configures, builds, flashes and simulates STM32 firmware projects. Each project carries its own Docker/VS Code dev container with the whole toolchain, so the host needs only Python, git and Docker.

$ stem create blinky        # new project with a dev container
$ cd blinky
$ stem configure            # menuconfig-style UI: pick the MCU, HAL/LL, ...
$ stem build                # CMake + Ninja, inside the container
$ stem flash                # OpenOCD
$ stem simulate             # Renode (or QEMU), no hardware needed

What stem does for you:

  • No per-project setup. CMSIS headers and the Cube HAL/LL drivers are cloned on demand from ST's GitHub repositories, pinned by tag in a manifest, into a cache (~/.stem/packs) shared by all projects.
  • Generated startup code and linker script for the selected part, from a bundled database of 1421 MCUs across the C0, F0, F1, F2, F3, F4, F7, G0, G4, H5, H7, L0, L1, L4, L5, U0, U5, WB and WL series.
  • A CMake package (find_package(Stem)), registered in ~/.cmake/packages the way Zephyr does it, providing stem_add_executable() and stem_add_library().
  • A dev container (Ubuntu 26.04, non-root user) with Arm's official Arm GNU Toolchain 15.3.rel2 (arm-none-eabi GCC and GDB), CMake, Ninja, OpenOCD, Renode and QEMU, plus the VS Code C/C++, CMake Tools and Cortex-Debug extensions.

Install

stem needs Python 3.11 or newer and has no runtime dependencies. With pipx, which gives it its own virtual environment:

$ pipx install git+https://github.com/adam3141/stem.git

or from a clone: pipx install .

On the host you also need git and Docker. To flash from a Linux host, install the udev rules for your debug probe (for example the stlink-tools or openocd package) so that your user can open it.

Using it

Create

stem create NAME makes the directory NAME in the current directory:

NAME/
  .devcontainer/      Dockerfile and devcontainer.json for the project's image
  .vscode/            settings, tasks, recommended extensions
  CMakeLists.txt      yours to edit
  CMakePresets.json
  src/main.c
  stem.toml           the project configuration

Settings can be given straight away, e.g. stem create blinky --mcu STM32F407VG --drivers ll.

Configure

stem configure with no flags opens the curses menu. Every setting is also available from the command line, for scripts and other tools:

$ stem configure --mcu STM32G071RB --drivers hal --type application
$ stem configure --set build.stack_size=0x800
$ stem configure --show --json        # current values
$ stem configure --options --json     # every setting, with types and choices
$ stem list-mcus 'F4*VG'              # search the device database (--json too)

The configuration lives in stem.toml. From it stem writes .stem/: the linker script, the startup assembly, an OpenOCD configuration and the CMake variables. .stem/ is regenerated whenever stem.toml changes, including when CMake is run by VS Code rather than by stem build.

Build

$ stem build                    # outputs build/NAME.elf, .bin, .hex, .map
$ stem build --configure-only   # just CMake configure: build/compile_commands.json
$ stem build --clean --build-type Release

Run on the host, build, flash and simulate build the project's image if needed and run inside it. Inside the dev container they run directly. --native (or STEM_NATIVE=1) uses tools installed on the machine instead.

Flash and debug

stem flash programs build/NAME.elf with OpenOCD using the probe set in stem.toml (stlink, jlink or cmsis-dap). In VS Code, the generated .vscode/launch.json starts a Cortex-Debug session over OpenOCD.

USB probes are passed into the container through /dev/bus/usb, which exists on Linux hosts only.

Simulate

stem simulate runs the firmware in Renode where Renode has a platform for the part, and stem simulate --gdb starts halted with a GDB server on port 3333. stem simulate --list shows which MCUs can be simulated.

CMake

The root CMakeLists.txt of a project is short:

cmake_minimum_required(VERSION 3.25)
find_package(Stem REQUIRED)          # before project(): selects the toolchain
project(blinky LANGUAGES C CXX ASM)

stem_add_executable(${PROJECT_NAME}
    src/main.c
)

stem_add_executable() links the startup code, linker script, CMSIS and the selected drivers, and adds flash and size targets. stem_add_library() builds a library with the MCU's flags and no startup code; an application can pull one in with add_subdirectory(). #include <stem.h> brings in the right device header for whichever MCU is configured.

Outside the container, register the package once with stem cmake-export.

Update stem in a project

The dev container installs stem from GitHub at the release recorded in .devcontainer/devcontainer.json (build.args.STEM_VERSION). stem create records the release of the stem that created the project. To move a project to another release:

$ stem update                   # the latest release
$ stem update --version v0.1.1  # a specific release (a branch or commit works too)

On the host this changes the recorded release; the next stem build, or Rebuild Container in VS Code, rebuilds only the image's stem layer. Inside the dev container it also installs that release immediately. Host builds warn when the project's release differs from the host's own stem. To move the host's stem to a release, run pipx install --force git+https://github.com/adam3141/stem.git@vX.Y.Z.

AI coding tools in the container

stem create looks for AI coding tools you already use on the host (Claude Code, Gemini CLI, Codex, Copilot CLI, Cursor, opencode, Qwen Code, Aider, Goose, Amazon Q, Amp, Continue). For each one found it mounts the tool's credential directory into the dev container, so you do not log in again after every rebuild, and sets the Dockerfile build argument (INSTALL_CLAUDE_CODE, INSTALL_GEMINI_CLI, ...) that installs its CLI in the image. API-key variables such as ANTHROPIC_API_KEY are forwarded by reference; their values are never written to a file.

$ stem ai list                  # known tools and what this host has
$ stem create blinky --ai none  # or --ai claude,codex
$ stem ai enable codex          # change an existing project

Some tools keep their login in the OS keychain rather than in files; stem ai list notes where a mount alone cannot carry the login.

Limitations

  • Dual-core H7 parts, STM32MP1 and TrustZone secure/non-secure split images are not supported. WL54/WL55 are built for the Cortex-M4 core only.

Developing stem

$ python3 -m venv .venv
$ .venv/bin/pip install -e '.[dev]'
$ .venv/bin/pytest -m "not network"
$ .venv/bin/ruff check && .venv/bin/ruff format --check

tools/gen_devicedb.py regenerates the bundled device database and tools/validate_vectors.py compares the generated vector tables with ST's own startup files.

Licence and attribution

stem is released under the MIT licence.

The device database is derived from the Embassy project's stm32-data (MIT or Apache-2.0) and from STMicroelectronics' CMSIS device packages (Apache-2.0); see src/stem/data/NOTICE. ST's CMSIS and HAL/LL sources are not bundled; they are downloaded from ST's repositories under their own licences.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages