64618480945fbbbb5ccc9612254362f1ec650f51
[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
50         int result = example.ftdi_setflowctrl(context, (1 << 8));
51         if (result != 0)
52             throw new RuntimeException("ftdi_setflowcontrol() returned " + result);
53     }
54     public synchronized void uart() {
55         int result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
56         if (result != 0)
57             throw new RuntimeException("ftdi_set_bitmode() returned " + result);
58         result = example.ftdi_setflowctrl(context, (1 << 8));
59         if (result != 0)
60             throw new RuntimeException("ftdi_setflowcontrol() returned " + result);
61     }
62     public synchronized void dbangmode() {
63         int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
64         if (result != 0)
65             throw new RuntimeException("ftdi_set_bitmode() returned " + result);
66     }
67
68     protected synchronized void cbangmode() {
69         int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
70         if (result != 0)
71             throw new RuntimeException("ftdi_set_bitmode() returned " + result);
72     }
73
74     protected int dbits = 0;
75     protected synchronized void dbang(int bit, boolean val) {
76         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
77         if (buffered) {
78             baos.write((byte)dbits);
79         } else {
80             dbang((byte)dbits);
81         }
82     }
83
84     protected synchronized void dbang(byte by) {
85         byte[] b = new byte[1];
86         b[0] = by;
87         int result = example.ftdi_write_data(context, b, 1);
88         if (result != 1)
89             throw new RuntimeException("ftdi_write_data() returned " + result);
90     }
91     protected synchronized void dbang(byte[] b, int len) {
92         example.ftdi_write_data(context, b, len);
93     }
94 }