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

Make the flasher return a struct of device information instead of printing directly #328

Merged
merged 1 commit into from
Dec 21, 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
4 changes: 2 additions & 2 deletions cargo-espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()?;
Expand Down
6 changes: 3 additions & 3 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()?;
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 13 additions & 2 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,20 @@ pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
}

/// 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(())
}
Expand Down
60 changes: 42 additions & 18 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// MAC address
pub mac_address: String,
}

/// Connect to and flash a target device
pub struct Flasher {
/// Connection for flash operations
Expand Down Expand Up @@ -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<DeviceInfo, Error> {
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::<Vec<_>>();
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
Expand Down