Compare commits

..

No commits in common. '6b79d67ff8099adc49373cf493094c9513162778' and '4e2c0f79a6d1c35c15fa107a04d5b726b0420132' have entirely different histories.

6
Cargo.lock generated

@ -75,7 +75,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cargo-dfu"
version = "0.0.4"
version = "0.0.3"
dependencies = [
"cargo-project",
"colored",
@ -156,9 +156,7 @@ dependencies = [
[[package]]
name = "dfu"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb1d1eb7e47b3cba338775698712a8cb285aa15d45e58329c59411a55fbc1b98"
version = "0.4.3"
dependencies = [
"log",
"nix",

@ -1,7 +1,7 @@
[package]
name = "cargo-dfu"
authors = ["Roman Kretschmer <roman@kretschmer.email>"]
version = "0.0.4"
version = "0.0.3"
edition = "2021"
description = "cargo extension for flashing embedded rust programs via dfu"
license = "MIT"
@ -22,4 +22,7 @@ cargo-project = "0.2.4"
structopt = "0.3.2"
maplit = "1.0.2"
log = "0.4.6"
dfu = "0.4"
dfu = {version = "0.4", path = "/home/roman/dfuflash/dfu"}
#[dependencies.dfu]
# path = "/home/roman/dfuflash/dfu"

@ -27,12 +27,6 @@ cargo dfu <args> --vid <vid> --pid <pid>
cargo dfu
```
#### specifying the chip
```bash
cargo dfu --chip stm32
```
#### specifying the vid and pid
```bash
@ -42,12 +36,8 @@ cargo dfu --vid 0x483 --pid 0xdf11
## Add chip definitions
feel free to open a PR to add chips to this
## Notes
some chips like the gd32vf103 need additional udev rules therefor you need to copy the rules from the udev file into /etc/udev/rules.d/
```lang=bash
sudo cp udev.rules /etc/udev/rules.d/cargo-dfu.rules
```
## Roadmap
- [ ] add chip to vendor map so one can optionally use --chip to specify the desired chip
- [ ] add some more chips to the crate (like the gd32vf103)
- [ ] 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

@ -17,13 +17,6 @@ fn main() {
// Skip the first arg which is the calling application name.
let opt = Opt::from_iter(std::env::args().skip(1));
if opt.list_chips {
for vendor in vendor_map() {
println!("{}", vendor.0);
}
return;
}
// Try and get the cargo project information.
let project = cargo_project::Project::query(".").expect("Couldn't parse the Cargo.toml");
@ -58,7 +51,7 @@ fn main() {
// todo, keep as iter. difficult because we want to filter map remove two items at once.
// Remove our args as cargo build does not understand them.
let flags = ["--pid", "--vid", "--chip"].iter();
let flags = ["--pid", "--vid"].iter();
for flag in flags {
if let Some(index) = args.iter().position(|x| x == flag) {
args.remove(index);
@ -83,22 +76,6 @@ fn main() {
let d = if let (Some(v), Some(p)) = (opt.vid, opt.pid) {
open_device_with_vid_pid(v, p)
.expect("Are you sure device is plugged in and in bootloader mode?")
} else if let Some(c) = opt.chip {
println!(" {} for a connected {}.", "Searching".green().bold(), c);
let mut device: Option<rusb::DeviceHandle<GlobalContext>> = None;
let vendor = vendor_map();
if let Some(products) = vendor.get(&c) {
for (v, p) in products {
if let Some(d) = open_device_with_vid_pid(*v, *p) {
device = Some(d);
break;
}
}
}
device.expect("Are you sure device is plugged in and in bootloader mode?")
} else {
println!(
" {} for a connected device with known vid/pid pair.",
@ -113,9 +90,11 @@ fn main() {
let mut device: Option<rusb::DeviceHandle<GlobalContext>> = None;
let vendor = vendor_map();
for d in devices {
for vendor in vendor_map() {
if vendor.1.contains(&(d.vendor_id(), d.product_id())) {
if let Some(products) = vendor.get(&d.vendor_id()) {
if products.contains(&d.product_id()) {
if let Some(d) = open_device_with_vid_pid(d.vendor_id(), d.product_id()) {
device = Some(d);
break;
@ -128,7 +107,7 @@ fn main() {
println!(
" {} {} {}",
"Found ".green().bold(),
"Trying ".green().bold(),
d.read_manufacturer_string_ascii(&d.device().device_descriptor().unwrap())
.unwrap(),
d.read_product_string_ascii(&d.device().device_descriptor().unwrap())
@ -203,9 +182,4 @@ struct Opt {
pid: Option<u16>,
#[structopt(name = "vid", long = "vid", parse(try_from_str = parse_hex_16))]
vid: Option<u16>,
#[structopt(name = "chip", long = "chip")]
chip: Option<String>,
#[structopt(name = "list-chips", long = "list-chips")]
list_chips: bool,
}

@ -8,15 +8,14 @@ use std::{fs::File, io::Read};
pub enum UtilError {
Elf(goblin::error::Error),
Dfu(dfu::error::Error),
File(std::io::Error),
File(std::io::Error)
}
/// Returns a contiguous bin with 0s between non-contiguous sections and starting address from an elf.
pub fn elf_to_bin(path: PathBuf) -> Result<(Vec<u8>, u32), UtilError> {
let mut file = File::open(path).map_err(|e| UtilError::File(e))?;
let mut buffer = vec![];
file.read_to_end(&mut buffer)
.map_err(|e| UtilError::File(e))?;
file.read_to_end(&mut buffer).map_err(|e| UtilError::File(e))?;
let binary = goblin::elf::Elf::parse(buffer.as_slice()).map_err(|e| UtilError::Elf(e))?;
@ -59,11 +58,9 @@ pub fn flash_bin(
address: u32,
d: &rusb::Device<GlobalContext>,
) -> Result<(), UtilError> {
let mut dfu = dfu::Dfu::from_bus_device(d.bus_number(), d.address(), 0_u32, 0_u32)
.map_err(|e| UtilError::Dfu(e))?;
let mut dfu = dfu::Dfu::from_bus_device(d.bus_number(), d.address(), 0_u32, 0_u32).map_err(|e| UtilError::Dfu(e))?;
if binary.len() < 2048 {
dfu.write_flash_from_slice(address, binary)
.map_err(|e| UtilError::Dfu(e))?;
dfu.write_flash_from_slice(address, binary).map_err(|e| UtilError::Dfu(e))?;
} else {
// hacky bug workaround
std::fs::write("target/out.bin", binary).map_err(|e| UtilError::File(e))?;
@ -74,17 +71,15 @@ pub fn flash_bin(
.map_err(|e| UtilError::File(e))?,
address,
None,
)
.map_err(|e| UtilError::Dfu(e))?;
).map_err(|e| UtilError::Dfu(e))?;
std::fs::remove_file("target/out.bin").map_err(|e| UtilError::File(e))?;
}
Ok(())
}
pub fn vendor_map() -> std::collections::HashMap<String, Vec<(u16, u16)>> {
pub fn vendor_map() -> std::collections::HashMap<u16, Vec<u16>> {
maplit::hashmap! {
"stm32".to_string() => vec![(0x0483, 0xdf11)],
"gd32vf103".to_string() => vec![(0x28e9, 0x0189)],
0x0483 => vec![0xdf11],
}
}

@ -1,2 +0,0 @@
# gd32vf103
SUBSYSTEMS=="usb" ATTRS{idVendor}=="28e9" ATTRS{idProduct}=="0189" MODE:="0666"
Loading…
Cancel
Save