From c921e5d8e1782fe67fddb2e42fcfb47dbd35d88c Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 2 Feb 2024 14:56:02 +0100 Subject: [PATCH 1/4] feat: Add InvalidSerialRead error --- espflash/src/connection/mod.rs | 17 +++++++++++++---- espflash/src/error.rs | 4 ++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/espflash/src/connection/mod.rs b/espflash/src/connection/mod.rs index 9dcd2193..1e130150 100644 --- a/espflash/src/connection/mod.rs +++ b/espflash/src/connection/mod.rs @@ -7,6 +7,7 @@ use std::{ io::{BufWriter, Write}, iter::zip, + str::from_utf8, thread::sleep, time::Duration, }; @@ -132,9 +133,14 @@ impl Connection { Ok(_) => { return Ok(()); } - Err(e) => { - debug!("Failed to reset, error {:#?}, retrying", e); - } + Err(e) => match e { + Error::InvalidSerialRead => { + return Err(Error::InvalidSerialRead); + } + _ => { + debug!("Failed to reset, error {:#?}, retrying", e); + } + }, } } @@ -168,7 +174,10 @@ impl Connection { ))); } - let read_slice = std::str::from_utf8(&buff[..read_bytes as usize]).unwrap(); + let read_slice = from_utf8(&buff[..read_bytes as usize]).map_err(|err| { + debug!("Error: {}", err); + return Error::InvalidSerialRead; + })?; let pattern = Regex::new(r"boot:(0x[0-9a-fA-F]+)(.*waiting for download)?").unwrap(); diff --git a/espflash/src/error.rs b/espflash/src/error.rs index 071db926..73eca91b 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -96,6 +96,10 @@ pub enum Error { #[error("The provided bootloader binary is invalid")] InvalidBootloader, + #[error("Invalid byte sequence read from the serial port while trying to detect Boot Mode")] + #[diagnostic(code(espflash::invalid_serial_read))] + InvalidSerialRead, + #[error("Specified bootloader path is not a .bin file")] #[diagnostic(code(espflash::invalid_bootloader_path))] InvalidBootloaderPath, From 3a326c319e5514da87507a213c41c8fd941d2be3 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 2 Feb 2024 15:12:49 +0100 Subject: [PATCH 2/4] style: Clippy lints --- espflash/src/connection/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/espflash/src/connection/mod.rs b/espflash/src/connection/mod.rs index 1e130150..6c2114ea 100644 --- a/espflash/src/connection/mod.rs +++ b/espflash/src/connection/mod.rs @@ -176,7 +176,7 @@ impl Connection { let read_slice = from_utf8(&buff[..read_bytes as usize]).map_err(|err| { debug!("Error: {}", err); - return Error::InvalidSerialRead; + Error::InvalidSerialRead })?; let pattern = Regex::new(r"boot:(0x[0-9a-fA-F]+)(.*waiting for download)?").unwrap(); From 621ac4afafa98016f4c2cd734b7923057e74bdae Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Sat, 3 Feb 2024 11:52:05 +0100 Subject: [PATCH 3/4] feat: Add help to the error --- espflash/src/error.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/espflash/src/error.rs b/espflash/src/error.rs index 73eca91b..57eee0fb 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -97,7 +97,10 @@ pub enum Error { InvalidBootloader, #[error("Invalid byte sequence read from the serial port while trying to detect Boot Mode")] - #[diagnostic(code(espflash::invalid_serial_read))] + #[diagnostic( + code(espflash::invalid_serial_read), + help("This migth be caused by a xtal frequency missmatch") + )] InvalidSerialRead, #[error("Specified bootloader path is not a .bin file")] @@ -108,8 +111,8 @@ pub enum Error { #[diagnostic( code(espflash::invalid_direct_boot), help( - "See the following page for documentation on how to set up your binary for direct boot:\ - https://github.com/espressif/esp32c3-direct-boot-example" + "See the following page for documentation on how to set up your binary for direct boot:\ + https://github.com/espressif/esp32c3-direct-boot-example" ) )] InvalidDirectBootBinary, From 3285bac03e66709cc3ca5fd43cb4b0ca4657f7b2 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Arcos Date: Sat, 3 Feb 2024 11:54:52 +0100 Subject: [PATCH 4/4] Update espflash/src/error.rs Co-authored-by: Scott Mabin --- espflash/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/espflash/src/error.rs b/espflash/src/error.rs index 57eee0fb..fe1fa16a 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -99,7 +99,7 @@ pub enum Error { #[error("Invalid byte sequence read from the serial port while trying to detect Boot Mode")] #[diagnostic( code(espflash::invalid_serial_read), - help("This migth be caused by a xtal frequency missmatch") + help("This might be caused by a xtal frequency mismatch") )] InvalidSerialRead,