Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Serial watch #36

Merged
merged 6 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Install libudev for Linux
if: runner.os == 'Linux'
run: sudo apt-get install libudev-dev
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run help
run: cargo run -- --help
3 changes: 3 additions & 0 deletions .github/workflows/nightly-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
toolchain: nightly
components: rustfmt, clippy
override: true
- name: Install libudev for Linux (optional)
if: matrix.os == 'Linux'
run: sudo apt-get install libudev-dev
- name: Build
run: cargo build --release
- name: Run help
Expand Down
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, #36

### 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 = "4.2.2"
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
[docsrs]: https://docs.rs/wlink
[nightly]: https://github.com/ch32-rs/wlink/releases/tag/nightly

**NOTE**: This tool is still in development and not ready for production use.
> **Note**
> This tool is still in development and not ready for production use.

## Feature Support

- [x] Flash firmware, support Intel HEX, ELF and raw binary format
- [x] Erase chip
- [x] Halt, resume, reset support
- [x] Read chip info
- [x] Read chip memory
- [x] Read chip register
- [x] Write chip register
- [x] Read chip memory(flash)
- [x] Read/write chip register - very handy for debugging
- [x] Code-Protect & Code-Unprotect for supported chips
- [x] [SDI print](https://www.cnblogs.com/liaigu/p/17628184.html) support
- [x] [SDI print](https://www.cnblogs.com/liaigu/p/17628184.html) support, requires 2.10+ firmware
- [x] Serial port watching(#36) for a smooth development experience

## Tested On

Expand Down Expand Up @@ -77,8 +79,15 @@ Current firmware version: 2.11 (aka. v31).

`cargo install --git https://github.com/ch32-rs/wlink` or download a binary from the [Nightly Release page](https://github.com/ch32-rs/wlink/releases/tag/nightly).

> **Note**
> On Linux, you should install libudev and libusb development lib first.
> Like `sudo apt install libudev-dev libusb-1.0-0-dev` on Ubuntu.

## Usage

> **Note**
> For help of wire connection for specific chips, please refer to `docs` subdirectory.

```console
> # Flash firmware.bin to Code FLASH at address 0x08000000
> wlink flash --address 0x08000000 ./firmware.bin
Expand All @@ -89,6 +98,21 @@ Current firmware version: 2.11 (aka. v31).
12:10:28 [INFO] Now reset...
12:10:28 [INFO] Resume executing...

> # Flash firmware.bin to System FLASH, enable SDI print, then watch serial port
> wlink flash --enable-sdi-print --watch-serial firmware.bin
02:54:34 [INFO] WCH-Link v2.11 (WCH-LinkE-CH32V305)
02:54:34 [INFO] Attached chip: CH32V003 [CH32V003F4P6] (ChipID: 0x00300500)
02:54:34 [INFO] Flash already unprotected
02:54:34 [INFO] Flash protected: false
02:54:35 [INFO] Flash done
02:54:35 [INFO] Now reset...
02:54:35 [INFO] Now connect to the WCH-Link serial port to read SDI print
Hello world from ch32v003 SDI print!
led toggle
led toggle
...


> # Dump Code FLASH, for verification
> # use `-v` or `-vv` for more logs
> wlink -v dump 0x08000000 100
Expand All @@ -109,10 +133,12 @@ Current firmware version: 2.11 (aka. v31).
08000050: f3 23 40 f1 b7 02 00 00 93 82 02 00 63 f4 72 00 ×#@×ו00×ו0c×r0
08000060: 6f 00 c0 29 o0×)


> # Dump System FLASH, BOOT_28KB
> wlink dump 0x1FFF8000 0x7000
....


> # Dump all general purpose registers
> wlink regs
16:24:20 [INFO] Dump GPRs
Expand Down
4 changes: 3 additions & 1 deletion src/commands/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ impl Command for SetPower {
}

/// SDI print support, only avaliable for WCH-LinkE
/// Firmware version >= 2.10
#[derive(Debug)]
pub enum SetSDIPrint {
Enable,
Disable,
}
impl Command for SetSDIPrint {
type Response = ();
// 0x00 success, 0xff not support
type Response = u8;
const COMMAND_ID: u8 = 0x0d;
fn payload(&self) -> Vec<u8> {
match self {
Expand Down
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/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub fn objcopy_binary(elf_data: &[u8]) -> Result<Vec<u8>> {
.data(endian, elf_data)
.map_err(|_| anyhow::format_err!("Failed to access data for an ELF segment."))?;
if !segment_data.is_empty() && segment.p_type(endian) == PT_LOAD {
log::info!(
log::debug!(
"Found loadable segment, physical address: {:#010x}, virtual address: {:#010x}, flags: {:#x}",
p_paddr,
p_vaddr,
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
Loading