Skip to content

Commit 60d7c53

Browse files
committed
samples: embassy: Start of an embassy demo
This demo shows shows a few async tasks that are coordinated using the Embassy embassy-thread executor. Signed-off-by: David Brown <[email protected]>
1 parent 1540e9a commit 60d7c53

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

samples/embassy/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(embassy_on_zephyr)
8+
rust_cargo_application()

samples/embassy/Cargo.toml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
# This must be rustapp for now.
6+
name = "rustapp"
7+
version = "0.1.0"
8+
edition = "2021"
9+
description = "A sample hello world application in Rust"
10+
license = "Apache-2.0 or MIT"
11+
12+
[lib]
13+
crate-type = ["staticlib"]
14+
15+
[dependencies]
16+
zephyr = { version = "0.1.0", features = ["time-driver"] }
17+
log = "0.4.22"
18+
static_cell = "2.1"
19+
20+
[dependencies.embassy-executor]
21+
version = "0.7.0"
22+
# path = "../../embassy/embassy-executor"
23+
features = [
24+
"log",
25+
"task-arena-size-1024",
26+
"arch-cortex-m",
27+
"executor-thread",
28+
]
29+
30+
[dependencies.embassy-futures]
31+
version = "0.1.1"
32+
# path = "../../embassy/embassy-futures"
33+
34+
[dependencies.embassy-sync]
35+
version = "0.6.2"
36+
# path = "../../embassy/embassy-sync"
37+
38+
[dependencies.embassy-time]
39+
version = "0.4.0"
40+
# path = "../../embassy/embassy-time"
41+
# This is board specific.
42+
features = ["tick-hz-10_000"]
43+
44+
[dependencies.critical-section]
45+
version = "1.2"
46+
47+
[profile.dev]
48+
opt-level = 1
49+
50+
[profile.release]
51+
debug = true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This board doesn't have a serial console, so use RTT.
5+
CONFIG_UART_CONSOLE=n
6+
CONFIG_RTT_CONSOLE=y
7+
CONFIG_USE_SEGGER_RTT=y

samples/embassy/prj.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_DEBUG=y
5+
6+
# The default 1k stack isn't large enough for rust string formatting with logging.
7+
CONFIG_MAIN_STACK_SIZE=4096
8+
9+
CONFIG_RUST=y
10+
11+
CONFIG_RUST_ALLOC=y
12+
# CONFIG_LOG=y
13+
14+
CONFIG_LOG_BACKEND_RTT=n

samples/embassy/rpi_pico.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This board doesn't have a serial console, so use RTT.
5+
CONFIG_UART_CONSOLE=n
6+
CONFIG_RTT_CONSOLE=y
7+
CONFIG_USE_SEGGER_RTT=y

samples/embassy/sample.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sample:
2+
description: Embassy Hello
3+
name: hello and basics from Embassy
4+
common:
5+
harness: console
6+
harness_config:
7+
type: one_line
8+
regex:
9+
- "Hello world from Rust on (.*)"
10+
tags: rust
11+
filter: CONFIG_RUST_SUPPORTED
12+
platform_allow:
13+
- qemu_cortex_m0
14+
- qemu_cortex_m3
15+
- qemu_riscv32
16+
- qemu_riscv64
17+
- nrf52840dk/nrf52840
18+
tests:
19+
sample.rust.helloworld:
20+
tags: introduction

samples/embassy/src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2024 Linaro LTD
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#![no_std]
5+
6+
use embassy_executor::{Executor, Spawner};
7+
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
8+
use embassy_time::{Duration, Timer};
9+
use log::info;
10+
use static_cell::StaticCell;
11+
12+
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
13+
14+
#[no_mangle]
15+
extern "C" fn rust_main() {
16+
unsafe {
17+
zephyr::set_logger().unwrap();
18+
}
19+
20+
info!("Hello world from Rust on {}", zephyr::kconfig::CONFIG_BOARD);
21+
22+
let executor = EXECUTOR_LOW.init(Executor::new());
23+
executor.run(|spawner| {
24+
spawner.spawn(sample_task(spawner)).unwrap();
25+
})
26+
}
27+
28+
static CHAN: Channel<CriticalSectionRawMutex, usize, 1> = Channel::new();
29+
30+
#[embassy_executor::task]
31+
async fn sample_task(spawner: Spawner) {
32+
info!("Started once");
33+
spawner.spawn(other_task(spawner)).unwrap();
34+
loop {
35+
// Wait for a message.
36+
let msg = CHAN.receive().await;
37+
info!("main task got: {}", msg);
38+
}
39+
}
40+
41+
#[embassy_executor::task]
42+
async fn other_task(_spawner: Spawner) {
43+
info!("The other task");
44+
let mut count = 0;
45+
loop {
46+
CHAN.send(count).await;
47+
count = count.wrapping_add(1);
48+
49+
Timer::after(Duration::from_secs(1)).await;
50+
}
51+
}

0 commit comments

Comments
 (0)