Skip to content

Commit 0f9f0c4

Browse files
committed
split of esp8266 flash target
1 parent 6b34f66 commit 0f9f0c4

File tree

4 files changed

+108
-32
lines changed

4 files changed

+108
-32
lines changed

espflash/src/chip/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
use std::{io::Write, str::FromStr};
1111

1212
use crate::flasher::SpiAttachParams;
13-
use crate::flashtarget::{ChipTarget, FlashTarget, RamTarget};
13+
use crate::flashtarget::{Esp32Target, Esp8266Target, FlashTarget, RamTarget};
1414
pub use esp32::Esp32;
1515
pub use esp32c3::Esp32c3;
1616
pub use esp8266::Esp8266;
@@ -147,7 +147,10 @@ impl Chip {
147147
}
148148

149149
pub fn flash_target(&self, spi_params: SpiAttachParams) -> Box<dyn FlashTarget> {
150-
Box::new(ChipTarget::new(*self, spi_params))
150+
match self {
151+
Chip::Esp8266 => Box::new(Esp8266Target::new()),
152+
_ => Box::new(Esp32Target::new(*self, spi_params)),
153+
}
151154
}
152155
}
153156

espflash/src/flashtarget/chip.rs renamed to espflash/src/flashtarget/esp32.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,26 @@ use crate::flashtarget::{begin_command, block_command, FlashTarget};
66
use crate::Chip;
77
use indicatif::{ProgressBar, ProgressStyle};
88

9-
pub struct ChipTarget {
9+
pub struct Esp32Target {
1010
chip: Chip,
1111
spi_attach_params: SpiAttachParams,
1212
}
1313

14-
impl ChipTarget {
14+
impl Esp32Target {
1515
pub fn new(chip: Chip, spi_attach_params: SpiAttachParams) -> Self {
16-
ChipTarget {
16+
Esp32Target {
1717
chip,
1818
spi_attach_params,
1919
}
2020
}
2121
}
2222

23-
impl FlashTarget for ChipTarget {
23+
impl FlashTarget for Esp32Target {
2424
fn begin(&mut self, connection: &mut Connection, _image: &FirmwareImage) -> Result<(), Error> {
25-
match self.chip {
26-
Chip::Esp8266 => {
27-
begin_command(
28-
connection,
29-
Command::FlashBegin,
30-
0,
31-
0,
32-
FLASH_WRITE_SIZE as u32,
33-
0,
34-
!(self.chip == Chip::Esp32 || self.chip == Chip::Esp8266),
35-
)?;
36-
}
37-
_ => {
38-
let spi_params = self.spi_attach_params.encode();
39-
connection.with_timeout(Command::SpiAttach.timeout(), |connection| {
40-
connection.command(Command::SpiAttach as u8, spi_params.as_slice(), 0)
41-
})?;
42-
}
43-
}
25+
let spi_params = self.spi_attach_params.encode();
26+
connection.with_timeout(Command::SpiAttach.timeout(), |connection| {
27+
connection.command(Command::SpiAttach as u8, spi_params.as_slice(), 0)
28+
})?;
4429
Ok(())
4530
}
4631

@@ -52,10 +37,7 @@ impl FlashTarget for ChipTarget {
5237
let addr = segment.addr;
5338
let block_count = (segment.data.len() + FLASH_WRITE_SIZE - 1) / FLASH_WRITE_SIZE;
5439

55-
let erase_size = match self.chip {
56-
Chip::Esp8266 => get_erase_size(addr as usize, segment.data.len()) as u32,
57-
_ => segment.data.len() as u32,
58-
};
40+
let erase_size = segment.data.len() as u32;
5941

6042
begin_command(
6143
connection,
@@ -64,7 +46,7 @@ impl FlashTarget for ChipTarget {
6446
block_count as u32,
6547
FLASH_WRITE_SIZE as u32,
6648
addr,
67-
!(self.chip == Chip::Esp32 || self.chip == Chip::Esp8266),
49+
self.chip != Chip::Esp32,
6850
)?;
6951

7052
let chunks = segment.data.chunks(FLASH_WRITE_SIZE);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use crate::connection::Connection;
2+
use crate::elf::{FirmwareImage, RomSegment};
3+
use crate::error::Error;
4+
use crate::flasher::{get_erase_size, Command, FLASH_WRITE_SIZE};
5+
use crate::flashtarget::{begin_command, block_command, FlashTarget};
6+
use indicatif::{ProgressBar, ProgressStyle};
7+
8+
pub struct Esp8266Target;
9+
10+
impl Esp8266Target {
11+
pub fn new() -> Self {
12+
Esp8266Target
13+
}
14+
}
15+
16+
impl FlashTarget for Esp8266Target {
17+
fn begin(&mut self, connection: &mut Connection, _image: &FirmwareImage) -> Result<(), Error> {
18+
begin_command(
19+
connection,
20+
Command::FlashBegin,
21+
0,
22+
0,
23+
FLASH_WRITE_SIZE as u32,
24+
0,
25+
false,
26+
)
27+
}
28+
29+
fn write_segment(
30+
&mut self,
31+
connection: &mut Connection,
32+
segment: RomSegment,
33+
) -> Result<(), Error> {
34+
let addr = segment.addr;
35+
let block_count = (segment.data.len() + FLASH_WRITE_SIZE - 1) / FLASH_WRITE_SIZE;
36+
37+
let erase_size = get_erase_size(addr as usize, segment.data.len()) as u32;
38+
39+
begin_command(
40+
connection,
41+
Command::FlashBegin,
42+
erase_size,
43+
block_count as u32,
44+
FLASH_WRITE_SIZE as u32,
45+
addr,
46+
false,
47+
)?;
48+
49+
let chunks = segment.data.chunks(FLASH_WRITE_SIZE);
50+
51+
let (_, chunk_size) = chunks.size_hint();
52+
let chunk_size = chunk_size.unwrap_or(0) as u64;
53+
let pb_chunk = ProgressBar::new(chunk_size);
54+
pb_chunk.set_style(
55+
ProgressStyle::default_bar()
56+
.template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
57+
.progress_chars("#>-"),
58+
);
59+
60+
for (i, block) in chunks.enumerate() {
61+
pb_chunk.set_message(format!("segment 0x{:X} writing chunks", addr));
62+
let block_padding = FLASH_WRITE_SIZE - block.len();
63+
block_command(
64+
connection,
65+
Command::FlashData,
66+
block,
67+
block_padding,
68+
0xff,
69+
i as u32,
70+
)?;
71+
pb_chunk.inc(1);
72+
}
73+
74+
pb_chunk.finish_with_message(format!("segment 0x{:X}", addr));
75+
76+
Ok(())
77+
}
78+
79+
fn finish(&mut self, connection: &mut Connection, reboot: bool) -> Result<(), Error> {
80+
connection.with_timeout(Command::FlashEnd.timeout(), |connection| {
81+
connection.write_command(Command::FlashEnd as u8, &[1][..], 0)
82+
})?;
83+
if reboot {
84+
connection.reset()
85+
} else {
86+
Ok(())
87+
}
88+
}
89+
}

espflash/src/flashtarget/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
mod chip;
1+
mod esp32;
2+
mod esp8266;
23
mod ram;
34

45
use crate::connection::Connection;
56
use crate::elf::{FirmwareImage, RomSegment};
67
use crate::error::Error;
78
use crate::flasher::{checksum, Command, Encoder, CHECKSUM_INIT, FLASH_WRITE_SIZE};
89
use bytemuck::{bytes_of, Pod, Zeroable};
9-
pub use chip::ChipTarget;
10+
pub use esp32::Esp32Target;
11+
pub use esp8266::Esp8266Target;
1012
pub use ram::RamTarget;
1113
use std::mem::size_of;
1214

0 commit comments

Comments
 (0)