diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ee9d94c..03de38f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added reset strategies (#487) - - Read esp-println generated defmt messages (#466) - Add --target-app-partition argument to flash command (#461) - Add --confirm-port argument to flash command (#455) +- Add --chip argument for flash and write-bin commands (#514) ### Fixed diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index df451cdc..27e2f6da 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -49,12 +49,18 @@ pub struct ConnectArgs { /// Baud rate at which to communicate with target device #[arg(short = 'b', long, env = "ESPFLASH_BAUD")] pub baud: Option, + /// Target device + #[arg(short = 'c', long)] + pub chip: Option, + /// Require confirmation before auto-connecting to a recognized device. + #[arg(short = 'C', long)] + pub confirm_port: bool, + /// Do not use the RAM stub for loading + #[arg(long)] + pub no_stub: bool, /// Serial port connected to target device #[arg(short = 'p', long, env = "ESPFLASH_PORT")] pub port: Option, - /// Require confirmation before auto-connecting to a recognized device. - #[arg(short = 'c', long)] - pub confirm_port: bool, /// DTR pin to use for the internal UART hardware. Uses BCM numbering. #[cfg(feature = "raspberry")] #[cfg_attr(feature = "raspberry", clap(long))] @@ -63,9 +69,6 @@ pub struct ConnectArgs { #[cfg(feature = "raspberry")] #[cfg_attr(feature = "raspberry", clap(long))] pub rts: Option, - /// Do not use the RAM stub for loading - #[arg(long)] - pub no_stub: bool, } /// Generate completions for the given shell @@ -89,11 +92,9 @@ pub struct EraseRegionArgs { /// Connection configuration #[clap(flatten)] pub connect_args: ConnectArgs, - /// Offset to start erasing from #[arg(value_name = "OFFSET", value_parser = parse_uint32)] pub addr: u32, - /// Size of the region to erase #[arg(value_name = "SIZE", value_parser = parse_uint32)] pub size: u32, @@ -129,6 +130,9 @@ pub struct FlashArgs { /// Image format to flash #[arg(long, value_enum)] pub format: Option, + /// Logging format. + #[arg(long, short = 'L', default_value = "serial", requires = "monitor")] + pub log_format: LogFormat, /// Open a serial monitor after flashing #[arg(short = 'M', long)] pub monitor: bool, @@ -144,9 +148,6 @@ pub struct FlashArgs { /// Load the application to RAM instead of Flash #[arg(long)] pub ram: bool, - /// Logging format. - #[arg(long, short = 'L', default_value = "serial", requires = "monitor")] - pub log_format: LogFormat, } /// Operations for partitions tables @@ -195,12 +196,12 @@ pub struct SaveImageArgs { /// Open the serial monitor without flashing #[derive(Debug, Args)] pub struct MonitorArgs { - /// Optional file name of the ELF image to load the symbols from - #[arg(short = 'e', long, value_name = "FILE")] - elf: Option, /// Connection configuration #[clap(flatten)] connect_args: ConnectArgs, + /// Optional file name of the ELF image to load the symbols from + #[arg(short = 'e', long, value_name = "FILE")] + elf: Option, /// Logging format. #[arg(long, short = 'L', default_value = "serial")] pub log_format: LogFormat, @@ -247,6 +248,7 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result { port_info, args.baud, !args.no_stub, + args.chip, )?) } diff --git a/espflash/src/error.rs b/espflash/src/error.rs index b2301e26..928c01d1 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -39,6 +39,13 @@ pub enum Error { )] ChipDetectError(u32), + #[error("Chip provided ({0}) with `-c/--chip` does not match the detected chip ({1})")] + #[diagnostic( + code(espflash::chip_missmatch), + help("Ensure that the correct chip is selected, or remove the `-c/--chip` option to autodetect the chip") + )] + ChipMismatch(String, String), + #[error("Supplied ELF image can not be run from RAM, as it includes segments mapped to ROM addresses")] #[diagnostic( code(espflash::not_ram_loadable), diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 67cc98c9..335d0ea0 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -394,6 +394,7 @@ impl Flasher { port_info: UsbPortInfo, speed: Option, use_stub: bool, + chip: Option, ) -> Result { // Establish a connection to the device using the default baud rate of 115,200 // and timeout of 3 seconds. @@ -403,11 +404,19 @@ impl Flasher { // Detect which chip we are connected to. let magic = connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?; - let chip = Chip::from_magic(magic)?; + let detected_chip = Chip::from_magic(magic)?; + if let Some(chip) = chip { + if chip != detected_chip { + return Err(Error::ChipMismatch( + chip.to_string(), + detected_chip.to_string(), + )); + } + } let mut flasher = Flasher { connection, - chip, + chip: detected_chip, flash_size: FlashSize::_4Mb, spi_params: SpiAttachParams::default(), use_stub,