Skip to content

Commit

Permalink
feat: use hpm_isp.bin by default if it exists when using flash command (
Browse files Browse the repository at this point in the history
#12)

Signed-off-by: tfx2001 <[email protected]>
  • Loading branch information
tfx2001 authored Mar 16, 2024
1 parent 31e7333 commit 6b3d03c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ sudo udevadm control --reload-rules
# Write to flash (use default memory config)
hpm_isp flash 0 write 0x400 flash.bin
# Write to flash (use custom memory config)
# Note: if hpm_isp.bin exists in the working directory, it will be used by default.
# So you don't need to pass -c option explicitly.
hpm_isp flash -c hpm_isp.bin 0 write 0x400 flash.bin
# Read from flash
hpm_isp read -c hpm_isp.bin 0 read 0x0 0x4000 flash.bin
# Use config wizard to generate config file (save to hpm_isp.bin)
hpm_isp read 0 read 0x0 0x4000 flash.bin
# Use config wizard to generate config file (save as hpm_isp.bin)
hpm_isp wizard
```

Expand Down
34 changes: 21 additions & 13 deletions hpm_isp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use hpm_isp::{
};
use hpm_rt::XpiNorConfigurationOption;

const DEFAULT_CONFIG_FILE: &'static str = "hpm_isp.bin";

#[derive(Parser)]
#[clap(version, about)]
struct Cli {
Expand All @@ -41,7 +43,7 @@ enum Commands {
/// Command of wizard to generate memory config file
Wizard {
/// Path of memory config file
#[clap(short, long, default_value = "hpm_isp.bin")]
#[clap(short, long, default_value = DEFAULT_CONFIG_FILE)]
path: PathBuf,
},
}
Expand Down Expand Up @@ -97,19 +99,25 @@ fn main() -> Result<(), Box<dyn Error>> {
{
let device = hid::HpmDevice::open().or_else(|_| Err("can't open HPMicro usb device"))?;
let mut memory_config_bin = Vec::new();

if let Some(path) = config {
let mut config_file = fs::File::open(path)?;

memory_config_bin.resize(12, 0);
config_file.read_exact(memory_config_bin.as_mut_slice())?;
if memory_config_bin[3] != 0xFC || memory_config_bin[2] != 0xF9 {
return Err("Invalid memory config file".into());
let is_default = config.is_none();
let config_path = config.unwrap_or(DEFAULT_CONFIG_FILE.into());

match fs::File::open(config_path) {
Ok(mut config_file) => {
memory_config_bin.resize(12, 0);
config_file.read_exact(memory_config_bin.as_mut_slice())?;
if memory_config_bin[3] != 0xFC || memory_config_bin[2] != 0xF9 {
return Err("Invalid memory config file".into());
}
}
// Use default config file and it isn't exists
Err(_) if is_default => {
let xpi_config = XpiNorConfigurationOption::new();
xpi_config.write(&mut memory_config_bin)?;
}
Err(err) => {
Err(err)?;
}
} else {
let xpi_config = XpiNorConfigurationOption::new();

xpi_config.write(&mut memory_config_bin)?;
}

// Config memory
Expand Down

0 comments on commit 6b3d03c

Please sign in to comment.