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

It is possible to use CH375DLL.dll to prevent change driver on Windows. #54

Open
DeqingSun opened this issue Aug 20, 2024 · 3 comments
Open

Comments

@DeqingSun
Copy link

Hi,
Today I came across that the wchisp was added as the tool for upload in arduino_core_ch32 by Adafruit.

I read the readme and browsed the code (Sorry I never used rust before), it seems the transport is using libUSB only. It is actually possible to check target device on Windows with libUSB first, then fallback to CH375DLL to do communication with WCH driver.

Some of my working code can be viewed on https://github.com/DeqingSun/vnproch551/blob/master/main.cpp

@DeqingSun
Copy link
Author

DeqingSun commented Jan 14, 2025

Ok, as my first try on Rust, I can confirm rust can use CH375DLL64.dll to access bootloader.

use libloading::{Library, Symbol};

fn main() {
    println!("Hello, world!");

    unsafe {
        let lib = match Library::new("CH375DLL64.dll") {
            Ok(lib) => lib,
            Err(e) => {
                println!("Failed to load CH375DLL64.dll: {}", e);
                return;
            }
        };

        let ch375_get_drv_version: Symbol<unsafe extern "C" fn() -> u32> = lib.get(b"CH375GetDrvVersion").expect("Failed to load function");
        let ch375_get_usb_id: Symbol<unsafe extern "C" fn(u32) -> u32> = lib.get(b"CH375GetUsbID").expect("Failed to load function");
        let ch375_open_device: Symbol<unsafe extern "C" fn(u32) -> u32> = lib.get(b"CH375OpenDevice").expect("Failed to load function");
        let ch375_write_data: Symbol<unsafe extern "C" fn(u32, *mut u8, *mut u32) -> bool> = lib.get(b"CH375WriteData").expect("Failed to load function");
        let ch375_read_data: Symbol<unsafe extern "C" fn(u32, *mut u8, *mut u32) -> bool> = lib.get(b"CH375ReadData").expect("Failed to load function");


        let version = ch375_get_drv_version();
        println!("Driver version: {}", version);
        let usb_id = ch375_get_usb_id(0);
        println!("USB ID: 0x{:08x}", usb_id);

        if usb_id == 0x55e04348 || usb_id == 0x55e01a86 {
            println!("CH375 detected");
            let result = ch375_open_device(0);
            if result > 0 {
                println!("CH375 open OK");

                // send detect command
                let mut detect_cmd = [0xA1, 0x12, 0x00, 0x00, 0x11, 0x4D, 0x43, 0x55, 0x20, 0x49, 0x53, 0x50, 0x20, 0x26, 0x20, 0x57, 0x43, 0x48, 0x2e, 0x43, 0x4E];
                let mut write_len = detect_cmd.len() as u32;

                let write_result = ch375_write_data(0, detect_cmd.as_mut_ptr(), &mut write_len);
                println!("Write result: {}", write_result);

                // read response

                let mut detect_respond = [0u8; 64];
                let mut read_len = (detect_cmd[1] + 3) as u32;
                let read_result = ch375_read_data(0, detect_respond.as_mut_ptr(), &mut read_len);
                println!("Read result: {}", read_result);
                println!("Read data: {:02x?}", detect_respond);

            } else {
                println!("CH375 open failed");
            }
        } else {
            println!("CH375 not detected");
        }
    }
}

and I got output from CH552.

Hello, world!
Driver version: 0
USB ID: 0x55e04348
CH375 detected
CH375 open OK
Write result: true
Read result: true
Read data: [a1, a7, 02, 00, 52, 11, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]

Let's see if I can merge the code into WCHISP and get it working without changing driver.

@andelf
Copy link
Contributor

andelf commented Jan 14, 2025

@DeqingSun
Copy link
Author

Thanks that is great.

Does wlink covers everything that wchisp can do? Or there are some feature only in wchisp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants