diff --git a/bestool/src/cmds/read_image.rs b/bestool/src/cmds/read_image.rs index 16ee892..ac1ef11 100644 --- a/bestool/src/cmds/read_image.rs +++ b/bestool/src/cmds/read_image.rs @@ -2,6 +2,7 @@ use crate::beslink::{ helper_sync_and_load_programmer, read_flash_data, send_device_reboot, BESLinkError, BES_PROGRAMMING_BAUDRATE, }; +use crate::serial_port_opener::open_serial_port_with_wait; use serialport::SerialPort; use std::fs::File; use std::io::prelude::*; @@ -9,26 +10,30 @@ use std::time::Duration; use tracing::error; use tracing::info; -pub fn cmd_read_image(input_file: String, serial_port: String, start: usize, length: usize) { +pub fn cmd_read_image( + input_file: &str, + port_name: &str, + start: usize, + length: usize, + wait_for_port: bool, +) { //First gain sync to the device - println!("Reading binary data from {serial_port} @ {BES_PROGRAMMING_BAUDRATE}"); - let mut serial_port = serialport::new(serial_port, BES_PROGRAMMING_BAUDRATE); - serial_port = serial_port.timeout(Duration::from_millis(5000)); + println!("Reading binary data from {port_name} @ {BES_PROGRAMMING_BAUDRATE}"); + let mut port = open_serial_port_with_wait(port_name, BES_PROGRAMMING_BAUDRATE, wait_for_port); + port.set_timeout(Duration::from_millis(5000)) + .expect("Cant set port timeout"); - match serial_port.open() { - Ok(mut port) => match do_read_flash_data(input_file, &mut port, start, length) { - Ok(_) => { - info!("Done..."); - } - Err(e) => { - error!("Failed {:?}", e); - } - }, - Err(e) => println!("Failed to open serial port - {e:?}"), + match do_read_flash_data(input_file, &mut port, start, length) { + Ok(_) => { + info!("Done..."); + } + Err(e) => { + error!("Failed {:?}", e); + } } } fn do_read_flash_data( - output_file_path: String, + output_file_path: &str, serial_port: &mut Box, start: usize, length: usize, diff --git a/bestool/src/cmds/serial_monitor.rs b/bestool/src/cmds/serial_monitor.rs index d881742..345b4d1 100644 --- a/bestool/src/cmds/serial_monitor.rs +++ b/bestool/src/cmds/serial_monitor.rs @@ -1,15 +1,9 @@ -use crate::serial_monitor::run_serial_monitor; +use crate::{serial_monitor::run_serial_monitor, serial_port_opener::open_serial_port_with_wait}; -pub fn cmd_serial_port_monitor(port_name: String, baud_rate: u32) { +pub fn cmd_serial_port_monitor(port_name: &str, baud_rate: u32, wait_for_port: bool) { // Span a basic serial port monitor attached to the serial port // Eventually we will hook in extra utility commands - println!("Opening serial monitor to {port_name} @ {baud_rate}"); - let serial_port = serialport::new(port_name, baud_rate); + let port = open_serial_port_with_wait(port_name, baud_rate, wait_for_port); - match serial_port.open() { - Ok(port) => { - let _ = run_serial_monitor(port); - } - Err(e) => println!("Failed to open serial port - {e:?}"), - } + run_serial_monitor(port).unwrap(); } diff --git a/bestool/src/cmds/write_image.rs b/bestool/src/cmds/write_image.rs index 52a1819..5033d79 100644 --- a/bestool/src/cmds/write_image.rs +++ b/bestool/src/cmds/write_image.rs @@ -2,6 +2,7 @@ use crate::beslink::{ burn_image_to_flash, helper_sync_and_load_programmer, send_device_reboot, BESLinkError, BES_PROGRAMMING_BAUDRATE, }; +use crate::serial_port_opener::open_serial_port_with_wait; use serialport::{ClearBuffer, SerialPort}; use std::fs; @@ -9,40 +10,36 @@ use std::time::Duration; use tracing::error; use tracing::info; -pub fn cmd_write_image(input_file: String, serial_port: String) { +pub fn cmd_write_image(input_file: &str, port_name: &str, wait_for_port: bool) { //First gain sync to the device - println!("Writing binary data to {serial_port} @ {BES_PROGRAMMING_BAUDRATE}"); - let mut serial_port = serialport::new(serial_port, BES_PROGRAMMING_BAUDRATE); - serial_port = serial_port.timeout(Duration::from_millis(5000)); + println!("Writing binary data to {port_name} @ {BES_PROGRAMMING_BAUDRATE}"); + let mut port = open_serial_port_with_wait(port_name, BES_PROGRAMMING_BAUDRATE, wait_for_port); + port.set_timeout(Duration::from_millis(5000)) + .expect("Cant set port timeout"); - match serial_port.open() { - Ok(mut port) => { - let _ = port.clear(ClearBuffer::All); - info!("Starting loader and checking communications"); - match helper_sync_and_load_programmer(&mut port) { - Ok(_) => { - info!("Done..."); - } - Err(e) => { - error!("Failed {:?}", e); - return; - } - } - info!("Now doing firmware load"); - match do_burn_image_to_flash(input_file, &mut port) { - Ok(_) => { - info!("Done..."); - } - Err(e) => { - error!("Failed {:?}", e); - } - } + let _ = port.clear(ClearBuffer::All); + info!("Starting loader and checking communications"); + match helper_sync_and_load_programmer(&mut port) { + Ok(_) => { + info!("Done..."); + } + Err(e) => { + error!("Failed {:?}", e); + return; + } + } + info!("Now doing firmware load"); + match do_burn_image_to_flash(input_file, &mut port) { + Ok(_) => { + info!("Done..."); + } + Err(e) => { + error!("Failed {:?}", e); } - Err(e) => println!("Failed to open serial port - {e:?}"), } } fn do_burn_image_to_flash( - input_file: String, + input_file: &str, serial_port: &mut Box, ) -> Result<(), BESLinkError> { // Open file, read file, call burn_image_to_flash diff --git a/bestool/src/cmds/write_image_then_monitor.rs b/bestool/src/cmds/write_image_then_monitor.rs index 553b591..ac60a56 100644 --- a/bestool/src/cmds/write_image_then_monitor.rs +++ b/bestool/src/cmds/write_image_then_monitor.rs @@ -3,6 +3,7 @@ use crate::beslink::{ BES_PROGRAMMING_BAUDRATE, }; use crate::serial_monitor::run_serial_monitor; +use crate::serial_port_opener::open_serial_port_with_wait; use serialport::{ClearBuffer, SerialPort}; use std::fs; @@ -12,62 +13,59 @@ use tracing::error; use tracing::info; pub fn cmd_write_image_then_monitor( - input_file: String, - serial_port: String, + input_file: &str, + serial_port: &str, monitor_baud_rate: u32, + wait_for_port: bool, ) { //First gain sync to the device println!( "Writing binary data to {serial_port} @ {BES_PROGRAMMING_BAUDRATE}; then monitoring at {monitor_baud_rate}" ); - let mut serial_port = serialport::new(serial_port, BES_PROGRAMMING_BAUDRATE); - serial_port = serial_port.timeout(Duration::from_millis(5000)); + let mut port = open_serial_port_with_wait(serial_port, BES_PROGRAMMING_BAUDRATE, wait_for_port); + port.set_timeout(Duration::from_millis(5000)) + .expect("Cant set port timeout"); - match serial_port.open() { - Ok(mut port) => { - let _ = port.clear(ClearBuffer::All); - info!("Starting loader and checking communications"); - match helper_sync_and_load_programmer(&mut port) { - Ok(_) => { - info!("Done..."); - } - Err(e) => { - error!("Failed {:?}", e); - return; - } - } - info!("Now doing firmware load"); - match do_burn_image_to_flash(input_file, &mut port) { - Ok(_) => { - info!("Done..."); - } - Err(e) => { - error!("Failed {:?}", e); - return; - } - } - info!("Starting monitoring"); - match port.set_baud_rate(monitor_baud_rate) { - Ok(_) => { - info!("Done..."); - } - Err(e) => { - error!("Failed {:?}", e); - return; - } - } - match run_serial_monitor(port) { - Ok(_) => {} - Err(e) => { - error!("Failed monitoring: {:?}", e); - } - } + let _ = port.clear(ClearBuffer::All); + info!("Starting loader and checking communications"); + match helper_sync_and_load_programmer(&mut port) { + Ok(_) => { + info!("Done..."); + } + Err(e) => { + error!("Failed {:?}", e); + return; + } + } + info!("Now doing firmware load"); + match do_burn_image_to_flash(input_file, &mut port) { + Ok(_) => { + info!("Done..."); + } + Err(e) => { + error!("Failed {:?}", e); + return; + } + } + info!("Starting monitoring"); + match port.set_baud_rate(monitor_baud_rate) { + Ok(_) => { + info!("Done..."); + } + Err(e) => { + error!("Failed {:?}", e); + return; + } + } + match run_serial_monitor(port) { + Ok(_) => {} + Err(e) => { + error!("Failed monitoring: {:?}", e); } - Err(e) => println!("Failed to open serial port - {e:?}"), } } fn do_burn_image_to_flash( - input_file: String, + input_file: &str, serial_port: &mut Box, ) -> Result<(), BESLinkError> { // Open file, read file, call burn_image_to_flash diff --git a/bestool/src/main.rs b/bestool/src/main.rs index 60c17ab..d225699 100644 --- a/bestool/src/main.rs +++ b/bestool/src/main.rs @@ -1,6 +1,7 @@ mod beslink; mod cmds; mod serial_monitor; +mod serial_port_opener; use crate::cmds::{ cmd_list_serial_ports, cmd_read_image, cmd_serial_port_monitor, cmd_write_image, cmd_write_image_then_monitor, @@ -38,33 +39,41 @@ struct SerialMonitor { serial_port_path: String, #[arg(short, long, default_value_t = 2000000)] baud_rate: u32, + #[arg(short, long, default_value_t = false)] + wait: bool, } #[derive(clap::Args, Debug)] #[command(author, version, about, long_about = None)] struct WriteImage { - firmware_path: Option, + firmware_path: std::path::PathBuf, #[arg(short, long)] port: String, + #[arg(short, long, default_value_t = false)] + wait: bool, } #[derive(clap::Args, Debug)] #[command(author, version, about, long_about = None)] struct WriteImageThenMonitor { - firmware_path: Option, + firmware_path: std::path::PathBuf, #[arg(short, long)] port: String, #[arg(short, long, default_value_t = 2000000)] monitor_baud_rate: u32, + #[arg(short, long, default_value_t = false)] + wait: bool, } #[derive(clap::Args, Debug)] #[command(author, version, about, long_about = None)] struct ReadImage { - firmware_path: Option, + firmware_path: std::path::PathBuf, #[arg(short, long)] port: String, #[arg(short, long, default_value_t = 1024*1024*4)] // default to full flash length: u32, #[arg(short, long, default_value_t = 0)] // default to start of flash offset: u32, + #[arg(short, long, default_value_t = false)] + wait: bool, } fn main() { @@ -78,22 +87,23 @@ fn main() { match BesTool::parse() { BesTool::ListSerialPorts(_) => cmd_list_serial_ports(), BesTool::SerialMonitor(args) => { - cmd_serial_port_monitor(args.serial_port_path, args.baud_rate); + cmd_serial_port_monitor(&args.serial_port_path, args.baud_rate, args.wait); + } + BesTool::WriteImage(args) => { + cmd_write_image(args.firmware_path.to_str().unwrap(), &args.port, args.wait) } - BesTool::WriteImage(args) => cmd_write_image( - args.firmware_path.unwrap().to_str().unwrap().to_owned(), - args.port, - ), BesTool::ReadImage(args) => cmd_read_image( - args.firmware_path.unwrap().to_str().unwrap().to_owned(), - args.port, + args.firmware_path.to_str().unwrap(), + &args.port, args.offset as usize, args.length as usize, + args.wait, ), BesTool::WriteImageThenMonitor(args) => cmd_write_image_then_monitor( - args.firmware_path.unwrap().to_str().unwrap().to_owned(), - args.port, + args.firmware_path.to_str().unwrap(), + &args.port, args.monitor_baud_rate, + args.wait, ), } }