Hello World
There is a lot to learn, so let's start simple. Our "hello world" is more of a logging practice.
-
Connect the board to your computer and access the first project:
cd exercises/hello_world -
Build, flash, and monitor the project with
cargo run. You should see:.. Commands: CTRL+R Reset chip CTRL+C Exit .. loop..! ..Tip
Try pressing
CTRL+Rto reset, then pressCTRL+Cto exit.The aim here is just run the binary.
Build, flash and monitor?
The .cargo/config.toml includes this config:
[target.riscv32imac-unknown-none-elf] # our processor arch.
runner = "espflash flash --monitor" # for `cargo run`.
Our cargo run is replaced by cargo build && espflash flash <elf_path> --monitor.
This builds and then flashes the binary. Finally, it monitors for logs.
Removing --monitor it won't wait for logs.
Exercise
We already know our code can print information with println! macro. For prettier logs we can use log or defmt. Let's try adding them:
- Take some time to read the code. Do you recognise the boilerplate discussed earlier?
- Try commenting out the line:
So that we can see what it provides. Then uncomment the line.use esp_backtrace as _; - Uncomment the "log lines" in
src/main.rs, - Add the
logdependency toCargo.toml. - The
logcrate logging level is controlled withESP_LOGunder the[env]section in.cargo/config.toml.-
Change the
ESP_LOGvariable to turnoffall logs. Re-runcargo run, to test how it works. -
Try with other levels, for example, with
trace.
-
The examples/hello_world.rs contains a solution.
You can run it with cargo run --example hello_world.
Important
Running the solution requires fixing the log-lines at the bottom of
Cargo.toml.
Suggested Reading
- esp-println crate's docs.
- log crate's docs.