From f69b388ae0a5f1f0fd4bed845ff84f4f19cd7898 Mon Sep 17 00:00:00 2001 From: Pavel Remygailo Date: Fri, 21 Jan 2022 20:35:27 +0200 Subject: [PATCH] Fix `flasher::sync`. * `connection::read` now loops until all data have been read * connection is flushed between `Command::Sync` sent and response is read * Fixes #116 --- espflash/src/connection.rs | 23 ++++++----------------- espflash/src/flasher.rs | 1 + 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/espflash/src/connection.rs b/espflash/src/connection.rs index c6b52bc6..18c8fedd 100644 --- a/espflash/src/connection.rs +++ b/espflash/src/connection.rs @@ -1,5 +1,4 @@ use std::{ - collections::VecDeque, io::{BufWriter, Write}, thread::sleep, time::Duration, @@ -29,7 +28,6 @@ pub struct CommandResponse { pub struct Connection { serial: Box, decoder: SlipDecoder, - buffer: VecDeque, } #[derive(Zeroable, Pod, Copy, Clone, Debug)] @@ -46,7 +44,6 @@ impl Connection { Connection { serial, decoder: SlipDecoder::new(), - buffer: VecDeque::with_capacity(128), } } @@ -119,7 +116,6 @@ impl Connection { } pub fn write_command(&mut self, command: Command) -> Result<(), Error> { - self.buffer.clear(); self.serial.clear(serialport::ClearBuffer::Input)?; let mut writer = BufWriter::new(&mut self.serial); let mut encoder = SlipEncoder::new(&mut writer)?; @@ -172,23 +168,16 @@ impl Connection { } fn read(&mut self, len: usize) -> Result>, Error> { - let mut output = Vec::with_capacity(1024); - self.decoder.decode(&mut self.serial, &mut output)?; - - self.buffer.extend(&output); - if self.buffer.len() < len { - return Ok(None); + let mut tmp = Vec::with_capacity(1024); + loop { + self.decoder.decode(&mut self.serial, &mut tmp)?; + if tmp.len() >= len { + return Ok(Some(tmp)); + } } - - // reuse allocation - output.clear(); - output.extend(self.buffer.drain(..)); - - Ok(Some(output)) } pub fn flush(&mut self) -> Result<(), Error> { - self.buffer.clear(); self.serial.flush()?; Ok(()) } diff --git a/espflash/src/flasher.rs b/espflash/src/flasher.rs index 95596993..63174455 100644 --- a/espflash/src/flasher.rs +++ b/espflash/src/flasher.rs @@ -225,6 +225,7 @@ impl Flasher { self.connection .with_timeout(CommandType::Sync.timeout(), |connection| { connection.write_command(Command::Sync)?; + connection.flush()?; for _ in 0..100 { match connection.read_response()? {