From 3071e1d496b98a4d444783b72be18d32d7634ad7 Mon Sep 17 00:00:00 2001 From: HalfSweet Date: Fri, 12 Jan 2024 20:39:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0SWD=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E8=B0=83=E8=AF=95=E5=99=A8=E5=92=8C=E9=80=9F=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AirISP.rs | 2 +- src/peripheral/swd.rs | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/AirISP.rs b/src/AirISP.rs index 60eee0f..6b1bc13 100644 --- a/src/AirISP.rs +++ b/src/AirISP.rs @@ -48,7 +48,7 @@ pub fn air_isp() -> Command .long("baud") .help(t!("baud_help")) .value_parser(value_parser! { u32 }) - .default_value("115200"); + .default_value("0"); let trace = Arg::new("trace") .global(true) diff --git a/src/peripheral/swd.rs b/src/peripheral/swd.rs index 1d14b2b..7c758d6 100644 --- a/src/peripheral/swd.rs +++ b/src/peripheral/swd.rs @@ -1,7 +1,7 @@ use std::error::Error; use colored::{Color, Colorize}; -use probe_rs::{flashing, MemoryInterface, Permissions, Session}; +use probe_rs::{flashing, Lister, MemoryInterface, Permissions, Session}; use probe_rs::flashing::DownloadOptions; use rust_i18n::t; @@ -26,10 +26,34 @@ impl Swd<'_> { } fn get_chip_session(&mut self) -> Result> { - let mut session; - let mut chip_name = self.target.clone(); - session = Session::auto_attach(chip_name, Permissions::default())?; - Ok(session) + let session; + let chip_name = self.target.clone(); + let mut speed = self.air_isp.get_baud(); + if speed == 0 { + speed = 200; // 默认速度200k + } + + // session = Session::auto_attach(chip_name, Permissions::default())?; + if self.air_isp.get_port() == "auto" { + session = Session::auto_attach(chip_name, Permissions::default())?; + return Ok(session) + } else { + let lister = Lister::new(); + let probe_list = lister.list_all(); + for i in probe_list { + // 输入的端口名称和扫描到的名称子串匹配 + if i.identifier.to_lowercase().contains(self.air_isp.get_port().as_str()) { + let mut probe = i.open(&lister)?; + probe.set_speed(speed)?; + session = probe.attach(chip_name, Permissions::default())?; + return Ok(session) + } + } + } + Err(Box::new(std::io::Error::new( + std::io::ErrorKind::Other, + "get probe fail", + ))) } } From d531fe715688c7ceedb090dca65111857be5e0b3 Mon Sep 17 00:00:00 2001 From: HalfSweet Date: Fri, 12 Jan 2024 20:44:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20general=5Fuart=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E4=B8=B2=E5=8F=A3=E5=92=8C=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=B3=A2=E7=89=B9=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/peripheral/general_uart.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/peripheral/general_uart.rs b/src/peripheral/general_uart.rs index 8a062e1..6083399 100644 --- a/src/peripheral/general_uart.rs +++ b/src/peripheral/general_uart.rs @@ -53,7 +53,21 @@ pub struct GeneralUart<'a> { impl GeneralUart<'_> { pub fn new(air_isp: &AirISP::AirISP) -> GeneralUart { - let port = serialport::new(air_isp.get_port(), air_isp.get_baud()) + let mut port = air_isp.get_port(); + if port == "auto" { + // 选择第一个串口 + let ports = serialport::available_ports().unwrap(); + if ports.len() == 0 { + LOG.error(t!("no_serial_port_help").as_str()); + std::process::exit(AirISP::ExitCode::PpError as i32); + } + port = ports[0].port_name.clone(); + } + let mut speed = air_isp.get_baud(); + if speed == 0 { + speed = 115200; // 默认波特率115200 + } + let port = serialport::new(port, speed) .timeout(std::time::Duration::from_millis(2000)) .parity(serialport::Parity::Even) .open() From dfdd36c5d5b73955a8829f3137c0acbecce4ffd8 Mon Sep 17 00:00:00 2001 From: HalfSweet Date: Fri, 12 Jan 2024 20:52:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E6=89=93=E5=8D=B0=E7=9A=84log?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/peripheral/general_uart.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/peripheral/general_uart.rs b/src/peripheral/general_uart.rs index 6083399..46b2676 100644 --- a/src/peripheral/general_uart.rs +++ b/src/peripheral/general_uart.rs @@ -53,31 +53,31 @@ pub struct GeneralUart<'a> { impl GeneralUart<'_> { pub fn new(air_isp: &AirISP::AirISP) -> GeneralUart { - let mut port = air_isp.get_port(); - if port == "auto" { + let mut port_name = air_isp.get_port(); + if port_name == "auto" { // 选择第一个串口 let ports = serialport::available_ports().unwrap(); if ports.len() == 0 { LOG.error(t!("no_serial_port_help").as_str()); std::process::exit(AirISP::ExitCode::PpError as i32); } - port = ports[0].port_name.clone(); + port_name = ports[0].port_name.clone(); } let mut speed = air_isp.get_baud(); if speed == 0 { speed = 115200; // 默认波特率115200 } - let port = serialport::new(port, speed) + let port = serialport::new(port_name.clone(), speed) .timeout(std::time::Duration::from_millis(2000)) .parity(serialport::Parity::Even) .open() // 显示错误信息,并退出程序 .unwrap_or_else(|e| { - LOG.error(t!("open_serial_fail_help", "TTY" => air_isp.get_port(), "error" => e).as_str()); + LOG.error(t!("open_serial_fail_help", "TTY" => port_name, "error" => e).as_str()); std::process::exit(AirISP::ExitCode::PpError as i32); }); - LOG.info(t!("open_serial_success_help", "TTY" => air_isp.get_port()).as_str(),Color::Green); + LOG.info(t!("open_serial_success_help", "TTY" => port_name).as_str(), Color::Green); GeneralUart { air_isp,