From 654a221f8f7eec0cc4b9692b4ef6f678a67d0ca6 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 21 Dec 2022 07:13:43 -0800 Subject: [PATCH] Make the flasher return a struct of device information instead of printing directly --- cargo-espflash/src/main.rs | 4 +-- espflash/src/bin/espflash.rs | 6 ++-- espflash/src/cli/mod.rs | 15 +++++++-- espflash/src/flasher/mod.rs | 60 +++++++++++++++++++++++++----------- 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index aef06b6b..91ad64ba 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -139,7 +139,7 @@ fn main() -> Result<()> { // Execute the correct action based on the provided subcommand and its // associated arguments. match args { - Commands::BoardInfo(args) => board_info(args, &config), + Commands::BoardInfo(args) => board_info(&args, &config), Commands::Flash(args) => flash(args, &config), Commands::Monitor(args) => serial_monitor(args, &config), Commands::PartitionTable(args) => partition_table(args), @@ -165,7 +165,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { // Print the board information once the project has successfully built. We do // here rather than upon connection to show the Cargo output prior to the board // information, rather than breaking up cargo-espflash's output. - flasher.board_info()?; + board_info(&args.connect_args, config)?; // Read the ELF data from the build path and load it to the target. let elf_data = fs::read(build_ctx.artifact_path).into_diagnostic()?; diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index 6bc4ebab..e99b4a5d 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -106,7 +106,7 @@ fn main() -> Result<()> { // Execute the correct action based on the provided subcommand and its // associated arguments. match args { - Commands::BoardInfo(args) => board_info(args, &config), + Commands::BoardInfo(args) => board_info(&args, &config), Commands::Flash(args) => flash(args, &config), Commands::Monitor(args) => serial_monitor(args, &config), Commands::PartitionTable(args) => partition_table(args), @@ -117,7 +117,7 @@ fn main() -> Result<()> { fn flash(args: FlashArgs, config: &Config) -> Result<()> { let mut flasher = connect(&args.connect_args, config)?; - flasher.board_info()?; + board_info(&args.connect_args, config)?; // Read the ELF data from the build path and load it to the target. let elf_data = fs::read(&args.image).into_diagnostic()?; @@ -213,7 +213,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> { fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> { let mut flasher = connect(&args.connect_args, config)?; - flasher.board_info()?; + board_info(&args.connect_args, config)?; let mut f = File::open(&args.bin_file).into_diagnostic()?; let size = f.metadata().into_diagnostic()?.len(); diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index e070e5db..bddd682f 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -276,9 +276,20 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result { } /// Connect to a target device and print information about its chip -pub fn board_info(args: ConnectArgs, config: &Config) -> Result<()> { +pub fn board_info(args: &ConnectArgs, config: &Config) -> Result<()> { let mut flasher = connect(&args, config)?; - flasher.board_info()?; + let info = flasher.device_info()?; + + print!("Chip type: {}", info.chip); + if let Some((major, minor)) = info.revision { + println!(" (revision v{major}.{minor})"); + } else { + println!(""); + } + println!("Crystal frequency: {}MHz", info.crystal_frequency); + println!("Flash size: {}", info.flash_size); + println!("Features: {}", info.features.join(", ")); + println!("MAC address: {}", info.mac_address); Ok(()) } diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 0a1427f0..94258adf 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -299,6 +299,23 @@ struct EntryParams { entry: u32, } +/// Information about the connected device +#[derive(Debug, Clone)] +pub struct DeviceInfo { + /// The chip being used + pub chip: Chip, + /// The revision of the chip + pub revision: Option<(u32, u32)>, + /// The crystal frequency of the chip + pub crystal_frequency: u32, + /// The total available flash size + pub flash_size: FlashSize, + /// Device features + pub features: Vec, + /// MAC address + pub mac_address: String, +} + /// Connect to and flash a target device pub struct Flasher { /// Connection for flash operations @@ -605,30 +622,37 @@ impl Flasher { self.chip } - /// Read and print any information we can about the connected board - pub fn board_info(&mut self) -> Result<(), Error> { + /// Read and print any information we can about the connected device + pub fn device_info(&mut self) -> Result { let chip = self.chip(); let target = chip.into_target(); - let features = target.chip_features(self.connection())?; - let freq = target.crystal_freq(self.connection())?; - let mac = target.mac_address(self.connection())?; - // The ESP8266 does not have readable major/minor revision numbers, so we have - // nothing to print if targeting it. - print!("Chip type: {chip}"); - if chip != Chip::Esp8266 { - let (major, minor) = target.chip_revision(self.connection())?; - println!(" (revision v{major}.{minor})"); + // nothing to return if targeting it. + let revision = if chip != Chip::Esp8266 { + Some(target.chip_revision(self.connection())?) } else { - println!(""); - } - println!("Crystal frequency: {freq}MHz"); - println!("Flash size: {}", self.flash_size); - println!("Features: {}", features.join(", ")); - println!("MAC address: {mac}"); + None + }; - Ok(()) + let crystal_frequency = target.crystal_freq(self.connection())?; + let features = target + .chip_features(self.connection())? + .iter() + .map(|s| s.to_string()) + .collect::>(); + let mac_address = target.mac_address(self.connection())?; + + let info = DeviceInfo { + chip, + revision, + crystal_frequency, + flash_size: self.flash_size, + features, + mac_address, + }; + + Ok(info) } /// Load an elf image to ram and execute it