Skip to content

Commit 8f43cc4

Browse files
Add error for cases when we cant read the serial monitor trying to decode the bootmode (#572)
* feat: Add InvalidSerialRead error * style: Clippy lints * feat: Add help to the error * Update espflash/src/error.rs Co-authored-by: Scott Mabin <scott@mabez.dev> --------- Co-authored-by: Scott Mabin <scott@mabez.dev>
1 parent a55179f commit 8f43cc4

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

espflash/src/connection/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use std::{
88
io::{BufWriter, Write},
99
iter::zip,
10+
str::from_utf8,
1011
thread::sleep,
1112
time::Duration,
1213
};
@@ -132,9 +133,14 @@ impl Connection {
132133
Ok(_) => {
133134
return Ok(());
134135
}
135-
Err(e) => {
136-
debug!("Failed to reset, error {:#?}, retrying", e);
137-
}
136+
Err(e) => match e {
137+
Error::InvalidSerialRead => {
138+
return Err(Error::InvalidSerialRead);
139+
}
140+
_ => {
141+
debug!("Failed to reset, error {:#?}, retrying", e);
142+
}
143+
},
138144
}
139145
}
140146

@@ -168,7 +174,10 @@ impl Connection {
168174
)));
169175
}
170176

171-
let read_slice = std::str::from_utf8(&buff[..read_bytes as usize]).unwrap();
177+
let read_slice = from_utf8(&buff[..read_bytes as usize]).map_err(|err| {
178+
debug!("Error: {}", err);
179+
Error::InvalidSerialRead
180+
})?;
172181

173182
let pattern = Regex::new(r"boot:(0x[0-9a-fA-F]+)(.*waiting for download)?").unwrap();
174183

espflash/src/error.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ pub enum Error {
9696
#[error("The provided bootloader binary is invalid")]
9797
InvalidBootloader,
9898

99+
#[error("Invalid byte sequence read from the serial port while trying to detect Boot Mode")]
100+
#[diagnostic(
101+
code(espflash::invalid_serial_read),
102+
help("This might be caused by a xtal frequency mismatch")
103+
)]
104+
InvalidSerialRead,
105+
99106
#[error("Specified bootloader path is not a .bin file")]
100107
#[diagnostic(code(espflash::invalid_bootloader_path))]
101108
InvalidBootloaderPath,
@@ -104,8 +111,8 @@ pub enum Error {
104111
#[diagnostic(
105112
code(espflash::invalid_direct_boot),
106113
help(
107-
"See the following page for documentation on how to set up your binary for direct boot:\
108-
https://github.com/espressif/esp32c3-direct-boot-example"
114+
"See the following page for documentation on how to set up your binary for direct boot:\
115+
https://github.com/espressif/esp32c3-direct-boot-example"
109116
)
110117
)]
111118
InvalidDirectBootBinary,

0 commit comments

Comments
 (0)