-
Notifications
You must be signed in to change notification settings - Fork 549
Description
[Chapter 7.3] Example for UART comm with core::fmt::Write not working out of the box
Platform: micro:bit v2
checked out commit: b48fe4a
Issue
So i was working myself through the book, currently at chapter 7.
Coming up with the naive solution for Sending a String via UART i then found the write! macro solution (write!(serial, "The quick brown ....").unwrap();). Keen to try it out i copied the whole code from here and was suprised about getting the following error:
$ cargo embed --features v2 --target thumbv7em-none-eabihf
error: could not compile `uart` (bin "uart") due to 1 previous error; 1 warning emitted
warning: unused import: `hal::prelude::*`
--> src/07-uart/src/main.rs:17:5
|
17 | hal::prelude::*,
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0599]: cannot write into `UartePort<UARTE0>`
|
::: src/07-uart/src/serial_setup.rs:11:1
|
11 | pub struct UartePort<T: Instance>(UarteTx<T>, UarteRx<T>);
| --------------------------------- method `write_fmt` not found for this struct
--> /rustc/9fc6b43126469e3858e2fe86cafb4f0fd5068869/library/core/src/fmt/mod.rs:202:8
|
= note: the method is available for `UartePort<UARTE0>` here
--> src/07-uart/src/main.rs:68:8
|
68 | write!(serial, "The quick brown fox jumps over the lazy dog.\r\n").unwrap();
| ^^^^^^
|
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
--> src/07-uart/src/main.rs:68:8
|
68 | write!(serial, "The quick brown fox jumps over the lazy dog.\r\n").unwrap();
| ^^^^^^
= help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
|
4 + use core::fmt::Write;
|
For more information about this error, try `rustc --explain E0599`.
Error Failed to run cargo build: exit code = Some(101).
Adding import core::fmt::Write with use core::fmt::Write; lead to the following error:
$ cargo embed --features v2 --target thumbv7em-none-eabihf
Compiling uart v0.1.0 (/home/nonroot/workspace_rust/discovery_microbitISSUE/microbit/src/07-uart)
error: could not compile `uart` (bin "uart") due to 2 previous errors; 2 warnings emitted
error[E0252]: the name `Write` is defined multiple times
--> src/07-uart/src/main.rs:27:5
|
7 | use core::fmt::Write;
| ---------------- previous import of the trait `Write` here
...
27 | use embedded_hal_nb::serial::Write;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Write` reimported here
|
= note: `Write` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
|
7 | use core::fmt::Write as OtherWrite;
| +++++++++++++
[...]
Some errors have detailed explanations: E0252, E0599.
For more information about an error, try `rustc --explain E0252`.
Error Failed to run cargo build: exit code = Some(101).
( #583 adds the line use embedded_hal_nb::serial::Write;)
The error rises from the same item name for two different imports (afaik i see it).
My solution: Changing binding name of import (use core::fmt::Write to use core::fmt::Write as CoreWrite; or use core::fmt::Write as _;) solves this BUT this can get confusing for readers of the book just wanting to compile their solution. As a solution, add changed binding name use core::fmt::Write as CoreWrite to the source code of microbit/src/07-uart/src/main.rs) and also adapt the source pictured in the book (Chapter 7).
Steps to reproduce
- clone repo and cd to microbit/src/07-uart
git clone https://github.com/rust-embedded/discovery.git
- change line
nb::block!(serial.write(b'X')).unwrap();towrite!(serial, "The quick brown fox jumps over the lazy dog.\r\n").unwrap();- thats what write! and core::fmt::Write section in Chapter 7 is finally about, right?
- run
cargo embed --features v2 --target thumbv7em-none-eabihf- results to 1st mentioned error
- add
use core::fmt::Write;and runcargo embed --features v2 --target thumbv7em-none-eabihf- results to 2nd mentioned error
Solution
- change binding name of a
Writeimport- microbit/src/07-uart/src/main.rs:
use embedded_hal_nb::serial::Write as _;oruse embedded_hal_nb::serial::Write as SerialWrite;
- microbit/src/07-uart/src/main.rs: