Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flasher::sync #123

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions espflash/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
collections::VecDeque,
io::{BufWriter, Write},
thread::sleep,
time::Duration,
Expand Down Expand Up @@ -29,7 +28,6 @@ pub struct CommandResponse {
pub struct Connection {
serial: Box<dyn SerialPort>,
decoder: SlipDecoder,
buffer: VecDeque<u8>,
}

#[derive(Zeroable, Pod, Copy, Clone, Debug)]
Expand All @@ -46,7 +44,6 @@ impl Connection {
Connection {
serial,
decoder: SlipDecoder::new(),
buffer: VecDeque::with_capacity(128),
}
}

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -172,23 +168,16 @@ impl Connection {
}

fn read(&mut self, len: usize) -> Result<Option<Vec<u8>>, 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(())
}
Expand Down
1 change: 1 addition & 0 deletions espflash/src/flasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()? {
Expand Down