6b398e76db87ba449ad6368b622f3e26eac7aa5c
[slipway.git] / src / com / ftdi / usb / FtdiUart.java
1 package com.ftdi.usb;
2 import java.io.*;
3
4 /**
5  *  A Java wrapper around libftdi.
6  *
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.
10  *
11  *  Flow control is also not properly supported.
12  */
13 public class FtdiUart {
14
15     private SWIGTYPE_p_ftdi_context context = FtdiUartNative.new_ftdi_context();
16
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);
23         FtdiUartNative.ftdi_setflowctrl(context, (1<<8));
24         purge();
25     }
26
27     /** the output stream to the uart or dbus pins (depending on mode) */
28     public OutputStream getOutputStream() { return out; }
29
30     /** the input stream from the uart or dbus pins (depending on mode) */
31     public InputStream  getInputStream() { return in; }
32
33     /**
34      *  Switch to uart mode, with read/write access to four CBUS lines.
35      *  This function is used to write to the CBUS lines (re-invoke it to change their state).
36      *  I think readPins() is used to read from them, but I'm not sure.
37      *
38      *  @param cbus_mask a four-bit mask; set bit=1 to write to a CBUS line, bit=0 to read from it
39      *  @param cbus_bits a four-bit mask; the bits to assert on the write-enabled CBUS lines
40      */
41     public synchronized void uart_and_cbus_mode(int cbus_mask, int cbus_bits) throws IOException {
42         FtdiUartNative.ftdi_set_bitmode(context, (short)((cbus_mask << 4) | cbus_bits), (short)0x20);
43         FtdiUartNative.ftdi_setflowctrl(context, (1<<8));
44     }
45
46     /**
47      *  Switch to dbus mode; CBUS lines will be released (ie they will float).
48      *  Use getInputStream()/getOutputStream() to read/write the eight DBUS lines.
49      * 
50      *  @param dbus_mask an eight-bit mask; set bit=1 to write to a DBUS line, bit=0 to read from it
51      */
52     public synchronized void dbus_mode(int dbus_mask) throws IOException {
53         FtdiUartNative.ftdi_set_bitmode(context, (short)dbus_mask, (short)0x01);
54     }
55
56     /** returns the instantaneous value present on the DBUS pins */
57     public synchronized int readPins() throws IOException {
58         getOutputStream().flush();
59         byte[] b = new byte[1];
60         FtdiUartNative.ftdi_read_pins(context, b);
61         return b[0];
62     }
63
64     /** purge the on-chip buffers */
65     public synchronized void purge() throws IOException {
66         FtdiUartNative.ftdi_usb_purge_buffers(context);
67     }
68
69     private final InputStream in = new InputStream() {
70             public int available() throws IOException {
71                 // FIXME
72                 return 0;
73             }
74             public int read() throws IOException {
75                 byte[] b = new byte[1];
76                 int result = 0;
77                 while(result==0) result = read(b, 0, 1);
78                 return b[0] & 0xff;
79             }
80             public int read(byte[] b, int off, int len) throws IOException {
81                 // FIXME: blocking reads?
82                 int result = 0;
83                 while(true) {
84                     if (len==0) return 0;
85                     byte[] b0 = new byte[len];
86                     synchronized(FtdiUart.this) {
87                         result = FtdiUartNative.ftdi_read_data(context, b0, len);
88                     }
89                     if (result>0) {
90                         System.arraycopy(b0, 0, b, off, result);
91                         return result;
92                     }
93                     try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
94                 }
95             }
96         };
97
98     private final OutputStream out = new BufferedOutputStream(new OutputStream() {
99             public void write(int b) throws IOException {
100                 byte[] d = new byte[1];
101                 d[0] = (byte)b;
102                 write(d, 0, 1);
103             }
104             public void write(byte[] b, int off, int len) throws IOException {
105                 byte[] b2 = new byte[64];
106                 while(len > 0) {
107                     System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
108                     int result;
109                     synchronized(FtdiUart.this) {
110                         result = FtdiUartNative.ftdi_write_data(context, b2, Math.min(b2.length, len));
111                     }
112                     off += result;
113                     len -= result;
114                 }
115             }
116         });
117 }