Skip to content

Commit 505b98c

Browse files
Add -c/--chip argument (#514)
* feat: Add `--chip` argument * style: Format args * docs: Update changelog
1 parent 2c7ee4c commit 505b98c

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
- Added reset strategies (#487)
12-
1312
- Read esp-println generated defmt messages (#466)
1413
- Add --target-app-partition argument to flash command (#461)
1514
- Add --confirm-port argument to flash command (#455)
15+
- Add --chip argument for flash and write-bin commands (#514)
1616

1717
### Fixed
1818

espflash/src/cli/mod.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,18 @@ pub struct ConnectArgs {
4949
/// Baud rate at which to communicate with target device
5050
#[arg(short = 'b', long, env = "ESPFLASH_BAUD")]
5151
pub baud: Option<u32>,
52+
/// Target device
53+
#[arg(short = 'c', long)]
54+
pub chip: Option<Chip>,
55+
/// Require confirmation before auto-connecting to a recognized device.
56+
#[arg(short = 'C', long)]
57+
pub confirm_port: bool,
58+
/// Do not use the RAM stub for loading
59+
#[arg(long)]
60+
pub no_stub: bool,
5261
/// Serial port connected to target device
5362
#[arg(short = 'p', long, env = "ESPFLASH_PORT")]
5463
pub port: Option<String>,
55-
/// Require confirmation before auto-connecting to a recognized device.
56-
#[arg(short = 'c', long)]
57-
pub confirm_port: bool,
5864
/// DTR pin to use for the internal UART hardware. Uses BCM numbering.
5965
#[cfg(feature = "raspberry")]
6066
#[cfg_attr(feature = "raspberry", clap(long))]
@@ -63,9 +69,6 @@ pub struct ConnectArgs {
6369
#[cfg(feature = "raspberry")]
6470
#[cfg_attr(feature = "raspberry", clap(long))]
6571
pub rts: Option<u8>,
66-
/// Do not use the RAM stub for loading
67-
#[arg(long)]
68-
pub no_stub: bool,
6972
}
7073

7174
/// Generate completions for the given shell
@@ -89,11 +92,9 @@ pub struct EraseRegionArgs {
8992
/// Connection configuration
9093
#[clap(flatten)]
9194
pub connect_args: ConnectArgs,
92-
9395
/// Offset to start erasing from
9496
#[arg(value_name = "OFFSET", value_parser = parse_uint32)]
9597
pub addr: u32,
96-
9798
/// Size of the region to erase
9899
#[arg(value_name = "SIZE", value_parser = parse_uint32)]
99100
pub size: u32,
@@ -129,6 +130,9 @@ pub struct FlashArgs {
129130
/// Image format to flash
130131
#[arg(long, value_enum)]
131132
pub format: Option<ImageFormatKind>,
133+
/// Logging format.
134+
#[arg(long, short = 'L', default_value = "serial", requires = "monitor")]
135+
pub log_format: LogFormat,
132136
/// Open a serial monitor after flashing
133137
#[arg(short = 'M', long)]
134138
pub monitor: bool,
@@ -144,9 +148,6 @@ pub struct FlashArgs {
144148
/// Load the application to RAM instead of Flash
145149
#[arg(long)]
146150
pub ram: bool,
147-
/// Logging format.
148-
#[arg(long, short = 'L', default_value = "serial", requires = "monitor")]
149-
pub log_format: LogFormat,
150151
}
151152

152153
/// Operations for partitions tables
@@ -195,12 +196,12 @@ pub struct SaveImageArgs {
195196
/// Open the serial monitor without flashing
196197
#[derive(Debug, Args)]
197198
pub struct MonitorArgs {
198-
/// Optional file name of the ELF image to load the symbols from
199-
#[arg(short = 'e', long, value_name = "FILE")]
200-
elf: Option<PathBuf>,
201199
/// Connection configuration
202200
#[clap(flatten)]
203201
connect_args: ConnectArgs,
202+
/// Optional file name of the ELF image to load the symbols from
203+
#[arg(short = 'e', long, value_name = "FILE")]
204+
elf: Option<PathBuf>,
204205
/// Logging format.
205206
#[arg(long, short = 'L', default_value = "serial")]
206207
pub log_format: LogFormat,
@@ -247,6 +248,7 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
247248
port_info,
248249
args.baud,
249250
!args.no_stub,
251+
args.chip,
250252
)?)
251253
}
252254

espflash/src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ pub enum Error {
3939
)]
4040
ChipDetectError(u32),
4141

42+
#[error("Chip provided ({0}) with `-c/--chip` does not match the detected chip ({1})")]
43+
#[diagnostic(
44+
code(espflash::chip_missmatch),
45+
help("Ensure that the correct chip is selected, or remove the `-c/--chip` option to autodetect the chip")
46+
)]
47+
ChipMismatch(String, String),
48+
4249
#[error("Supplied ELF image can not be run from RAM, as it includes segments mapped to ROM addresses")]
4350
#[diagnostic(
4451
code(espflash::not_ram_loadable),

espflash/src/flasher/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ impl Flasher {
394394
port_info: UsbPortInfo,
395395
speed: Option<u32>,
396396
use_stub: bool,
397+
chip: Option<Chip>,
397398
) -> Result<Self, Error> {
398399
// Establish a connection to the device using the default baud rate of 115,200
399400
// and timeout of 3 seconds.
@@ -403,11 +404,19 @@ impl Flasher {
403404

404405
// Detect which chip we are connected to.
405406
let magic = connection.read_reg(CHIP_DETECT_MAGIC_REG_ADDR)?;
406-
let chip = Chip::from_magic(magic)?;
407+
let detected_chip = Chip::from_magic(magic)?;
408+
if let Some(chip) = chip {
409+
if chip != detected_chip {
410+
return Err(Error::ChipMismatch(
411+
chip.to_string(),
412+
detected_chip.to_string(),
413+
));
414+
}
415+
}
407416

408417
let mut flasher = Flasher {
409418
connection,
410-
chip,
419+
chip: detected_chip,
411420
flash_size: FlashSize::_4Mb,
412421
spi_params: SpiAttachParams::default(),
413422
use_stub,

0 commit comments

Comments
 (0)