Skip to content

Commit

Permalink
feat: impl serial watch
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Nov 4, 2023
1 parent 5133cee commit ad30077
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `--watch-serial` for `flash` subcommand

### Changed

- No erase by default when flashing
Expand Down
145 changes: 145 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ keywords = ["embedded", "WCH", "CH32V", "WCH-Link"]
readme = "README.md"
license = "MIT/Apache-2.0"

[features]
default = []

[dependencies]
anyhow = "1.0.68"
bitfield = "0.14.0"
Expand All @@ -29,3 +32,4 @@ object = { version = "0.32", default-features = false, features = [
"std",
] }
indicatif = "0.17.7"
serialport = { version = "4.2.2" }
4 changes: 2 additions & 2 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
Result, RiscvChip,
};

const VENDOR_ID: u16 = 0x1a86;
const PRODUCT_ID: u16 = 0x8010;
pub const VENDOR_ID: u16 = 0x1a86;
pub const PRODUCT_ID: u16 = 0x8010;

const ENDPOINT_OUT: u8 = 0x01;
const ENDPOINT_IN: u8 = 0x81;
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub enum Error {
DmiFailed,
#[error("Operation timeout")]
Timeout,
#[error("Serial port error: {0}")]
Serial(#[from] serialport::Error),
#[error("Io error: {0}")]
Io(#[from] std::io::Error),
}

#[derive(Debug, Clone, Copy)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod dmi;
pub mod error;
pub mod flash_op;
pub mod format;
mod operations;
pub mod operations;
pub mod regs;
pub mod transport;

Expand Down
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ enum Commands {
/// Enable SDI print after reset
#[arg(long, default_value = "false")]
enable_sdi_print: bool,
/// Open serial port(print only) after reset
#[arg(long, default_value = "false")]
watch_serial: bool,
/// Path to the firmware file to flash
path: String,
},
Expand Down Expand Up @@ -310,6 +313,7 @@ fn main() -> Result<()> {
no_run,
path,
enable_sdi_print,
watch_serial,
} => {
probe.dump_info(false)?;

Expand Down Expand Up @@ -339,9 +343,13 @@ fn main() -> Result<()> {
if enable_sdi_print {
probe.enable_sdi_print(true)?;
will_detach = false;
log::info!("Now you can connect to the WCH-Link serial port");
log::info!("Now connect to the WCH-Link serial port to read SDI print");
}
if watch_serial {
wlink::operations::watch_serial()?;
} else {
sleep(Duration::from_millis(500));
}
sleep(Duration::from_millis(500));
}
}
Unprotect {} => {
Expand Down
37 changes: 37 additions & 0 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,40 @@ fn parse_misa(misa: u32) -> Option<String> {
}
Some(s)
}

/// SDI print
pub fn watch_serial() -> Result<()> {
use serialport::SerialPortType;

let port_info = serialport::available_ports()?
.into_iter()
.find(|port| {
if let SerialPortType::UsbPort(info) = &port.port_type {
info.vid == crate::device::VENDOR_ID && info.pid == crate::device::PRODUCT_ID
} else {
false
}
})
.ok_or_else(|| Error::Custom("No serial port found".to_string()))?;
log::debug!("Opening serial port: {:?}", port_info.port_name);

let mut port = serialport::new(&port_info.port_name, 115200)
.timeout(Duration::from_millis(1000))
.open()?;

log::trace!("Serial port opened: {:?}", port);

loop {
let mut buf = [0u8; 1024];
match port.read(&mut buf) {
Ok(n) => {
if n > 0 {
let s = String::from_utf8_lossy(&buf[..n]);
print!("{}", s);
}
}
Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut => (),
Err(e) => return Err(e.into()),
}
}
}

0 comments on commit ad30077

Please sign in to comment.