-
Notifications
You must be signed in to change notification settings - Fork 628
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
ninjasource
wants to merge
11
commits into
slint-ui:master
Choose a base branch
from
ninjasource:add-embassy-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,385
−3
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
172bb20
Add embassy microcontroller example demonstrating async
ninjasource fbbb181
Fixed some CI issues
ninjasource 5308ba3
Added mcu-embassy tests to CI
ninjasource 5a091fe
Attempt to fix CI issue with mcu-embassy check
ninjasource 30ebef3
Updated copyright notice and embassy dependency
ninjasource af1a064
Fixed up dependencies
ninjasource 0e4a72a
Further attempts with dependencies
ninjasource 75b05ea
Update readme with latest probe-rs install instructions
ninjasource 16af3e5
Fixed issue with conflicting embassy-time-driver
ninjasource e02bfc1
Fix continuous integration failure
ninjasource efa3e8e
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# 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" | ||
resolver = "2" | ||
license = "MIT" | ||
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", | ||
] } | ||
static_cell = { version = "2", optional = true } | ||
|
||
slint = { version = "=1.9.0", path = "../../api/rs/slint", default-features = false, features = [ | ||
ninjasource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"compat-1-2", | ||
"unsafe-single-threaded", | ||
"libm", | ||
"renderer-software", | ||
] } | ||
i-slint-core-macros = { version = "=1.9.0", path = "../../internal/core-macros" } | ||
ninjasource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "4790f8f", optional = true, features = [ | ||
"stm32u5g9zj", | ||
"time-driver-tim2", | ||
"exti", | ||
"memory-x", | ||
"unstable-pac", | ||
"chrono", | ||
"time", | ||
] } | ||
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "4790f8f", optional = true } | ||
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "4790f8f", optional = true, features = [ | ||
"task-arena-size-32768", | ||
"arch-cortex-m", | ||
"executor-thread", | ||
] } | ||
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "4790f8f", optional = true } | ||
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "4790f8f", optional = true } | ||
|
||
env_logger = { version = "0.9.0", optional = true } | ||
sdl2 = { git = "https://github.com/Rust-SDL2/rust-sdl2", 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-stm32/defmt", | ||
"embassy-sync", | ||
"embassy-sync/defmt", | ||
"embassy-executor", | ||
"embassy-executor/arch-cortex-m", | ||
"embassy-executor/executor-interrupt", | ||
"embassy-executor/defmt", | ||
"embassy-time", | ||
"embassy-time/tick-hz-32_768", | ||
"embassy-time/defmt", | ||
"embassy-time/defmt-timestamp-uptime", | ||
"embassy-futures", | ||
"embedded-alloc", | ||
"embedded-hal", | ||
"gt911", | ||
] | ||
|
||
simulator = [ | ||
"slint/renderer-software", | ||
"slint/backend-winit", | ||
"slint/std", | ||
"embassy-executor/arch-std", | ||
"embassy-time/std", | ||
"embassy-futures", | ||
"embassy-time", | ||
"embassy-time/std", | ||
"embassy-sync", | ||
"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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. At the time of writing (7 Dec 2024) you need the latest version of probe-rs to flash to STM32U5G9ZJTxQ, not the one in crates.io. | ||
|
||
```bash | ||
cargo install --force --locked --git https://github.com/probe-rs/probe-rs 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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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 = { version = "=1.9.0", path = "../../../api/rs/slint", default-features = false, features = [ | ||
"compat-1-2", | ||
"unsafe-single-threaded", | ||
"libm", | ||
"renderer-software", | ||
] } | ||
i-slint-core-macros = { version = "=1.9.0", path = "../../../internal/core-macros" } | ||
|
||
[build-dependencies] | ||
slint-build = { version = "=1.9.0", path = "../../../api/rs/build" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
I tried locally, could reproduce it, and I think this is the easy fix: