Skip to content

Commit 1a0a848

Browse files
committed
Make the flasher return a struct of device information instead of printing directly
1 parent f10d0fd commit 1a0a848

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

cargo-espflash/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn main() -> Result<()> {
139139
// Execute the correct action based on the provided subcommand and its
140140
// associated arguments.
141141
match args {
142-
Commands::BoardInfo(args) => board_info(args, &config),
142+
Commands::BoardInfo(args) => board_info(&args, &config),
143143
Commands::Flash(args) => flash(args, &config),
144144
Commands::Monitor(args) => serial_monitor(args, &config),
145145
Commands::PartitionTable(args) => partition_table(args),
@@ -165,7 +165,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> {
165165
// Print the board information once the project has successfully built. We do
166166
// here rather than upon connection to show the Cargo output prior to the board
167167
// information, rather than breaking up cargo-espflash's output.
168-
flasher.board_info()?;
168+
board_info(&args.connect_args, config)?;
169169

170170
// Read the ELF data from the build path and load it to the target.
171171
let elf_data = fs::read(build_ctx.artifact_path).into_diagnostic()?;

espflash/src/bin/espflash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn main() -> Result<()> {
106106
// Execute the correct action based on the provided subcommand and its
107107
// associated arguments.
108108
match args {
109-
Commands::BoardInfo(args) => board_info(args, &config),
109+
Commands::BoardInfo(args) => board_info(&args, &config),
110110
Commands::Flash(args) => flash(args, &config),
111111
Commands::Monitor(args) => serial_monitor(args, &config),
112112
Commands::PartitionTable(args) => partition_table(args),
@@ -117,7 +117,7 @@ fn main() -> Result<()> {
117117

118118
fn flash(args: FlashArgs, config: &Config) -> Result<()> {
119119
let mut flasher = connect(&args.connect_args, config)?;
120-
flasher.board_info()?;
120+
board_info(&args.connect_args, config)?;
121121

122122
// Read the ELF data from the build path and load it to the target.
123123
let elf_data = fs::read(&args.image).into_diagnostic()?;
@@ -213,7 +213,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> {
213213

214214
fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> {
215215
let mut flasher = connect(&args.connect_args, config)?;
216-
flasher.board_info()?;
216+
board_info(&args.connect_args, config)?;
217217

218218
let mut f = File::open(&args.bin_file).into_diagnostic()?;
219219
let size = f.metadata().into_diagnostic()?.len();

espflash/src/cli/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,20 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
276276
}
277277

278278
/// Connect to a target device and print information about its chip
279-
pub fn board_info(args: ConnectArgs, config: &Config) -> Result<()> {
279+
pub fn board_info(args: &ConnectArgs, config: &Config) -> Result<()> {
280280
let mut flasher = connect(&args, config)?;
281-
flasher.board_info()?;
281+
let info = flasher.device_info()?;
282+
283+
print!("Chip type: {}", info.chip);
284+
if let Some((major, minor)) = info.revision {
285+
println!(" (revision v{major}.{minor})");
286+
} else {
287+
println!("");
288+
}
289+
println!("Crystal frequency: {}MHz", info.crystal_frequency);
290+
println!("Flash size: {}", info.flash_size);
291+
println!("Features: {}", info.features.join(", "));
292+
println!("MAC address: {}", info.mac_address);
282293

283294
Ok(())
284295
}

espflash/src/flasher/mod.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ struct EntryParams {
299299
entry: u32,
300300
}
301301

302+
/// Information about the connected device
303+
#[derive(Debug, Clone)]
304+
pub struct DeviceInfo {
305+
/// The chip being used
306+
pub chip: Chip,
307+
/// The revision of the chip
308+
pub revision: Option<(u32, u32)>,
309+
/// The crystal frequency of the chip
310+
pub crystal_frequency: u32,
311+
/// The total available flash size
312+
pub flash_size: FlashSize,
313+
/// Device features
314+
pub features: Vec<String>,
315+
/// MAC address
316+
pub mac_address: String,
317+
}
318+
302319
/// Connect to and flash a target device
303320
pub struct Flasher {
304321
/// Connection for flash operations
@@ -605,30 +622,37 @@ impl Flasher {
605622
self.chip
606623
}
607624

608-
/// Read and print any information we can about the connected board
609-
pub fn board_info(&mut self) -> Result<(), Error> {
625+
/// Read and print any information we can about the connected device
626+
pub fn device_info(&mut self) -> Result<DeviceInfo, Error> {
610627
let chip = self.chip();
611628
let target = chip.into_target();
612629

613-
let features = target.chip_features(self.connection())?;
614-
let freq = target.crystal_freq(self.connection())?;
615-
let mac = target.mac_address(self.connection())?;
616-
617630
// The ESP8266 does not have readable major/minor revision numbers, so we have
618-
// nothing to print if targeting it.
619-
print!("Chip type: {chip}");
620-
if chip != Chip::Esp8266 {
621-
let (major, minor) = target.chip_revision(self.connection())?;
622-
println!(" (revision v{major}.{minor})");
631+
// nothing to return if targeting it.
632+
let revision = if chip != Chip::Esp8266 {
633+
Some(target.chip_revision(self.connection())?)
623634
} else {
624-
println!("");
625-
}
626-
println!("Crystal frequency: {freq}MHz");
627-
println!("Flash size: {}", self.flash_size);
628-
println!("Features: {}", features.join(", "));
629-
println!("MAC address: {mac}");
635+
None
636+
};
630637

631-
Ok(())
638+
let crystal_frequency = target.crystal_freq(self.connection())?;
639+
let features = target
640+
.chip_features(self.connection())?
641+
.iter()
642+
.map(|s| s.to_string())
643+
.collect::<Vec<_>>();
644+
let mac_address = target.mac_address(self.connection())?;
645+
646+
let info = DeviceInfo {
647+
chip,
648+
revision,
649+
crystal_frequency,
650+
flash_size: self.flash_size,
651+
features,
652+
mac_address,
653+
};
654+
655+
Ok(info)
632656
}
633657

634658
/// Load an elf image to ram and execute it

0 commit comments

Comments
 (0)