From ee954b2c7baffd04b166e1a103ffd9ac29a7a544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 24 Mar 2025 16:00:24 +0100 Subject: [PATCH] Fix connecting to S2 over USB (#813) --- CHANGELOG.md | 1 + espflash/src/connection/mod.rs | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe82c3d..356c89c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `address` and `size` in `erase-region` have to be multiples of 4096 (#771) - Fixed typos in error variant names (#782) - Fix `read-flash` which didn't work with some lengths (#804) +- espflash can now flash an ESP32-S2 in download mode over USB (#813) ### Removed diff --git a/espflash/src/connection/mod.rs b/espflash/src/connection/mod.rs index a8ae14b..cc0b905 100644 --- a/espflash/src/connection/mod.rs +++ b/espflash/src/connection/mod.rs @@ -181,16 +181,23 @@ impl Connection { // Reset the chip to bootloader (download mode) reset_strategy.reset(&mut self.serial)?; + // S2 in USB download mode responds with 0 available bytes here let available_bytes = self.serial.bytes_to_read()?; - buff = vec![0; available_bytes as usize]; - let read_bytes = self.serial.read(&mut buff)? as u32; - if read_bytes != available_bytes { - return Err(Error::Connection(ConnectionError::ReadMissmatch( - available_bytes, - read_bytes, - ))); - } + buff = vec![0; available_bytes as usize]; + let read_bytes = if available_bytes > 0 { + let read_bytes = self.serial.read(&mut buff)? as u32; + + if read_bytes != available_bytes { + return Err(Error::Connection(ConnectionError::ReadMissmatch( + available_bytes, + read_bytes, + ))); + } + read_bytes + } else { + 0 + }; let read_slice = String::from_utf8_lossy(&buff[..read_bytes as usize]).into_owned();