# `defmt` - [`defmt book`](https://defmt.ferrous-systems.com/introduction) ## `esp-println` ### Features - The `defmt-espflash` adds framming bytes so `espflash` knows that is a `defmt` message - Only usable when using `espflash` - `probe-rs` does not require framming bytes as it only expects `defmt` messages - `probe-rs` can also read `rtt` messages withouth `defmt` since `rtt` uses two different channel for `rtt` and non-`rtt`. - `espflash` requires framming bytes as when using `defmt` it also need to prints non-`defmt` messages like the bootloader prints. - Uses [rzcobs encoding](https://github.com/Dirbaio/rzcobs) ### How to use 1. Enable the `defmt_espflash` feature 1. Select the communication method: `uart`or `jstag-serial` 1. Add the ` "-C", "link-arg=-Tdefmt.x",` `rustflag` in `.cargo/config.toml` 1. Add the `defmt` crate 1. Add `use esp_println as _;` into your `main.rs` 1. Use `defmt::<macro>` to print messages 1. Compile/run setting the log level. Eg: `DEFMT_LOG=debug cargo r` ## [`rtt`](https://wiki.segger.com/RTT) - RTT works by writing to a buffer in memory and needs a debugger to read the memory from the target. - Espflash cant do that, you need to use `probe-rs` - In theory also gdb/openOCD can do it but I think the Espressif forks don't include that functionality ### How to use it 1. Set the proper dependencies/features. Eg: ```toml defmt = "=0.3.5" hal = { package = "esp32c3-hal", version = "0.13.0" } esp-backtrace = { version = "0.9.0", features = [ "esp32c3", "exception-handler", "panic-handler", "print-rtt", ] } esp-println = { version = "0.7.1", features = [ "defmt", "esp32c3", "rtt", ], default-features = false } ``` 1. Add the `"-C", "link-arg=-Tdefmt.x",` `rustflag`. 1. Add `use esp_println as _;` on `main.rs` 1. Set runner: `runner = "probe-rs run --chip esp32c3"` 2. Running the code `DEFMT_LOG=debug cargo r` results is: ``` ... Programming pages ✔ [00:00:07] [#######################################################################################################################################################] 248.00 KiB/248.00 KiB @ 31.98 KiB/s (eta 0s ) Finished in 12.727s WARN Hello world! └─ defmt::__risc_v_rt__main @ src/main.rs:16 INFO Loop... └─ defmt::__risc_v_rt__main @ src/main.rs:18 ``` ## `probe-rs` - By default, uses `defmt`, with any of the [available encodings](https://defmt.ferrous-systems.com/encoding), and `rtt` - `probe-rs` is not able to read UART or Serial JTAG