diff --git a/cargo-espflash/README.md b/cargo-espflash/README.md index 732b08fc..7b1fd9ba 100644 --- a/cargo-espflash/README.md +++ b/cargo-espflash/README.md @@ -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 diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 4097c162..56562577 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -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, }; @@ -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), } @@ -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 { @@ -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(()) diff --git a/espflash/README.md b/espflash/README.md index d69bef19..42ce8548 100644 --- a/espflash/README.md +++ b/espflash/README.md @@ -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 diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index d1d3ed0b..7ed5e817 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -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, @@ -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], diff --git a/espflash/src/cli/monitor.rs b/espflash/src/cli/monitor.rs index b5170ef7..f38e4738 100644 --- a/espflash/src/cli/monitor.rs +++ b/espflash/src/cli/monitor.rs @@ -83,7 +83,11 @@ impl Drop for RawModeGuard { } } -pub fn monitor(mut serial: Box, elf: &[u8], pid: u16) -> serialport::Result<()> { +pub fn monitor( + mut serial: Box, + elf: Option<&[u8]>, + pid: u16, +) -> serialport::Result<()> { println!("Commands:"); println!(" CTRL+R Reset chip"); println!(" CTRL+C Exit"); @@ -98,10 +102,14 @@ pub fn monitor(mut serial: Box, 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 { diff --git a/espflash/src/main.rs b/espflash/src/main.rs index 47bc3301..6b8a8293 100644 --- a/espflash/src/main.rs +++ b/espflash/src/main.rs @@ -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, }; @@ -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 @@ -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), } @@ -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(())