aboutsummaryrefslogtreecommitdiff
path: root/docs/modules
diff options
context:
space:
mode:
Diffstat (limited to 'docs/modules')
-rw-r--r--docs/modules/ROOT/nav.adoc7
-rw-r--r--docs/modules/ROOT/pages/best_practices.adoc53
-rw-r--r--docs/modules/ROOT/pages/embassy_in_the_wild.adoc9
-rw-r--r--docs/modules/ROOT/pages/faq.adoc20
-rw-r--r--docs/modules/ROOT/pages/new_project.adoc178
5 files changed, 264 insertions, 3 deletions
diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc
index 8d7f6f411..fabb80e31 100644
--- a/docs/modules/ROOT/nav.adoc
+++ b/docs/modules/ROOT/nav.adoc
@@ -1,6 +1,8 @@
1* xref:getting_started.adoc[Getting started] 1* xref:getting_started.adoc[Getting started]
2** xref:basic_application.adoc[Basic application] 2** xref:basic_application.adoc[Basic application]
3** xref:project_structure.adoc[Project Structure] 3** xref:project_structure.adoc[Project Structure]
4** xref:new_project.adoc[Starting a new Embassy project]
5** xref:best_practices.adoc[Best Practices]
4* xref:layer_by_layer.adoc[Bare metal to async] 6* xref:layer_by_layer.adoc[Bare metal to async]
5* xref:runtime.adoc[Executor] 7* xref:runtime.adoc[Executor]
6* xref:delaying_a_task.adoc[Delaying a Task] 8* xref:delaying_a_task.adoc[Delaying a Task]
@@ -10,6 +12,7 @@
10* xref:bootloader.adoc[Bootloader] 12* xref:bootloader.adoc[Bootloader]
11 13
12* xref:examples.adoc[Examples] 14* xref:examples.adoc[Examples]
13* xref:developer.adoc[Developer] 15* xref:developer.adoc[Developer Docs]
14** xref:developer_stm32.adoc[Developer: STM32] 16** xref:developer_stm32.adoc[Developer Docs: STM32]
17* xref:embassy_in_the_wild.adoc[Embassy in the wild]
15* xref:faq.adoc[Frequently Asked Questions] 18* xref:faq.adoc[Frequently Asked Questions]
diff --git a/docs/modules/ROOT/pages/best_practices.adoc b/docs/modules/ROOT/pages/best_practices.adoc
new file mode 100644
index 000000000..1e02f9ba9
--- /dev/null
+++ b/docs/modules/ROOT/pages/best_practices.adoc
@@ -0,0 +1,53 @@
1= Best Practices
2
3Over time, a couple of best practices have emerged. The following list should serve as a guideline for developers writing embedded software in _Rust_, especially in the context of the _Embassy_ framework.
4
5== Passing Buffers by Reference
6It may be tempting to pass arrays or wrappers, like link:https://docs.rs/heapless/latest/heapless/[`heapless::Vec`], to a function or return one just like you would with a `std::Vec`. However, in most embedded applications you don't want to spend ressources on an allocator and end up placing buffers on the stack.
7This, however, can easily blow up your stack if you are not careful.
8
9Consider the following example:
10[,rust]
11----
12fn process_buffer(mut buf: [u8; 1024]) -> [u8; 1024] {
13 // do stuff and return new buffer
14 for elem in buf.iter_mut() {
15 *elem = 0;
16 }
17 buf
18}
19
20pub fn main() -> () {
21 let buf = [1u8; 1024];
22 let buf_new = process_buffer(buf);
23 // do stuff with buf_new
24 ()
25}
26----
27When calling `process_buffer` in your program, a copy of the buffer you pass to the function will be created,
28consuming another 1024 bytes.
29After the processing, another 1024 byte buffer will be placed on the stack to be returned to the caller.
30(You can check the assembly, there will be two memcopy operations, e.g., `bl __aeabi_memcpy` when compiling for a Cortex-M processor.)
31
32*Possible Solution:*
33
34Pass the data by reference and not by value on both, the way in and the way out.
35For example, you could return a slice of the input buffer as the output.
36Requiring the lifetime of the input slice and the output slice to be the same, the memory safetly of this procedure will be enforced by the compiler.
37
38[,rust]
39----
40fn process_buffer<'a>(buf: &'a mut [u8]) -> &'a mut[u8] {
41 for elem in buf.iter_mut() {
42 *elem = 0;
43 }
44 buf
45}
46
47pub fn main() -> () {
48 let mut buf = [1u8; 1024];
49 let buf_new = process_buffer(&mut buf);
50 // do stuff with buf_new
51 ()
52}
53----
diff --git a/docs/modules/ROOT/pages/embassy_in_the_wild.adoc b/docs/modules/ROOT/pages/embassy_in_the_wild.adoc
new file mode 100644
index 000000000..a1c31bfc7
--- /dev/null
+++ b/docs/modules/ROOT/pages/embassy_in_the_wild.adoc
@@ -0,0 +1,9 @@
1= Embassy in the wild!
2
3Here are known examples of real-world projects which make use of Embassy. Feel free to link:https://github.com/embassy-rs/embassy/blob/main/docs/modules/ROOT/pages/embassy_in_the_wild.adoc[add more]!
4
5* link:https://github.com/cbruiz/printhor/[Printhor: The highly reliable but not necessarily functional 3D printer firmware]
6** Targets some STM32 MCUs
7* link:https://github.com/card-io-ecg/card-io-fw[Card/IO firmware] - firmware for an open source ECG device
8** Targets the ESP32-S3 or ESP32-C6 MCU
9* The link:https://github.com/lora-rs/lora-rs[lora-rs] project includes link:https://github.com/lora-rs/lora-rs/tree/main/examples/stm32l0/src/bin[various standalone examples] for NRF52840, RP2040, STM32L0 and STM32WL
diff --git a/docs/modules/ROOT/pages/faq.adoc b/docs/modules/ROOT/pages/faq.adoc
index df3f66658..d1a012978 100644
--- a/docs/modules/ROOT/pages/faq.adoc
+++ b/docs/modules/ROOT/pages/faq.adoc
@@ -47,7 +47,8 @@ The first step to managing your binary size is to set up your link:https://doc.r
47debug = false 47debug = false
48lto = true 48lto = true
49opt-level = "s" 49opt-level = "s"
50incremental = true 50incremental = false
51codegen-units = 1
51---- 52----
52 53
53All of these flags are elaborated on in the Rust Book page linked above. 54All of these flags are elaborated on in the Rust Book page linked above.
@@ -135,3 +136,20 @@ embassy-time = { git = "https://github.com/embassy-rs/embassy.git", rev = "e5fdd
135---- 136----
136 137
137Note that the git revision should match any other embassy patches or git dependencies that you are using! 138Note that the git revision should match any other embassy patches or git dependencies that you are using!
139
140== How can I optimize the speed of my embassy-stm32 program?
141
142* Make sure RCC is set up to go as fast as possible
143* Make sure link:https://docs.rs/cortex-m/latest/cortex_m/peripheral/struct.SCB.html[flash cache] is enabled
144* build with `--release`
145* Set the following keys for the release profile in your `Cargo.toml`:
146 ** `opt-level = "s"`
147 ** `lto = "fat"`
148* Set the following keys in the `[unstable]` section of your `.cargo/config.toml`
149 ** `build-std = ["core"]`
150 ** `build-std-features = ["panic_immediate_abort"]`
151* Enable feature `embassy-time/generic-queue`, disable feature `embassy-executor/integrated-timers`
152* When using `InterruptExecutor`:
153 ** disable `executor-thread`
154 ** make `main`` spawn everything, then enable link:https://docs.rs/cortex-m/latest/cortex_m/peripheral/struct.SCB.html#method.set_sleeponexit[SCB.SLEEPONEXIT] and `loop { cortex_m::asm::wfi() }`
155 ** *Note:* If you need 2 priority levels, using 2 interrupt executors is better than 1 thread executor + 1 interrupt executor. \ No newline at end of file
diff --git a/docs/modules/ROOT/pages/new_project.adoc b/docs/modules/ROOT/pages/new_project.adoc
new file mode 100644
index 000000000..ce139ed8d
--- /dev/null
+++ b/docs/modules/ROOT/pages/new_project.adoc
@@ -0,0 +1,178 @@
1= Starting a new Embassy project
2
3Once you’ve successfully xref:getting_started.adoc[run some example projects], the next step is to make a standalone Embassy project. The easiest way to do this is to adapt an example for a similar chip to the one you’re targeting.
4
5As an example, let’s create a new embassy project from scratch for a STM32G474. The same instructions are applicable for any supported chip with some minor changes.
6
7Run:
8
9[source,bash]
10----
11cargo new stm32g474-example
12cd stm32g474-example
13----
14
15to create an empty rust project:
16
17[source]
18----
19stm32g474-example
20├── Cargo.toml
21└── src
22 └── main.rs
23----
24
25Looking in link:https://github.com/embassy-rs/embassy/tree/main/examples[the Embassy examples], we can see there’s a `stm32g4` folder. Find `src/blinky.rs` and copy its contents into our `src/main.rs`.
26
27== .cargo/config.toml
28
29Currently, we’d need to provide cargo with a target triple every time we run `cargo build` or `cargo run`. Let’s spare ourselves that work by copying `.cargo/config.toml` from `examples/stm32g4` into our project.
30
31[source]
32----
33stm32g474-example
34├── .cargo
35│   └── config.toml
36├── Cargo.toml
37└── src
38 └── main.rs
39----
40
41In addition to a target triple, `.cargo/config.toml` contains a `runner` key which allows us to conveniently run our project on hardware with `cargo run` via probe-rs. In order for this to work, we need to provide the correct chip ID. We can do this by checking `probe-rs chip list`:
42
43[source,bash]
44----
45$ probe-rs chip list | grep -i stm32g474re
46 STM32G474RETx
47----
48
49and copying `STM32G474RETx` into `.cargo/config.toml` as so:
50
51[source,toml]
52----
53[target.'cfg(all(target_arch = "arm", target_os = "none"))']
54# replace STM32G071C8Rx with your chip as listed in `probe-rs chip list`
55runner = "probe-rs run --chip STM32G474RETx"
56----
57
58== Cargo.toml
59
60Now that cargo knows what target to compile for (and probe-rs knows what chip to run it on), we’re ready to add some dependencies.
61
62Looking in `examples/stm32g4/Cargo.toml`, we can see that the examples require a number of embassy crates. For blinky, we’ll only need three of them: `embassy-stm32`, `embassy-executor` and `embassy-time`.
63
64At the time of writing, the latest version of embassy isn‘t available on crates.io, so we need to install it straight from the git repository. The recommended way of doing so is as follows:
65
66* Copy the required `embassy-*` lines from the example `Cargo.toml`
67* Make any necessary changes to `features`, e.g. requiring the `stm32g474re` feature of `embassy-stm32`
68* Remove the `path = ""` keys in the `embassy-*` entries
69* Create a `[patch.crates-io]` section, with entries for each embassy crate we need. These should all contain identical values: a link to the git repository, and a reference to the commit we’re checking out. Assuming you want the latest commit, you can find it by running `git ls-remote https://github.com/embassy-rs/embassy.git HEAD`
70
71NOTE: When using this method, it’s necessary that the `version` keys in `[dependencies]` match up with the versions defined in each crate’s `Cargo.toml` in the specificed `rev` under `[patch.crates.io]`. This means that when updating, you have to a pick a new revision, change everything in `[patch.crates.io]` to match it, and then correct any versions under `[dependencies]` which have changed. Hopefully this will no longer be necessary once embassy is released on crates.io!
72
73At the time of writing, this method produces the following results:
74
75[source,toml]
76----
77[dependencies]
78embassy-stm32 = {version = "0.1.0", features = ["defmt", "time-driver-any", "stm32g474re", "memory-x", "unstable-pac", "exti"]}
79embassy-executor = { version = "0.3.3", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
80embassy-time = { version = "0.2", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
81
82[patch.crates-io]
83embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "7703f47c1ecac029f603033b7977d9a2becef48c" }
84embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "7703f47c1ecac029f603033b7977d9a2becef48c" }
85embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "7703f47c1ecac029f603033b7977d9a2becef48c" }
86----
87
88There are a few other dependencies we need to build the project, but fortunately they’re much simpler to install. Copy their lines from the example `Cargo.toml` to the the `[dependencies]` section in the new `Cargo.toml`:
89
90[source,toml]
91----
92defmt = "0.3.5"
93defmt-rtt = "0.4.0"
94cortex-m = {version = "0.7.7", features = ["critical-section-single-core"]}
95cortex-m-rt = "0.7.3"
96panic-probe = "0.3.1"
97----
98
99These are the bare minimum dependencies required to run `blinky.rs`, but it’s worth taking a look at the other dependencies specified in the example `Cargo.toml`, and noting what features are required for use with embassy – for example `futures = { version = "0.3.17", default-features = false, features = ["async-await"] }`.
100
101Finally, copy the `[profile.release]` section from the example `Cargo.toml` into ours.
102
103[source,toml]
104----
105[profile.release]
106debug = 2
107----
108
109== rust-toolchain.toml
110
111Before we can build our project, we need to add an additional file to tell cargo to use the nightly toolchain. Copy the `rust-toolchain.toml` from the embassy repo to ours, and trim the list of targets down to only the target triple relevent for our project — in this case, `thumbv7em-none-eabi`:
112
113[source]
114----
115stm32g474-example
116├── .cargo
117│   └── config.toml
118├── Cargo.toml
119├── rust-toolchain.toml
120└── src
121 └── main.rs
122----
123
124[source,toml]
125----
126# Before upgrading check that everything is available on all tier1 targets here:
127# https://rust-lang.github.io/rustup-components-history
128[toolchain]
129channel = "nightly-2023-11-01"
130components = [ "rust-src", "rustfmt", "llvm-tools", "miri" ]
131targets = ["thumbv7em-none-eabi"]
132----
133
134== build.rs
135
136In order to produce a working binary for our target, cargo requires a custom build script. Copy `build.rs` from the example to our project:
137
138[source]
139----
140stm32g474-example
141├── build.rs
142├── .cargo
143│ └── config.toml
144├── Cargo.toml
145├── rust-toolchain.toml
146└── src
147 └── main.rs
148----
149
150== Building and running
151
152At this point, we‘re finally ready to build and run our project! Connect your board via a debug probe and run:
153
154[source,bash]
155----
156cargo run --release
157----
158
159should result in a blinking LED (if there’s one attached to the pin in `src/main.rs` – change it if not!) and the following output:
160
161[source]
162----
163 Compiling stm32g474-example v0.1.0 (/home/you/stm32g474-example)
164 Finished release [optimized + debuginfo] target(s) in 0.22s
165 Running `probe-rs run --chip STM32G474RETx target/thumbv7em-none-eabi/release/stm32g474-example`
166 Erasing sectors ✔ [00:00:00] [#########################################################] 18.00 KiB/18.00 KiB @ 54.09 KiB/s (eta 0s )
167 Programming pages ✔ [00:00:00] [#########################################################] 17.00 KiB/17.00 KiB @ 35.91 KiB/s (eta 0s ) Finished in 0.817s
1680.000000 TRACE BDCR configured: 00008200
169└─ embassy_stm32::rcc::bd::{impl#3}::init::{closure#4} @ /home/you/.cargo/git/checkouts/embassy-9312dcb0ed774b29/7703f47/embassy-stm32/src/fmt.rs:117
1700.000000 DEBUG rcc: Clocks { sys: Hertz(16000000), pclk1: Hertz(16000000), pclk1_tim: Hertz(16000000), pclk2: Hertz(16000000), pclk2_tim: Hertz(16000000), hclk1: Hertz(16000000), hclk2: Hertz(16000000), pll1_p: None, adc: None, adc34: None, rtc: Some(Hertz(32000)) }
171└─ embassy_stm32::rcc::set_freqs @ /home/you/.cargo/git/checkouts/embassy-9312dcb0ed774b29/7703f47/embassy-stm32/src/fmt.rs:130
1720.000000 INFO Hello World!
173└─ embassy_stm32g474::____embassy_main_task::{async_fn#0} @ src/main.rs:14
1740.000091 INFO high
175└─ embassy_stm32g474::____embassy_main_task::{async_fn#0} @ src/main.rs:19
1760.300201 INFO low
177└─ embassy_stm32g474::____embassy_main_task::{async_fn#0} @ src/main.rs:23
178---- \ No newline at end of file