Added test application for flashing

This commit is contained in:
Sebastian 2024-10-04 16:20:36 +02:00
parent 77eebdf795
commit b2830a1fbc
5 changed files with 115 additions and 3 deletions

View file

@ -1,10 +1,16 @@
use std::time::Duration;
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() -> () {
pub fn main() -> anyhow::Result<()> {
let mut port = serialport::new("/dev/ttyACM0", 115_200)
.timeout(Duration::from_millis(10))
.open()
@ -13,4 +19,28 @@ pub fn main() -> () {
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(())
}