Compare commits

...

6 Commits

Author SHA1 Message Date
Roman Kretschmer 8800b22edf
remove `dfu` subcommand from being parsed by `clap`
4 years ago
Roman Kretschmer bca314c152
Merge pull request #2 from gnxlxnxx/dfu-libusb
4 years ago
Roman Kretschmer 14ddda22d9
Update README
4 years ago
Roman Kretschmer 3f2a0b3a7a Update dfu-libusb
4 years ago
Roman Kretschmer be0b991e66
remove address
4 years ago
Roman Kretschmer a6e389c29d
Move to dfu-libusb
4 years ago

110
Cargo.lock generated

@ -64,6 +64,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bytes"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8"
[[package]] [[package]]
name = "cargo-dfu" name = "cargo-dfu"
version = "0.0.4" version = "0.0.4"
@ -71,7 +77,7 @@ dependencies = [
"cargo-project", "cargo-project",
"clap", "clap",
"colored", "colored",
"dfu", "dfu-libusb",
"goblin", "goblin",
"log", "log",
"maplit", "maplit",
@ -161,15 +167,37 @@ dependencies = [
] ]
[[package]] [[package]]
name = "dfu" name = "dfu-core"
version = "0.4.2" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb1d1eb7e47b3cba338775698712a8cb285aa15d45e58329c59411a55fbc1b98" checksum = "3b82ffc1790e4af9371dbc3399c6c068a3ee506357632e21eb7111df199c59f3"
dependencies = [ dependencies = [
"log", "bytes",
"nix", "displaydoc",
"serde", "thiserror",
"usbapi", ]
[[package]]
name = "dfu-libusb"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c2092d59a31cc15f8e9a916ff6b020a0fe794b1ec8626d08eefeb7ad2bcb8f7"
dependencies = [
"dfu-core",
"libusb1-sys",
"rusb",
"thiserror",
]
[[package]]
name = "displaydoc"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bf95dc3f046b9da4f2d51833c0d3547d8564ef6910f5c1ed130306a75b92886"
dependencies = [
"proc-macro2",
"quote",
"syn",
] ]
[[package]] [[package]]
@ -315,15 +343,6 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "memoffset"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.4.4" version = "0.4.4"
@ -334,19 +353,6 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "nix"
version = "0.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5e06129fb611568ef4e868c14b326274959aa70ff7776e9d55323531c374945"
dependencies = [
"bitflags",
"cc",
"cfg-if",
"libc",
"memoffset",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.44" version = "0.1.44"
@ -522,9 +528,6 @@ name = "serde"
version = "1.0.130" version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913"
dependencies = [
"serde_derive",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
@ -566,15 +569,6 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "sysfs-serde"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95eb4a78bc7d9d0bbc4a4cd2f5d2756e67e27a4d11bb52df6406508dae15066a"
dependencies = [
"log",
]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.2" version = "1.1.2"
@ -590,6 +584,26 @@ version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"
@ -616,18 +630,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "usbapi"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc33ce833e969eaf16937f8ec81bd10e52428159e4d45dd3d70b0c0f6558e416"
dependencies = [
"libc",
"log",
"nix",
"sysfs-serde",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"

@ -22,4 +22,4 @@ cargo-project = "0.2.4"
clap = {version = "3.0.10", features=["derive"]} clap = {version = "3.0.10", features=["derive"]}
maplit = "1.0.2" maplit = "1.0.2"
log = "0.4.6" log = "0.4.6"
dfu = "0.4" dfu-libusb = "0.2.0"

@ -49,5 +49,4 @@ sudo cp udev.rules /etc/udev/rules.d/cargo-dfu.rules
``` ```
## Roadmap ## Roadmap
- [ ] make this crate multi-platform (PR to either the dfu crate to use rusb or the usbapi to add platform support)
- [ ] check if multiple chips are connected - [ ] check if multiple chips are connected

@ -16,7 +16,7 @@ fn main() {
// Get commandline options. // Get commandline options.
// Skip the first arg which is the calling application name. // Skip the first arg which is the calling application name.
let opt = Opt::parse_from(std::env::args()); let opt = Opt::parse_from(std::env::args().skip(1));
if opt.list_chips { if opt.list_chips {
for vendor in vendor_map() { for vendor in vendor_map() {
@ -138,14 +138,12 @@ fn main() {
println!(" {} {:?}", "Flashing".green().bold(), path); println!(" {} {:?}", "Flashing".green().bold(), path);
let (binary, address) = elf_to_bin(path).unwrap(); let (binary, _) = elf_to_bin(path).unwrap();
// Start timer. // Start timer.
let instant = Instant::now(); let instant = Instant::now();
// let bininfo = hf2::bin_info(&d).expect("bin_info failed"); flash_bin(&binary, &d.device()).unwrap();
// log::debug!("{:?}", bininfo);
flash_bin(&binary, address, &d.device()).unwrap();
// Stop timer. // Stop timer.
let elapsed = instant.elapsed(); let elapsed = instant.elapsed();

@ -7,7 +7,7 @@ use std::{fs::File, io::Read};
#[derive(Debug)] #[derive(Debug)]
pub enum UtilError { pub enum UtilError {
Elf(goblin::error::Error), Elf(goblin::error::Error),
Dfu(dfu::error::Error), Dfu(dfu_libusb::Error),
File(std::io::Error), File(std::io::Error),
} }
@ -54,31 +54,27 @@ pub fn elf_to_bin(path: PathBuf) -> Result<(Vec<u8>, u32), UtilError> {
Ok((data, start_address as u32)) Ok((data, start_address as u32))
} }
pub fn flash_bin( pub fn flash_bin(binary: &[u8], d: &rusb::Device<GlobalContext>) -> Result<(), UtilError> {
binary: &[u8], let mut dfu = dfu_libusb::DfuLibusb::open(
address: u32, &rusb::Context::new().unwrap(),
d: &rusb::Device<GlobalContext>, d.device_descriptor().unwrap().vendor_id(),
) -> Result<(), UtilError> { d.device_descriptor().unwrap().product_id(),
let mut dfu = dfu::Dfu::from_bus_device(d.bus_number(), d.address(), 0_u32, 0_u32) 0,
.map_err(|e| UtilError::Dfu(e))?; 0,
if binary.len() < 2048 { )
dfu.write_flash_from_slice(address, binary) .map_err(|e| UtilError::Dfu(e))?;
.map_err(|e| UtilError::Dfu(e))?;
} else {
// hacky bug workaround
std::fs::write("target/out.bin", binary).map_err(|e| UtilError::File(e))?;
dfu.download_raw(
&mut std::fs::OpenOptions::new()
.read(true)
.open("target/out.bin")
.map_err(|e| UtilError::File(e))?,
address,
None,
)
.map_err(|e| UtilError::Dfu(e))?;
std::fs::remove_file("target/out.bin").map_err(|e| UtilError::File(e))?;
}
std::fs::write("target/out.bin", binary).map_err(|e| UtilError::File(e))?;
dfu.download(
&mut std::fs::OpenOptions::new()
.read(true)
.open("target/out.bin")
.map_err(|e| UtilError::File(e))?,
std::fs::metadata("target/out.bin")
.map_err(|e| UtilError::File(e))?
.len() as u32,
)
.unwrap();
Ok(()) Ok(())
} }

Loading…
Cancel
Save