Skip to content

Commit

Permalink
Wait for port (#22)
Browse files Browse the repository at this point in the history
* Create trivial serial port opener to handle waiting for a port

* Stitch in new wait command

* use PathBuf directly for file paths on disk

* Bump version
  • Loading branch information
Ralim authored May 9, 2024
1 parent bd176ea commit 1f882a7
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 116 deletions.
2 changes: 1 addition & 1 deletion bestool/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bestool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bestool"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 3 additions & 3 deletions bestool/src/beslink/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn read_message(serial_port: &mut Box<dyn SerialPort>) -> Result<BesMessage,
}
}
pub fn validate_packet_checksum(packet: &[u8]) -> Result<(), BESLinkError> {
let checksum = calculate_message_checksum(&packet[0..packet.len()-1]);
let checksum = calculate_message_checksum(&packet[0..packet.len() - 1]);
if checksum == packet[packet.len() - 1] {
return Ok(());
}
Expand Down Expand Up @@ -275,7 +275,7 @@ mod tests {
],
];
for v in test_messages {
assert!(validate_packet_checksum(&v).is_ok())
}
assert!(validate_packet_checksum(&v).is_ok())
}
}
}
36 changes: 21 additions & 15 deletions bestool/src/cmds/read_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ 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::path::PathBuf;
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: &PathBuf,
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: &PathBuf,
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();
}
55 changes: 26 additions & 29 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::path::PathBuf;
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: &PathBuf, 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: &PathBuf,
serial_port: &mut Box<dyn SerialPort>,
) -> Result<(), BESLinkError> {
// Open file, read file, call burn_image_to_flash
Expand Down
89 changes: 44 additions & 45 deletions bestool/src/cmds/write_image_then_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,70 @@ 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;

use std::path::PathBuf;
use std::time::Duration;
use tracing::error;
use tracing::info;

pub fn cmd_write_image_then_monitor(
input_file: String,
serial_port: String,
input_file_path: &PathBuf,
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_path, &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: &PathBuf,
serial_port: &mut Box<dyn SerialPort>,
) -> Result<(), BESLinkError> {
// Open file, read file, call burn_image_to_flash
Expand Down
32 changes: 20 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,21 @@ 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.unwrap().to_str().unwrap().to_owned(),
args.port,
),
BesTool::WriteImage(args) => cmd_write_image(&args.firmware_path, &args.port, args.wait),
BesTool::ReadImage(args) => cmd_read_image(
args.firmware_path.unwrap().to_str().unwrap().to_owned(),
args.port,
&args.firmware_path,
&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,
&args.port,
args.monitor_baud_rate,
args.wait,
),
}
}
Loading

0 comments on commit 1f882a7

Please sign in to comment.