Skip to content

Commit

Permalink
Add serial-monitor subcommand (#205)
Browse files Browse the repository at this point in the history
* Add serial_monitor subcommand

* Add serial-monitor documentation

* Format subcommands

* Fix rustfmt
  • Loading branch information
SergioGasquez committed Jul 6, 2022
1 parent 2174374 commit d0bd11b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions cargo-espflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ SUBCOMMANDS:
help Print this message or the help of the given subcommand(s)
partition-table Operations for partitions tables
save-image Save the image to disk instead of flashing to device
serial-monitor Open the serial monitor without flashing
```

## Configuration
Expand Down
7 changes: 5 additions & 2 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use clap::Parser;
use espflash::{
cli::{
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
serial_monitor, ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
},
Chip, Config, ImageFormatId,
};
Expand Down Expand Up @@ -57,6 +57,8 @@ pub enum SubCommand {
BoardInfo(ConnectOpts),
/// Save the image to disk instead of flashing to device
SaveImage(SaveImageOpts),
/// Open the serial monitor without flashing
SerialMonitor(ConnectOpts),
/// Operations for partitions tables
PartitionTable(PartitionTableOpts),
}
Expand Down Expand Up @@ -132,6 +134,7 @@ fn main() -> Result<()> {
match subcommand {
BoardInfo(opts) => board_info(opts, config),
SaveImage(opts) => save_image(opts, metadata, cargo_config),
SerialMonitor(opts) => serial_monitor(opts, config),
PartitionTable(opts) => partition_table(opts),
}
} else {
Expand Down Expand Up @@ -204,7 +207,7 @@ fn flash(

if opts.flash_opts.monitor {
let pid = flasher.get_usb_pid()?;
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
monitor(flasher.into_serial(), Some(&elf_data), pid).into_diagnostic()?;
}

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions espflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ OPTIONS:
Print version information
SUBCOMMANDS:
board-info Display information about the connected board and exit without flashing
help Print this message or the help of the given subcommand(s)
partition-table Operations for partitions tables
save-image Save the image to disk instead of flashing to device
board-info Display information about the connected board and exit without flashing
help Print this message or the help of the given subcommand(s)
partition-table Operations for partitions tables
save-image Save the image to disk instead of flashing to device
serial-monitor Open the serial monitor without flashing
write-bin-to-flash Writes a binary file to a specific address in the chip's flash
```

## Configuration
Expand Down
9 changes: 9 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use serialport::{FlowControl, SerialPortType, UsbPortInfo};
use strum::VariantNames;

use crate::{
cli::monitor::monitor,
cli::serial::get_serial_port_info,
elf::{ElfFirmwareImage, FlashFrequency, FlashMode},
error::Error,
Expand Down Expand Up @@ -138,6 +139,14 @@ pub fn board_info(opts: ConnectOpts, config: Config) -> Result<()> {
Ok(())
}

pub fn serial_monitor(opts: ConnectOpts, config: Config) -> Result<()> {
let flasher = connect(&opts, &config)?;
let pid = flasher.get_usb_pid()?;
monitor(flasher.into_serial(), None, pid).into_diagnostic()?;

Ok(())
}

pub fn save_elf_as_image(
chip: Chip,
elf_data: &[u8],
Expand Down
18 changes: 13 additions & 5 deletions espflash/src/cli/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ impl Drop for RawModeGuard {
}
}

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

let stdout = stdout();
let mut stdout = stdout.lock();

let symbols = load_bin_context(elf).ok();

let mut serial_state = SerialState::new(symbols);
let mut serial_state;
if let Some(elf) = elf {
let symbols = load_bin_context(elf).ok();
serial_state = SerialState::new(symbols);
} else {
serial_state = SerialState::new(None);
reset_after_flash(&mut *serial, pid)?;
}

let mut buff = [0; 1024];
loop {
Expand Down
9 changes: 6 additions & 3 deletions espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use clap::{IntoApp, Parser};
use espflash::{
cli::{
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
write_bin_to_flash, ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
WriteBinToFlashOpts,
serial_monitor, write_bin_to_flash, ConnectOpts, FlashConfigOpts, FlashOpts,
PartitionTableOpts, WriteBinToFlashOpts,
},
Chip, Config, ImageFormatId,
};
Expand Down Expand Up @@ -36,6 +36,8 @@ pub enum SubCommand {
BoardInfo(ConnectOpts),
/// Save the image to disk instead of flashing to device
SaveImage(SaveImageOpts),
/// Open the serial monitor without flashing
SerialMonitor(ConnectOpts),
/// Operations for partitions tables
PartitionTable(PartitionTableOpts),
/// Writes a binary file to a specific address in the chip's flash
Expand Down Expand Up @@ -97,6 +99,7 @@ fn main() -> Result<()> {
match subcommand {
BoardInfo(opts) => board_info(opts, config),
SaveImage(opts) => save_image(opts),
SerialMonitor(opts) => serial_monitor(opts, config),
PartitionTable(opts) => partition_table(opts),
WriteBinToFlash(opts) => write_bin_to_flash(opts),
}
Expand Down Expand Up @@ -145,7 +148,7 @@ fn flash(opts: Opts, config: Config) -> Result<()> {

if opts.flash_opts.monitor {
let pid = flasher.get_usb_pid()?;
monitor(flasher.into_serial(), &elf_data, pid).into_diagnostic()?;
monitor(flasher.into_serial(), Some(&elf_data), pid).into_diagnostic()?;
}

Ok(())
Expand Down

0 comments on commit d0bd11b

Please sign in to comment.