Skip to content

Commit 8f161cf

Browse files
authored
Fix flasher::sync. (#123)
* `connection::read` now loops until all data have been read * connection is flushed between `Command::Sync` sent and response is read * Fixes #116
1 parent fd205c7 commit 8f161cf

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

espflash/src/connection.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
collections::VecDeque,
32
io::{BufWriter, Write},
43
thread::sleep,
54
time::Duration,
@@ -29,7 +28,6 @@ pub struct CommandResponse {
2928
pub struct Connection {
3029
serial: Box<dyn SerialPort>,
3130
decoder: SlipDecoder,
32-
buffer: VecDeque<u8>,
3331
}
3432

3533
#[derive(Zeroable, Pod, Copy, Clone, Debug)]
@@ -46,7 +44,6 @@ impl Connection {
4644
Connection {
4745
serial,
4846
decoder: SlipDecoder::new(),
49-
buffer: VecDeque::with_capacity(128),
5047
}
5148
}
5249

@@ -119,7 +116,6 @@ impl Connection {
119116
}
120117

121118
pub fn write_command(&mut self, command: Command) -> Result<(), Error> {
122-
self.buffer.clear();
123119
self.serial.clear(serialport::ClearBuffer::Input)?;
124120
let mut writer = BufWriter::new(&mut self.serial);
125121
let mut encoder = SlipEncoder::new(&mut writer)?;
@@ -172,23 +168,16 @@ impl Connection {
172168
}
173169

174170
fn read(&mut self, len: usize) -> Result<Option<Vec<u8>>, Error> {
175-
let mut output = Vec::with_capacity(1024);
176-
self.decoder.decode(&mut self.serial, &mut output)?;
177-
178-
self.buffer.extend(&output);
179-
if self.buffer.len() < len {
180-
return Ok(None);
171+
let mut tmp = Vec::with_capacity(1024);
172+
loop {
173+
self.decoder.decode(&mut self.serial, &mut tmp)?;
174+
if tmp.len() >= len {
175+
return Ok(Some(tmp));
176+
}
181177
}
182-
183-
// reuse allocation
184-
output.clear();
185-
output.extend(self.buffer.drain(..));
186-
187-
Ok(Some(output))
188178
}
189179

190180
pub fn flush(&mut self) -> Result<(), Error> {
191-
self.buffer.clear();
192181
self.serial.flush()?;
193182
Ok(())
194183
}

espflash/src/flasher.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl Flasher {
225225
self.connection
226226
.with_timeout(CommandType::Sync.timeout(), |connection| {
227227
connection.write_command(Command::Sync)?;
228+
connection.flush()?;
228229

229230
for _ in 0..100 {
230231
match connection.read_response()? {

0 commit comments

Comments
 (0)