- structural changes (Device as instance)
git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@101 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -13,16 +13,17 @@ import ch.ntb.usb.USBTimeoutException;
|
||||
public class UartDispatch {
|
||||
|
||||
/**
|
||||
* Maximal number of bytes allowed as UART payload. This value is specified
|
||||
* in <code>UART_INBUF_LEN</code> in the file <code>uart.h</code>.
|
||||
* Size of the UART buffer on the USB controller. <br>
|
||||
* This value is specified in <code>UART_BUF_LEN</code> in the file
|
||||
* <code>uart.h</code>.
|
||||
*/
|
||||
public static final int MAX_UART_PAYLOAD = 128;
|
||||
public static final int UART_BUF_LEN = 256;
|
||||
|
||||
private static boolean running = false;
|
||||
|
||||
static Thread dispatchThread;
|
||||
private static Thread dispatchThread;
|
||||
|
||||
static LinkedList<Uart> uarts = new LinkedList<Uart>();
|
||||
private static LinkedList<Uart> uarts = new LinkedList<Uart>();
|
||||
|
||||
/**
|
||||
* Starts the read thread for all Uarts. If the thread is already running,
|
||||
|
||||
@@ -36,7 +36,7 @@ public class UartOutputStream extends OutputStream {
|
||||
}
|
||||
int newLen = 0;
|
||||
do {
|
||||
newLen = Math.min(len, UartDispatch.MAX_UART_PAYLOAD);
|
||||
newLen = Math.min(len, UartDispatch.UART_BUF_LEN);
|
||||
byte[] data = new byte[newLen];
|
||||
for (int i = 0; i < newLen; i++) {
|
||||
data[i] = b[off + i];
|
||||
@@ -47,6 +47,6 @@ public class UartOutputStream extends OutputStream {
|
||||
throw new IOException(e.getMessage());
|
||||
}
|
||||
len -= newLen;
|
||||
} while (len > UartDispatch.MAX_UART_PAYLOAD);
|
||||
} while (len > UartDispatch.UART_BUF_LEN);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Uart0 {
|
||||
*/
|
||||
private static final byte STYPE_UART_0_IN = 0x11;
|
||||
|
||||
public static final int MAX_UART_PAYLOAD = UartDispatch.MAX_UART_PAYLOAD;
|
||||
public static final int MAX_UART_PAYLOAD = UartDispatch.UART_BUF_LEN;
|
||||
|
||||
private static void write(byte packetSubType, byte[] data, int len)
|
||||
throws USBException {
|
||||
|
||||
@@ -30,7 +30,7 @@ public class UartTest {
|
||||
}
|
||||
|
||||
static public void button1() {
|
||||
byte[] buffer = new byte[UartDispatch.MAX_UART_PAYLOAD];
|
||||
byte[] buffer = new byte[UartDispatch.UART_BUF_LEN];
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = (byte) ('0' + (i % ('z' - '0')));
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class UartTest {
|
||||
|
||||
static InputStream in;
|
||||
|
||||
static byte[] buffer = new byte[UartDispatch.MAX_UART_PAYLOAD];
|
||||
static byte[] buffer = new byte[UartDispatch.UART_BUF_LEN];
|
||||
|
||||
public static void init() {
|
||||
|
||||
@@ -49,7 +49,7 @@ public class UartTest {
|
||||
}
|
||||
|
||||
static public void button1() {
|
||||
byte[] buffer = new byte[UartDispatch.MAX_UART_PAYLOAD];
|
||||
byte[] buffer = new byte[UartDispatch.UART_BUF_LEN];
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = (byte) ('0' + (i % ('z' - '0')));
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import ch.ntb.usb.USB;
|
||||
import ch.ntb.usb.USBException;
|
||||
|
||||
public class Dispatch {
|
||||
|
||||
|
||||
private static Logger logger = LogUtil.ch_ntb_mcdp_usb;
|
||||
|
||||
// Main Types
|
||||
@@ -44,7 +44,7 @@ public class Dispatch {
|
||||
*/
|
||||
public static final byte STYPE_ERROR_PACKET_END = 0x72;
|
||||
|
||||
private static byte[] usbData = new byte[USB.MAX_DATA_SIZE];
|
||||
private static byte[] usbData = new byte[USB.HIGHSPEED_MAX_BULK_PACKET_SIZE];
|
||||
|
||||
private static LinkedList<DataPacket> bdiData, uartData;
|
||||
|
||||
@@ -69,7 +69,8 @@ public class Dispatch {
|
||||
}
|
||||
mainType = data[index++];
|
||||
subtype = data[index++];
|
||||
int dataLen = (data[index++] & 0xFF) * 0x100 + (data[index++] & 0xFF);
|
||||
int dataLen = (data[index++] & 0xFF) * 0x100
|
||||
+ (data[index++] & 0xFF);
|
||||
if (data[index + dataLen] != DataPacket.PACKET_END) {
|
||||
throw new DispatchException("PACKET_END or packetLen ("
|
||||
+ dataLen + " bytes) wrong");
|
||||
@@ -118,7 +119,8 @@ public class Dispatch {
|
||||
if (!bdiData.isEmpty()) {
|
||||
return bdiData.poll();
|
||||
}
|
||||
int dataLength = USBDevice.read_BDI(usbData, USB.MAX_DATA_SIZE);
|
||||
int dataLength = USBDevice.read_BDI(usbData, USBDevice
|
||||
.getMaxPacketSize());
|
||||
dispatch(usbData, dataLength);
|
||||
return bdiData.poll();
|
||||
}
|
||||
@@ -128,7 +130,8 @@ public class Dispatch {
|
||||
logger.info("buffer not empty");
|
||||
return uartData.poll();
|
||||
}
|
||||
int dataLength = USBDevice.read_UART(usbData, USB.MAX_DATA_SIZE);
|
||||
int dataLength = USBDevice.read_UART(usbData, USBDevice
|
||||
.getMaxPacketSize());
|
||||
dispatch(usbData, dataLength);
|
||||
return uartData.poll();
|
||||
}
|
||||
|
||||
@@ -27,11 +27,10 @@ public class USBDevice {
|
||||
private static final int BDI_Timeout = 1000;
|
||||
|
||||
private static final int UART_Timeout = 50;
|
||||
|
||||
|
||||
private static Device dev;
|
||||
|
||||
static {
|
||||
// set data for our device
|
||||
dev = USB.getDevice(IdVendor, IdProduct);
|
||||
}
|
||||
|
||||
@@ -51,8 +50,7 @@ public class USBDevice {
|
||||
dev.bulkwrite(OUT_Endpoint_BDI, data, length, BDI_Timeout);
|
||||
}
|
||||
|
||||
public static synchronized int read_BDI(byte[] data, int size)
|
||||
throws USBException {
|
||||
public static int read_BDI(byte[] data, int size) throws USBException {
|
||||
return dev.bulkread(IN_Endpoint_BDI, data, size, BDI_Timeout);
|
||||
}
|
||||
|
||||
@@ -60,8 +58,15 @@ public class USBDevice {
|
||||
dev.bulkwrite(OUT_Endpoint_UART, data, length, UART_Timeout);
|
||||
}
|
||||
|
||||
public static synchronized int read_UART(byte[] data, int size)
|
||||
throws USBException {
|
||||
public static int read_UART(byte[] data, int size) throws USBException {
|
||||
return dev.bulkread(IN_Endpoint_UART, data, size, UART_Timeout);
|
||||
}
|
||||
|
||||
public static int getMaxPacketSize() {
|
||||
return dev.getMaxPacketSize();
|
||||
}
|
||||
|
||||
public static Device getDevice() {
|
||||
return dev;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user