Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serial-monitor subcommand #205

Merged
merged 4 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -56,6 +56,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 @@ -128,6 +130,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 @@ -200,7 +203,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 @@ -35,6 +35,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 @@ -94,6 +96,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 @@ -142,7 +145,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