|
| 1 | +# esp-println |
| 2 | + |
| 3 | +A library that provides `print!`, `println!`, `dbg!` implementations and |
| 4 | +logging capabilities for Espressif devices. |
| 5 | + |
| 6 | +- Supports all Espressif ESP32 family devices. |
| 7 | +- Supports different communication methods: |
| 8 | + - UART (Default) |
| 9 | + - JTAG-Serial (Only available in ESP32-C3, ESP32-C6, ESP32-H2, ESP32-S3) |
| 10 | + - No-op: Turns printing into a no-op |
| 11 | +- Supports [`defmt`] backend |
| 12 | + |
| 13 | +# Usage |
| 14 | + |
| 15 | +```toml |
| 16 | +esp-println = { version = "0.9.1", features = ["esp32c2"] } |
| 17 | +``` |
| 18 | + |
| 19 | +or `cargo add esp-println --features esp32c2` |
| 20 | +It's important to specify your target device as feature. |
| 21 | + |
| 22 | +Then in your program: |
| 23 | + |
| 24 | +```rust |
| 25 | +use esp_println::println; |
| 26 | +``` |
| 27 | + |
| 28 | +You can now `println!("Hello world")` as usual. |
| 29 | + |
| 30 | +# Features |
| 31 | + |
| 32 | +- There is one feature for each supported target: `esp32`, `esp32c2`, |
| 33 | + `esp32c3`, `esp32c6`, `esp32h2`, `esp32s2`, and `esp32s3`. |
| 34 | + - One of these features must be enabled. |
| 35 | + - Only one of these features can be enabled at a time. |
| 36 | +- There is one feature for each supported communication method: `uart`, `jtag-serial` and `no-op`. |
| 37 | + - Only one of these features can be enabled at a time. |
| 38 | +- `log`: Enables logging using [`log` crate]. |
| 39 | +- `colors` enable colored logging. |
| 40 | + - Only effective when using the `log` feature. |
| 41 | +- `critical-section` enables critical sections. |
| 42 | +- `defmt-espflash`: This is intended to be used with [`espflash`], see `-L/--log-format` argument |
| 43 | + of `flash` or `monitor` subcommands of `espflash` and `cargo-espflash`. Uses [rzCOBS] encoding |
| 44 | + and adds framing. |
| 45 | + |
| 46 | +## Default Features |
| 47 | + |
| 48 | +By default, we use the `uart`, `critial-section` and `colors` features. |
| 49 | +Which means that it will print to the UART, use critical sections and output |
| 50 | +messages will be colored. |
| 51 | +If we want to use a communication method that is not `uart`, the default |
| 52 | +one, we need to [disable the default features]. |
| 53 | + |
| 54 | +## Logging |
| 55 | + |
| 56 | +With the feature `log` activated you can initialize a simple logger like this |
| 57 | + |
| 58 | +```rust |
| 59 | +init_logger(log::LevelFilter::Info); |
| 60 | +``` |
| 61 | + |
| 62 | +There is a default feature `colors` which enables colored log output. |
| 63 | + |
| 64 | +Additionally, you can use |
| 65 | + |
| 66 | +```rust |
| 67 | +init_logger_from_env(); |
| 68 | +``` |
| 69 | + |
| 70 | +In this case the following environment variables are used: |
| 71 | + |
| 72 | +- `ESP_LOGLEVEL` sets the log level, use values like `trace`, `info` etc. |
| 73 | +- `ESP_LOGTARGETS` if set you should provide the crate names of crates (optionally with a path e.g. `esp_wifi::compat::common`) which should get logged, separated by `,` and no additional whitespace between |
| 74 | + |
| 75 | +If this simple logger implementation isn't sufficient for your needs, you can implement your own logger on top of `esp-println`. See [Implementing a Logger section log documentaion] |
| 76 | + |
| 77 | +## `defmt` |
| 78 | + |
| 79 | +Using the `defmt-espflash` feature, `esp-println` will install a `defmt` global logger. The logger will |
| 80 | +output to the same data stream as `println!()`, and adds framing bytes so it can be used even with |
| 81 | +other, non-`defmt` output. Using the `defmt-espflash` feature automatically uses the [rzCOBS] encoding and does |
| 82 | +not allow changing the encoding. |
| 83 | + |
| 84 | +Follow the [`defmt` book's setup instructions] on how to |
| 85 | +set up `defmt`. Remember, the global logger is already installed for you by `esp-println`! |
| 86 | + |
| 87 | +Please note that `defmt` does _not_ provide MSRV guarantees with releases, and as such we are not able to make any MSRV guarantees when this feature is enabled. For more information refer to the MSRV section of `defmt`'s README: |
| 88 | +https://github.com/knurling-rs/defmt?tab=readme-ov-file#msrv |
| 89 | + |
| 90 | +[`defmt`]: https://github.com/knurling-rs/defmt |
| 91 | +[`log` crate]: https://github.com/rust-lang/log |
| 92 | +[rzCOBS]: https://github.com/Dirbaio/rzcobs |
| 93 | +[`espflash`]: https://github.com/esp-rs/espflash |
| 94 | +[disable the default features]: https://doc.rust-lang.org/cargo/reference/features.html#the-default-feature |
| 95 | +[Implementing a Logger section log documentaion]: https://docs.rs/log/0.4.17/log/#implementing-a-logger |
| 96 | +[`defmt` book's setup instructions]: https://defmt.ferrous-systems.com/setup |
| 97 | + |
| 98 | +# Troubleshooting linker errors |
| 99 | + |
| 100 | +If you experience linker errors, make sure you have _some_ reference to `esp_println` in your code. |
| 101 | +If you don't use `esp_println` directly, you'll need to add e.g. `use esp_println as _;` to your |
| 102 | +import statements. This ensures that the global logger will not be removed by the compiler. |
| 103 | + |
| 104 | +# License |
| 105 | + |
| 106 | +Licensed under either of: |
| 107 | + |
| 108 | +- Apache License, Version 2.0 ([LICENSE-APACHE](../LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 109 | +- MIT license ([LICENSE-MIT](../LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 110 | + |
| 111 | +at your option. |
| 112 | + |
| 113 | +# Contribution |
| 114 | + |
| 115 | +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in |
| 116 | +the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without |
| 117 | +any additional terms or conditions. |
0 commit comments