5 * A Java wrapper around libftdi.
7 * Note: blocking reads are currently implemented by busy-waiting.
8 * This is really ugly. Check the linux kernel source to see how to
9 * get libftdi to do it properly.
11 * Flow control is also not properly supported.
13 public class FtdiUart {
15 private SWIGTYPE_p_ftdi_context context = FtdiUartNative.new_ftdi_context();
17 public FtdiUart(int vendor, int product, int baud) throws IOException {
18 FtdiUartNative.ftdi_init(context);
19 FtdiUartNative.ftdi_usb_open(context, vendor, product);
20 FtdiUartNative.ftdi_usb_reset(context);
21 FtdiUartNative.ftdi_set_baudrate(context, baud);
22 FtdiUartNative.ftdi_set_line_property(context, 8, 0, 0);
26 /** the output stream to the uart or dbus pins (depending on mode) */
27 public OutputStream getOutputStream() { return out; }
29 /** the input stream from the uart or dbus pins (depending on mode) */
30 public InputStream getInputStream() { return in; }
33 * Switch to uart mode, with read/write access to four CBUS lines.
34 * This function is used to write to the CBUS lines (re-invoke it to change their state).
35 * I think readPins() is used to read from them, but I'm not sure.
37 * @param cbus_mask a four-bit mask; set bit=1 to write to a CBUS line, bit=0 to read from it
38 * @param cbus_bits a four-bit mask; the bits to assert on the write-enabled CBUS lines
40 public synchronized void uart_and_cbus_mode(int cbus_mask, int cbus_bits) throws IOException {
41 FtdiUartNative.ftdi_set_bitmode(context, (short)((cbus_mask << 4) | cbus_bits), (short)0x20);
45 * Switch to dbus mode; CBUS lines will be released (ie they will float).
46 * Use getInputStream()/getOutputStream() to read/write the eight DBUS lines.
48 * @param dbus_mask an eight-bit mask; set bit=1 to write to a DBUS line, bit=0 to read from it
50 public synchronized void dbus_mode(int dbus_mask) throws IOException {
51 FtdiUartNative.ftdi_set_bitmode(context, (short)dbus_mask, (short)0x01);
54 /** returns the instantaneous value present on the DBUS pins */
55 public synchronized int readPins() throws IOException {
56 getOutputStream().flush();
57 byte[] b = new byte[1];
58 FtdiUartNative.ftdi_read_pins(context, b);
62 /** purge the on-chip buffers */
63 public synchronized void purge() throws IOException {
64 FtdiUartNative.ftdi_usb_purge_buffers(context);
67 private final InputStream in = new InputStream() {
68 public int available() throws IOException {
72 public int read() throws IOException {
73 byte[] b = new byte[1];
75 while(result==0) result = read(b, 0, 1);
78 public int read(byte[] b, int off, int len) throws IOException {
79 // FIXME: blocking reads?
83 byte[] b0 = new byte[len];
84 synchronized(FtdiUart.this) {
85 result = FtdiUartNative.ftdi_read_data(context, b0, len);
88 System.arraycopy(b0, 0, b, off, result);
91 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
96 private final OutputStream out = new BufferedOutputStream(new OutputStream() {
97 public void write(int b) throws IOException {
98 byte[] d = new byte[1];
102 public void write(byte[] b, int off, int len) throws IOException {
103 byte[] b2 = new byte[64];
105 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
107 synchronized(FtdiUart.this) {
108 result = FtdiUartNative.ftdi_write_data(context, b2, Math.min(b2.length, len));