From 829def59b101c1663d09d34ef695c0bd335cdfde Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Mon, 15 Jan 2024 10:32:50 +0100 Subject: [PATCH 01/12] feat: Support local config file and add config parameters --- espflash/src/bin/espflash.rs | 42 +++++++++++++++++++++++++++++------- espflash/src/cli/config.rs | 42 +++++++++++++++++++++++++++++------- espflash/src/cli/mod.rs | 2 +- espflash/src/cli/serial.rs | 2 +- 4 files changed, 70 insertions(+), 18 deletions(-) diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index bf42833a..ab5542f7 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -179,8 +179,8 @@ fn main() -> Result<()> { Commands::Flash(args) => flash(args, &config), Commands::Monitor(args) => serial_monitor(args, &config), Commands::PartitionTable(args) => partition_table(args), - Commands::SaveImage(args) => save_image(args), Commands::ReadFlash(args) => read_flash(args, &config), + Commands::SaveImage(args) => save_image(args, &config), Commands::WriteBin(args) => write_bin(args, &config), Commands::ChecksumMd5(args) => checksum_md5(&args, &config), } @@ -234,8 +234,16 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { if args.flash_args.ram { flasher.load_elf_to_ram(&elf_data, Some(&mut EspflashProgress::default()))?; } else { - let bootloader = args.flash_args.bootloader.as_deref(); - let partition_table = args.flash_args.partition_table.as_deref(); + let bootloader = args + .flash_args + .bootloader + .as_deref() + .or(config.bootloader.as_deref()); + let partition_table = args + .flash_args + .partition_table + .as_deref() + .or(config.partition_table.as_deref()); if let Some(path) = bootloader { println!("Bootloader: {}", path.display()); @@ -297,7 +305,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { } } -fn save_image(args: SaveImageArgs) -> Result<()> { +fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { let elf_data = fs::read(&args.image) .into_diagnostic() .wrap_err_with(|| format!("Failed to open image {}", args.image.display()))?; @@ -310,10 +318,20 @@ fn save_image(args: SaveImageArgs) -> Result<()> { } println!("Merge: {}", args.save_image_args.merge); println!("Skip padding: {}", args.save_image_args.skip_padding); - if let Some(path) = &args.save_image_args.bootloader { + if let Some(path) = &args + .save_image_args + .bootloader + .clone() + .or(config.bootloader.clone()) + { println!("Bootloader: {}", path.display()); } - if let Some(path) = &args.save_image_args.partition_table { + if let Some(path) = &args + .save_image_args + .partition_table + .clone() + .or(config.partition_table.clone()) + { println!("Partition table: {}", path.display()); } @@ -324,8 +342,16 @@ fn save_image(args: SaveImageArgs) -> Result<()> { ); let flash_data = FlashData::new( - args.save_image_args.bootloader.as_deref(), - args.save_image_args.partition_table.as_deref(), + args.save_image_args + .bootloader + .clone() + .or(config.bootloader.clone()) + .as_deref(), + args.save_image_args + .partition_table + .clone() + .or(config.partition_table.clone()) + .as_deref(), args.save_image_args.partition_table_offset, args.format, args.save_image_args.target_app_partition, diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 0fdb0605..8f4c85bf 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -7,15 +7,15 @@ //! [cargo-espflash]: https://crates.io/crates/cargo-espflash //! [espflash]: https://crates.io/crates/espflash -use std::{ - fs::{create_dir_all, read_to_string, write}, - path::PathBuf, -}; - +use crate::error::Error; use directories::ProjectDirs; use miette::{IntoDiagnostic, Result, WrapErr}; use serde::{Deserialize, Serialize}; use serialport::UsbPortInfo; +use std::{ + fs::{create_dir_all, read_to_string, write}, + path::PathBuf, +}; /// A configured, known serial connection #[derive(Debug, Deserialize, Serialize, Default, Clone)] @@ -79,6 +79,18 @@ pub struct Config { /// Preferred serial port connection information #[serde(default)] pub connection: Connection, + /// Bootloader path + #[serde(default)] + pub bootloader: Option, + /// Baudrate + #[serde(default)] + pub baudrate: Option, + /// Partition table path + #[serde(default)] + pub partition_table: Option, + /// Port + #[serde(default)] + pub port: Option, /// Preferred USB devices #[serde(default)] pub usb_device: Vec, @@ -88,17 +100,31 @@ pub struct Config { } impl Config { + /// Gets the path to the configuration file. + pub fn get_config_path() -> Result { + let local_config = std::env::current_dir()? + .join(".config") + .join("espflash.toml"); + if local_config.exists() { + return Ok(local_config); + } + + let project_dirs = ProjectDirs::from("rs", "esp", "espflash").unwrap(); + let global_config = project_dirs.config_dir().join("espflash.toml"); + Ok(global_config) + } + /// Load configuration from the configuration file pub fn load() -> Result { - let dirs = ProjectDirs::from("rs", "esp", "espflash").unwrap(); - let file = dirs.config_dir().join("espflash.toml"); + let file = Self::get_config_path()?; let mut config = if let Ok(data) = read_to_string(&file) { toml::from_str(&data).into_diagnostic()? } else { Self::default() }; - config.save_path = file; + + println!("Config: {:#?}", &config); Ok(config) } diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index b650a19b..708a9def 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -342,7 +342,7 @@ pub fn connect( Ok(Flasher::connect( interface, port_info, - args.baud, + args.baud.or(config.baudrate), !args.no_stub, !no_verify, !no_skip, diff --git a/espflash/src/cli/serial.rs b/espflash/src/cli/serial.rs index badbd045..0014281c 100644 --- a/espflash/src/cli/serial.rs +++ b/espflash/src/cli/serial.rs @@ -35,7 +35,7 @@ pub fn get_serial_port_info( let ports = detect_usb_serial_ports().unwrap_or_default(); - if let Some(serial) = &matches.port { + if let Some(serial) = &matches.port.clone().or(config.port.clone()) { find_serial_port(&ports, serial) } else if let Some(serial) = &config.connection.serial { find_serial_port(&ports, serial) From 667d9e1b89d03d652c0fa2ec3b983b9ba48308ff Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Tue, 16 Jan 2024 10:51:40 +0100 Subject: [PATCH 02/12] refactor: Avoid cloning and clippy lints --- espflash/src/bin/espflash.rs | 8 ++++---- espflash/src/cli/config.rs | 17 ++++++++++------- espflash/src/cli/serial.rs | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index ab5542f7..6ed84d67 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -321,16 +321,16 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { if let Some(path) = &args .save_image_args .bootloader - .clone() - .or(config.bootloader.clone()) + .as_deref() + .or(config.bootloader.as_deref()) { println!("Bootloader: {}", path.display()); } if let Some(path) = &args .save_image_args .partition_table - .clone() - .or(config.partition_table.clone()) + .as_deref() + .or(config.partition_table.as_deref()) { println!("Partition table: {}", path.display()); } diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 8f4c85bf..41a4d53e 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -7,16 +7,19 @@ //! [cargo-espflash]: https://crates.io/crates/cargo-espflash //! [espflash]: https://crates.io/crates/espflash -use crate::error::Error; -use directories::ProjectDirs; -use miette::{IntoDiagnostic, Result, WrapErr}; -use serde::{Deserialize, Serialize}; -use serialport::UsbPortInfo; use std::{ fs::{create_dir_all, read_to_string, write}, path::PathBuf, }; +use directories::ProjectDirs; +use log::debug; +use miette::{IntoDiagnostic, Result, WrapErr}; +use serde::{Deserialize, Serialize}; +use serialport::UsbPortInfo; + +use crate::error::Error; + /// A configured, known serial connection #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct Connection { @@ -118,13 +121,13 @@ impl Config { pub fn load() -> Result { let file = Self::get_config_path()?; - let mut config = if let Ok(data) = read_to_string(&file) { + let config = if let Ok(data) = read_to_string(file) { toml::from_str(&data).into_diagnostic()? } else { Self::default() }; - println!("Config: {:#?}", &config); + debug!("Config: {:#?}", &config); Ok(config) } diff --git a/espflash/src/cli/serial.rs b/espflash/src/cli/serial.rs index 0014281c..7405c6c9 100644 --- a/espflash/src/cli/serial.rs +++ b/espflash/src/cli/serial.rs @@ -35,7 +35,7 @@ pub fn get_serial_port_info( let ports = detect_usb_serial_ports().unwrap_or_default(); - if let Some(serial) = &matches.port.clone().or(config.port.clone()) { + if let Some(serial) = &matches.port.as_deref().or(config.port.as_deref()) { find_serial_port(&ports, serial) } else if let Some(serial) = &config.connection.serial { find_serial_port(&ports, serial) From d11c7ccd1eae6854f71a5b24b89f560fa495f93e Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Tue, 16 Jan 2024 11:41:13 +0100 Subject: [PATCH 03/12] feat: Verify partition table and bootloader extensions --- espflash/src/cli/config.rs | 14 ++++++++++++++ espflash/src/error.rs | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 41a4d53e..23aafa5c 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -8,6 +8,7 @@ //! [espflash]: https://crates.io/crates/espflash use std::{ + ffi::OsStr, fs::{create_dir_all, read_to_string, write}, path::PathBuf, }; @@ -127,6 +128,19 @@ impl Config { Self::default() }; + if let Some(table) = &config.partition_table { + match table.extension() { + Some(ext) if ext == "bin" || ext == "csv" => {} + _ => return Err(Error::InvalidPartitionTablePath.into()), + } + } + + if let Some(bootloader) = &config.bootloader { + if bootloader.extension() != Some(OsStr::new("bin")) { + return Err(Error::InvalidBootloaderPath.into()); + } + } + debug!("Config: {:#?}", &config); Ok(config) } diff --git a/espflash/src/error.rs b/espflash/src/error.rs index 936ef27d..071db926 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -96,6 +96,10 @@ pub enum Error { #[error("The provided bootloader binary is invalid")] InvalidBootloader, + #[error("Specified bootloader path is not a .bin file")] + #[diagnostic(code(espflash::invalid_bootloader_path))] + InvalidBootloaderPath, + #[error("Binary is not set up correctly to support direct boot")] #[diagnostic( code(espflash::invalid_direct_boot), @@ -117,6 +121,10 @@ pub enum Error { #[error(transparent)] IoError(#[from] std::io::Error), + #[error("Specified partition table path is not a .bin or .csv file")] + #[diagnostic(code(espflash::invalid_partition_table_path))] + InvalidPartitionTablePath, + #[error("No serial ports could be detected")] #[diagnostic( code(espflash::no_serial), From f456a72a7a5276b33944f8d35f26a7fc12b1f84d Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Tue, 16 Jan 2024 12:00:46 +0100 Subject: [PATCH 04/12] fix: Update save_path --- espflash/src/cli/config.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 23aafa5c..1c0c4770 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -122,7 +122,7 @@ impl Config { pub fn load() -> Result { let file = Self::get_config_path()?; - let config = if let Ok(data) = read_to_string(file) { + let mut config = if let Ok(data) = read_to_string(&file) { toml::from_str(&data).into_diagnostic()? } else { Self::default() @@ -141,6 +141,7 @@ impl Config { } } + config.save_path = file; debug!("Config: {:#?}", &config); Ok(config) } From 037bbb49bac24e18109032e4ba83afa2efbad2b0 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Tue, 16 Jan 2024 12:01:52 +0100 Subject: [PATCH 05/12] feat: Replace cargo metadata for config file --- cargo-espflash/src/error.rs | 8 -------- cargo-espflash/src/main.rs | 18 +++++++----------- cargo-espflash/src/package_metadata.rs | 26 ++------------------------ 3 files changed, 9 insertions(+), 43 deletions(-) diff --git a/cargo-espflash/src/error.rs b/cargo-espflash/src/error.rs index 0afa8f92..2bcb645d 100644 --- a/cargo-espflash/src/error.rs +++ b/cargo-espflash/src/error.rs @@ -10,14 +10,6 @@ use thiserror::Error; #[derive(Debug, Diagnostic, Error)] #[non_exhaustive] pub enum Error { - #[error("Specified bootloader path is not a .bin file")] - #[diagnostic(code(cargo_espflash::invalid_bootloader_path))] - InvalidBootloaderPath, - - #[error("Specified partition table path is not a .bin or .csv file")] - #[diagnostic(code(cargo_espflash::invalid_partition_table_path))] - InvalidPartitionTablePath, - #[error("The current workspace is invalid, and could not be loaded")] #[diagnostic( code(cargo_espflash::invalid_workspace), diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index ab30c8c1..a085b952 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -222,7 +222,7 @@ fn main() -> Result<()> { Commands::Monitor(args) => serial_monitor(args, &config), Commands::PartitionTable(args) => partition_table(args), Commands::ReadFlash(args) => read_flash(args, &config), - Commands::SaveImage(args) => save_image(args), + Commands::SaveImage(args) => save_image(args, &config), Commands::ChecksumMd5(args) => checksum_md5(&args, &config), } } @@ -239,14 +239,10 @@ pub fn erase_parts(args: ErasePartsArgs, config: &Config) -> Result<()> { return Err(EspflashError::StubRequired).into_diagnostic(); } - let metadata_partition_table = PackageMetadata::load(&args.package) - .ok() - .and_then(|m| m.partition_table); - let partition_table = args .partition_table .as_deref() - .or(metadata_partition_table.as_deref()); + .or(config.partition_table.as_deref()); let mut flash = connect(&args.connect_args, config, false, false)?; let partition_table = match partition_table { @@ -303,14 +299,14 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { .flash_args .bootloader .as_deref() - .or(metadata.bootloader.as_deref()) + .or(config.bootloader.as_deref()) .or(build_ctx.bootloader_path.as_deref()); let partition_table = args .flash_args .partition_table .as_deref() - .or(metadata.partition_table.as_deref()) + .or(config.partition_table.as_deref()) .or(build_ctx.partition_table_path.as_deref()); if let Some(path) = &bootloader { @@ -534,7 +530,7 @@ fn build( Ok(build_ctx) } -fn save_image(args: SaveImageArgs) -> Result<()> { +fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { let metadata = PackageMetadata::load(&args.build_args.package)?; let cargo_config = CargoConfig::load(&metadata.workspace_root, &metadata.package_root); @@ -545,7 +541,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> { .save_image_args .bootloader .as_deref() - .or(metadata.bootloader.as_deref()) + .or(config.bootloader.as_deref()) .or(build_ctx.bootloader_path.as_deref()) .map(|p| p.to_path_buf()); @@ -553,7 +549,7 @@ fn save_image(args: SaveImageArgs) -> Result<()> { .save_image_args .partition_table .as_deref() - .or(metadata.partition_table.as_deref()) + .or(config.partition_table.as_deref()) .or(build_ctx.partition_table_path.as_deref()) .map(|p| p.to_path_buf()); diff --git a/cargo-espflash/src/package_metadata.rs b/cargo-espflash/src/package_metadata.rs index d42206d2..b3498773 100644 --- a/cargo-espflash/src/package_metadata.rs +++ b/cargo-espflash/src/package_metadata.rs @@ -1,4 +1,4 @@ -use std::{ffi::OsStr, path::PathBuf, str::FromStr}; +use std::{path::PathBuf, str::FromStr}; use cargo::{ core::{Package, Workspace}, @@ -14,9 +14,7 @@ use crate::error::Error; pub struct PackageMetadata { pub workspace_root: PathBuf, pub package_root: PathBuf, - pub bootloader: Option, pub format: Option, - pub partition_table: Option, } impl PackageMetadata { @@ -37,19 +35,6 @@ impl PackageMetadata { let package = Self::load_package(&workspace, package_name)?; let metadata = Self::load_metadata(&workspace, &package)?; - if let Some(table) = &metadata.partition_table { - match table.extension() { - Some(ext) if ext == "bin" || ext == "csv" => {} - _ => return Err(Error::InvalidPartitionTablePath.into()), - } - } - - if let Some(bootloader) = &metadata.bootloader { - if bootloader.extension() != Some(OsStr::new("bin")) { - return Err(Error::InvalidBootloaderPath.into()); - } - } - Ok(metadata) } @@ -84,17 +69,10 @@ impl PackageMetadata { Some(meta) if meta.is_table() => { let meta = meta.as_table().unwrap(); - espflash_meta.bootloader = meta - .get("bootloader") - .map(|bl| package.root().join(bl.as_str().unwrap())); - + // TO BE REMOVED? espflash_meta.format = meta .get("format") .map(|fmt| ImageFormatKind::from_str(fmt.as_str().unwrap()).unwrap()); - - espflash_meta.partition_table = meta - .get("partition_table") - .map(|pt| package.root().join(pt.as_str().unwrap())); } _ => {} }, From 8ccc1e11f6fb3f01627c601084a386380ec945ca Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Tue, 16 Jan 2024 12:25:53 +0100 Subject: [PATCH 06/12] style: Improve redability --- espflash/src/bin/espflash.rs | 37 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index 6ed84d67..50992e69 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -310,6 +310,17 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { .into_diagnostic() .wrap_err_with(|| format!("Failed to open image {}", args.image.display()))?; + let bootloader = args + .save_image_args + .bootloader + .as_deref() + .or(config.bootloader.as_deref()); + let partition_table = args + .save_image_args + .partition_table + .as_deref() + .or(config.partition_table.as_deref()); + // Since we have no `Flasher` instance and as such cannot print the board // information, we will print whatever information we _do_ have. println!("Chip type: {}", args.save_image_args.chip); @@ -318,20 +329,10 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { } println!("Merge: {}", args.save_image_args.merge); println!("Skip padding: {}", args.save_image_args.skip_padding); - if let Some(path) = &args - .save_image_args - .bootloader - .as_deref() - .or(config.bootloader.as_deref()) - { + if let Some(path) = &bootloader { println!("Bootloader: {}", path.display()); } - if let Some(path) = &args - .save_image_args - .partition_table - .as_deref() - .or(config.partition_table.as_deref()) - { + if let Some(path) = &partition_table { println!("Partition table: {}", path.display()); } @@ -342,16 +343,8 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { ); let flash_data = FlashData::new( - args.save_image_args - .bootloader - .clone() - .or(config.bootloader.clone()) - .as_deref(), - args.save_image_args - .partition_table - .clone() - .or(config.partition_table.clone()) - .as_deref(), + bootloader, + partition_table, args.save_image_args.partition_table_offset, args.format, args.save_image_args.target_app_partition, From 6371d0d5bab453ea6336c45bb4c6d7867073ae1e Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Thu, 1 Feb 2024 11:52:36 +0100 Subject: [PATCH 07/12] docs: Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1716ac3f..146a02b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Created `FlashData`, `FlashDataBuilder` and `FlashSettings` structs to reduce number of input arguments in some functions (#512, #566) - `espflash` will now exit with an error if `defmt` is selected but not usable (#524) +- Unify configuration methods (#551) ### Removed From 8f46c8c9739536eef85248c50f0150698f8ea977 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Thu, 1 Feb 2024 14:41:51 +0100 Subject: [PATCH 08/12] feat: Update local config path --- espflash/src/cli/config.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 1c0c4770..15c4ee54 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -106,9 +106,7 @@ pub struct Config { impl Config { /// Gets the path to the configuration file. pub fn get_config_path() -> Result { - let local_config = std::env::current_dir()? - .join(".config") - .join("espflash.toml"); + let local_config = std::env::current_dir()?.join("espflash.toml"); if local_config.exists() { return Ok(local_config); } From 5677283f6196740b71fd5588f1437aea20aa5ca9 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 2 Feb 2024 09:27:39 +0100 Subject: [PATCH 09/12] feat: Remove format param from cargo metadata --- cargo-espflash/src/main.rs | 4 ++-- cargo-espflash/src/package_metadata.rs | 23 ++--------------------- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index a085b952..81cfee2f 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -326,7 +326,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { bootloader, partition_table, args.flash_args.partition_table_offset, - args.flash_args.format.or(metadata.format), + args.flash_args.format, args.flash_args.target_app_partition, flash_settings, args.flash_args.min_chip_rev, @@ -578,7 +578,7 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { bootloader.as_deref(), partition_table.as_deref(), args.save_image_args.partition_table_offset, - args.format.or(metadata.format), + args.format, args.save_image_args.target_app_partition, flash_settings, args.save_image_args.min_chip_rev, diff --git a/cargo-espflash/src/package_metadata.rs b/cargo-espflash/src/package_metadata.rs index b3498773..60a0f308 100644 --- a/cargo-espflash/src/package_metadata.rs +++ b/cargo-espflash/src/package_metadata.rs @@ -1,10 +1,9 @@ -use std::{path::PathBuf, str::FromStr}; +use std::path::PathBuf; use cargo::{ core::{Package, Workspace}, util::Config, }; -use espflash::image_format::ImageFormatKind; use miette::{IntoDiagnostic, Result}; use serde::Deserialize; @@ -14,7 +13,6 @@ use crate::error::Error; pub struct PackageMetadata { pub workspace_root: PathBuf, pub package_root: PathBuf, - pub format: Option, } impl PackageMetadata { @@ -57,28 +55,11 @@ impl PackageMetadata { } fn load_metadata(workspace: &Workspace, package: &Package) -> Result { - let mut espflash_meta = PackageMetadata { + let espflash_meta = PackageMetadata { workspace_root: workspace.root_manifest().parent().unwrap().to_path_buf(), package_root: package.root().to_path_buf(), - - ..PackageMetadata::default() }; - match package.manifest().custom_metadata() { - Some(meta) if meta.is_table() => match meta.as_table().unwrap().get("espflash") { - Some(meta) if meta.is_table() => { - let meta = meta.as_table().unwrap(); - - // TO BE REMOVED? - espflash_meta.format = meta - .get("format") - .map(|fmt| ImageFormatKind::from_str(fmt.as_str().unwrap()).unwrap()); - } - _ => {} - }, - _ => {} - } - Ok(espflash_meta) } } From e371800a37c6093688e3b7e51ed3b5ddd8a2bab3 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 2 Feb 2024 10:00:57 +0100 Subject: [PATCH 10/12] feat: Remove port param --- espflash/src/cli/config.rs | 3 --- espflash/src/cli/serial.rs | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 15c4ee54..50bedea8 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -92,9 +92,6 @@ pub struct Config { /// Partition table path #[serde(default)] pub partition_table: Option, - /// Port - #[serde(default)] - pub port: Option, /// Preferred USB devices #[serde(default)] pub usb_device: Vec, diff --git a/espflash/src/cli/serial.rs b/espflash/src/cli/serial.rs index 7405c6c9..badbd045 100644 --- a/espflash/src/cli/serial.rs +++ b/espflash/src/cli/serial.rs @@ -35,7 +35,7 @@ pub fn get_serial_port_info( let ports = detect_usb_serial_ports().unwrap_or_default(); - if let Some(serial) = &matches.port.as_deref().or(config.port.as_deref()) { + if let Some(serial) = &matches.port { find_serial_port(&ports, serial) } else if let Some(serial) = &config.connection.serial { find_serial_port(&ports, serial) From c3fb6830d274cef27e0b3f30f31b67557975aa13 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 2 Feb 2024 10:41:08 +0100 Subject: [PATCH 11/12] docs: Update usage section --- cargo-espflash/README.md | 14 ++------------ espflash/README.md | 2 ++ espflash/src/cli/config.rs | 8 ++++---- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/cargo-espflash/README.md b/cargo-espflash/README.md index 12335eb0..10234638 100644 --- a/cargo-espflash/README.md +++ b/cargo-espflash/README.md @@ -17,7 +17,6 @@ Supports the **ESP32**, **ESP32-C2/C3/C6**, **ESP32-H2**, **ESP32-P4**, **ESP32- - [Permissions on Linux](#permissions-on-linux) - [Windows Subsystem for Linux](#windows-subsystem-for-linux) - [Bootloader and Partition Table](#bootloader-and-partition-table) -- [Package Metadata](#package-metadata) - [Configuration File](#configuration-file) - [Configuration Examples](#configuration-examples) - [Logging Format](#logging-format) @@ -83,7 +82,9 @@ Commands: flash Flash an application in ELF format to a target device monitor Open the serial monitor without flashing the connected target device partition-table Convert partition tables between CSV and binary format + read-flash Read SPI flash content save-image Generate a binary application image and save it to a local disk + checksum-md5 Calculate the MD5 checksum of the given region help Print this message or the help of the given subcommand(s) Options: @@ -113,17 +114,6 @@ If the `--bootloader` and/or `--partition-table` options are provided then these [esp-idf-sys]: https://github.com/esp-rs/esp-idf-sys -## Package Metadata - -You're able to specify paths to bootloader and partition table files and image format in your package's Cargo metadata for per-project configuration: - -```toml -[package.metadata.espflash] -bootloader = "bootloader.bin" # Must be a binary file -partition_table = "partitions.csv" # Supports CSV and binary formats -format = "direct-boot" # Can be 'esp-bootloader' or 'direct-boot' -``` - ## Configuration File It's possible to specify a serial port and/or USB VID/PID values by setting them in a configuration file. The location of this file differs based on your operating system: diff --git a/espflash/README.md b/espflash/README.md index 21da4d52..c6524f5c 100644 --- a/espflash/README.md +++ b/espflash/README.md @@ -78,8 +78,10 @@ Commands: flash Flash an application in ELF format to a connected target device monitor Open the serial monitor without flashing the connected target device partition-table Convert partition tables between CSV and binary format + read-flash Read SPI flash content save-image Generate a binary application image and save it to a local disk write-bin Write a binary file to a specific address in a target device's flash + checksum-md5 Calculate the MD5 checksum of the given region help Print this message or the help of the given subcommand(s) Options: diff --git a/espflash/src/cli/config.rs b/espflash/src/cli/config.rs index 50bedea8..8c3c8e51 100644 --- a/espflash/src/cli/config.rs +++ b/espflash/src/cli/config.rs @@ -80,15 +80,15 @@ impl UsbDevice { /// Deserialized contents of a configuration file #[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct Config { - /// Preferred serial port connection information + /// Baudrate #[serde(default)] - pub connection: Connection, + pub baudrate: Option, /// Bootloader path #[serde(default)] pub bootloader: Option, - /// Baudrate + /// Preferred serial port connection information #[serde(default)] - pub baudrate: Option, + pub connection: Connection, /// Partition table path #[serde(default)] pub partition_table: Option, From a6ea978bec503b7a03b1d42a2e886de6053d7cfd Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Fri, 2 Feb 2024 10:42:43 +0100 Subject: [PATCH 12/12] docs: Update config documentation --- cargo-espflash/README.md | 64 ++++++++++++++++++++++++---------------- espflash/README.md | 64 ++++++++++++++++++++++++---------------- 2 files changed, 78 insertions(+), 50 deletions(-) diff --git a/cargo-espflash/README.md b/cargo-espflash/README.md index 10234638..f59318d8 100644 --- a/cargo-espflash/README.md +++ b/cargo-espflash/README.md @@ -18,7 +18,7 @@ Supports the **ESP32**, **ESP32-C2/C3/C6**, **ESP32-H2**, **ESP32-P4**, **ESP32- - [Windows Subsystem for Linux](#windows-subsystem-for-linux) - [Bootloader and Partition Table](#bootloader-and-partition-table) - [Configuration File](#configuration-file) - - [Configuration Examples](#configuration-examples) + - [Configuration precedence](#configuration-precedence) - [Logging Format](#logging-format) - [License](#license) - [Contribution](#contribution) @@ -116,30 +116,44 @@ If the `--bootloader` and/or `--partition-table` options are provided then these ## Configuration File -It's possible to specify a serial port and/or USB VID/PID values by setting them in a configuration file. The location of this file differs based on your operating system: - -| Operating System | Configuration Path | -| :--------------- | :---------------------------------------------------------------- | -| Linux | `$HOME/.config/espflash/espflash.toml` | -| macOS | `$HOME/Library/Application Support/rs.esp.espflash/espflash.toml` | -| Windows | `%APPDATA%\esp\espflash\espflash.toml` | - -### Configuration Examples - -You can either configure the serial port name like so: - -``` -[connection] -serial = "/dev/ttyUSB0" -``` - -Or specify one or more USB `vid`/`pid` couple: - -``` -[[usb_device]] -vid = "303a" -pid = "1001" -``` +The configuration file allows you to define various parameters for your application: +- Serial port: + - By name: + ```toml + [connection] + serial = "/dev/ttyUSB0" + ``` + - By USB VID/PID values: + ```toml + [[usb_device]] + vid = "303a" + pid = "1001" + ``` +- Baudrate: + ```toml + baudrate = 460800 + ``` +- Bootloader: + ```toml + bootloader = "path/to/custom/bootloader.bin" + ``` +- Partition table + ```toml + partition_table = "path/to/custom/partition-table.bin" + ``` + +You can have a local and/or a global configuration file: +- For local configurations, store the file under the current working directory with the name `espflash.toml` +- Global file location differs based on your operating system: + - Linux: `$HOME/.config/espflash/espflash.toml` + - macOS: `$HOME/Library/Application Support/rs.esp.espflash/espflash.toml` + - Windows: `%APPDATA%\esp\espflash\espflash.toml` + +### Configuration precedence + +1. Environment variables: If `ESPFLASH_PORT` or `ESPFLASH_BAUD` are set, the will be used instead of the config file value. +2. Local configuration file +3. Global configuration file ## Logging Format diff --git a/espflash/README.md b/espflash/README.md index c6524f5c..ba3fc787 100644 --- a/espflash/README.md +++ b/espflash/README.md @@ -20,7 +20,7 @@ Supports the **ESP32**, **ESP32-C2/C3/C6**, **ESP32-H2**, **ESP32-P4**,**ESP32-S - [Cargo Runner](#cargo-runner) - [Using `espflash` as a Library](#using-espflash-as-a-library) - [Configuration File](#configuration-file) - - [Configuration Examples](#configuration-examples) + - [Configuration precedence](#configuration-precedence) - [Logging Format](#logging-format) - [License](#license) - [Contribution](#contribution) @@ -135,30 +135,44 @@ espflash = { version = "2.1", default-features = false, features = ["raspberry"] ## Configuration File -It's possible to specify a serial port and/or USB VID/PID values by setting them in a configuration file. The location of this file differs based on your operating system: - -| Operating System | Configuration Path | -| :--------------- | :---------------------------------------------------------------- | -| Linux | `$HOME/.config/espflash/espflash.toml` | -| macOS | `$HOME/Library/Application Support/rs.esp.espflash/espflash.toml` | -| Windows | `%APPDATA%\esp\espflash\espflash.toml` | - -### Configuration Examples - -You can either configure the serial port name like so: - -``` -[connection] -serial = "/dev/ttyUSB0" -``` - -Or specify one or more USB `vid`/`pid` couple: - -``` -[[usb_device]] -vid = "303a" -pid = "1001" -``` +The configuration file allows you to define various parameters for your application: +- Serial port: + - By name: + ```toml + [connection] + serial = "/dev/ttyUSB0" + ``` + - By USB VID/PID values: + ```toml + [[usb_device]] + vid = "303a" + pid = "1001" + ``` +- Baudrate: + ```toml + baudrate = 460800 + ``` +- Bootloader: + ```toml + bootloader = "path/to/custom/bootloader.bin" + ``` +- Partition table + ```toml + partition_table = "path/to/custom/partition-table.bin" + ``` + +You can have a local and/or a global configuration file: +- For local configurations, store the file under the current working directory with the name `espflash.toml` +- Global file location differs based on your operating system: + - Linux: `$HOME/.config/espflash/espflash.toml` + - macOS: `$HOME/Library/Application Support/rs.esp.espflash/espflash.toml` + - Windows: `%APPDATA%\esp\espflash\espflash.toml` + +### Configuration precedence + +1. Environment variables: If `ESPFLASH_PORT` or `ESPFLASH_BAUD` are set, the will be used instead of the config file value. +2. Local configuration file +3. Global configuration file ## Logging Format