embassy/docs/modules/ROOT/pages/basic_application.adoc

81 lines
3.1 KiB
Text
Raw Normal View History

2021-12-10 11:04:12 +00:00
= A basic Embassy application
So 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.
== Main
2021-12-10 11:04:12 +00:00
The full example can be found link:https://github.com/embassy-rs/embassy/tree/master/docs/modules/ROOT/examples/basic[here].
2021-12-10 11:04:12 +00:00
=== Bare metal
The first thing you'll notice is a few declarations, two of which indicate that Embassy is suitable for bare metal development:
[source,rust]
----
include::example$basic/src/main.rs[lines="1..2"]
----
2021-12-10 11:04:12 +00:00
=== Rust Nightly
The next declaration is a Rust Unstable feature, which means that Embassy requires Rust Nightly:
2021-12-10 11:04:12 +00:00
[source,rust]
----
include::example$basic/src/main.rs[lines="3"]
2021-12-10 11:04:12 +00:00
----
=== Dealing with errors
Then, 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:
[source,rust]
----
include::example$basic/src/main.rs[lines="10"]
2021-12-10 11:04:12 +00:00
----
=== Task declaration
After a bit of import declaration, the tasks run by the application should be declared:
[source,rust]
----
include::example$basic/src/main.rs[lines="12..20"]
2021-12-10 11:04:12 +00:00
----
An 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.
2022-01-06 10:11:52 +00:00
NOTE: Notice that there is no 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.
2021-12-10 11:04:12 +00:00
=== Main
The main entry point of an Embassy application is defined using the `#[embassy_executor::main]` macro. The entry point is also required to take a `Spawner` and a `Peripherals` argument.
2021-12-10 11:04:12 +00:00
The `Spawner` is the way the main application spawns other tasks. The `Peripherals` type comes from the HAL and 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:
2021-12-10 11:04:12 +00:00
[source,rust]
2021-12-10 11:04:12 +00:00
----
include::example$basic/src/main.rs[lines="22..-1"]
2021-12-10 11:04:12 +00:00
----
What happens when the `blinker` task has 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_executor::main]` macro. The macro does the following:
2021-12-10 11:04:12 +00:00
. Creates an Embassy Executor
. Initializes the microcontroller HAL to get the `Peripherals`
2021-12-10 11:04:12 +00:00
. Defines a main task for the entry point
. Runs the executor spawning the main task
There is also a way to run the executor without using the macro, in which case you have to create the `Executor` instance yourself.
2021-12-10 11:27:44 +00:00
== The Cargo.toml
The project definition needs to contain the embassy dependencies:
[source,toml]
----
include::example$basic/Cargo.toml[lines="9..11"]
2021-12-10 11:27:44 +00:00
----
Depending on your microcontroller, you may need to replace `embassy-nrf` with something else (`embassy-stm32` for STM32. Remember to update feature flags as well).
In this particular case, the nrf52840 chip is selected, and the RTC1 peripheral is used as the time driver.