Skip to content

Commit e857dd4

Browse files
committed
feat: Use monitor_args in monitor()
1 parent 6901915 commit e857dd4

File tree

4 files changed

+40
-45
lines changed

4 files changed

+40
-45
lines changed

cargo-espflash/src/main.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,23 +352,22 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
352352

353353
if args.flash_args.monitor {
354354
let pid = flasher.get_usb_pid()?;
355+
let mut monitor_args = args.flash_args.monitor_args;
355356

356357
// The 26MHz ESP32-C2's need to be treated as a special case.
357-
let default_baud = if chip == Chip::Esp32c2 && target_xtal_freq == XtalFrequency::_26Mhz {
358+
if chip == Chip::Esp32c2
359+
&& target_xtal_freq == XtalFrequency::_26Mhz
360+
&& monitor_args.baud_rate == 115_200
361+
{
358362
// 115_200 * 26 MHz / 40 MHz = 74_880
359-
74_880
360-
} else {
361-
115_200
362-
};
363+
monitor_args.baud_rate = 74_880;
364+
}
363365

364366
monitor(
365367
flasher.into_serial(),
366368
Some(&elf_data),
367369
pid,
368-
args.flash_args.monitor_args.baud.unwrap_or(default_baud),
369-
args.flash_args.monitor_args.log_format,
370-
!args.flash_args.monitor_args.non_interactive,
371-
args.flash_args.monitor_args.processors,
370+
monitor_args,
372371
Some(build_ctx.artifact_path),
373372
)
374373
} else {

espflash/src/bin/espflash.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,22 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
287287

288288
if args.flash_args.monitor {
289289
let pid = flasher.get_usb_pid()?;
290+
let mut monitor_args = args.flash_args.monitor_args;
290291

291292
// The 26MHz ESP32-C2's need to be treated as a special case.
292-
let default_baud = if chip == Chip::Esp32c2 && target_xtal_freq == XtalFrequency::_26Mhz {
293+
if chip == Chip::Esp32c2
294+
&& target_xtal_freq == XtalFrequency::_26Mhz
295+
&& monitor_args.baud_rate == 115_200
296+
{
293297
// 115_200 * 26 MHz / 40 MHz = 74_880
294-
74_880
295-
} else {
296-
115_200
297-
};
298+
monitor_args.baud_rate = 74_880;
299+
}
298300

299301
monitor(
300302
flasher.into_serial(),
301303
Some(&elf_data),
302304
pid,
303-
args.flash_args.monitor_args.baud.unwrap_or(default_baud),
304-
args.flash_args.monitor_args.log_format,
305-
!args.flash_args.monitor_args.non_interactive,
306-
args.flash_args.monitor_args.processors,
305+
monitor_args,
307306
Some(args.image),
308307
)
309308
} else {

espflash/src/cli/mod.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,15 @@ pub struct MonitorArgs {
273273
#[derive(Debug, Args)]
274274
#[non_exhaustive]
275275
pub struct MonitorConfigArgs {
276-
// /// Connection configuration
277-
// #[clap(flatten)]
278-
// connect_args: ConnectArgs,
279-
// /// Optional file name of the ELF image to load the symbols from
280-
// #[arg(short = 'e', long, value_name = "FILE")]
281-
// elf: Option<PathBuf>,
282276
/// Baud rate at which to communicate with target device
283-
#[arg(short = 'B', long, env = "MONITOR_BAUD")]
284-
pub baud: Option<u32>,
277+
#[arg(short = 'r', long, env = "MONITOR_BAUD", default_value = "115_200", value_parser = parse_uint32)]
278+
pub baud_rate: u32,
285279
/// Avoids asking the user for interactions like resetting the device
286280
#[arg(long)]
287281
pub non_interactive: bool,
282+
/// Avoids restarting the device before monitoring
283+
#[arg(long, requires = "non_interactive")]
284+
pub no_reset: bool,
288285
/// Logging format.
289286
#[arg(long, short = 'L', default_value = "serial", requires = "elf")]
290287
pub log_format: LogFormat,
@@ -459,24 +456,22 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
459456
let chip = flasher.chip();
460457
let target = chip.into_target();
461458

459+
let mut monitor_args = args.monitor_args;
460+
462461
// The 26MHz ESP32-C2's need to be treated as a special case.
463-
let default_baud = if chip == Chip::Esp32c2
462+
if chip == Chip::Esp32c2
464463
&& target.crystal_freq(flasher.connection())? == XtalFrequency::_26Mhz
464+
&& monitor_args.baud_rate == 115_200
465465
{
466466
// 115_200 * 26 MHz / 40 MHz = 74_880
467-
74_880
468-
} else {
469-
115_200
470-
};
467+
monitor_args.baud_rate = 74_880;
468+
}
471469

472470
monitor(
473471
flasher.into_serial(),
474472
elf.as_deref(),
475473
pid,
476-
args.monitor_args.baud.unwrap_or(default_baud),
477-
args.monitor_args.log_format,
478-
!args.monitor_args.non_interactive,
479-
args.monitor_args.processors,
474+
monitor_args,
480475
args.elf,
481476
)
482477
}

espflash/src/cli/monitor/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ use serialport::SerialPort;
2929
use strum::{Display, EnumIter, EnumString, VariantNames};
3030

3131
use crate::{
32-
cli::monitor::parser::{InputParser, ResolvingPrinter},
32+
cli::{
33+
monitor::parser::{InputParser, ResolvingPrinter},
34+
MonitorConfigArgs,
35+
},
3336
connection::{reset::reset_after_flash, Port},
3437
};
3538

@@ -74,20 +77,19 @@ pub fn monitor(
7477
mut serial: Port,
7578
elf: Option<&[u8]>,
7679
pid: u16,
77-
baud: u32,
78-
log_format: LogFormat,
79-
interactive_mode: bool,
80-
processors: Option<String>,
80+
monitor_args: MonitorConfigArgs,
8181
elf_file: Option<PathBuf>,
8282
) -> miette::Result<()> {
83-
if interactive_mode {
83+
if !monitor_args.non_interactive {
8484
println!("Commands:");
8585
println!(" CTRL+R Reset chip");
8686
println!(" CTRL+C Exit");
8787
println!();
88-
} else {
88+
} else if !monitor_args.no_reset {
8989
reset_after_flash(&mut serial, pid).into_diagnostic()?;
9090
}
91+
92+
let baud = monitor_args.baud_rate;
9193
println!("Baud rate: {}", baud);
9294

9395
// Explicitly set the baud rate when starting the serial monitor, to allow using
@@ -103,12 +105,12 @@ pub fn monitor(
103105
let stdout = stdout();
104106
let mut stdout = ResolvingPrinter::new(elf, stdout.lock());
105107

106-
let mut parser: Box<dyn InputParser> = match log_format {
108+
let mut parser: Box<dyn InputParser> = match monitor_args.log_format {
107109
LogFormat::Defmt => Box::new(parser::esp_defmt::EspDefmt::new(elf)?),
108110
LogFormat::Serial => Box::new(parser::serial::Serial),
109111
};
110112

111-
let mut external_processors = ExternalProcessors::new(processors, elf_file)?;
113+
let mut external_processors = ExternalProcessors::new(monitor_args.processors, elf_file)?;
112114

113115
let mut buff = [0; 1024];
114116
loop {
@@ -125,7 +127,7 @@ pub fn monitor(
125127
// Don't forget to flush the writer!
126128
stdout.flush().ok();
127129

128-
if interactive_mode && poll(Duration::from_secs(0)).into_diagnostic()? {
130+
if !monitor_args.non_interactive && poll(Duration::from_secs(0)).into_diagnostic()? {
129131
if let Event::Key(key) = read().into_diagnostic()? {
130132
if key.kind == KeyEventKind::Press {
131133
if key.modifiers.contains(KeyModifiers::CONTROL) {

0 commit comments

Comments
 (0)