mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-08 00:16:13 +00:00
add dedicated handling for Ch34x baud rate 921600
This commit is contained in:
parent
76f0260a55
commit
18e300efa3
@ -229,8 +229,8 @@ public class DeviceTest {
|
|||||||
doReadWrite(reason, -1);
|
doReadWrite(reason, -1);
|
||||||
}
|
}
|
||||||
private void doReadWrite(String reason, int readWait) throws Exception {
|
private void doReadWrite(String reason, int readWait) throws Exception {
|
||||||
byte[] buf1 = new byte[]{ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
|
byte[] buf1 = new byte[]{ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x55, 0x55};
|
||||||
byte[] buf2 = new byte[]{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26};
|
byte[] buf2 = new byte[]{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x55, 0x55};
|
||||||
byte[] data;
|
byte[] data;
|
||||||
|
|
||||||
telnet.write(buf1);
|
telnet.write(buf1);
|
||||||
@ -420,6 +420,36 @@ public class DeviceTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void Ch34xBaudRate() throws Exception {
|
||||||
|
Assume.assumeTrue("only for Ch34x", usb.serialDriver instanceof Ch34xSerialDriver);
|
||||||
|
usb.open();
|
||||||
|
|
||||||
|
int[] baudRates = {
|
||||||
|
115200, 230400, 256000, 307200, 460800, 921600, 1000000, 1228800
|
||||||
|
};
|
||||||
|
for (int baudRate : baudRates) {
|
||||||
|
telnet.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
|
usb.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
|
doReadWrite(baudRate + "");
|
||||||
|
try {
|
||||||
|
usb.setParameters(baudRate + (1 << 29), 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
|
doReadWrite(baudRate + "+(1<<29)");
|
||||||
|
|
||||||
|
usb.setParameters(baudRate - 1, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
|
doReadWrite(baudRate + "-1");
|
||||||
|
|
||||||
|
usb.setParameters(baudRate + 1, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
|
doReadWrite(baudRate + "+1");
|
||||||
|
if (baudRate == 921600)
|
||||||
|
fail("error expected for " + baudRate + " baud");
|
||||||
|
} catch(AssertionError err) {
|
||||||
|
if (baudRate != 921600)
|
||||||
|
throw(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void baudRate() throws Exception {
|
public void baudRate() throws Exception {
|
||||||
usb.open();
|
usb.open();
|
||||||
|
@ -10,6 +10,9 @@ import android.hardware.usb.UsbDevice;
|
|||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
import android.hardware.usb.UsbEndpoint;
|
import android.hardware.usb.UsbEndpoint;
|
||||||
import android.hardware.usb.UsbInterface;
|
import android.hardware.usb.UsbInterface;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.hoho.android.usbserial.BuildConfig;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -191,29 +194,39 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
|
|
||||||
private void setBaudRate(int baudRate) throws IOException {
|
private void setBaudRate(int baudRate) throws IOException {
|
||||||
final long CH341_BAUDBASE_FACTOR = 1532620800;
|
long factor;
|
||||||
final int CH341_BAUDBASE_DIVMAX = 3;
|
long divisor;
|
||||||
|
|
||||||
long factor = CH341_BAUDBASE_FACTOR / baudRate;
|
if (baudRate == 921600) {
|
||||||
int divisor = CH341_BAUDBASE_DIVMAX;
|
divisor = 7;
|
||||||
|
factor = 0xf300;
|
||||||
|
} else {
|
||||||
|
final long BAUDBASE_FACTOR = 1532620800;
|
||||||
|
final int BAUDBASE_DIVMAX = 3;
|
||||||
|
|
||||||
|
if(BuildConfig.DEBUG && (baudRate & (3<<29)) == (1<<29))
|
||||||
|
baudRate &= ~(1<<29); // for testing purpose bypass dedicated baud rate handling
|
||||||
|
factor = BAUDBASE_FACTOR / baudRate;
|
||||||
|
divisor = BAUDBASE_DIVMAX;
|
||||||
while ((factor > 0xfff0) && divisor > 0) {
|
while ((factor > 0xfff0) && divisor > 0) {
|
||||||
factor >>= 3;
|
factor >>= 3;
|
||||||
divisor--;
|
divisor--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (factor > 0xfff0) {
|
if (factor > 0xfff0) {
|
||||||
throw new UnsupportedOperationException("Unsupported baud rate: " + baudRate);
|
throw new UnsupportedOperationException("Unsupported baud rate: " + baudRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
factor = 0x10000 - factor;
|
factor = 0x10000 - factor;
|
||||||
|
}
|
||||||
|
|
||||||
divisor |= 0x0080; // else ch341a waits until buffer full
|
divisor |= 0x0080; // else ch341a waits until buffer full
|
||||||
int ret = controlOut(0x9a, 0x1312, (int) ((factor & 0xff00) | divisor));
|
int val1 = (int) ((factor & 0xff00) | divisor);
|
||||||
|
int val2 = (int) (factor & 0xff);
|
||||||
|
Log.d(TAG, String.format("baud rate=%d, 0x1312=0x%04x, 0x0f2c=0x%04x", baudRate, val1, val2));
|
||||||
|
int ret = controlOut(0x9a, 0x1312, val1);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
throw new IOException("Error setting baud rate: #1)");
|
throw new IOException("Error setting baud rate: #1)");
|
||||||
}
|
}
|
||||||
|
ret = controlOut(0x9a, 0x0f2c, val2);
|
||||||
ret = controlOut(0x9a, 0x0f2c, (int) (factor & 0xff));
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
throw new IOException("Error setting baud rate: #2");
|
throw new IOException("Error setting baud rate: #2");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user