Skip to content
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
9 changes: 8 additions & 1 deletion espflash/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{io::Write, mem::size_of, time::Duration};
use bytemuck::{bytes_of, Pod, Zeroable};
use strum::Display;

use crate::flasher::{checksum, SpiAttachParams, CHECKSUM_INIT};
use crate::flasher::{checksum, SpiAttachParams, SpiSetParams, CHECKSUM_INIT};

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(3);
const ERASE_REGION_TIMEOUT_PER_MB: Duration = Duration::from_secs(30);
Expand Down Expand Up @@ -120,6 +120,9 @@ pub enum Command<'a> {
ReadReg {
address: u32,
},
SpiSetParams {
spi_params: SpiSetParams,
},
SpiAttach {
spi_params: SpiAttachParams,
},
Expand Down Expand Up @@ -169,6 +172,7 @@ impl<'a> Command<'a> {
Command::Sync => CommandType::Sync,
Command::WriteReg { .. } => CommandType::WriteReg,
Command::ReadReg { .. } => CommandType::ReadReg,
Command::SpiSetParams { .. } => CommandType::SpiSetParams,
Command::SpiAttach { .. } => CommandType::SpiAttach,
Command::SpiAttachStub { .. } => CommandType::SpiAttach,
Command::ChangeBaud { .. } => CommandType::ChangeBaud,
Expand Down Expand Up @@ -292,6 +296,9 @@ impl<'a> Command<'a> {
Command::ReadReg { address } => {
write_basic(writer, &address.to_le_bytes(), 0)?;
}
Command::SpiSetParams { spi_params } => {
write_basic(writer, &spi_params.encode(), 0)?;
}
Command::SpiAttach { spi_params } => {
write_basic(writer, &spi_params.encode(false), 0)?;
}
Expand Down
3 changes: 2 additions & 1 deletion espflash/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::{io::BufWriter, thread::sleep, time::Duration};

use binrw::{io::Cursor, BinRead, BinReaderExt};
use log::info;
use log::{debug, info};
use serialport::UsbPortInfo;
use slip_codec::SlipDecoder;

Expand Down Expand Up @@ -210,6 +210,7 @@ impl Connection {

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

serial.clear(serialport::ClearBuffer::Input)?;
Expand Down
47 changes: 47 additions & 0 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,43 @@ impl FromStr for FlashSize {
}
}

/// Parameters of the attached SPI flash chip (sizes, etc).
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct SpiSetParams {
fl_id: u32,
total_size: u32,
block_size: u32,
sector_size: u32,
page_size: u32,
status_mask: u32,
}

impl SpiSetParams {
pub const fn default(size: u32) -> Self {
SpiSetParams {
fl_id: 0,
total_size: size,
block_size: 64 * 1024,
sector_size: 4 * 1024,
page_size: 256,
status_mask: 0xFFFF,
}
}

/// Encode the parameters into a byte array
pub fn encode(&self) -> Vec<u8> {
let mut encoded: Vec<u8> = Vec::new();
encoded.extend_from_slice(&self.fl_id.to_le_bytes());
encoded.extend_from_slice(&self.total_size.to_le_bytes());
encoded.extend_from_slice(&self.block_size.to_le_bytes());
encoded.extend_from_slice(&self.sector_size.to_le_bytes());
encoded.extend_from_slice(&self.page_size.to_le_bytes());
encoded.extend_from_slice(&self.status_mask.to_le_bytes());
encoded
}
}

/// Parameters for attaching to a target devices SPI flash
#[derive(Copy, Clone, Debug)]
#[repr(C)]
Expand Down Expand Up @@ -492,6 +529,16 @@ impl Flasher {
self.flash_size = flash_size;
self.spi_params = spi_params;

let spi_set_params = SpiSetParams::default(self.flash_size.size());
self.connection.with_timeout(
CommandType::SpiSetParams.timeout(),
|connection| {
connection.command(Command::SpiSetParams {
spi_params: spi_set_params,
})
},
)?;

return Ok(());
}

Expand Down