[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/SolveBugs/NormalCode/patch-1/Ch340Chip.java [Back]  [Original]

public class CH340Chip  {
    
    public boolean config(UsbDeviceConnection usbDeviceConnection) {
        if (!UartInit(usbDeviceConnection)) {//
            return false;

        } else {//
            if (SetConfig(usbDeviceConnection, baudRate, (byte) 8, (byte) 1,
                    (byte) 0, (byte) 0)) {
                Logger.i(TAG, "Uart Configed");
            } else {
                return false;
            }
        }
        return true;
    }


    private boolean UartInit(UsbDeviceConnection usbDeviceConnection) {
        int ret;
        int size = 8;
        byte[] buffer = new byte[size];
        Uart_Control_Out(usbDeviceConnection, UartCmd.VENDOR_SERIAL_INIT, 0x0000, 0x0000);
        ret = Uart_Control_In(usbDeviceConnection, UartCmd.VENDOR_VERSION, 0x0000, 0x0000, buffer, 2);
        if (ret < 0)
            return false;
        Uart_Control_Out(usbDeviceConnection, UartCmd.VENDOR_WRITE, 0x1312, 0xD982);
        Uart_Control_Out(usbDeviceConnection, UartCmd.VENDOR_WRITE, 0x0f2c, 0x0004);
        ret = Uart_Control_In(usbDeviceConnection, UartCmd.VENDOR_READ, 0x2518, 0x0000, buffer, 2);
        if (ret < 0)
            return false;
        Uart_Control_Out(usbDeviceConnection, UartCmd.VENDOR_WRITE, 0x2727, 0x0000);
        Uart_Control_Out(usbDeviceConnection, UartCmd.VENDOR_MODEM_OUT, 0x00ff, 0x0000);
        return true;
    }

    private int Uart_Control_Out(UsbDeviceConnection usbDeviceConnection, int request, int value, int index) {
        int retval = 0;
        retval = usbDeviceConnection.controlTransfer(UsbType.USB_TYPE_VENDOR
                        | UsbType.USB_RECIP_DEVICE | UsbType.USB_DIR_OUT, request,
                value, index, null, 0, timeOut);

        return retval;
    }

    private int Uart_Control_In(UsbDeviceConnection usbDeviceConnection, int request, int value, int index,
                                byte[] buffer, int length) {
        int retval = 0;
        retval = usbDeviceConnection.controlTransfer(UsbType.USB_TYPE_VENDOR
                        | UsbType.USB_RECIP_DEVICE | UsbType.USB_DIR_IN, request,
                value, index, buffer, length, timeOut);
        return retval;
    }

    private final class UartCmd {
        public static final int VENDOR_WRITE_TYPE = 0x40;
        public static final int VENDOR_READ_TYPE = 0xC0;
        public static final int VENDOR_READ = 0x95;
        public static final int VENDOR_WRITE = 0x9A;
        public static final int VENDOR_SERIAL_INIT = 0xA1;
        public static final int VENDOR_MODEM_OUT = 0xA4;
        public static final int VENDOR_VERSION = 0x5F;
    }

    private final class UsbType {
        public static final int USB_TYPE_VENDOR = (0x02 

Web Proxy Viewer  |  New URL  |  Original Page