1 package edu.berkeley.obits.device.atmel;
5 public class FtdiChip {
7 protected int bits = 0;
8 protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
10 public OutputStream getOutputStream() { return out; }
11 public InputStream getInputStream() { return in; }
14 example.ftdi_init(context);
15 example.ftdi_usb_open(context, 0x6666, 0x3133);
16 example.ftdi_usb_reset(context);
17 example.ftdi_set_baudrate(context, 750 * 1000);
18 example.ftdi_set_line_property(context, 8, 0, 0);
21 public synchronized int readPins() {
22 byte[] b = new byte[1];
23 example.ftdi_read_pins(context, b);
27 ByteArrayOutputStream baos = new ByteArrayOutputStream();
30 byte[] bytes = baos.toByteArray();
31 baos = new ByteArrayOutputStream();
32 out.write(bytes, 0, bytes.length);
34 } catch (IOException e) { throw new RuntimeException(e); }
37 protected static int mask =
44 public synchronized void purge() {
45 example.ftdi_usb_purge_buffers(context);
47 public synchronized void uart() {
48 example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
49 example.ftdi_setflowctrl(context, (1 << 8));
51 public synchronized void dbangmode(int dmask) {
52 example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
55 protected int dbits = 0;
57 public boolean buffered = false;
58 public void buffered() { buffered = true; }
59 public void buffered(boolean buf) { buffered = buf; }
61 protected synchronized void dbang(int bit, boolean val) {
62 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
64 out.write((byte)dbits);
65 if (!buffered) out.flush();
66 } catch (IOException e) { throw new RuntimeException(e); }
70 private final InputStream in = new InputStream() {
71 public int available() throws IOException {
75 public int read() throws IOException {
76 byte[] b = new byte[1];
78 while(result==0) result = read(b, 0, 1);
81 public int read(byte[] b, int off, int len) throws IOException {
82 // FIXME: blocking reads?
86 byte[] b0 = new byte[len];
87 synchronized(FtdiChip.this) {
88 result = example.ftdi_read_data(context, b0, len);
91 System.arraycopy(b0, 0, b, off, result);
94 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
99 private final OutputStream out = new BufferedOutputStream(new OutputStream() {
100 public void write(int b) throws IOException {
101 byte[] d = new byte[1];
105 public void write(byte[] b, int off, int len) throws IOException {
106 byte[] b2 = new byte[64];
108 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
110 synchronized(FtdiChip.this) {
111 result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));