checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / FtdiChip.java
1 package edu.berkeley.obits.device.atmel;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 public class FtdiChip {
6
7     protected int bits = 0;
8     protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
9
10     public FtdiChip() {
11         example.ftdi_init(context);
12         example.ftdi_usb_open(context, 0x6666, 0x3133);
13         example.ftdi_set_baudrate(context, 750 * 1000);
14         example.ftdi_set_line_property(context, 8, 0, 0);
15     }
16
17     public synchronized int readPins() {
18         byte[] b = new byte[1];
19         example.ftdi_read_pins(context, b);
20         return b[0];
21     }
22     ByteArrayOutputStream baos = new ByteArrayOutputStream();
23     public void flush() {
24         byte[] bytes = baos.toByteArray();
25         baos = new ByteArrayOutputStream();
26         dbang(bytes, bytes.length);
27     }
28
29     public boolean buffered = false;
30     protected static int mask =
31         (1<<0) |
32         (1<<1)// |
33         //(1<<2) |
34         //(1<<3)
35         ;
36
37     protected static int dmask =
38         //(1<<0) |
39         (1<<1) |
40         (1<<2) |
41         //(1<<3) |
42         //(1<<4) |
43         (1<<5) |
44         (1<<6) |
45         (1<<7);
46
47     public synchronized void purge() {
48         example.ftdi_usb_purge_buffers(context);
49         example.ftdi_setflowctrl(context, (1 << 8));
50     }
51     public synchronized void uart() {
52         cbangmode();
53     }
54     public synchronized void dbangmode() {
55         example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
56     }
57
58     protected synchronized void cbangmode() {
59         example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
60         example.ftdi_setflowctrl(context, (1 << 8));
61     }
62
63     protected int dbits = 0;
64     protected synchronized void dbang(int bit, boolean val) {
65         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
66         if (buffered) {
67             baos.write((byte)dbits);
68         } else {
69             dbang((byte)dbits);
70         }
71     }
72
73     protected synchronized void dbang(byte by) {
74         byte[] b = new byte[1];
75         b[0] = by;
76         example.ftdi_write_data(context, b, 1);
77     }
78     protected synchronized void dbang(byte[] b, int len) {
79         example.ftdi_write_data(context, b, len);
80     }
81
82     private OutputStream os = new ChipOutputStream();
83     private InputStream  is = new ChipInputStream();
84     public OutputStream getOutputStream() { return os; }
85     public InputStream  getInputStream() { return is; }
86     
87     public class ChipInputStream extends InputStream {
88         public int available() throws IOException {
89             // FIXME
90             return 0;
91         }
92         public long skip(long l) throws IOException {
93             throw new RuntimeException("not supported");
94         }
95         public int read() throws IOException {
96             System.out.println("read()");
97             byte[] b = new byte[1];
98             int result = 0;
99             while(result==0)
100                 result = read(b, 0, 1);
101             return b[0] & 0xff;
102         }
103         public int read(byte[] b, int off, int len) throws IOException {
104             // FIXME: blocking reads?
105             int result = 0;
106             while(true) {
107                 if (len==0) return 0;
108                     byte[] b0 = new byte[len];
109                     synchronized(FtdiChip.this) {
110                         result = example.ftdi_read_data(context, b0, len);
111                     }
112                     if (result>0) {
113                         System.arraycopy(b0, 0, b, off, result);
114                         return result;
115                     }
116                 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
117             }
118         }
119     }
120
121     public class ChipOutputStream extends OutputStream {
122         public void write(int b) throws IOException {
123             byte[] d = new byte[1];
124             d[0] = (byte)b;
125             write(d, 0, 1);
126         }
127         public void write(byte[] b, int off, int len) throws IOException {
128             byte[] b2 = new byte[64];
129             while(len > 0) {
130                 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
131                 synchronized(FtdiChip.this) {
132                     int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
133                     off += result;
134                     len -= result;
135                 }
136             }
137         }
138     }
139 }