Skip to content

Commit a9d51de

Browse files
mertzt89ryankurte
authored andcommitted
Fix SpiAttach when using stub
1 parent 57572d9 commit a9d51de

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

espflash/src/chip/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,14 @@ impl Chip {
299299
Box::new(RamTarget::new(entry))
300300
}
301301

302-
pub fn flash_target(&self, spi_params: SpiAttachParams) -> Box<dyn FlashTarget> {
302+
pub fn flash_target(
303+
&self,
304+
spi_params: SpiAttachParams,
305+
use_stub: bool,
306+
) -> Box<dyn FlashTarget> {
303307
match self {
304308
Chip::Esp8266 => Box::new(Esp8266Target::new()),
305-
_ => Box::new(Esp32Target::new(*self, spi_params)),
309+
_ => Box::new(Esp32Target::new(*self, spi_params, use_stub)),
306310
}
307311
}
308312

espflash/src/command.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ pub enum Command<'a> {
112112
SpiAttach {
113113
spi_params: SpiAttachParams,
114114
},
115+
SpiAttachStub {
116+
spi_params: SpiAttachParams,
117+
},
115118
ChangeBaud {
116119
/// New baud rate
117120
new_baud: u32,
@@ -150,6 +153,7 @@ impl<'a> Command<'a> {
150153
Command::WriteReg { .. } => CommandType::WriteReg,
151154
Command::ReadReg { .. } => CommandType::ReadReg,
152155
Command::SpiAttach { .. } => CommandType::SpiAttach,
156+
Command::SpiAttachStub { .. } => CommandType::SpiAttach,
153157
Command::ChangeBaud { .. } => CommandType::ChangeBaud,
154158
Command::FlashDeflateBegin { .. } => CommandType::FlashDeflateBegin,
155159
Command::FlashDeflateData { .. } => CommandType::FlashDeflateData,
@@ -268,9 +272,15 @@ impl<'a> Command<'a> {
268272
write_basic(writer, &address.to_le_bytes(), 0)?;
269273
}
270274
Command::SpiAttach { spi_params } => {
271-
write_basic(writer, &spi_params.encode(), 0)?;
275+
write_basic(writer, &spi_params.encode(false), 0)?;
272276
}
273-
Command::ChangeBaud { new_baud, prior_baud } => {
277+
Command::SpiAttachStub { spi_params } => {
278+
write_basic(writer, &spi_params.encode(true), 0)?;
279+
}
280+
Command::ChangeBaud {
281+
new_baud,
282+
prior_baud,
283+
} => {
274284
// length
275285
writer.write_all(&(8u16.to_le_bytes()))?;
276286
// checksum

espflash/src/flash_target/esp32.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,30 @@ use std::io::Write;
1313
pub struct Esp32Target {
1414
chip: Chip,
1515
spi_attach_params: SpiAttachParams,
16+
use_stub: bool,
1617
}
1718

1819
impl Esp32Target {
19-
pub fn new(chip: Chip, spi_attach_params: SpiAttachParams) -> Self {
20+
pub fn new(chip: Chip, spi_attach_params: SpiAttachParams, use_stub: bool) -> Self {
2021
Esp32Target {
2122
chip,
2223
spi_attach_params,
24+
use_stub,
2325
}
2426
}
2527
}
2628

2729
impl FlashTarget for Esp32Target {
2830
fn begin(&mut self, connection: &mut Connection) -> Result<(), Error> {
2931
connection.with_timeout(CommandType::SpiAttach.timeout(), |connection| {
30-
connection.command(Command::SpiAttach {
31-
spi_params: self.spi_attach_params,
32+
connection.command(if self.use_stub {
33+
Command::SpiAttachStub {
34+
spi_params: self.spi_attach_params,
35+
}
36+
} else {
37+
Command::SpiAttach {
38+
spi_params: self.spi_attach_params,
39+
}
3240
})
3341
})?;
3442

espflash/src/flasher.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,20 @@ impl SpiAttachParams {
138138
}
139139
}
140140

141-
pub fn encode(self) -> Vec<u8> {
141+
pub fn encode(self, stub: bool) -> Vec<u8> {
142142
let packed = ((self.hd as u32) << 24)
143143
| ((self.cs as u32) << 18)
144144
| ((self.d as u32) << 12)
145145
| ((self.q as u32) << 6)
146146
| (self.clk as u32);
147-
if packed == 0 {
148-
vec![0; 5]
149-
} else {
150-
packed.to_le_bytes().to_vec()
147+
148+
let mut encoded: Vec<u8> = packed.to_le_bytes().to_vec();
149+
150+
if !stub {
151+
encoded.append(&mut vec![0u8; 4]);
151152
}
153+
154+
encoded
152155
}
153156
}
154157

@@ -369,7 +372,11 @@ impl Flasher {
369372
_ => {
370373
self.connection
371374
.with_timeout(CommandType::SpiAttach.timeout(), |connection| {
372-
connection.command(Command::SpiAttach { spi_params })
375+
connection.command(if self.use_stub {
376+
Command::SpiAttachStub { spi_params }
377+
} else {
378+
Command::SpiAttach { spi_params }
379+
})
373380
})?;
374381
}
375382
}
@@ -537,7 +544,7 @@ impl Flasher {
537544
) -> Result<(), Error> {
538545
let image = ElfFirmwareImage::try_from(elf_data)?;
539546

540-
let mut target = self.chip.flash_target(self.spi_params);
547+
let mut target = self.chip.flash_target(self.spi_params, self.use_stub);
541548
target.begin(&mut self.connection).flashing()?;
542549

543550
let flash_image = self.chip.get_flash_image(
@@ -564,7 +571,7 @@ impl Flasher {
564571

565572
/// Load an bin image to flash at a specific address
566573
pub fn write_bin_to_flash(&mut self, addr: u32, data: &[u8]) -> Result<(), Error> {
567-
let mut target = self.chip.flash_target(self.spi_params);
574+
let mut target = self.chip.flash_target(self.spi_params, self.use_stub);
568575
target.begin(&mut self.connection).flashing()?;
569576
let segment = RomSegment {
570577
addr,
@@ -606,7 +613,10 @@ impl Flasher {
606613

607614
self.connection
608615
.with_timeout(CommandType::ChangeBaud.timeout(), |connection| {
609-
connection.command(Command::ChangeBaud { new_baud: speed, prior_baud })
616+
connection.command(Command::ChangeBaud {
617+
new_baud: speed,
618+
prior_baud,
619+
})
610620
})?;
611621
self.connection.set_baud(speed)?;
612622
std::thread::sleep(Duration::from_secs_f32(0.05));

0 commit comments

Comments
 (0)