From 24ddb3fb2ac86cfa53212e002f1a5880c86a1298 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Tue, 10 Oct 2023 09:49:10 +0200 Subject: [PATCH 1/2] feat: Sent SpiSetParams --- espflash/src/command.rs | 9 ++++++- espflash/src/connection.rs | 3 ++- espflash/src/flasher/mod.rs | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/espflash/src/command.rs b/espflash/src/command.rs index d581b510..a5aa7ddc 100644 --- a/espflash/src/command.rs +++ b/espflash/src/command.rs @@ -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); @@ -120,6 +120,9 @@ pub enum Command<'a> { ReadReg { address: u32, }, + SpiSetParams { + spi_params: SpiSetParams, + }, SpiAttach { spi_params: SpiAttachParams, }, @@ -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, @@ -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)?; } diff --git a/espflash/src/connection.rs b/espflash/src/connection.rs index 899fb0dc..1835dcdf 100644 --- a/espflash/src/connection.rs +++ b/espflash/src/connection.rs @@ -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; @@ -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)?; diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 7399dd69..07970d89 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -229,6 +229,44 @@ 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, + } + } + + // https://github.com/espressif/esptool/blob/master/esptool/loader.py#L1197-L1205 + /// Encode the parameters into a byte array + pub fn encode(&self) -> Vec { + let mut encoded: Vec = 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)] @@ -492,6 +530,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(()); } From bb7f13c507e0d44d482fc498044e27a312581619 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Wed, 11 Oct 2023 09:57:15 +0200 Subject: [PATCH 2/2] chore: Remove esptool link --- espflash/src/flasher/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 07970d89..705e0e61 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -253,7 +253,6 @@ impl SpiSetParams { } } - // https://github.com/espressif/esptool/blob/master/esptool/loader.py#L1197-L1205 /// Encode the parameters into a byte array pub fn encode(&self) -> Vec { let mut encoded: Vec = Vec::new();