Skip to content

Commit f4454e2

Browse files
KerryRJicewind1991
authored andcommitted
Improve flashing reliability at higher speeds
1 parent 2321bb0 commit f4454e2

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

cargo-espflash/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-espflash"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
description = "Cargo subcommand for flashing the ESP8266 and ESP32 over serial"
55
license = "GPL-2.0"
66
authors = [

espflash/src/connection.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,22 @@ impl Connection {
120120
) -> Result<u32, Error> {
121121
self.write_command(command, data, check)?;
122122

123-
match self.read_response()? {
124-
Some(response) if response.return_op == command as u8 => {
125-
if response.status == 1 {
126-
Err(Error::RomError(RomError::from(response.error)))
127-
} else {
128-
Ok(response.value)
123+
for _ in 0..100 {
124+
match self.read_response()? {
125+
Some(response) if response.return_op == command as u8 => {
126+
if response.status == 1 {
127+
let _error = self.flush();
128+
return Err(Error::RomError(RomError::from(response.error)));
129+
} else {
130+
return Ok(response.value);
131+
}
132+
}
133+
_ => {
134+
continue;
129135
}
130136
}
131-
_ => Err(Error::ConnectionFailed),
132137
}
138+
Err(Error::ConnectionFailed)
133139
}
134140

135141
fn read(&mut self) -> Result<Vec<u8>, Error> {

espflash/src/flasher.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,18 @@ impl Flasher {
229229
self.connection
230230
.with_timeout(Duration::from_millis(100), |connection| {
231231
let data = &[
232-
0x07u8, 0x07, 0x012, 0x20, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
232+
0x07, 0x07, 0x12, 0x20, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
233233
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
234-
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
234+
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
235235
][..];
236236

237237
connection.write_command(Command::Sync as u8, data, 0)?;
238238

239-
for _ in 0..10 {
239+
for _ in 0..100 {
240240
match connection.read_response()? {
241241
Some(response) if response.return_op == Command::Sync as u8 => {
242242
if response.status == 1 {
243+
let _error = connection.flush();
243244
return Err(Error::RomError(RomError::from(response.error)));
244245
} else {
245246
break;
@@ -249,16 +250,17 @@ impl Flasher {
249250
}
250251
}
251252

252-
for _ in 0..7 {
253-
loop {
254-
match connection.read_response()? {
255-
Some(_) => break,
256-
_ => continue,
257-
}
258-
}
259-
}
260253
Ok(())
261-
})
254+
})?;
255+
for _ in 0..7 {
256+
for _ in 0..100 {
257+
match self.connection.read_response()? {
258+
Some(_) => break,
259+
_ => continue,
260+
}
261+
}
262+
}
263+
Ok(())
262264
}
263265

264266
fn start_connection(&mut self) -> Result<(), Error> {
@@ -541,12 +543,17 @@ impl Flasher {
541543
}
542544

543545
pub fn change_baud(&mut self, speed: BaudRate) -> Result<(), Error> {
546+
let new_speed = (speed.speed() as u32).to_le_bytes();
547+
let old_speed = 0u32.to_le_bytes();
548+
544549
self.connection.command(
545550
Command::ChangeBaud as u8,
546-
&(speed.speed()).to_le_bytes()[..],
551+
&[new_speed, old_speed].concat()[..],
547552
0,
548553
)?;
549554
self.connection.set_baud(speed)?;
555+
std::thread::sleep(Duration::from_secs_f32(0.05));
556+
self.connection.flush()?;
550557
Ok(())
551558
}
552559
}

0 commit comments

Comments
 (0)