- 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:
schlaepfer
2006-02-17 17:04:25 +00:00
parent 27bfb3683f
commit 438202bfef
7 changed files with 31 additions and 22 deletions

View File

@@ -13,16 +13,17 @@ import ch.ntb.usb.USBTimeoutException;
public class UartDispatch { public class UartDispatch {
/** /**
* Maximal number of bytes allowed as UART payload. This value is specified * Size of the UART buffer on the USB controller. <br>
* in <code>UART_INBUF_LEN</code> in the file <code>uart.h</code>. * 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; 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, * Starts the read thread for all Uarts. If the thread is already running,

View File

@@ -36,7 +36,7 @@ public class UartOutputStream extends OutputStream {
} }
int newLen = 0; int newLen = 0;
do { do {
newLen = Math.min(len, UartDispatch.MAX_UART_PAYLOAD); newLen = Math.min(len, UartDispatch.UART_BUF_LEN);
byte[] data = new byte[newLen]; byte[] data = new byte[newLen];
for (int i = 0; i < newLen; i++) { for (int i = 0; i < newLen; i++) {
data[i] = b[off + i]; data[i] = b[off + i];
@@ -47,6 +47,6 @@ public class UartOutputStream extends OutputStream {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} }
len -= newLen; len -= newLen;
} while (len > UartDispatch.MAX_UART_PAYLOAD); } while (len > UartDispatch.UART_BUF_LEN);
} }
} }

View File

@@ -14,7 +14,7 @@ public class Uart0 {
*/ */
private static final byte STYPE_UART_0_IN = 0x11; 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) private static void write(byte packetSubType, byte[] data, int len)
throws USBException { throws USBException {

View File

@@ -30,7 +30,7 @@ public class UartTest {
} }
static public void button1() { 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++) { for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) ('0' + (i % ('z' - '0'))); buffer[i] = (byte) ('0' + (i % ('z' - '0')));
} }

View File

@@ -15,7 +15,7 @@ public class UartTest {
static InputStream in; 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() { public static void init() {
@@ -49,7 +49,7 @@ public class UartTest {
} }
static public void button1() { 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++) { for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) ('0' + (i % ('z' - '0'))); buffer[i] = (byte) ('0' + (i % ('z' - '0')));
} }

View File

@@ -8,7 +8,7 @@ import ch.ntb.usb.USB;
import ch.ntb.usb.USBException; import ch.ntb.usb.USBException;
public class Dispatch { public class Dispatch {
private static Logger logger = LogUtil.ch_ntb_mcdp_usb; private static Logger logger = LogUtil.ch_ntb_mcdp_usb;
// Main Types // Main Types
@@ -44,7 +44,7 @@ public class Dispatch {
*/ */
public static final byte STYPE_ERROR_PACKET_END = 0x72; 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; private static LinkedList<DataPacket> bdiData, uartData;
@@ -69,7 +69,8 @@ public class Dispatch {
} }
mainType = data[index++]; mainType = data[index++];
subtype = 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) { if (data[index + dataLen] != DataPacket.PACKET_END) {
throw new DispatchException("PACKET_END or packetLen (" throw new DispatchException("PACKET_END or packetLen ("
+ dataLen + " bytes) wrong"); + dataLen + " bytes) wrong");
@@ -118,7 +119,8 @@ public class Dispatch {
if (!bdiData.isEmpty()) { if (!bdiData.isEmpty()) {
return bdiData.poll(); return bdiData.poll();
} }
int dataLength = USBDevice.read_BDI(usbData, USB.MAX_DATA_SIZE); int dataLength = USBDevice.read_BDI(usbData, USBDevice
.getMaxPacketSize());
dispatch(usbData, dataLength); dispatch(usbData, dataLength);
return bdiData.poll(); return bdiData.poll();
} }
@@ -128,7 +130,8 @@ public class Dispatch {
logger.info("buffer not empty"); logger.info("buffer not empty");
return uartData.poll(); return uartData.poll();
} }
int dataLength = USBDevice.read_UART(usbData, USB.MAX_DATA_SIZE); int dataLength = USBDevice.read_UART(usbData, USBDevice
.getMaxPacketSize());
dispatch(usbData, dataLength); dispatch(usbData, dataLength);
return uartData.poll(); return uartData.poll();
} }

View File

@@ -27,11 +27,10 @@ public class USBDevice {
private static final int BDI_Timeout = 1000; private static final int BDI_Timeout = 1000;
private static final int UART_Timeout = 50; private static final int UART_Timeout = 50;
private static Device dev; private static Device dev;
static { static {
// set data for our device
dev = USB.getDevice(IdVendor, IdProduct); dev = USB.getDevice(IdVendor, IdProduct);
} }
@@ -51,8 +50,7 @@ public class USBDevice {
dev.bulkwrite(OUT_Endpoint_BDI, data, length, BDI_Timeout); dev.bulkwrite(OUT_Endpoint_BDI, data, length, BDI_Timeout);
} }
public static synchronized int read_BDI(byte[] data, int size) public static int read_BDI(byte[] data, int size) throws USBException {
throws USBException {
return dev.bulkread(IN_Endpoint_BDI, data, size, BDI_Timeout); 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); dev.bulkwrite(OUT_Endpoint_UART, data, length, UART_Timeout);
} }
public static synchronized int read_UART(byte[] data, int size) public static int read_UART(byte[] data, int size) throws USBException {
throws USBException {
return dev.bulkread(IN_Endpoint_UART, data, size, UART_Timeout); return dev.bulkread(IN_Endpoint_UART, data, size, UART_Timeout);
} }
public static int getMaxPacketSize() {
return dev.getMaxPacketSize();
}
public static Device getDevice() {
return dev;
}
} }