Skip to content

Commit c10c8e7

Browse files
committed
Workshop 1 materials
0 parents  commit c10c8e7

File tree

13 files changed

+205
-0
lines changed

13 files changed

+205
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

book.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[book]
2+
authors = ["Armandas Jarušauskas"]
3+
language = "en"
4+
src = "src"
5+
title = "Rust Workshop"

src/SUMMARY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Summary
2+
3+
- [Setup](./setup/README.md)
4+
- [Software setup](./setup/software.md)
5+
- [Hardware information](./setup/hardware.md)
6+
- [Hello World](./hello_world.md)
7+
- [Blinky](./blinky.md)
8+
- [Exercise: working with inputs](./button.md)

src/blinky.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Blinky
2+
3+
We have created a typical "hello world" application, but in embedded systems,
4+
the equivalent of "hello world" is blinky: simply blinking an LED.
5+
6+
We will continue working in the `hello_world` project.
7+
8+
## Change the program
9+
10+
In order to blink an LED, we need to define an output pin and make a couple of other changes.
11+
12+
Firstly, let's rename the `_peripherals` variable to `peripherals`.
13+
14+
> 💡 In Rust, a variable can be prefixed with an underscore (`_`) to indicate that it is unused.
15+
> Since we'll be using it, let's remove the leading underscore.
16+
17+
Next, we will define our LED:
18+
19+
```rust
20+
let mut led = Output::new(
21+
peripherals.GPIO19,
22+
Level::Low,
23+
OutputConfig::default().with_drive_mode(DriveMode::OpenDrain),
24+
);
25+
```
26+
27+
The code above will not compile as is, since the compiler won't know where `Output` and other types are coming from. To fix that, import the required types like so:
28+
29+
```rust
30+
use esp_hal::gpio::{DriveMode, Level, Output, OutputConfig};
31+
```
32+
33+
> 💡 Whenever a you have a missing declaration error, you can use `rust-analyzer` to help you fix it.
34+
>
35+
> Place the cursor on the missing type name and press `Ctrl` + `.` on the keyboard.
36+
> In the context menu, select `Import <Type>` or `Qualify <Type>`.
37+
>
38+
> "Import" will add the required `use` declaration, while "Qualify" will prefix the module path in-place.
39+
40+
Finally, add the following line to the body of the `loop` to toggle the LED every cycle:
41+
42+
```rust
43+
led.toggle();
44+
```
45+
46+
## Experiment
47+
48+
`Output` has other methods than `toggle`. Try using `set_high` and `set_low` methods to change the LED blinking pattern.

src/button.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Working with inputs
2+
3+
We have worked through an example of driving a GPIO output.
4+
Now it's time for you to try and extend the program to read the GPIO input state.
5+
6+
- Use the `esp_hal` crate documentation to create and initialize a GPIO [`Input`][1].
7+
- The button is connected to `GPIO9`.
8+
- Write a program that turns the LED on only while the button is pressed.
9+
10+
<details>
11+
<summary><strong>Solution</strong></summary>
12+
13+
1. Add new imports:
14+
```rust
15+
use esp_hal::gpio::{Input, InputConfig};
16+
```
17+
2. Declare the button:
18+
```rust
19+
let button = Input::new(peripherals.GPIO9, InputConfig::default());
20+
```
21+
3. Modify the loop code:
22+
```rust
23+
if button.is_high() {
24+
led.set_high();
25+
} else {
26+
led.set_low();
27+
}
28+
```
29+
30+
</details>
31+
32+
[1]: https://docs.espressif.com/projects/rust/esp-hal/1.0.0-rc.0/esp32c6/esp_hal/gpio/struct.Input.html
424 KB
Binary file not shown.
982 KB
Binary file not shown.

src/files/pinout.png

592 KB
Loading

src/hardware.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Hardware

src/hello_world.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Hello World
2+
3+
## Create the project
4+
5+
Let's create our first embedded Rust project. For this, we will be using `esp-generate` tool installed earlier.
6+
7+
To begin with, run the following command:
8+
9+
```text
10+
esp-generate --headless --chip=esp32c6 -o log -o unstable-hal hello_world
11+
```
12+
13+
This will create a new project in `hello_world` directory. Go ahead and open it with: `code hello_world`.
14+
15+
> 💡 You can run `esp-generate --chip=esp32c6` to open the tool in interactive mode and explore the available options.
16+
17+
## Run
18+
19+
With the ESP board plugged into your computer, you should be able to run the firmware with:
20+
21+
```text
22+
cargo run
23+
```
24+
25+
You may have to select the appropriate device. If this is the case, the tool will prompt for it.
26+
27+
If everything goes well, your device will boot and start printing the "hello world" message:
28+
29+
```text
30+
INFO - Hello world!
31+
INFO - Hello world!
32+
INFO - Hello world!
33+
INFO - Hello world!
34+
INFO - Hello world!
35+
```
36+
37+
## Experiment
38+
39+
Experiment with this example a little bit.
40+
41+
- Change the message to print something else.
42+
- Change the log level (e.g. to `warn`).
43+
- Change the printing frequency by adjusting the sleep duration.
44+
45+
## Bonus
46+
47+
The way the delay is currently written is a bit verbose.
48+
We can simplify the code by using [`Delay`][1] from the `esp_hal` crate.
49+
50+
1. Add the following line next to the existing `use` declarations:
51+
```rust
52+
use esp_hal::delay::Delay;
53+
```
54+
2. Create a delay variable:
55+
```rust
56+
let delay = Delay::new();
57+
```
58+
3. Replace the two lines using `delay_start` with the following one line:
59+
```rust
60+
delay.delay_millis(1000);
61+
```
62+
63+
[1]: https://docs.espressif.com/projects/rust/esp-hal/1.0.0-rc.0/esp32c6/esp_hal/delay/struct.Delay.html

0 commit comments

Comments
 (0)