Skip to content

Commit c9b28f7

Browse files
committed
docs: Update to esp-generate
1 parent d360fa5 commit c9b28f7

File tree

4 files changed

+104
-82
lines changed

4 files changed

+104
-82
lines changed

book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ additional-js = ["assets/mermaid.min.js", "assets/mermaid-init.js"]
1515
"/overview/bare-metal.html" = "https://esp-rs.github.io/book/overview/using-the-core-library.html"
1616
"/tooling/text-editors-and-ides.html" = "https://esp-rs.github.io/book/tooling/visual-studio-code.html"
1717
"/debugging/vscode-debugging.html" = "https://esp-rs.github.io/book/debugging/vscode.html"
18-
"/writing-your-own-application/no-std-applications/understanding-esp-template.html" = "https://esp-rs.github.io/book/writing-your-own-application/generate-project/esp-template.html"
18+
"/writing-your-own-application/no-std-applications/understanding-esp-template.html" = "https://esp-rs.github.io/book/writing-your-own-application/generate-project/esp-generate.html"
1919
"installation/installation.html" = "https://esp-rs.github.io/book/installation/index.html"
2020
"troubleshooting/espflash.html" = "https://esp-rs.github.io/book/troubleshooting/index.html"
2121

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [Using Containers](./installation/using-containers.md)
1313
- [Writing Your Own Application](./writing-your-own-application/index.md)
1414
- [Generating Projects from Templates](./writing-your-own-application/generate-project/index.md)
15-
- [Understanding `esp-template`](./writing-your-own-application/generate-project/esp-template.md)
15+
- [Understanding `esp-generate`](./writing-your-own-application/generate-project/esp-generate.md)
1616
- [Understanding `esp-idf-template`](./writing-your-own-application/generate-project/esp-idf-template.md)
1717
- [Writing `no_std` Applications](./writing-your-own-application/nostd.md)
1818
- [Writing `std` Applications](./writing-your-own-application/std.md)
Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Understanding `esp-template`
1+
# Understanding `esp-generate`
22

33
Now that we know how to [generate a `no_std` project][generate-no-std], let's inspect what the generated
44
project contains, try to understand every part of it, and run it.
@@ -7,48 +7,52 @@ project contains, try to understand every part of it, and run it.
77

88
## Inspecting the Generated Project
99

10-
When creating a project from [`esp-template`][esp-template] with the following answers:
11-
- Which MCU to target? · `esp32c3`
12-
- Configure advanced template options? · `false`
13-
14-
For this explanation, we will use the default values, if you want further modifications, see the [additional prompts][prompts] when not using default values.
10+
When creating a project from [`esp-generate`][esp-generate] with no extra options:
11+
```
12+
esp-generate --chip esp32c3 your-project
13+
```
1514

1615
It should generate a file structure like this:
1716

1817
```text
18+
├── build.rs
1919
├── .cargo
20-
│   └── config.toml
21-
├── src
22-
│   └── main.rs
23-
├── .gitignore
20+
│ └── config.toml
2421
├── Cargo.toml
25-
├── LICENSE-APACHE
26-
├── LICENSE-MIT
27-
└── rust-toolchain.toml
22+
├── .gitignore
23+
├── rust-toolchain.toml
24+
├── src
25+
│ ├── bin
26+
│ │ └── main.rs
27+
│ └── lib.rs
28+
└── .vscode
29+
└── settings.json
2830
```
2931

3032
Before going further, let's see what these files are for.
31-
33+
- [`build.rs`][build.rs]
34+
- Sets the linker script arguments based on the template options.
3235
- [`.cargo/config.toml`][config-toml]
3336
- The Cargo configuration
3437
- This defines a few options to correctly build the project
35-
- Contains `runner = "espflash flash --monitor"` - this means you can just use `cargo run` to flash and monitor your code
36-
- `src/main.rs`
37-
- The main source file of the newly created project
38-
- For details, see the [Understanding `main.rs`][main-rs] section below
39-
- [`.gitignore`][gitignore]
40-
- Tells `git` which folders and files to ignore
38+
- Contains the custom runner command for `espflash` or `probe-rs`. For example, `runner = "espflash flash --monitor"` - this means you can just use `cargo run` to flash and monitor your code
4139
- [`Cargo.toml`][cargo-toml]
4240
- The usual Cargo manifest declares some meta-data and dependencies of the project
43-
- `LICENSE-APACHE`, `LICENSE_MIT`
44-
- Those are the most common licenses used in the Rust ecosystem
45-
- If you want to use a different license, you can delete these files and change the license in `Cargo.toml`
41+
- [`.gitignore`][gitignore]
42+
- Tells `git` which folders and files to ignore
4643
- [`rust-toolchain.toml`][rust-toolchain-toml]
4744
- Defines which Rust toolchain to use
4845
- The toolchain will be `nightly` or `esp` depending on your target
46+
- `src/bin/main.rs`
47+
- The main source file of the newly created project
48+
- For details, see the [Understanding `main.rs`][main-rs] section below
49+
- `src/lib.rs`
50+
- This tells the Rust compiler that this code doesn't use `libstd`
51+
- `.vscode/settings.json`
52+
- Defines a set of settings for Visual Studio Code to make Rust Analyzer work.
4953

50-
[esp-template]: https://github.com/esp-rs/esp-template
51-
[prompts]: https://github.com/esp-rs/esp-template#esp-template
54+
[esp-generate]: https://github.com/esp-rs/esp-generate
55+
[build.rs]: https://doc.rust-lang.org/cargo/reference/build-scripts.html
5256
[main-rs]: #understanding-mainrs
5357
[cargo-toml]: https://doc.rust-lang.org/cargo/reference/manifest.html
5458
[gitignore]: https://git-scm.com/docs/gitignore
@@ -68,44 +72,43 @@ Before going further, let's see what these files are for.
6872
- The `no_main` attribute says that this program won't use the standard main interface, which is usually used when a full operating system is available. Instead of the standard main, we'll use the entry attribute from the `esp-riscv-rt` crate to define a custom entry point. In this program, we have named the entry point `main`, but any other name could have been used. The entry point function must be a [diverging function][diverging-function]. I.e. it has the signature `fn foo() -> !`; this type indicates that the function never returns – which means that the program never terminates.
6973

7074
```rust,ignore
71-
4 use esp_backtrace as _;
72-
5 use esp_println::println;
73-
6 use esp_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc};
75+
4 use esp_backtrace as _;
76+
5 use esp_hal::delay::Delay;
77+
6 use esp_hal::prelude::*;
78+
7 use log::info;
7479
```
7580
- `use esp_backtrace as _;`
7681
- Since we are in a bare-metal environment, we need a panic handler that runs if a panic occurs in code
7782
- There are a few different crates you can use (e.g `panic-halt`) but `esp-backtrace` provides an implementation that prints the address of a backtrace - together with `espflash` these addresses can get decoded into source code locations
78-
- `use esp_println::println;`
79-
- Provides `println!` implementation
80-
- `use esp_hal::{...}`
81-
- We need to bring in some types we are going to use
83+
- `use esp_hal::delay::Delay;`
84+
- Provides `Delay` driver implementation.
85+
- `use esp_hal::prelude::*;`
86+
- Imports the `esp-hal` [prelude][prelude].
8287

8388
```rust,ignore
8489
8 #[entry]
8590
9 fn main() -> ! {
86-
10 let peripherals = Peripherals::take();
87-
11 let system = peripherals.SYSTEM.split();
88-
12 let clocks = ClockControl::max(system.clock_control).freeze();
89-
13
90-
14 println!("Hello world!");
91-
15
92-
16 loop {}
91+
10 esp_println::logger::init_logger_from_env();
92+
11
93+
12 let delay = Delay::new();
94+
13 loop {
95+
14 info!("Hello world!");
96+
15 delay.delay(500.millis());
97+
16 }
9398
17 }
9499
```
100+
95101
Inside the `main` function we can find:
96-
- `let peripherals = Peripherals::take()`
97-
- HAL drivers usually take ownership of peripherals accessed via the PAC
98-
- Here we take all the peripherals from the PAC to pass them to the HAL drivers later
99-
- `let mut system = peripherals.SYSTEM.split();`
100-
- Sometimes a peripheral (here the System peripheral) is coarse-grained and doesn't exactly fit the HAL drivers - so here we split the System peripheral into smaller pieces which get passed to the drivers
101-
- `let clocks = ClockControl::max(system.clock_control).freeze();`
102-
- Here we configure the system clocks - in this case, boost to the maxiumum for the chip
103-
- We freeze the clocks, which means we can't change them later
104-
- Some drivers need a reference to the clocks to know how to calculate rates and durations
105-
- `println!("Hello world!");`
106-
- Prints "Hello world!"
102+
- `esp_println::logger::init_logger_from_env();`
103+
- Initializes the logger, if `ESP_LOG` environment variable is defined, it will use that log level.
104+
- `let delay = Delay::new();`
105+
- Creates a delay instance.
107106
- `loop {}`
108-
- Since our function is supposed to never return, we just "do nothing" in a loop
107+
- Since our function is supposed to never return, we use a loop
108+
- `info!("Hello world!");`
109+
- Creates a log message with `info` level that prints "Hello world!".
110+
- `delay.delay(500.millis());`
111+
- Waits for 500 milliseconds.
109112

110113
[diverging-function]: https://doc.rust-lang.org/beta/rust-by-example/fn/diverging.html
111114

@@ -114,7 +117,7 @@ Inside the `main` function we can find:
114117
Building and running the code is as easy as
115118

116119
```shell
117-
cargo run
120+
cargo run --release
118121
```
119122

120123
This builds the code according to the configuration and executes [`espflash`][espflash] to flash the code to the board.
@@ -127,26 +130,26 @@ Make sure that you have [`espflash`][espflash] installed, otherwise this step wi
127130
You should see something similar to this:
128131

129132
```text
130-
[2023-04-17T14:17:08Z INFO ] Serial port: '/dev/ttyACM0'
131-
[2023-04-17T14:17:08Z INFO ] Connecting...
132-
[2023-04-17T14:17:09Z INFO ] Using flash stub
133-
[2023-04-17T14:17:09Z WARN ] Setting baud rate higher than 115,200 can cause issues
133+
...
134+
[2024-11-14T09:29:32Z INFO ] Serial port: '/dev/ttyUSB0'
135+
[2024-11-14T09:29:32Z INFO ] Connecting...
136+
[2024-11-14T09:29:32Z INFO ] Using flash stub
137+
[2024-11-14T09:29:33Z WARN ] Setting baud rate higher than 115,200 can cause issues
134138
Chip type: esp32c3 (revision v0.3)
135-
Crystal frequency: 40MHz
139+
Crystal frequency: 40 MHz
136140
Flash size: 4MB
137141
Features: WiFi, BLE
138-
MAC address: 60:55:f9:c0:39:7c
139-
App/part. size: 203,920/4,128,768 bytes, 4.94%
142+
MAC address: a0:76:4e:5a:d2:c8
143+
App/part. size: 76,064/4,128,768 bytes, 1.84%
140144
[00:00:00] [========================================] 13/13 0x0
141145
[00:00:00] [========================================] 1/1 0x8000
142-
[00:00:01] [========================================] 64/64 0x10000
143-
[2023-04-17T14:17:11Z INFO ] Flashing has completed!
146+
[00:00:00] [========================================] 11/11 0x10000
147+
[2024-11-14T09:29:35Z INFO ] Flashing has completed!
144148
Commands:
145149
CTRL+R Reset chip
146150
CTRL+C Exit
147-
148151
...
149-
Hello world!
152+
INFO - Hello world!
150153
```
151154

152155
What you see here are messages from the first and second stage bootloader, and then, our "Hello world" message!
@@ -157,6 +160,8 @@ You can reboot with `CTRL+R` or exit with `CTRL+C`.
157160

158161
If you encounter any issues while building the project, please, see the [Troubleshooting][troubleshooting] chapter.
159162

163+
164+
[prelude]: https://doc.rust-lang.org/reference/names/preludes.html
160165
[espflash]: https://github.com/esp-rs/espflash/tree/main/espflash
161166
[runner-config]: https://doc.rust-lang.org/cargo/reference/config.html#targettriplerunner
162167
[troubleshooting]: ../../troubleshooting/index.md

src/writing-your-own-application/generate-project/index.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
# Generating Projects from Templates
22

33
We currently maintain two template repositories:
4-
- [`esp-template`][esp-template] - `no_std` template.
4+
- [`esp-generate`][esp-generate] - `no_std` template.
55
- [`esp-idf-template`][esp-idf-template] - `std` template.
66

7-
Both templates are based on [`cargo-generate`][cargo-generate], a tool that allows you to create a new project based on some existing template. In our case, [`esp-idf-template`][esp-idf-template] or [`esp-template`][esp-template] can be used to generate an application with all the required configurations and dependencies.
7+
8+
## `esp-generate`
9+
10+
`esp-generate` is project generation tool that can be used to generate an application with all the required configurations and dependencies
11+
12+
1. Install `esp-generate`:
13+
```shell
14+
cargo install esp-generate
15+
```
16+
2. Generate a project based on the template, selecting the chip and the name of the project:
17+
```shell
18+
esp-generate --chip=esp32c6 your-project
19+
```
20+
See [Understanding `esp-generate`][understanding-esp-generate] for more details on the template project.
21+
22+
When the `esp-generate` subcommand is invoked, you will be prompted with a TUI where you can select the configuration of your application. Upon completion of this process, you will have a buildable project with all the correct configurations.
23+
24+
3. Build/Run the generated project:
25+
- Use `cargo build` to compile the project using the appropriate toolchain and target.
26+
- Use `cargo run` to compile the project, flash it, and open a serial monitor with our target device.
27+
28+
[esp-generate]: https://github.com/esp-rs/esp-generate
29+
[understanding-esp-generate]: ./esp-generate.md
30+
31+
## `esp-idf-template`
32+
33+
`esp-idf-tempalte` is based on [`cargo-generate`][cargo-generate], a tool that allows you to create a new project based on some existing template. In our case, [`esp-idf-template`][esp-idf-template] can be used to generate an application with all the required configurations and dependencies.
834

935
1. Install `cargo generate`:
1036
```shell
1137
cargo install cargo-generate
1238
```
13-
2. Generate a project based on one of the templates:
14-
- `esp-template`:
15-
```shell
16-
cargo generate esp-rs/esp-template
17-
```
18-
See [Understanding `esp-template`][understanding-esp-template] for more details on the template project.
19-
- `esp-idf-template`:
20-
```shell
21-
cargo generate esp-rs/esp-idf-template cargo
22-
```
23-
See [Understanding `esp-idf-template`][understanding-esp-idf-template] for more details on the template project.
39+
2. Generate a project based on the template:
40+
```shell
41+
cargo generate esp-rs/esp-idf-template cargo
42+
```
43+
See [Understanding `esp-idf-template`][understanding-esp-idf-template] for more details on the template project.
2444

2545
When the `cargo generate` subcommand is invoked, you will be prompted to answer several questions regarding the target of your application. Upon completion of this process, you will have a buildable project with all the correct configurations.
2646

@@ -30,17 +50,14 @@ Both templates are based on [`cargo-generate`][cargo-generate], a tool that allo
3050

3151
[cargo-generate]: https://github.com/cargo-generate/cargo-generate
3252
[esp-idf-template]: https://github.com/esp-rs/esp-idf-template
33-
[esp-template]: https://github.com/esp-rs/esp-template
34-
[understanding-esp-template]: ./esp-template.md
3553
[understanding-esp-idf-template]: ./esp-idf-template.md
3654

3755
## Using Dev Containers in the Templates
3856

39-
Both template repositories have a prompt for Dev Containers support, see details in [Dev Containers][dev-container] section of the template README.
57+
Both template repositories have a prompt for Dev Containers support.
4058

4159
Dev Containers use the [`idf-rust`][idf-rust] container image, which was explained in the [Using Container][using-container] section of the [Setting up a Development Environment][setting-env] chapter. This image provides an environment ready to develop Rust applications for Espressif chips with no installation required. Dev Containers also have integration with [Wokwi simulator][wokwi], to simulate the project, and allow flashing from the container using [`web-flash`][web-flash].
4260

43-
[dev-container]: https://github.com/esp-rs/esp-template/tree/main/docs#dev-containers
4461
[idf-rust]: https://hub.docker.com/r/espressif/idf-rust/tags
4562
[using-container]: ../../installation/using-containers.md
4663
[wokwi]: https://wokwi.com/

0 commit comments

Comments
 (0)