Added progress to flash tool
This commit is contained in:
parent
b2830a1fbc
commit
57fdf05f00
4 changed files with 290 additions and 48 deletions
112
daemon/src/bin/flash-dfu.rs
Normal file
112
daemon/src/bin/flash-dfu.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
use std::{
|
||||
io::{self, Seek},
|
||||
thread,
|
||||
time::{self, Duration},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use clap::Parser;
|
||||
use dfu_libusb::{Dfu, DfuLibusb};
|
||||
use postcard::{from_bytes_cobs, to_stdvec_cobs};
|
||||
use radomctl_protocol::*;
|
||||
use radomctld::logger::setup_logger;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
/// The usb serial number of the radom-controller
|
||||
#[arg(short, long)]
|
||||
serialnumber: String,
|
||||
|
||||
/// The firmware file to flash
|
||||
#[arg(short, long)]
|
||||
firmware: std::path::PathBuf,
|
||||
}
|
||||
|
||||
pub fn main() -> anyhow::Result<()> {
|
||||
let args = Cli::parse();
|
||||
|
||||
let ports = serialport::available_ports().unwrap_or(Vec::<serialport::SerialPortInfo>::new());
|
||||
|
||||
let mut radom_port: Option<String> = None;
|
||||
for port in ports {
|
||||
match port.port_type {
|
||||
serialport::SerialPortType::UsbPort(usb_port_info) => {
|
||||
match usb_port_info.serial_number {
|
||||
Some(serial) => {
|
||||
if serial == args.serialnumber {
|
||||
radom_port = Some(port.port_name.to_owned());
|
||||
println!("Found radom-controller as {}", port.port_name)
|
||||
}
|
||||
}
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
let radom_port = match radom_port {
|
||||
Some(port) => port,
|
||||
_ => {
|
||||
return Err(anyhow!("No matching port found."));
|
||||
}
|
||||
};
|
||||
|
||||
println!("Setting radom-controller to dfu boot...");
|
||||
|
||||
let mut port = serialport::new(radom_port, 115_200)
|
||||
.timeout(Duration::from_millis(10))
|
||||
.open()
|
||||
.expect("Failed to open port");
|
||||
|
||||
let host_msg = HostMessage::TriggerDFUBootloader;
|
||||
let msg_bytes = to_stdvec_cobs(&host_msg).unwrap();
|
||||
port.write_all(&msg_bytes).unwrap();
|
||||
drop(port);
|
||||
|
||||
let context = rusb::Context::new()?;
|
||||
let mut file = std::fs::File::open(args.firmware).context("firmware file not found")?;
|
||||
|
||||
thread::sleep(time::Duration::from_millis(2000));
|
||||
|
||||
let file_size =
|
||||
u32::try_from(file.seek(io::SeekFrom::End(0))?).context("the firmware file is too big")?;
|
||||
file.seek(io::SeekFrom::Start(0))?;
|
||||
|
||||
let vid = 0x0483;
|
||||
let pid = 0xdf11;
|
||||
let intf = 0;
|
||||
let alt = 0;
|
||||
let mut device: Dfu<rusb::Context> =
|
||||
DfuLibusb::open(&context, vid, pid, intf, alt).context("could not open device")?;
|
||||
|
||||
println!("Flashing radom-controller ...");
|
||||
|
||||
let bar = indicatif::ProgressBar::new(file_size as u64);
|
||||
bar.set_style(
|
||||
indicatif::ProgressStyle::default_bar()
|
||||
.template(
|
||||
"{spinner:.green} [{elapsed_precise}] [{bar}] \
|
||||
{bytes}/{total_bytes} ({bytes_per_sec}) ({eta}) {msg:10}",
|
||||
)?
|
||||
.progress_chars("=>-"),
|
||||
);
|
||||
|
||||
device.with_progress({
|
||||
let bar = bar.clone();
|
||||
move |count| {
|
||||
bar.inc(count as u64);
|
||||
if bar.position() == file_size as u64 {
|
||||
bar.finish();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
device
|
||||
.download(file, file_size)
|
||||
.context("could not write firmware to the device")?;
|
||||
|
||||
println!("Done!");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
use std::{
|
||||
io::{self, Seek},
|
||||
thread,
|
||||
time::{self, Duration},
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use dfu_libusb::{Dfu, DfuLibusb};
|
||||
use postcard::{from_bytes_cobs, to_stdvec_cobs};
|
||||
use radomctl_protocol::*;
|
||||
use radomctld::logger::setup_logger;
|
||||
|
||||
pub fn main() -> anyhow::Result<()> {
|
||||
let mut port = serialport::new("/dev/ttyACM0", 115_200)
|
||||
.timeout(Duration::from_millis(10))
|
||||
.open()
|
||||
.expect("Failed to open port");
|
||||
|
||||
let host_msg = HostMessage::TriggerDFUBootloader;
|
||||
let msg_bytes = to_stdvec_cobs(&host_msg).unwrap();
|
||||
port.write_all(&msg_bytes).unwrap();
|
||||
drop(port);
|
||||
|
||||
let context = rusb::Context::new()?;
|
||||
let mut file = std::fs::File::open("../../firmware/radomctl-firmware.bin")
|
||||
.context("firmware file not found")?;
|
||||
|
||||
thread::sleep(time::Duration::from_millis(1000));
|
||||
|
||||
let file_size =
|
||||
u32::try_from(file.seek(io::SeekFrom::End(0))?).context("the firmware file is too big")?;
|
||||
file.seek(io::SeekFrom::Start(0))?;
|
||||
|
||||
let vid = 0x0483;
|
||||
let pid = 0xdf11;
|
||||
let intf = 0;
|
||||
let alt = 0;
|
||||
let mut device: Dfu<rusb::Context> =
|
||||
DfuLibusb::open(&context, vid, pid, intf, alt).context("could not open device")?;
|
||||
|
||||
device
|
||||
.download(file, file_size)
|
||||
.context("could not write firmware to the device")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue