Skip to content

Commit

Permalink
Merge pull request #2 from Air-duino/swd
Browse files Browse the repository at this point in the history
新增SWD下载方式的调试器和速率选择
  • Loading branch information
HalfSweet authored Jan 12, 2024
2 parents c8b8da6 + dfdd36c commit 091f032
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/AirISP.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions src/peripheral/general_uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,31 @@ 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_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_name = ports[0].port_name.clone();
}
let mut speed = air_isp.get_baud();
if speed == 0 {
speed = 115200; // 默认波特率115200
}
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,
Expand Down
34 changes: 29 additions & 5 deletions src/peripheral/swd.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -26,10 +26,34 @@ impl Swd<'_> {
}

fn get_chip_session(&mut self) -> Result<Session, Box<dyn Error>> {
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",
)))
}
}

Expand Down

0 comments on commit 091f032

Please sign in to comment.