Skip to content

Commit

Permalink
Erase flash (#3)
Browse files Browse the repository at this point in the history
* Add command to erase flash. (For esp-rs#460)

* Add `erase-parts` command to erase named partitions.

* Fix timeout logic when erasing regions.
  • Loading branch information
jnross committed Aug 22, 2023
1 parent 779ffb0 commit f59218f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
33 changes: 30 additions & 3 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use espflash::{
cli::{
self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image,
monitor::monitor, parse_partition_table, partition_table, print_board_info,
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress,
FlashConfigArgs, MonitorArgs, PartitionTableArgs,
save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EraseFlashArgs,
ErasePartsArgs, EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs,
},
image_format::ImageFormatKind,
logging::initialize_logger,
targets::Chip,
update::check_for_update,
};
use log::{debug, LevelFilter};
use log::{debug, info, LevelFilter};
use miette::{IntoDiagnostic, Result, WrapErr};

#[derive(Debug, Parser)]
Expand All @@ -42,6 +42,10 @@ enum Commands {
/// depending on which shell is being used; consult your shell's
/// documentation to determine the appropriate path.
Completions(CompletionsArgs),
/// Erase Flash entirely
EraseFlash(EraseFlashArgs),
/// Erase specified partitions
EraseParts(ErasePartsArgs),
/// Flash an application in ELF format to a connected target device
///
/// Given a path to an ELF file, first convert it into the appropriate
Expand Down Expand Up @@ -148,6 +152,8 @@ fn main() -> Result<()> {
match args {
Commands::BoardInfo(args) => board_info(&args, &config),
Commands::Completions(args) => completions(&args, &mut Cli::command(), "espflash"),
Commands::EraseFlash(args) => erase_flash(args, &config),
Commands::EraseParts(args) => erase_parts(args, &config),
Commands::Flash(args) => flash(args, &config),
Commands::Monitor(args) => serial_monitor(args, &config),
Commands::PartitionTable(args) => partition_table(args),
Expand All @@ -156,6 +162,27 @@ fn main() -> Result<()> {
}
}

fn erase_flash(args: EraseFlashArgs, config: &Config) -> Result<()> {
info!("Erasing Flash...");
let mut flash = connect(&args.connect_args, config)?;
flash.erase_flash()?;

Ok(())
}

fn erase_parts(args: ErasePartsArgs, config: &Config) -> Result<()> {
let mut flash = connect(&args.connect_args, config)?;
let partition_table = parse_partition_table(&args.partition_table)?;
erase_partitions(
&mut flash,
Some(partition_table),
Some(args.erase_parts),
None,
)?;

Ok(())
}

fn flash(args: FlashArgs, config: &Config) -> Result<()> {
let mut flasher = connect(&args.connect_args, config)?;

Expand Down
23 changes: 23 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ pub struct MonitorArgs {
connect_args: ConnectArgs,
}

/// Erase entire flash of target device
#[derive(Debug, Args)]
pub struct EraseFlashArgs {
/// Connection configuration
#[clap(flatten)]
pub connect_args: ConnectArgs,
}

/// Erase named partitions based on provided partition table
#[derive(Debug, Args)]
pub struct ErasePartsArgs {
/// Connection configuration
#[clap(flatten)]
pub connect_args: ConnectArgs,

#[arg(value_name = "LABELS", value_delimiter = ',')]
pub erase_parts: Vec<String>,

/// Input partition table
#[arg(long, value_name = "FILE")]
pub partition_table: PathBuf,
}

/// Select a serial port and establish a connection with a target device
pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
let port_info = get_serial_port_info(args, config)?;
Expand Down
16 changes: 13 additions & 3 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,21 @@ impl Flasher {
pub fn erase_region(&mut self, offset: u32, size: u32) -> Result<(), Error> {
debug!("Erasing region of 0x{:x}B at 0x{:08x}", size, offset);

self.connection.with_timeout(
CommandType::EraseRegion.timeout_for_size(size),
|connection| connection.command(Command::EraseRegion { offset, size }),
)?;
std::thread::sleep(Duration::from_secs_f32(0.05));
self.connection.flush()?;
Ok(())
}

pub fn erase_flash(&mut self) -> Result<(), Error> {
self.connection
.with_timeout(CommandType::EraseRegion.timeout(), |connection| {
connection.command(Command::EraseRegion { offset, size })
.with_timeout(CommandType::EraseFlash.timeout(), |connection| {
connection.command(Command::EraseFlash)
})?;
std::thread::sleep(Duration::from_secs_f32(0.05));
sleep(Duration::from_secs_f32(0.05));
self.connection.flush()?;
Ok(())
}
Expand Down

0 comments on commit f59218f

Please sign in to comment.