Skip to content

Commit 3249861

Browse files
authored
flasher: add fallible try_connect() (#1032)
Add a function to `Flasher` to support returning the `Connection` if an error occurs. This allows for retrying the operation with the same file descriptor without closing and opening the serial port. Signed-off-by: Sean Cross <sean@xobs.io>
1 parent 288ca4b commit 3249861

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added `Flasher::try_connect()` as a variant of `Flasher::connect()` that returns the `Connection`
13+
1214
### Changed
1315

1416
### Fixed

espflash/src/flasher/mod.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -557,39 +557,59 @@ impl Flasher {
557557
/// connecting, Flasher will change the baud rate to the `baud`
558558
/// parameter.
559559
pub fn connect(
560-
mut connection: Connection,
560+
connection: Connection,
561561
use_stub: bool,
562562
verify: bool,
563563
skip: bool,
564564
chip: Option<Chip>,
565565
baud: Option<u32>,
566566
) -> Result<Self, Error> {
567+
Self::try_connect(connection, use_stub, verify, skip, chip, baud).map_err(|e| e.0)
568+
}
569+
570+
/// Attempt to connect to the boot rom. If an error is encountered,
571+
/// return the error along with the [Connection] that was passed in.
572+
pub fn try_connect(
573+
mut connection: Connection,
574+
use_stub: bool,
575+
verify: bool,
576+
skip: bool,
577+
chip: Option<Chip>,
578+
baud: Option<u32>,
579+
) -> Result<Self, Box<(Error, Connection)>> {
567580
// The connection should already be established with the device using the
568581
// default baud rate of 115,200 and timeout of 3 seconds.
569-
connection.begin()?;
570-
connection.set_timeout(DEFAULT_TIMEOUT)?;
582+
if let Err(e) = connection.begin() {
583+
return Err(Box::new((e, connection)));
584+
}
585+
if let Err(e) = connection.set_timeout(DEFAULT_TIMEOUT) {
586+
return Err(Box::new((e, connection)));
587+
}
571588

572589
detect_sdm(&mut connection);
573590

574591
let detected_chip = if connection.before_operation() != ResetBeforeOperation::NoResetNoSync
575592
{
576593
// Detect which chip we are connected to.
577-
let detected_chip = connection.detect_chip(use_stub)?;
594+
let detected_chip = match connection.detect_chip(use_stub) {
595+
Ok(detected_chip) => detected_chip,
596+
Err(e) => return Err(Box::new((e, connection))),
597+
};
578598
if let Some(chip) = chip
579599
&& chip != detected_chip
580600
{
581-
return Err(Error::ChipMismatch(
582-
chip.to_string(),
583-
detected_chip.to_string(),
584-
));
601+
return Err(Box::new((
602+
Error::ChipMismatch(chip.to_string(), detected_chip.to_string()),
603+
connection,
604+
)));
585605
}
586606
detected_chip
587607
} else if connection.before_operation() == ResetBeforeOperation::NoResetNoSync
588608
&& chip.is_some()
589609
{
590610
chip.unwrap()
591611
} else {
592-
return Err(Error::ChipNotProvided);
612+
return Err(Box::new((Error::ChipNotProvided, connection)));
593613
};
594614

595615
let chip_revision = if !connection.secure_download_mode {
@@ -626,10 +646,14 @@ impl Flasher {
626646
// Load flash stub if enabled.
627647
if use_stub {
628648
info!("Using flash stub");
629-
flasher.load_stub()?;
649+
if let Err(e) = flasher.load_stub() {
650+
return Err(Box::new((e, flasher.into_connection())));
651+
}
630652
}
631653
// Flash size autodetection doesn't work in Secure Download Mode.
632-
flasher.spi_autodetect()?;
654+
if let Err(e) = flasher.spi_autodetect() {
655+
return Err(Box::new((e, flasher.into_connection())));
656+
}
633657
} else if use_stub {
634658
warn!("Stub is not supported in Secure Download Mode, setting --no-stub");
635659
flasher.use_stub = false;
@@ -641,7 +665,9 @@ impl Flasher {
641665
&& baud > 115_200
642666
{
643667
warn!("Setting baud rate higher than 115,200 can cause issues");
644-
flasher.change_baud(baud)?;
668+
if let Err(e) = flasher.change_baud(baud) {
669+
return Err(Box::new((e, flasher.into_connection())));
670+
}
645671
}
646672

647673
Ok(flasher)

0 commit comments

Comments
 (0)