Skip to content

Commit 632c7d5

Browse files
Send SpiSetParams command (#489)
* feat: Sent SpiSetParams * chore: Remove esptool link
1 parent 1dfe0a1 commit 632c7d5

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

espflash/src/command.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{io::Write, mem::size_of, time::Duration};
55
use bytemuck::{bytes_of, Pod, Zeroable};
66
use strum::Display;
77

8-
use crate::flasher::{checksum, SpiAttachParams, CHECKSUM_INIT};
8+
use crate::flasher::{checksum, SpiAttachParams, SpiSetParams, CHECKSUM_INIT};
99

1010
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(3);
1111
const ERASE_REGION_TIMEOUT_PER_MB: Duration = Duration::from_secs(30);
@@ -122,6 +122,9 @@ pub enum Command<'a> {
122122
ReadReg {
123123
address: u32,
124124
},
125+
SpiSetParams {
126+
spi_params: SpiSetParams,
127+
},
125128
SpiAttach {
126129
spi_params: SpiAttachParams,
127130
},
@@ -171,6 +174,7 @@ impl<'a> Command<'a> {
171174
Command::Sync => CommandType::Sync,
172175
Command::WriteReg { .. } => CommandType::WriteReg,
173176
Command::ReadReg { .. } => CommandType::ReadReg,
177+
Command::SpiSetParams { .. } => CommandType::SpiSetParams,
174178
Command::SpiAttach { .. } => CommandType::SpiAttach,
175179
Command::SpiAttachStub { .. } => CommandType::SpiAttach,
176180
Command::ChangeBaud { .. } => CommandType::ChangeBaud,
@@ -294,6 +298,9 @@ impl<'a> Command<'a> {
294298
Command::ReadReg { address } => {
295299
write_basic(writer, &address.to_le_bytes(), 0)?;
296300
}
301+
Command::SpiSetParams { spi_params } => {
302+
write_basic(writer, &spi_params.encode(), 0)?;
303+
}
297304
Command::SpiAttach { spi_params } => {
298305
write_basic(writer, &spi_params.encode(false), 0)?;
299306
}

espflash/src/connection.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use std::{io::BufWriter, thread::sleep, time::Duration};
88

99
use binrw::{io::Cursor, BinRead, BinReaderExt};
10-
use log::info;
10+
use log::{debug, info};
1111
use serialport::UsbPortInfo;
1212
use slip_codec::SlipDecoder;
1313

@@ -210,6 +210,7 @@ impl Connection {
210210

211211
/// Write a command to the serial port
212212
pub fn write_command(&mut self, command: Command) -> Result<(), Error> {
213+
debug!("Writing command: {:?}", command);
213214
let serial = self.serial.serial_port_mut();
214215

215216
serial.clear(serialport::ClearBuffer::Input)?;

espflash/src/flasher/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,43 @@ impl FromStr for FlashSize {
229229
}
230230
}
231231

232+
/// Parameters of the attached SPI flash chip (sizes, etc).
233+
#[derive(Copy, Clone, Debug)]
234+
#[repr(C)]
235+
pub struct SpiSetParams {
236+
fl_id: u32,
237+
total_size: u32,
238+
block_size: u32,
239+
sector_size: u32,
240+
page_size: u32,
241+
status_mask: u32,
242+
}
243+
244+
impl SpiSetParams {
245+
pub const fn default(size: u32) -> Self {
246+
SpiSetParams {
247+
fl_id: 0,
248+
total_size: size,
249+
block_size: 64 * 1024,
250+
sector_size: 4 * 1024,
251+
page_size: 256,
252+
status_mask: 0xFFFF,
253+
}
254+
}
255+
256+
/// Encode the parameters into a byte array
257+
pub fn encode(&self) -> Vec<u8> {
258+
let mut encoded: Vec<u8> = Vec::new();
259+
encoded.extend_from_slice(&self.fl_id.to_le_bytes());
260+
encoded.extend_from_slice(&self.total_size.to_le_bytes());
261+
encoded.extend_from_slice(&self.block_size.to_le_bytes());
262+
encoded.extend_from_slice(&self.sector_size.to_le_bytes());
263+
encoded.extend_from_slice(&self.page_size.to_le_bytes());
264+
encoded.extend_from_slice(&self.status_mask.to_le_bytes());
265+
encoded
266+
}
267+
}
268+
232269
/// Parameters for attaching to a target devices SPI flash
233270
#[derive(Copy, Clone, Debug)]
234271
#[repr(C)]
@@ -492,6 +529,16 @@ impl Flasher {
492529
self.flash_size = flash_size;
493530
self.spi_params = spi_params;
494531

532+
let spi_set_params = SpiSetParams::default(self.flash_size.size());
533+
self.connection.with_timeout(
534+
CommandType::SpiSetParams.timeout(),
535+
|connection| {
536+
connection.command(Command::SpiSetParams {
537+
spi_params: spi_set_params,
538+
})
539+
},
540+
)?;
541+
495542
return Ok(());
496543
}
497544

0 commit comments

Comments
 (0)