Skip to content

Commit

Permalink
Add -c/--chip argument (#514)
Browse files Browse the repository at this point in the history
* feat: Add `--chip` argument

* style: Format args

* docs: Update changelog
  • Loading branch information
SergioGasquez authored Nov 21, 2023
1 parent 2c7ee4c commit 505b98c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 16 additions & 14 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
/// Target device
#[arg(short = 'c', long)]
pub chip: Option<Chip>,
/// 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<String>,
/// 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))]
Expand All @@ -63,9 +69,6 @@ pub struct ConnectArgs {
#[cfg(feature = "raspberry")]
#[cfg_attr(feature = "raspberry", clap(long))]
pub rts: Option<u8>,
/// Do not use the RAM stub for loading
#[arg(long)]
pub no_stub: bool,
}

/// Generate completions for the given shell
Expand All @@ -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,
Expand Down Expand Up @@ -129,6 +130,9 @@ pub struct FlashArgs {
/// Image format to flash
#[arg(long, value_enum)]
pub format: Option<ImageFormatKind>,
/// 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,
Expand All @@ -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
Expand Down Expand Up @@ -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<PathBuf>,
/// 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<PathBuf>,
/// Logging format.
#[arg(long, short = 'L', default_value = "serial")]
pub log_format: LogFormat,
Expand Down Expand Up @@ -247,6 +248,7 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
port_info,
args.baud,
!args.no_stub,
args.chip,
)?)
}

Expand Down
7 changes: 7 additions & 0 deletions espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 11 additions & 2 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ impl Flasher {
port_info: UsbPortInfo,
speed: Option<u32>,
use_stub: bool,
chip: Option<Chip>,
) -> Result<Self, Error> {
// Establish a connection to the device using the default baud rate of 115,200
// and timeout of 3 seconds.
Expand All @@ -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,
Expand Down

0 comments on commit 505b98c

Please sign in to comment.