Skip to content

Commit

Permalink
Add command to erase flash. (For esp-rs#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnross committed Aug 20, 2023
1 parent c80b91b commit 1786b53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
17 changes: 14 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,
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,8 @@ 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),
/// 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 +150,7 @@ 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::Flash(args) => flash(args, &config),
Commands::Monitor(args) => serial_monitor(args, &config),
Commands::PartitionTable(args) => partition_table(args),
Expand All @@ -156,6 +159,14 @@ 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 flash(args: FlashArgs, config: &Config) -> Result<()> {
let mut flasher = connect(&args.connect_args, config)?;

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

#[derive(Debug, Args)]
pub struct EraseFlashArgs {
/// Connection configuration
#[clap(flatten)]
pub connect_args: ConnectArgs,
}

/// 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
10 changes: 10 additions & 0 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,16 @@ impl Flasher {
Ok(())
}

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

pub fn into_interface(self) -> Interface {
self.connection.into_interface()
}
Expand Down

0 comments on commit 1786b53

Please sign in to comment.