Skip to content

Commit

Permalink
Stitch in new wait command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralim committed May 9, 2024
1 parent 9306066 commit 9babaf9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 110 deletions.
35 changes: 20 additions & 15 deletions bestool/src/cmds/read_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@ 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::*;
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<dyn SerialPort>,
start: usize,
length: usize,
Expand Down
14 changes: 4 additions & 10 deletions bestool/src/cmds/serial_monitor.rs
Original file line number Diff line number Diff line change
@@ -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();
}
53 changes: 25 additions & 28 deletions bestool/src/cmds/write_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,44 @@ 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;

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<dyn SerialPort>,
) -> Result<(), BESLinkError> {
// Open file, read file, call burn_image_to_flash
Expand Down
88 changes: 43 additions & 45 deletions bestool/src/cmds/write_image_then_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<dyn SerialPort>,
) -> Result<(), BESLinkError> {
// Open file, read file, call burn_image_to_flash
Expand Down
34 changes: 22 additions & 12 deletions bestool/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<std::path::PathBuf>,
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<std::path::PathBuf>,
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<std::path::PathBuf>,
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() {
Expand All @@ -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,
),
}
}

0 comments on commit 9babaf9

Please sign in to comment.