Skip to content

CalinRadoni/rust-stm32-container

Repository files navigation

rust-stm32-container

This container is for programming STM32 controllers in Rust.

The variants are:

  • rust-stm32:buster
  • rust-stm32:slim
  • rust-stm32:alpine is removed until the segfault from cargo generate is solved

from docker.io/library/rust alpine, buster and slim tags.

It adds to the official Rust image:

  • gdb-multiarch
  • openocd
  • cargo-binutils
  • cargo-generate
  • cargo-flash
  • llvm-tools-preview rustup component

and these rustup targets:

  • thumbv6m-none-eabi for Cortex-M0 and Cortex-M0+
  • thumbv7m-none-eabi for Cortex-M3
  • thumbv7em-none-eabi for Cortex-M4 and Cortex-M7 (no FPU)
  • thumbv7em-none-eabihf for Cortex-M4F and Cortex-M7F (with FPU)

Builder

You need buildah or docker to build them.

Build the containers locally with the create-variants.sh script:

  • run create-variants.sh without parameters to create the Dockerfiles
  • run create-variants.sh -b to build them
  • run create-variants.sh -l to list generated containers

To build a single container call buildah or podman directly:

# for Docker use:
docker build --tag calinradoni/rust-stm32:buster buster

# for Podman use:
buildah build-using-dockerfile --tag calinradoni/rust-stm32:buster buster

You can also get them from Docker Hub .

Usage

Read about CARGO HOME environment variable: CARGO HOME functions as a download and source cache. When building a crate, Cargo stores downloaded build dependencies in the Cargo home.

Note: I use Podman. To use Docker just replace podman with docker in examples.

Simple usage

The following code will mount current directory as the /source directory in the container.

Go to your project directory then start the container:

podman run --rm -it \
    --env USER=$USER \
    --volume $PWD:/source \
    --workdir /source \
    calinradoni/rust-stm32:buster

In this case CARGO HOME is inside the container and is not persistent. After every start of the container, to build the crates, cargo build will have to download and compile build dependencies.

Without connected board

To persist the CARGO HOME, rust's download and source cache, I use the ~/.cargo directory on the host. Make sure you have this directory or create it:

mkdir -p ~/.cargo

before starting the container with:

podman run --rm -it \
    --env USER=$USER \
    --env CARGO_HOME=/cargo \
    --volume $PWD:/source \
    --volume $HOME/.cargo:/cargo \
    --workdir /source \
    calinradoni/rust-stm32:buster

With connected board

ST-LINK access permissions

You need to set some udev permissions to be able to access the ST-LINK from a non-root account.

See the better approach from my Non-root access for ST-LINK and USB-to-serial devices article or use the quick procedure from this section.

On the host, create and execute this script:

#!/bin/bash

sudo tee /etc/udev/rules.d/70-st-link.rules > /dev/null <<'EOF'
# ST-LINK V2
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv2_%n"

# ST-LINK V2.1
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv2-1_%n"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3752", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv2-1_%n"

# ST-LINK V3
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374d", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv3loader_%n"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374e", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv3_%n"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374f", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv3_%n"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3753", MODE="600", TAG+="uaccess", SYMLINK+="stlinkv3_%n"
EOF

sudo udevadm control --reload-rules

If a ST-LINK board was plugged, unplug it then plug it again.

Usage

With a board connected through a ST-LINK interface, run ls /dev/stlink* to find the corresponding device then start the container with the --device option.

Example for a ST-LINK V2 interface:

$ ls /dev/stlink*
/dev/stlinkv2_5

start the container with:

podman run --rm -it \
    --env USER=$USER \
    --env CARGO_HOME=/cargo \
    --volume $PWD:/source \
    --volume $HOME/.cargo:/cargo \
    --workdir /source \
    --device=/dev/stlinkv2_5 \
    calinradoni/rust-stm32:buster

License

This is available as open source under the terms of the MIT License.

Releases

No releases published

Packages

No packages published