diff --git a/Cargo.lock b/Cargo.lock index 2aa10ea8..7d8d3730 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,10 +30,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" dependencies = [ "cpp_demangle", - "fallible-iterator 0.3.0", - "gimli 0.28.0", + "fallible-iterator", + "gimli", "memmap2", - "object 0.32.1", + "object", "rustc-demangle", "smallvec", ] @@ -163,7 +163,7 @@ dependencies = [ "cfg-if", "libc", "miniz_oxide", - "object 0.32.1", + "object", "rustc-demangle", ] @@ -834,9 +834,9 @@ dependencies = [ [[package]] name = "defmt-decoder" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69494bdf0927405e6d22641d1307c664ad3e9483452b85078940529883945044" +checksum = "5cc8c950fcaefdfce609498332b92c9b235c431d85c6c71908142cbb55709140" dependencies = [ "anyhow", "byteorder", @@ -844,10 +844,10 @@ dependencies = [ "defmt-json-schema", "defmt-parser", "dissimilar", - "gimli 0.27.3", + "gimli", "log", "nom", - "object 0.31.1", + "object", "ryu", "serde", "serde_json", @@ -1132,12 +1132,6 @@ dependencies = [ "xmas-elf", ] -[[package]] -name = "fallible-iterator" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" - [[package]] name = "fallible-iterator" version = "0.3.0" @@ -1328,23 +1322,13 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "gimli" -version = "0.27.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" -dependencies = [ - "fallible-iterator 0.2.0", - "stable_deref_trait", -] - [[package]] name = "gimli" version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" dependencies = [ - "fallible-iterator 0.3.0", + "fallible-iterator", "stable_deref_trait", ] @@ -2721,15 +2705,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" -[[package]] -name = "object" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bda667d9f2b5051b8833f59f3bf748b28ef54f850f4fcb389a252aa383866d1" -dependencies = [ - "memchr", -] - [[package]] name = "object" version = "0.32.1" diff --git a/cargo-espflash/Cargo.toml b/cargo-espflash/Cargo.toml index 506ece05..0c4e970a 100644 --- a/cargo-espflash/Cargo.toml +++ b/cargo-espflash/Cargo.toml @@ -41,6 +41,3 @@ cargo = { version = "0.73.1", features = ["vendored-openssl"] } [target.'cfg(windows)'.dependencies] cargo = "0.73.1" - -[features] -defmt = ["espflash/defmt"] diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index e639edd7..c124e786 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -6,13 +6,13 @@ use std::{ use cargo_metadata::Message; use clap::{Args, CommandFactory, Parser, Subcommand}; -use espflash::cli::{erase_flash, erase_region, EraseFlashArgs, EraseRegionArgs}; use espflash::{ cli::{ - self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image, - monitor::monitor, parse_partition_table, partition_table, print_board_info, - save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EspflashProgress, - FlashConfigArgs, MonitorArgs, PartitionTableArgs, + self, board_info, completions, config::Config, connect, erase_flash, erase_partitions, + erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table, + partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs, + ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs, + MonitorArgs, PartitionTableArgs, }, error::Error as EspflashError, image_format::ImageFormatKind, @@ -341,11 +341,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { 115_200 }; - monitor( + monitor_with( flasher.into_interface(), Some(&elf_data), pid, args.flash_args.monitor_baud.unwrap_or(default_baud), + args.flash_args.log_format, ) .into_diagnostic()?; } diff --git a/espflash/Cargo.toml b/espflash/Cargo.toml index cf7e4a82..36ea006b 100644 --- a/espflash/Cargo.toml +++ b/espflash/Cargo.toml @@ -40,7 +40,8 @@ clap_complete = { version = "4.4.3", optional = true } comfy-table = { version = "7.0.1", optional = true } crossterm = { version = "0.25.0", optional = true } # 0.26.x causes issues on Windows ctrlc = { version = "3.4.0", optional = true } -defmt-decoder = { version = "=0.3.8", features = ["unstable"], optional = true } +# defmt dependencies are pinned since defmt does not guarantee MSRV even for patch releases +defmt-decoder = { version = "=0.3.9", features = ["unstable"], optional = true } defmt-parser = { version = "=0.3.3", features = ["unstable"], optional = true } dialoguer = { version = "0.10.4", optional = true } directories = { version = "5.0.1", optional = true } @@ -74,6 +75,8 @@ cli = [ "dep:comfy-table", "dep:crossterm", "dep:ctrlc", + "dep:defmt-decoder", + "dep:defmt-parser", "dep:dialoguer", "dep:directories", "dep:env_logger", @@ -84,8 +87,5 @@ cli = [ "dep:regex", "dep:update-informer", ] -defmt = [ - "dep:defmt-decoder", - "dep:defmt-parser", -] + raspberry = ["dep:rppal"] diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index e95cde63..1546ee88 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -8,7 +8,7 @@ use clap::{Args, CommandFactory, Parser, Subcommand}; use espflash::{ cli::{ self, board_info, completions, config::Config, connect, erase_flash, erase_partitions, - erase_region, flash_elf_image, monitor::monitor, parse_partition_table, parse_uint32, + erase_region, flash_elf_image, monitor::monitor_with, parse_partition_table, parse_uint32, partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs, @@ -263,11 +263,12 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { 115_200 }; - monitor( + monitor_with( flasher.into_interface(), Some(&elf_data), pid, args.flash_args.monitor_baud.unwrap_or(default_baud), + args.flash_args.log_format, ) .into_diagnostic()?; } diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index 6377d4bd..909e97f8 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -24,7 +24,11 @@ use log::{debug, info}; use miette::{IntoDiagnostic, Result, WrapErr}; use serialport::{SerialPortType, UsbPortInfo}; -use self::{config::Config, monitor::monitor, serial::get_serial_port_info}; +use self::{ + config::Config, + monitor::{monitor_with, LogFormat}, + serial::get_serial_port_info, +}; use crate::{ elf::ElfFirmwareImage, error::{Error, MissingPartition, MissingPartitionTable}, @@ -134,6 +138,9 @@ pub struct FlashArgs { /// Load the application to RAM instead of Flash #[arg(long)] pub ram: bool, + /// Logging format. + #[arg(long, short = 'f', default_value = "serial", requires = "monitor")] + pub log_format: LogFormat, } /// Operations for partitions tables @@ -185,6 +192,9 @@ pub struct MonitorArgs { /// Connection configuration #[clap(flatten)] connect_args: ConnectArgs, + /// Logging format. + #[arg(long, short = 'f', default_value = "serial")] + pub log_format: LogFormat, } /// Select a serial port and establish a connection with a target device @@ -291,11 +301,12 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> { 115_200 }; - monitor( + monitor_with( flasher.into_interface(), elf.as_deref(), pid, args.connect_args.baud.unwrap_or(default_baud), + args.log_format, ) .into_diagnostic()?; diff --git a/espflash/src/cli/monitor/mod.rs b/espflash/src/cli/monitor/mod.rs index 3e08b0fe..b99eb9dc 100644 --- a/espflash/src/cli/monitor/mod.rs +++ b/espflash/src/cli/monitor/mod.rs @@ -21,6 +21,7 @@ use crossterm::{ }; use log::error; use miette::{IntoDiagnostic, Result}; +use strum::{Display, EnumIter, EnumString, EnumVariantNames}; use crate::{ cli::monitor::parser::{InputParser, ResolvingPrinter}, @@ -33,6 +34,17 @@ pub mod parser; mod line_endings; mod symbols; +#[cfg_attr(feature = "cli", derive(clap::ValueEnum))] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumIter, EnumString, EnumVariantNames)] +#[non_exhaustive] +#[strum(serialize_all = "lowercase")] +pub enum LogFormat { + /// defmt + Defmt, + /// serial + Serial, +} + /// Type that ensures that raw mode is disabled when dropped. struct RawModeGuard; @@ -58,22 +70,16 @@ pub fn monitor( pid: u16, baud: u32, ) -> serialport::Result<()> { - #[cfg(feature = "defmt")] - let parser = parser::esp_defmt::EspDefmt::new(elf); - - #[cfg(not(feature = "defmt"))] - let parser = parser::serial::Serial; - - monitor_with(serial, elf, pid, baud, parser) + monitor_with(serial, elf, pid, baud, LogFormat::Serial) } /// Open a serial monitor on the given interface, using the given input parser. -pub fn monitor_with( +pub fn monitor_with( mut serial: Interface, elf: Option<&[u8]>, pid: u16, baud: u32, - mut parser: L, + log_format: LogFormat, ) -> serialport::Result<()> { println!("Commands:"); println!(" CTRL+R Reset chip"); @@ -102,7 +108,16 @@ pub fn monitor_with( err => err, }?; - parser.feed(&buff[0..read_count], &mut stdout); + match log_format { + LogFormat::Defmt => { + let mut parser = parser::esp_defmt::EspDefmt::new(elf); + parser.feed(&buff[0..read_count], &mut stdout); + } + LogFormat::Serial => { + let mut parser = parser::serial::Serial; + parser.feed(&buff[0..read_count], &mut stdout); + } + } // Don't forget to flush the writer! stdout.flush().ok(); diff --git a/espflash/src/cli/monitor/parser/mod.rs b/espflash/src/cli/monitor/parser/mod.rs index c2fc0329..0a62c2a7 100644 --- a/espflash/src/cli/monitor/parser/mod.rs +++ b/espflash/src/cli/monitor/parser/mod.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "defmt")] pub mod esp_defmt; pub mod serial;