Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embassy microcontroller example demonstrating async #7064

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/build_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ jobs:
api/node/docs
docs/site
- name: "Check for docs warnings in internal crates"
run: cargo doc --workspace --no-deps --all-features --exclude slint-node --exclude pyslint --exclude mcu-board-support --exclude printerdemo_mcu --exclude carousel --exclude test-* --exclude plotter --exclude uefi-demo --exclude ffmpeg --exclude gstreamer-player --exclude slint-cpp --exclude slint-python
run: cargo doc --workspace --no-deps --all-features --exclude slint-node --exclude pyslint --exclude mcu-board-support --exclude mcu-embassy --exclude printerdemo_mcu --exclude carousel --exclude test-* --exclude plotter --exclude uefi-demo --exclude ffmpeg --exclude gstreamer-player --exclude slint-cpp --exclude slint-python
- name: Clean cache # Don't cache docs to avoid them including removed classes being published
run: |
rm -rf target/doc target/cppdocs api/node/docs
Expand Down
18 changes: 16 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ jobs:
cargo update -p home --precise 0.5.9
fi
- name: Run tests (not qt)
run: cargo test --verbose --all-features --workspace ${{ matrix.extra_args }} --exclude slint-node --exclude pyslint --exclude test-driver-node --exclude slint-node --exclude test-driver-nodejs --exclude test-driver-cpp --exclude mcu-board-support --exclude printerdemo_mcu --exclude uefi-demo --exclude slint-cpp --exclude slint-python -- --skip=_qt::t
run: cargo test --verbose --all-features --workspace ${{ matrix.extra_args }} --exclude slint-node --exclude pyslint --exclude test-driver-node --exclude slint-node --exclude test-driver-nodejs --exclude test-driver-cpp --exclude mcu-board-support --exclude mcu-embassy --exclude printerdemo_mcu --exclude uefi-demo --exclude slint-cpp --exclude slint-python -- --skip=_qt::t
env:
SLINT_CREATE_SCREENSHOTS: 1
shell: bash
- name: Run tests (qt)
run: cargo test --verbose --all-features --workspace ${{ matrix.extra_args }} --exclude slint-node --exclude pyslint --exclude test-driver-node --exclude slint-node --exclude test-driver-nodejs --exclude test-driver-cpp --exclude mcu-board-support --exclude printerdemo_mcu --exclude uefi-demo --exclude slint-cpp --exclude slint-python --bin test-driver-rust -- _qt --test-threads=1
run: cargo test --verbose --all-features --workspace ${{ matrix.extra_args }} --exclude slint-node --exclude pyslint --exclude test-driver-node --exclude slint-node --exclude test-driver-nodejs --exclude test-driver-cpp --exclude mcu-board-support --exclude mcu-embassy --exclude printerdemo_mcu --exclude uefi-demo --exclude slint-cpp --exclude slint-python --bin test-driver-rust -- _qt --test-threads=1
shell: bash
- name: Archive screenshots after failed tests
if: ${{ failure() }}
Expand Down Expand Up @@ -384,6 +384,20 @@ jobs:
- name: Check
run: cargo check --target=${{matrix.target}} -p printerdemo_mcu --no-default-features --features=mcu-board-support/${{matrix.feature}} --release

# test to compile the mcu backend for the arm target (no_std) using embassy
mcu-embassy:
env:
SLINT_FONT_SIZES: 8,11,10,12,13,14,15,16,18,20,22,24,32
RUSTFLAGS: --cfg slint_int_coord -D warnings
CARGO_PROFILE_DEV_DEBUG: 0
CARGO_PROFILE_RELEASE_OPT_LEVEL: s
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-rust
- name: Check
run: cargo check -p mcu_embassy --bin ui_mcu --target=thumbv8m.main-none-eabihf --no-default-features --features="mcu-embassy/mcu" --release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reasons I don't quite understand, the CI didn't run on the latest. But I ran it manually and noticed this error:

error: cannot specify features for packages outside of workspace

I tried locally, could reproduce it, and I think this is the easy fix:

Suggested change
- name: Check
run: cargo check -p mcu_embassy --bin ui_mcu --target=thumbv8m.main-none-eabihf --no-default-features --features="mcu-embassy/mcu" --release
- name: Check
working-directory: examples/mcu-embassy
run: cargo check --bin ui_mcu --target=thumbv8m.main-none-eabihf --no-default-features --features="mcu-embassy/mcu" --release


mcu_esp:
env:
RUSTFLAGS: -D warnings
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
'examples/carousel/rust',
'demos/energy-monitor',
'demos/home-automation/rust',
'examples/mcu-embassy',
'examples/mcu-board-support',
'examples/uefi-demo',
'demos/weather-demo',
Expand Down
17 changes: 17 additions & 0 deletions examples/mcu-embassy/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
rustflags = [
"-C",
"link-arg=--nmagic",
"-C",
"link-arg=-Tlink.x",
"-C",
"link-arg=-Tdefmt.x",
]
runner = "probe-rs run --chip STM32U5G9ZJTxQ"

[build]
target = "thumbv8m.main-none-eabihf"

[env]
DEFMT_LOG = "info"
RUST_LOG = "info"
9 changes: 9 additions & 0 deletions examples/mcu-embassy/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// uncomment this to have rust-analyzer work with mcu related code
"rust-analyzer.cargo.features": [
"mcu",
//"simulator",
],
"rust-analyzer.check.allTargets": false,
"rust-analyzer.showUnlinkedFileNotification": false,
}
115 changes: 115 additions & 0 deletions examples/mcu-embassy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright © 2025 David Haig
# SPDX-License-Identifier: MIT

ninjasource marked this conversation as resolved.
Show resolved Hide resolved
[package]
name = "mcu-embassy"
version = "0.1.0"
edition = "2021"
authors = ["David Haig <[email protected]>"]
readme = "README.md"
license = "MIT"
resolver = "2"
publish = false
repository = "https://github.com/slint-ui/slint"
homepage = "https://slint.dev"

[dependencies]
log = "0.4"
defmt = { version = "0.3", optional = true, features = ["alloc"] }
defmt-rtt = { version = "0.4", optional = true }
panic-probe = { version = "0.3", optional = true, features = ["print-defmt"] }
cortex-m = { version = "0.7.7", optional = true, features = [
"inline-asm",
"critical-section-single-core",
] }
cortex-m-rt = { version = "0.7.3", optional = true }
slint-generated = { path = "./slint_generated" }
embedded-alloc = { version = "0.5", optional = true }
heapless = { version = "0.8", default-features = false, features = [
"defmt-03",
] }
tinybmp = { version = "0.5" }
static_cell = { version = "2", optional = true }

#slint = { version = "1.9.1", default-features = false, features = [
# "compat-1-2",
# "unsafe-single-threaded",
# "libm",
#] }

slint = { path = "../../api/rs/slint", default-features = false, features = [
"compat-1-2",
"unsafe-single-threaded",
"libm",
] }

embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "72976fe", optional = true, features = [
"stm32u5g9zj",
"time-driver-tim2",
"exti",
"memory-x",
"unstable-pac",
"chrono",
"time",
"defmt",
] }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "72976fe" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "72976fe", features = [
"task-arena-size-32768",
"executor-thread",
"integrated-timers",
] }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "72976fe" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "72976fe" }

env_logger = { version = "0.9.0", optional = true }
sdl2 = { git = "https://github.com/Rust-SDL2/rust-sdl2.git", rev = "400e033", optional = true }
object-pool = { version = "0.6.0", optional = true }
embedded-hal = { version = "1.0.0", optional = true }
gt911 = { version = "0.1", features = ["defmt"], optional = true }

[features]
default = ["mcu"]
mcu = [
"defmt",
"defmt-rtt",
"panic-probe",
"cortex-m",
"cortex-m-rt",
"embassy-stm32",
"embassy-sync/defmt",
"embassy-executor/arch-cortex-m",
"embassy-executor/executor-interrupt",
"embassy-executor/defmt",
"embassy-time/tick-hz-32_768",
"embassy-time/defmt",
"embassy-time/defmt-timestamp-uptime",
"embedded-alloc",
"embedded-hal",
"gt911",
]

simulator = [
"slint/renderer-software",
"slint/backend-winit",
"slint/std",
"embassy-executor/arch-std",
"embassy-time/std",
"env_logger",
"sdl2",
"object-pool",
"static_cell",
]

[profile.release]
debug = true # required for decent panic messages and log line locations
opt-level = "s"
lto = "thin"

[[bin]]
name = "ui_mcu"
required-features = ["mcu"]

[[bin]]
name = "ui_simulator"
required-features = ["simulator"]
88 changes: 88 additions & 0 deletions examples/mcu-embassy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Embassy Slint stm32u5g9j-dk2 Demo

An embedded async Slint GUI demo using Embassy and an stm32u5g9j-dk2 development kit. This demo was written to run on a resource constrained device, not a PC or laptop.
The simulator can run on a PC if you do not have the dev kit on hand but it is not meant to be a reference design for an async GUI implementation on a PC.

The stm32u5g9j-dk2 was chosen because of its availability and price point and has enough onboard ram (3MB) and flash (4MB) to run Slint without external psram and flash, reducing setup complexity.
It comes with a 5" 800x480 IPS touchscreen display. Async is useful for building more complex UIs because you don't have to hand code your own state machines.

Things that are demonstrated here:
- Sending rendered display buffer to LCD screen asynchronously freeing up the mcu to do other things
- Responding to hardware events (pressing the USER button on the DK2 changes the colour of the grey circle to blue)
- Touchscreen actions setting physical hardware (toggling the switch on the touchscreen to turn on the green led on the DK2)
- Cooperative multitasking (red led continues to flash on a separate task regardless of UI actions)
- UI animations work
- The application can be simulated on a PC without having to download to the DK2 every time you want to test something

# Installation instructions

Install the cross compilation target for the mcu:

```bash
rustup target add thumbv8m.main-none-eabihf
```

You need software to be able to flash the firmware to the dev kit.

```bash
cargo install --force --locked probe-rs-tools
```

# Running the application

Plug a usbc cable into the ST-LINK port on the dk2 and run the following:

```bash
cargo run --bin ui_mcu --release --features=mcu
```

Troubleshooting:

If you are getting some complication errors from cortex-m like "error: invalid register `r1`: unknown register" make sure that you are cross compiling for the correct cpu target:

You can specify the target in the cargo run command in the following file:

In `.cargo/Cargo.toml`
```toml
[build]
target = "thumbv8m.main-none-eabihf"
```

If using vscode then make sure `rust-analyzer.cargo.features` is set to `mcu` in `.vscode/settings.json`

You may be wondering why you get the following message in the logs: `invalid location: defmt frame-index`
In the Slint workspace `Cargo.toml` file overrides the `Cargo.toml` file in this crate so make sure the release profile is as follows in that workspace file:
```toml
[profile.release]
debug = true # required for decent panic messages and log line locations
opt-level = "s"
lto = "thin"
```

# Running the simulator

Of course you can use Slint's vscode plugin to preview slint files but you may want to actually run your application and simulate the hardware interactions.
The simulator runs Embassy on the host machine (instead of on an mcu) and renders to the screen using the sdl2 library.
Hardware like leds and buttons are emulated in the hardware module.

To install SDL2 follow the instructions here: https://github.com/Rust-SDL2/rust-sdl2

To run the simulator:
```bash
cargo run --bin ui_simulator --release --features=simulator
```


Troubleshooting:

If you are getting some compilation errors from arrayvec like "error: requires `sized` lang_item" make sure you are NOT targeting the mcu when building for your pc.

Set the target correctly in the command line or comment out the following:

In `.cargo/Cargo.toml`
```toml
#[build]
#target = "thumbv8m.main-none-eabihf"
```

If using vscode then make sure `rust-analyzer.cargo.features` is set to `simulator` in `.vscode/settings.json`
39 changes: 39 additions & 0 deletions examples/mcu-embassy/slint_generated/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright © 2025 David Haig
# SPDX-License-Identifier: MIT

[package]
name = "slint-generated"
version = "0.1.0"
edition = "2021"
build = "build.rs"
authors = ["David Haig <[email protected]>"]
readme = "README.md"
resolver = "2"
license = "MIT"
publish = false
repository = "https://github.com/slint-ui/slint"
homepage = "https://slint.dev"

[dependencies]
slint = { path = "../../../api/rs/slint", default-features = false, features = [
"compat-1-2",
"unsafe-single-threaded",
"libm",
"renderer-software",
] }
i-slint-core-macros = { path = "../../../internal/core-macros" }

[build-dependencies]
slint-build = { path = "../../../api/rs/build" }

#[dependencies]
#slint = { version = "1.9.1", default-features = false, features = [
# "compat-1-2",
# "unsafe-single-threaded",
# "libm",
# "renderer-software",
#] }
#i-slint-core-macros = { version = "1.9.1" }
#
#[build-dependencies]
#slint-build = { version = "1.9.1" }
3 changes: 3 additions & 0 deletions examples/mcu-embassy/slint_generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated

This crate is here to separate the `.slint` file compilation from the main application.
9 changes: 9 additions & 0 deletions examples/mcu-embassy/slint_generated/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright © 2025 David Haig
// SPDX-License-Identifier: MIT

fn main() {
let config = slint_build::CompilerConfiguration::new()
.embed_resources(slint_build::EmbedResourcesKind::EmbedForSoftwareRenderer);
slint_build::compile_with_config("../ui/main.slint", config).unwrap();
slint_build::print_rustc_flags().unwrap();
}
6 changes: 6 additions & 0 deletions examples/mcu-embassy/slint_generated/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright © 2025 David Haig
// SPDX-License-Identifier: MIT

#![no_std]

slint::include_modules!();
Loading
Loading