X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fcom%2Fftdi%2Fusb%2FFtdiUart.java;h=0990079da37307b3d1487418ced466db06de56fa;hb=9752e92536ce2e72fd2a214690743d21d644df5a;hp=14a2a3a486977e4cac489215f6f91ca4019bc747;hpb=1569edbaee7c8ff330a7f3289856970c46082a12;p=slipway.git diff --git a/src/com/ftdi/usb/FtdiUart.java b/src/com/ftdi/usb/FtdiUart.java index 14a2a3a..0990079 100644 --- a/src/com/ftdi/usb/FtdiUart.java +++ b/src/com/ftdi/usb/FtdiUart.java @@ -1,66 +1,75 @@ package com.ftdi.usb; import java.io.*; +/** + * A Java wrapper around libftdi. + * + * Note: blocking reads are currently implemented by busy-waiting. + * This is really ugly. Check the linux kernel source to see how to + * get libftdi to do it properly. + * + * Flow control is also not properly supported. + */ public class FtdiUart { - protected int bits = 0; - protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context(); + private SWIGTYPE_p_ftdi_context context = FtdiUartNative.new_ftdi_context(); + public FtdiUart(int vendor, int product, int baud) throws IOException { + FtdiUartNative.ftdi_init(context); + FtdiUartNative.ftdi_usb_open(context, vendor, product); + FtdiUartNative.ftdi_usb_reset(context); + FtdiUartNative.ftdi_set_baudrate(context, baud); + FtdiUartNative.ftdi_set_line_property(context, 8, 0, 0); + FtdiUartNative.ftdi_setflowctrl(context, (1<<8)); + purge(); + } + + /** the output stream to the uart or dbus pins (depending on mode) */ public OutputStream getOutputStream() { return out; } + + /** the input stream from the uart or dbus pins (depending on mode) */ public InputStream getInputStream() { return in; } - public FtdiUart() { - example.ftdi_init(context); - example.ftdi_usb_open(context, 0x6666, 0x3133); - example.ftdi_usb_reset(context); - //example.ftdi_set_baudrate(context, 750 * 1000); - example.ftdi_set_baudrate(context, 1500 * 1000); - //example.ftdi_set_baudrate(context, 750 * 1000 * 4); - example.ftdi_set_line_property(context, 8, 0, 0); - purge(); + /** + * Switch to uart mode, with read/write access to four CBUS lines. + * This function is used to write to the CBUS lines (re-invoke it to change their state). + * I think readPins() is used to read from them, but I'm not sure. + * + * @param cbus_mask a four-bit mask; set bit=1 to write to a CBUS line, bit=0 to read from it + * @param cbus_bits a four-bit mask; the bits to assert on the write-enabled CBUS lines + */ + public synchronized void uart_and_cbus_mode(int cbus_mask, int cbus_bits) throws IOException { + FtdiUartNative.ftdi_set_bitmode(context, (short)((cbus_mask << 4) | cbus_bits), (short)0x20); + FtdiUartNative.ftdi_setflowctrl(context, (1<<8)); } - public synchronized int readPins() { - flush(); - byte[] b = new byte[1]; - example.ftdi_read_pins(context, b); - return b[0]; + /** + * Switch to dbus mode; CBUS lines will be released (ie they will float). + * Use getInputStream()/getOutputStream() to read/write the eight DBUS lines. + * + * @param dbus_mask an eight-bit mask; set bit=1 to write to a DBUS line, bit=0 to read from it + */ + public synchronized void dbus_mode(int dbus_mask) throws IOException { + FtdiUartNative.ftdi_set_bitmode(context, (short)dbus_mask, (short)0x01); } - public void flush() { - try { - getOutputStream().flush(); - } catch (Exception e) { throw new RuntimeException(e); } + public synchronized void setBitRate(int bitRate) throws IOException { + FtdiUartNative.ftdi_set_baudrate(context, bitRate); } - protected static int mask = - (1<<0) | - (1<<1)// | - //(1<<2) | - //(1<<3) - ; - - public synchronized void purge() { - example.ftdi_usb_purge_buffers(context); - } - public synchronized void uart() { - example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20); - //example.ftdi_setflowctrl(context, (1 << 8)); - } - public synchronized void dbangmode(int dmask) { - example.ftdi_set_bitmode(context, (short)dmask, (short)0x01); + /** returns the instantaneous value present on the DBUS pins */ + public synchronized int readPins() throws IOException { + getOutputStream().flush(); + byte[] b = new byte[1]; + FtdiUartNative.ftdi_read_pins(context, b); + return b[0]; } - protected int dbits = 0; - - protected synchronized void dbang(int bit, boolean val) { - dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit))); - try { - out.write((byte)dbits); - } catch (IOException e) { throw new RuntimeException(e); } + /** purge the on-chip buffers */ + public synchronized void purge() throws IOException { + FtdiUartNative.ftdi_usb_purge_buffers(context); } - private final InputStream in = new InputStream() { public int available() throws IOException { // FIXME @@ -79,7 +88,7 @@ public class FtdiUart { if (len==0) return 0; byte[] b0 = new byte[len]; synchronized(FtdiUart.this) { - result = example.ftdi_read_data(context, b0, len); + result = FtdiUartNative.ftdi_read_data(context, b0, len); } if (result>0) { System.arraycopy(b0, 0, b, off, result); @@ -102,7 +111,7 @@ public class FtdiUart { System.arraycopy(b, off, b2, 0, Math.min(b2.length, len)); int result; synchronized(FtdiUart.this) { - result = example.ftdi_write_data(context, b2, Math.min(b2.length, len)); + result = FtdiUartNative.ftdi_write_data(context, b2, Math.min(b2.length, len)); } off += result; len -= result;