Skip to content

Commit d0bd11b

Browse files
Add serial-monitor subcommand (#205)
* Add serial_monitor subcommand * Add serial-monitor documentation * Format subcommands * Fix rustfmt
1 parent 2174374 commit d0bd11b

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

cargo-espflash/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ SUBCOMMANDS:
8181
help Print this message or the help of the given subcommand(s)
8282
partition-table Operations for partitions tables
8383
save-image Save the image to disk instead of flashing to device
84+
serial-monitor Open the serial monitor without flashing
8485
```
8586

8687
## Configuration

cargo-espflash/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use clap::Parser;
1010
use espflash::{
1111
cli::{
1212
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
13-
ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
13+
serial_monitor, ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
1414
},
1515
Chip, Config, ImageFormatId,
1616
};
@@ -57,6 +57,8 @@ pub enum SubCommand {
5757
BoardInfo(ConnectOpts),
5858
/// Save the image to disk instead of flashing to device
5959
SaveImage(SaveImageOpts),
60+
/// Open the serial monitor without flashing
61+
SerialMonitor(ConnectOpts),
6062
/// Operations for partitions tables
6163
PartitionTable(PartitionTableOpts),
6264
}
@@ -132,6 +134,7 @@ fn main() -> Result<()> {
132134
match subcommand {
133135
BoardInfo(opts) => board_info(opts, config),
134136
SaveImage(opts) => save_image(opts, metadata, cargo_config),
137+
SerialMonitor(opts) => serial_monitor(opts, config),
135138
PartitionTable(opts) => partition_table(opts),
136139
}
137140
} else {
@@ -204,7 +207,7 @@ fn flash(
204207

205208
if opts.flash_opts.monitor {
206209
let pid = flasher.get_usb_pid()?;
207-
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
210+
monitor(flasher.into_serial(), Some(&elf_data), pid).into_diagnostic()?;
208211
}
209212

210213
Ok(())

espflash/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ OPTIONS:
6060
Print version information
6161
6262
SUBCOMMANDS:
63-
board-info Display information about the connected board and exit without flashing
64-
help Print this message or the help of the given subcommand(s)
65-
partition-table Operations for partitions tables
66-
save-image Save the image to disk instead of flashing to device
63+
board-info Display information about the connected board and exit without flashing
64+
help Print this message or the help of the given subcommand(s)
65+
partition-table Operations for partitions tables
66+
save-image Save the image to disk instead of flashing to device
67+
serial-monitor Open the serial monitor without flashing
68+
write-bin-to-flash Writes a binary file to a specific address in the chip's flash
6769
```
6870

6971
## Configuration

espflash/src/cli/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use serialport::{FlowControl, SerialPortType, UsbPortInfo};
1616
use strum::VariantNames;
1717

1818
use crate::{
19+
cli::monitor::monitor,
1920
cli::serial::get_serial_port_info,
2021
elf::{ElfFirmwareImage, FlashFrequency, FlashMode},
2122
error::Error,
@@ -138,6 +139,14 @@ pub fn board_info(opts: ConnectOpts, config: Config) -> Result<()> {
138139
Ok(())
139140
}
140141

142+
pub fn serial_monitor(opts: ConnectOpts, config: Config) -> Result<()> {
143+
let flasher = connect(&opts, &config)?;
144+
let pid = flasher.get_usb_pid()?;
145+
monitor(flasher.into_serial(), None, pid).into_diagnostic()?;
146+
147+
Ok(())
148+
}
149+
141150
pub fn save_elf_as_image(
142151
chip: Chip,
143152
elf_data: &[u8],

espflash/src/cli/monitor.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ impl Drop for RawModeGuard {
8383
}
8484
}
8585

86-
pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8], pid: u16) -> serialport::Result<()> {
86+
pub fn monitor(
87+
mut serial: Box<dyn SerialPort>,
88+
elf: Option<&[u8]>,
89+
pid: u16,
90+
) -> serialport::Result<()> {
8791
println!("Commands:");
8892
println!(" CTRL+R Reset chip");
8993
println!(" CTRL+C Exit");
@@ -98,10 +102,14 @@ pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8], pid: u16) -> serialp
98102

99103
let stdout = stdout();
100104
let mut stdout = stdout.lock();
101-
102-
let symbols = load_bin_context(elf).ok();
103-
104-
let mut serial_state = SerialState::new(symbols);
105+
let mut serial_state;
106+
if let Some(elf) = elf {
107+
let symbols = load_bin_context(elf).ok();
108+
serial_state = SerialState::new(symbols);
109+
} else {
110+
serial_state = SerialState::new(None);
111+
reset_after_flash(&mut *serial, pid)?;
112+
}
105113

106114
let mut buff = [0; 1024];
107115
loop {

espflash/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use clap::{IntoApp, Parser};
44
use espflash::{
55
cli::{
66
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
7-
write_bin_to_flash, ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
8-
WriteBinToFlashOpts,
7+
serial_monitor, write_bin_to_flash, ConnectOpts, FlashConfigOpts, FlashOpts,
8+
PartitionTableOpts, WriteBinToFlashOpts,
99
},
1010
Chip, Config, ImageFormatId,
1111
};
@@ -36,6 +36,8 @@ pub enum SubCommand {
3636
BoardInfo(ConnectOpts),
3737
/// Save the image to disk instead of flashing to device
3838
SaveImage(SaveImageOpts),
39+
/// Open the serial monitor without flashing
40+
SerialMonitor(ConnectOpts),
3941
/// Operations for partitions tables
4042
PartitionTable(PartitionTableOpts),
4143
/// Writes a binary file to a specific address in the chip's flash
@@ -97,6 +99,7 @@ fn main() -> Result<()> {
9799
match subcommand {
98100
BoardInfo(opts) => board_info(opts, config),
99101
SaveImage(opts) => save_image(opts),
102+
SerialMonitor(opts) => serial_monitor(opts, config),
100103
PartitionTable(opts) => partition_table(opts),
101104
WriteBinToFlash(opts) => write_bin_to_flash(opts),
102105
}
@@ -145,7 +148,7 @@ fn flash(opts: Opts, config: Config) -> Result<()> {
145148

146149
if opts.flash_opts.monitor {
147150
let pid = flasher.get_usb_pid()?;
148-
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
151+
monitor(flasher.into_serial(), Some(&elf_data), pid).into_diagnostic()?;
149152
}
150153

151154
Ok(())

0 commit comments

Comments
 (0)