aboutsummaryrefslogtreecommitdiff
path: root/docs/modules/ROOT/pages/basic_application.adoc
diff options
context:
space:
mode:
Diffstat (limited to 'docs/modules/ROOT/pages/basic_application.adoc')
-rw-r--r--docs/modules/ROOT/pages/basic_application.adoc72
1 files changed, 72 insertions, 0 deletions
diff --git a/docs/modules/ROOT/pages/basic_application.adoc b/docs/modules/ROOT/pages/basic_application.adoc
new file mode 100644
index 000000000..46a375c86
--- /dev/null
+++ b/docs/modules/ROOT/pages/basic_application.adoc
@@ -0,0 +1,72 @@
1= A basic Embassy application
2
3So you've got one of the xref:examples.adoc[examples] running, but what now? Let's go through a simple Embassy application for the nRF52 DK to understand it better.
4
5== Main
6
7The full example can be found link:https://github.com/embassy-rs/embassy/tree/book-poc/docs/modules/ROOT/examples/basic[here].
8
9=== Rust Nightly
10
11The first thing you'll notice is a few declarations stating that Embassy requires some nightly features:
12
13[source,rust]
14----
15include::example$basic/src/main.rs[lines="1..3"]
16----
17
18=== Dealing with errors
19
20Then, what follows are some declarations on how to deal with panics and faults. During development, a good practice is to rely on `defmt-rtt` and `panic-probe` to print diagnostics to the terminal:
21
22[source,rust]
23----
24include::example$basic/src/main.rs[lines="5..6"]
25----
26
27=== Task declaration
28
29After a bit of import declaration, the tasks run by the application should be declared:
30
31[source,rust]
32----
33include::example$basic/src/main.rs[lines="18..27"]
34----
35
36An embassy task must be declared `async`, and may NOT take generic arguments. In this case, we are handed the LED that should be blinked and the interval of the blinking.
37
38NOTE: Notice that there is not busy waiting going on in this task. It is using the Embassy timer to yield execution, allowing the microcontroller to sleep in between the blinking.
39
40=== Main
41
42The main entry point of an Embassy application is defined using the `#[embassy::main]` macro. The entry point is also required to take a `Spawner` and a `Peripherals` argument.
43
44The `Spawner` is the way the main application spawns other tasks. The `Peripherals` type holds all peripherals that the application may use. In this case, we want to configure one of the pins as a GPIO output driving the LED:
45
46[source,rust]
47----
48include::example$basic/src/main.rs[lines="28..-1"]
49----
50
51
52What happens when the `blinker` task have been spawned and main returns? Well, the main entry point is actually just like any other task, except that you can only have one and it takes some specific type arguments. The magic lies within the `#[embassy::main]` macro. The macro does the following:
53
54. Creates an Embassy Executor instance
55. Initializes the microcontroller to get the `Peripherals`
56. Defines a main task for the entry point
57. Runs the executor spawning the main task
58
59There is also a way to run the executor without using the macro, in which case you have to create the `Executor` instance yourself.
60
61== The Cargo.toml
62
63The project definition needs to contain the embassy dependencies:
64
65[source,toml]
66----
67include::example$basic/Cargo.toml[lines="8..9"]
68----
69
70Depending on your microcontroller, you may need to replace `embassy-nrf` with something else (`embassy-stm32` for STM32. Remember to update feature flags as well).
71
72In this particular case, the nrf52840 chip is selected, and the RTC1 peripheral is used as the time driver.