- uart working in both directions
git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@59 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -2,7 +2,6 @@ package ch.ntb.mcdp.uart;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public abstract class Uart {
|
||||
|
||||
@@ -10,10 +9,12 @@ public abstract class Uart {
|
||||
|
||||
UartInputStream in;
|
||||
|
||||
Uart(LinkedList<Uart> list) {
|
||||
list.add(this);
|
||||
Uart() {
|
||||
UartDispatch.getUartList().add(this);
|
||||
out = new UartOutputStream(getSTYPE_IN());
|
||||
in = new UartInputStream();
|
||||
// if the read-Thread is already started, this statement has no effect
|
||||
UartDispatch.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,7 +22,7 @@ public abstract class Uart {
|
||||
*
|
||||
* @return OutputStream to write to target device
|
||||
*/
|
||||
OutputStream getOutputStream() {
|
||||
public OutputStream getOutputStream() {
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -30,13 +31,13 @@ public abstract class Uart {
|
||||
*
|
||||
* @return InputStream to read from target device
|
||||
*/
|
||||
InputStream getInputStream() {
|
||||
public InputStream getInputStream() {
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* The packet subtype specified for this UART packet (from target to PC).
|
||||
* <br>
|
||||
* This constant is defined in <code>Dispatch.h</code>.<br>
|
||||
* Note: This direction is different from the input/output direction of the
|
||||
* streams.
|
||||
*
|
||||
@@ -46,7 +47,7 @@ public abstract class Uart {
|
||||
|
||||
/**
|
||||
* The packet subtype specified for this UART packet (from PC to target).
|
||||
* <br>
|
||||
* This constant is defined in <code>Dispatch.h</code>.<br>
|
||||
* Note: This direction is different from the input/output direction of the
|
||||
* streams.
|
||||
*
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
package ch.ntb.mcdp.uart;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Uart0 extends Uart {
|
||||
|
||||
Uart0(LinkedList<Uart> list) {
|
||||
super(list);
|
||||
}
|
||||
|
||||
// UART 0 Subtypes
|
||||
/**
|
||||
* Data to UART 0
|
||||
@@ -20,12 +15,12 @@ public class Uart0 extends Uart {
|
||||
private static final byte STYPE_UART_0_OUT = 0x22;
|
||||
|
||||
@Override
|
||||
byte getSTYPE_IN() {
|
||||
return STYPE_UART_0_IN;
|
||||
protected byte getSTYPE_OUT() {
|
||||
return STYPE_UART_0_OUT;
|
||||
}
|
||||
|
||||
@Override
|
||||
byte getSTYPE_OUT() {
|
||||
return STYPE_UART_0_OUT;
|
||||
protected byte getSTYPE_IN() {
|
||||
return STYPE_UART_0_IN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
package ch.ntb.mcdp.uart;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import ch.ntb.mcdp.usb.DataPacket;
|
||||
import ch.ntb.mcdp.usb.Dispatch;
|
||||
import ch.ntb.mcdp.usb.DispatchException;
|
||||
import ch.ntb.mcdp.usb.USBDevice;
|
||||
import ch.ntb.usb.USB;
|
||||
import ch.ntb.usb.USBException;
|
||||
import ch.ntb.usb.USBTimeoutException;
|
||||
|
||||
public class UartDispatch {
|
||||
|
||||
public static final int MAX_UART_PAYLOAD = USB.MAX_DATA_SIZE
|
||||
- DataPacket.PACKET_MIN_LENGTH;
|
||||
/**
|
||||
* 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>.
|
||||
*/
|
||||
public static final int MAX_UART_PAYLOAD = 128;
|
||||
|
||||
private static boolean running = false;
|
||||
|
||||
@@ -21,6 +24,10 @@ public class UartDispatch {
|
||||
|
||||
static LinkedList<Uart> uarts = new LinkedList<Uart>();
|
||||
|
||||
/**
|
||||
* Starts the read thread for all Uarts. If the thread is already running,
|
||||
* no action is taken.
|
||||
*/
|
||||
public static void start() {
|
||||
if (dispatchThread == null) {
|
||||
dispatchThread = new Thread() {
|
||||
@@ -31,49 +38,99 @@ public class UartDispatch {
|
||||
try {
|
||||
data = Dispatch.readUART();
|
||||
if (data != null) {
|
||||
while (uarts.iterator().hasNext()) {
|
||||
Uart obj = uarts.iterator().next();
|
||||
if (obj.getSTYPE_OUT() == data.subtype) {
|
||||
obj.in.bufferList.add(data);
|
||||
Iterator iterator = uarts.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Uart uartObj = (Uart) iterator.next();
|
||||
if (uartObj.getSTYPE_OUT() == data.subtype) {
|
||||
uartObj.in.bufferList.add(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: remove
|
||||
sleep(200);
|
||||
} catch (USBTimeoutException e) {
|
||||
// ignore TimeoutExceptions
|
||||
} catch (USBException e) {
|
||||
// TODO: Exceptionhandling
|
||||
e.printStackTrace();
|
||||
try {
|
||||
sleep(2000);
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} catch (DispatchException e) {
|
||||
// TODO: Exceptionhandling
|
||||
e.printStackTrace();
|
||||
try {
|
||||
sleep(2000);
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Exceptionhandling
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
} else {
|
||||
}
|
||||
if (!running) {
|
||||
running = true;
|
||||
dispatchThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the Uart read-Thread.
|
||||
*/
|
||||
public static void stop() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
public LinkedList<Uart> getUartList() {
|
||||
/**
|
||||
* Check whether the read-Thread is still running.
|
||||
*
|
||||
* @return state of the read-Thread
|
||||
*/
|
||||
public static boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the LinkedList which contains all Uart-objects.
|
||||
*
|
||||
* @return The LinkedList containing all Uart-objects.
|
||||
*/
|
||||
public static LinkedList<Uart> getUartList() {
|
||||
return uarts;
|
||||
}
|
||||
|
||||
public static void write(byte packetSubType, byte[] data, int len)
|
||||
/**
|
||||
* Write a Uart data-packet to the target. The maximal number of bytes is
|
||||
* specified by <code>MAX_UART_PAYLOAD</code>. If more data is sent in
|
||||
* one packet, only <code>MAX_UART_PAYLOAD</code> bytes are forwarded to
|
||||
* the uart.
|
||||
*
|
||||
* @param packetSubType
|
||||
* The Subtype specified in <code>Dispatch.h</code>.
|
||||
* @param data
|
||||
* The data to be sent.
|
||||
* @param len
|
||||
* Length of the data to be sent.
|
||||
* @throws USBException
|
||||
*/
|
||||
protected static void write(byte packetSubType, byte[] data, int len)
|
||||
throws USBException {
|
||||
byte[] usbData = new byte[len + DataPacket.PACKET_MIN_LENGTH];
|
||||
usbData[0] = DataPacket.PACKET_HEADER;
|
||||
usbData[1] = Dispatch.MTYPE_UART;
|
||||
usbData[2] = packetSubType;
|
||||
usbData[3] = (byte) (len / 0x100);
|
||||
usbData[4] = (byte) (len & 0xFF);
|
||||
for (int i = 0; i < len; i++) {
|
||||
usbData[DataPacket.PACKET_DATA_OFFSET + i] = data[i];
|
||||
}
|
||||
usbData[DataPacket.PACKET_DATA_OFFSET + len] = DataPacket.PACKET_END;
|
||||
USBDevice.write_UART(data, len);
|
||||
USBDevice.write_BDI(usbData, usbData.length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public class UartOutputStream extends OutputStream {
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
byte[] data = new byte[1];
|
||||
data[0] = (byte) b;
|
||||
try {
|
||||
UartDispatch.write(packetSubType, data, 1);
|
||||
} catch (USBException e) {
|
||||
|
||||
115
mcdp/src/ch/ntb/mcdp/uart/test/UartTest.java
Normal file
115
mcdp/src/ch/ntb/mcdp/uart/test/UartTest.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package ch.ntb.mcdp.uart.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import ch.ntb.mcdp.uart.Uart0;
|
||||
import ch.ntb.mcdp.uart.UartDispatch;
|
||||
|
||||
public class UartTest {
|
||||
|
||||
static Thread reader;
|
||||
|
||||
static OutputStream out;
|
||||
|
||||
static InputStream in;
|
||||
|
||||
static byte[] buffer = new byte[UartDispatch.MAX_UART_PAYLOAD];
|
||||
|
||||
public static void init() {
|
||||
|
||||
// create an uart object
|
||||
Uart0 uart = new Uart0();
|
||||
// get the streams
|
||||
out = uart.getOutputStream();
|
||||
in = uart.getInputStream();
|
||||
|
||||
reader = new Thread() {
|
||||
public void run() {
|
||||
int readLen = 0;
|
||||
while (true) {
|
||||
try {
|
||||
readLen = in.read(buffer);
|
||||
if (readLen > 0) {
|
||||
for (int i = 0; i < readLen; i++) {
|
||||
System.out.print((char) buffer[i]);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
reader.start();
|
||||
}
|
||||
|
||||
static public void button1() {
|
||||
byte[] buffer = new byte[UartDispatch.MAX_UART_PAYLOAD];
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
buffer[i] = (byte) ('0' + (i % ('z' - '0')));
|
||||
}
|
||||
try {
|
||||
out.write(buffer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static public void button2() {
|
||||
|
||||
}
|
||||
|
||||
static public void button3() {
|
||||
|
||||
}
|
||||
|
||||
static public void button4() {
|
||||
|
||||
}
|
||||
|
||||
static public void button5() {
|
||||
|
||||
}
|
||||
|
||||
static public void button6() {
|
||||
|
||||
}
|
||||
|
||||
static public void button7() {
|
||||
|
||||
}
|
||||
|
||||
static public void button8() {
|
||||
|
||||
}
|
||||
|
||||
static public void button9() {
|
||||
|
||||
}
|
||||
|
||||
static public void button10() {
|
||||
|
||||
}
|
||||
|
||||
static public void button11() {
|
||||
|
||||
}
|
||||
|
||||
static public void button12() {
|
||||
|
||||
}
|
||||
|
||||
static public void button13() {
|
||||
|
||||
}
|
||||
|
||||
static public void button14() {
|
||||
|
||||
}
|
||||
|
||||
static public void button15() {
|
||||
|
||||
}
|
||||
}
|
||||
216
mcdp/src/ch/ntb/mcdp/uart/test/UartTestApp.java
Normal file
216
mcdp/src/ch/ntb/mcdp/uart/test/UartTestApp.java
Normal file
@@ -0,0 +1,216 @@
|
||||
package ch.ntb.mcdp.uart.test;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.layout.RowLayout;
|
||||
import org.eclipse.swt.widgets.Button;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
|
||||
import ch.ntb.mcdp.uart.UartDispatch;
|
||||
import ch.ntb.mcdp.usb.USBDevice;
|
||||
import ch.ntb.usb.USBException;
|
||||
|
||||
public class UartTestApp {
|
||||
|
||||
private Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"
|
||||
|
||||
private Button button1 = null;
|
||||
|
||||
private Button button2 = null;
|
||||
|
||||
private Button button3 = null;
|
||||
|
||||
private Button button4 = null;
|
||||
|
||||
private Button button5 = null;
|
||||
|
||||
private Button button6 = null;
|
||||
|
||||
private Button button7 = null;
|
||||
|
||||
private Button button8 = null;
|
||||
|
||||
private Button button9 = null;
|
||||
|
||||
private Button button10 = null;
|
||||
|
||||
private Button button15 = null;
|
||||
|
||||
private Button button11 = null;
|
||||
|
||||
private Button button12 = null;
|
||||
|
||||
private Button button13 = null;
|
||||
|
||||
private Button button14 = null;
|
||||
|
||||
/**
|
||||
* This method initializes sShell
|
||||
*/
|
||||
private void createSShell() {
|
||||
sShell = new Shell();
|
||||
sShell.setText("Shell");
|
||||
sShell.setLayout(new RowLayout());
|
||||
sShell.setSize(new org.eclipse.swt.graphics.Point(312,110));
|
||||
button1 = new Button(sShell, SWT.NONE);
|
||||
button1.setText("writeData");
|
||||
button1
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button1();
|
||||
}
|
||||
});
|
||||
button2 = new Button(sShell, SWT.NONE);
|
||||
button2.setText("not assigned");
|
||||
button2
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button2();
|
||||
}
|
||||
});
|
||||
button3 = new Button(sShell, SWT.NONE);
|
||||
button3.setText("not assigned");
|
||||
button3
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button3();
|
||||
}
|
||||
});
|
||||
button4 = new Button(sShell, SWT.NONE);
|
||||
button4.setText("not assigned");
|
||||
button4
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button4();
|
||||
}
|
||||
});
|
||||
button5 = new Button(sShell, SWT.NONE);
|
||||
button5.setText("not assigned");
|
||||
button6 = new Button(sShell, SWT.NONE);
|
||||
button6.setText("not assigned");
|
||||
button6
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button6();
|
||||
}
|
||||
});
|
||||
button7 = new Button(sShell, SWT.NONE);
|
||||
button7.setText("not assigned");
|
||||
button8 = new Button(sShell, SWT.NONE);
|
||||
button8.setText("not assigned");
|
||||
button8
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button8();
|
||||
}
|
||||
});
|
||||
button9 = new Button(sShell, SWT.NONE);
|
||||
button9.setText("not assigned");
|
||||
button10 = new Button(sShell, SWT.NONE);
|
||||
button10.setText("not assigned");
|
||||
button11 = new Button(sShell, SWT.NONE);
|
||||
button11.setText("not assigned");
|
||||
button11.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button11();
|
||||
}
|
||||
});
|
||||
button12 = new Button(sShell, SWT.NONE);
|
||||
button12.setText("not assigned");
|
||||
button12.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button12();
|
||||
}
|
||||
});
|
||||
button13 = new Button(sShell, SWT.NONE);
|
||||
button13.setText("not assigned");
|
||||
button13.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button13();
|
||||
}
|
||||
});
|
||||
button14 = new Button(sShell, SWT.NONE);
|
||||
button14.setText("not assigned");
|
||||
button14.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button14();
|
||||
}
|
||||
});
|
||||
button15 = new Button(sShell, SWT.NONE);
|
||||
button15.setText("not assigned");
|
||||
button15.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button15();
|
||||
}
|
||||
});
|
||||
button10
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button10();
|
||||
}
|
||||
});
|
||||
button9
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button9();
|
||||
}
|
||||
});
|
||||
button7
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button7();
|
||||
}
|
||||
});
|
||||
button5
|
||||
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
|
||||
public void widgetSelected(
|
||||
org.eclipse.swt.events.SelectionEvent e) {
|
||||
UartTest.button5();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
UartTestApp app = new UartTestApp();
|
||||
app.createSShell();
|
||||
app.sShell.open();
|
||||
|
||||
Display display = app.sShell.getDisplay();
|
||||
|
||||
try {
|
||||
USBDevice.open();
|
||||
System.out.println("open device...");
|
||||
} catch (USBException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
UartDispatch.start();
|
||||
UartTest.init();
|
||||
|
||||
while (!app.sShell.isDisposed()) {
|
||||
if (!display.readAndDispatch()) {
|
||||
display.sleep();
|
||||
}
|
||||
}
|
||||
|
||||
UartDispatch.stop();
|
||||
|
||||
try {
|
||||
USBDevice.close();
|
||||
System.out.println("closing device...");
|
||||
} catch (USBException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user