1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-07 07:56:20 +00:00

Merge pull request #195 from kai-morich/ch340-parameter

CH34x: data bits, parity, stop bits
This commit is contained in:
kai-morich 2019-10-04 16:50:16 +02:00 committed by GitHub
commit c89ca2c96a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 8 deletions

View File

@ -49,6 +49,17 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
private final UsbDevice mDevice;
private final UsbSerialPort mPort;
private static final int LCR_ENABLE_RX = 0x80;
private static final int LCR_ENABLE_TX = 0x40;
private static final int LCR_MARK_SPACE = 0x20;
private static final int LCR_PAR_EVEN = 0x10;
private static final int LCR_ENABLE_PAR = 0x08;
private static final int LCR_STOP_BITS_2 = 0x04;
private static final int LCR_CS8 = 0x03;
private static final int LCR_CS7 = 0x02;
private static final int LCR_CS6 = 0x01;
private static final int LCR_CS5 = 0x00;
public Ch34xSerialDriver(UsbDevice device) {
mDevice = device;
mPort = new Ch340SerialPort(mDevice, 0);
@ -288,7 +299,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
checkState("init #4", 0x95, 0x2518, new int[]{-1 /* 0x56, c3*/, 0x00});
if (controlOut(0x9a, 0x2518, 0x0050) < 0) {
if (controlOut(0x9a, 0x2518, LCR_ENABLE_RX | LCR_ENABLE_TX | LCR_CS8) < 0) {
throw new IOException("init failed! #5");
}
@ -340,7 +351,60 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
throws IOException {
setBaudRate(baudRate);
// TODO databit, stopbit and paraty set not implemented
int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX;
switch (dataBits) {
case DATABITS_5:
lcr |= LCR_CS5;
break;
case DATABITS_6:
lcr |= LCR_CS6;
break;
case DATABITS_7:
lcr |= LCR_CS7;
break;
case DATABITS_8:
lcr |= LCR_CS8;
break;
default:
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits);
}
switch (parity) {
case PARITY_NONE:
break;
case PARITY_ODD:
lcr |= LCR_ENABLE_PAR;
break;
case PARITY_EVEN:
lcr |= LCR_ENABLE_PAR | LCR_PAR_EVEN;
break;
case PARITY_MARK:
lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE;
break;
case PARITY_SPACE:
lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN;
break;
default:
throw new IllegalArgumentException("Unknown parity value: " + parity);
}
switch (stopBits) {
case STOPBITS_1:
break;
case STOPBITS_1_5:
throw new IllegalArgumentException("Unsupported stopBits value: 1.5");
case STOPBITS_2:
lcr |= LCR_STOP_BITS_2;
break;
default:
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
}
int ret = controlOut(0x9a, 0x2518, lcr);
if (ret < 0) {
throw new IOException("Error setting control byte");
}
}
@Override

View File

@ -264,26 +264,38 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
configDataBits |= 0x0800;
break;
default:
configDataBits |= 0x0800;
break;
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits);
}
switch (parity) {
case PARITY_NONE:
break;
case PARITY_ODD:
configDataBits |= 0x0010;
break;
case PARITY_EVEN:
configDataBits |= 0x0020;
break;
case PARITY_MARK:
configDataBits |= 0x0030;
break;
case PARITY_SPACE:
configDataBits |= 0x0040;
break;
default:
throw new IllegalArgumentException("Unknown parity value: " + parity);
}
switch (stopBits) {
case STOPBITS_1:
configDataBits |= 0;
break;
case STOPBITS_1_5:
throw new IllegalArgumentException("Unsupported stopBits value: 1.5");
case STOPBITS_2:
configDataBits |= 2;
break;
default:
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
}
setConfigSingle(SILABSER_SET_LINE_CTL_REQUEST_CODE, configDataBits);
}

View File

@ -378,7 +378,18 @@ public class FtdiSerialDriver implements UsbSerialDriver {
throws IOException {
setBaudRate(baudRate);
int config = dataBits;
int config = 0;
switch (dataBits) {
case DATABITS_5:
case DATABITS_6:
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits);
case DATABITS_7:
case DATABITS_8:
config |= dataBits;
break;
default:
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits);
}
switch (parity) {
case PARITY_NONE:
@ -405,8 +416,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
config |= (0x00 << 11);
break;
case STOPBITS_1_5:
config |= (0x01 << 11);
break;
throw new IllegalArgumentException("Unsupported stopBits value: 1.5");
case STOPBITS_2:
config |= (0x02 << 11);
break;