aboutsummaryrefslogtreecommitdiff
path: root/docs/modules
diff options
context:
space:
mode:
authorBarnaby Walters <[email protected]>2023-12-06 23:51:13 +0100
committerBarnaby Walters <[email protected]>2023-12-06 23:51:13 +0100
commit536e91d263ecfcf684f91598e5ca987ae0acc820 (patch)
tree750f7c24b73c8fcf7e5ce3d0da7ae7e877ea8ac2 /docs/modules
parent7703f47c1ecac029f603033b7977d9a2becef48c (diff)
Added a step-by-step guide to starting a new embassy project
Based off an example, noting what to copy, what to change and why Briefly summarizing how to require embassy crates via github All steps tested and proven working at the time of writing
Diffstat (limited to 'docs/modules')
-rw-r--r--docs/modules/ROOT/nav.adoc1
-rw-r--r--docs/modules/ROOT/pages/new_project.adoc179
2 files changed, 180 insertions, 0 deletions
diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc
index 8d7f6f411..70a4a4159 100644
--- a/docs/modules/ROOT/nav.adoc
+++ b/docs/modules/ROOT/nav.adoc
@@ -1,6 +1,7 @@
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]
4* xref:layer_by_layer.adoc[Bare metal to async] 5* xref:layer_by_layer.adoc[Bare metal to async]
5* xref:runtime.adoc[Executor] 6* xref:runtime.adoc[Executor]
6* xref:delaying_a_task.adoc[Delaying a Task] 7* xref:delaying_a_task.adoc[Delaying a Task]
diff --git a/docs/modules/ROOT/pages/new_project.adoc b/docs/modules/ROOT/pages/new_project.adoc
new file mode 100644
index 000000000..ae819aa83
--- /dev/null
+++ b/docs/modules/ROOT/pages/new_project.adoc
@@ -0,0 +1,179 @@
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----
11mkdir embassy-stm32g474
12cd embassy-stm32g474
13cargo init
14----
15
16to create an empty rust project:
17
18[source]
19----
20embassy-stm32g474
21├── Cargo.toml
22└── src
23 └── main.rs
24----
25
26Looking 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`.
27
28== .cargo/config.toml
29
30Currently, 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.
31
32[source]
33----
34embassy-stm32g474
35├── .cargo
36│   └── config.toml
37├── Cargo.toml
38└── src
39 └── main.rs
40----
41
42In 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`:
43
44[source,bash]
45----
46$ probe-rs chip list | grep -i stm32g474re
47 STM32G474RETx
48----
49
50and copying `STM32G474RETx` into `.cargo/config.toml` as so:
51
52[source,toml]
53----
54[target.'cfg(all(target_arch = "arm", target_os = "none"))']
55# replace STM32G071C8Rx with your chip as listed in `probe-rs chip list`
56runner = "probe-rs run --chip STM32G474RETx"
57----
58
59== Cargo.toml
60
61Now 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.
62
63Looking 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`.
64
65At 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:
66
67* Copy the required `embassy-*` lines from the example `Cargo.toml`
68* Make any necessary changes to `features`, e.g. requiring the `stm32g474re` feature of `embassy-stm32`
69* Remove the `path = ""` keys in the `embassy-*` entries
70* 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`
71
72NOTE: 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!
73
74At the time of writing, this method produces the following results:
75
76[source,toml]
77----
78[dependencies]
79embassy-stm32 = {version = "0.1.0", features = ["defmt", "time-driver-any", "stm32g474re", "memory-x", "unstable-pac", "exti"]}
80embassy-executor = { version = "0.3.3", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
81embassy-time = { version = "0.2", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
82
83[patch.crates-io]
84embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "7703f47c1ecac029f603033b7977d9a2becef48c" }
85embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "7703f47c1ecac029f603033b7977d9a2becef48c" }
86embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "7703f47c1ecac029f603033b7977d9a2becef48c" }
87----
88
89There 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`:
90
91[source,toml]
92----
93defmt = "0.3.5"
94defmt-rtt = "0.4.0"
95cortex-m = {version = "0.7.7", features = ["critical-section-single-core"]}
96cortex-m-rt = "0.7.3"
97panic-probe = "0.3.1"
98----
99
100These 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"] }`.
101
102Finally, copy the `[profile.release]` section from the example `Cargo.toml` into ours.
103
104[source,toml]
105----
106[profile.release]
107debug = 2
108----
109
110== rust-toolchain.toml
111
112Before 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`:
113
114[source]
115----
116embassy-stm32g474
117├── .cargo
118│   └── config.toml
119├── Cargo.toml
120├── rust-toolchain.toml
121└── src
122 └── main.rs
123----
124
125[source,toml]
126----
127# Before upgrading check that everything is available on all tier1 targets here:
128# https://rust-lang.github.io/rustup-components-history
129[toolchain]
130channel = "nightly-2023-11-01"
131components = [ "rust-src", "rustfmt", "llvm-tools", "miri" ]
132targets = ["thumbv7em-none-eabi"]
133----
134
135== build.rs
136
137In order to produce a working binary for our target, cargo requires a custom build script. Copy `build.rs` from the example to our project:
138
139[source]
140----
141embassy-stm32g474
142├── build.rs
143├── .cargo
144│ └── config.toml
145├── Cargo.toml
146├── rust-toolchain.toml
147└── src
148 └── main.rs
149----
150
151== Building and running
152
153At this point, we‘re finally ready to build and run our project! Connect your board via a debug probe and run:
154
155[source,bash]
156----
157cargo run --release
158----
159
160should 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:
161
162[source]
163----
164 Compiling embassy-stm32g474 v0.1.0 (/home/you/embassy-stm32g474)
165 Finished release [optimized + debuginfo] target(s) in 0.22s
166 Running `probe-rs run --chip STM32G474RETx target/thumbv7em-none-eabi/release/embassy-stm32g474`
167 Erasing sectors ✔ [00:00:00] [#########################################################] 18.00 KiB/18.00 KiB @ 54.09 KiB/s (eta 0s )
168 Programming pages ✔ [00:00:00] [#########################################################] 17.00 KiB/17.00 KiB @ 35.91 KiB/s (eta 0s ) Finished in 0.817s
1690.000000 TRACE BDCR configured: 00008200
170└─ embassy_stm32::rcc::bd::{impl#3}::init::{closure#4} @ /home/you/.cargo/git/checkouts/embassy-9312dcb0ed774b29/7703f47/embassy-stm32/src/fmt.rs:117
1710.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)) }
172└─ embassy_stm32::rcc::set_freqs @ /home/you/.cargo/git/checkouts/embassy-9312dcb0ed774b29/7703f47/embassy-stm32/src/fmt.rs:130
1730.000000 INFO Hello World!
174└─ embassy_stm32g474::____embassy_main_task::{async_fn#0} @ src/main.rs:14
1750.000091 INFO high
176└─ embassy_stm32g474::____embassy_main_task::{async_fn#0} @ src/main.rs:19
1770.300201 INFO low
178└─ embassy_stm32g474::____embassy_main_task::{async_fn#0} @ src/main.rs:23
179---- \ No newline at end of file