Skip to content

Commit cd21e74

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

File tree

4 files changed

+46
-63
lines changed

4 files changed

+46
-63
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: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct FlashArgs {
124124
pub flash_config_args: FlashConfigArgs,
125125
/// Flashing arguments
126126
#[clap(flatten)]
127-
pub flash_args: cli::FlashArgs,
127+
flash_args: cli::FlashArgs,
128128
/// ELF image to flash
129129
image: PathBuf,
130130
}
@@ -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: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,9 @@ pub struct FlashArgs {
136136
/// Erase specified data partitions
137137
#[arg(long, value_name = "PARTS", value_enum, value_delimiter = ',')]
138138
pub erase_data_parts: Option<Vec<DataType>>,
139-
// /// Logging format.
140-
// #[arg(long, short = 'L', default_value = "serial", requires = "monitor")]
141-
// pub log_format: LogFormat,
142139
/// Open a serial monitor after flashing
143140
#[arg(short = 'M', long)]
144141
pub monitor: bool,
145-
// /// Baud rate at which to read console output
146-
// #[arg(long, requires = "monitor", value_name = "BAUD")]
147-
// pub monitor_baud: Option<u32>,
148-
// /// Avoids asking the user for interactions like resetting the device
149-
// #[arg(long, requires = "monitor")]
150-
// pub non_interactive: bool,
151142
/// Monitor configuration
152143
#[clap(flatten)]
153144
pub monitor_args: MonitorConfigArgs,
@@ -162,9 +153,6 @@ pub struct FlashArgs {
162153
pub no_skip: bool,
163154
#[clap(flatten)]
164155
pub image: ImageArgs,
165-
/// External log processors to use (comma separated executables)
166-
#[arg(long, requires = "monitor")]
167-
pub processors: Option<String>,
168156
}
169157

170158
/// Operations for partitions tables
@@ -273,24 +261,21 @@ pub struct MonitorArgs {
273261
#[derive(Debug, Args)]
274262
#[non_exhaustive]
275263
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>,
282264
/// Baud rate at which to communicate with target device
283-
#[arg(short = 'B', long, env = "MONITOR_BAUD")]
284-
pub baud: Option<u32>,
265+
#[arg(short = 'r', long, env = "MONITOR_BAUD", default_value = "115_200", value_parser = parse_uint32)]
266+
pub baud_rate: u32,
285267
/// Avoids asking the user for interactions like resetting the device
286268
#[arg(long)]
287-
pub non_interactive: bool,
269+
non_interactive: bool,
270+
/// Avoids restarting the device before monitoring
271+
#[arg(long, requires = "non_interactive")]
272+
no_reset: bool,
288273
/// Logging format.
289274
#[arg(long, short = 'L', default_value = "serial", requires = "elf")]
290-
pub log_format: LogFormat,
275+
log_format: LogFormat,
291276
/// External log processors to use (comma separated executables)
292277
#[arg(long)]
293-
pub processors: Option<String>,
278+
processors: Option<String>,
294279
}
295280

296281
#[derive(Debug, Args)]
@@ -459,24 +444,22 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> {
459444
let chip = flasher.chip();
460445
let target = chip.into_target();
461446

447+
let mut monitor_args = args.monitor_args;
448+
462449
// The 26MHz ESP32-C2's need to be treated as a special case.
463-
let default_baud = if chip == Chip::Esp32c2
450+
if chip == Chip::Esp32c2
464451
&& target.crystal_freq(flasher.connection())? == XtalFrequency::_26Mhz
452+
&& monitor_args.baud_rate == 115_200
465453
{
466454
// 115_200 * 26 MHz / 40 MHz = 74_880
467-
74_880
468-
} else {
469-
115_200
470-
};
455+
monitor_args.baud_rate = 74_880;
456+
}
471457

472458
monitor(
473459
flasher.into_serial(),
474460
elf.as_deref(),
475461
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,
462+
monitor_args,
480463
args.elf,
481464
)
482465
}

espflash/src/cli/monitor/mod.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ use crossterm::{
2222
terminal::{disable_raw_mode, enable_raw_mode},
2323
};
2424
use external_processors::ExternalProcessors;
25-
use log::error;
25+
use log::{debug, error};
2626
use miette::{IntoDiagnostic, Result};
2727
#[cfg(feature = "serialport")]
2828
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,21 +77,20 @@ 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-
println!("Baud rate: {}", baud);
91+
92+
let baud = monitor_args.baud_rate;
93+
debug!("Opening serial monitor with baudrate: {}", baud);
9294

9395
// Explicitly set the baud rate when starting the serial monitor, to allow using
9496
// different rates for flashing.
@@ -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)