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();
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);
17 public synchronized int readPins() {
18 byte[] b = new byte[1];
19 example.ftdi_read_pins(context, b);
22 ByteArrayOutputStream baos = new ByteArrayOutputStream();
24 byte[] bytes = baos.toByteArray();
25 baos = new ByteArrayOutputStream();
26 dbang(bytes, bytes.length);
29 public boolean buffered = false;
30 protected static int mask =
37 protected static int dmask =
47 public synchronized void purge() {
48 example.ftdi_usb_purge_buffers(context);
49 example.ftdi_setflowctrl(context, (1 << 8));
51 public synchronized void uart() {
52 example.ftdi_set_bitmode(context, (short)0, (short)0x00);
53 example.ftdi_setflowctrl(context, (1 << 8));
55 public synchronized void dbangmode() {
56 example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
59 protected synchronized void cbangmode() {
60 example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
61 example.ftdi_setflowctrl(context, (1 << 8));
64 protected int dbits = 0;
65 protected synchronized void dbang(int bit, boolean val) {
66 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
68 baos.write((byte)dbits);
74 protected synchronized void dbang(byte by) {
75 byte[] b = new byte[1];
77 example.ftdi_write_data(context, b, 1);
79 protected synchronized void dbang(byte[] b, int len) {
80 example.ftdi_write_data(context, b, len);
83 private OutputStream os = new ChipOutputStream();
84 private InputStream is = new ChipInputStream();
85 public OutputStream getOutputStream() { return os; }
86 public InputStream getInputStream() { return is; }
88 public class ChipInputStream extends InputStream {
89 public int available() throws IOException {
93 public long skip(long l) throws IOException {
94 throw new RuntimeException("not supported");
96 public int read() throws IOException {
97 System.out.println("read()");
98 byte[] b = new byte[1];
101 result = read(b, 0, 1);
104 public int read(byte[] b, int off, int len) throws IOException {
105 // FIXME: blocking reads?
108 if (len==0) return 0;
109 byte[] b0 = new byte[len];
110 synchronized(FtdiChip.this) {
111 result = example.ftdi_read_data(context, b0, len);
114 System.arraycopy(b0, 0, b, off, result);
117 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
122 public class ChipOutputStream extends OutputStream {
123 public void write(int b) throws IOException {
124 byte[] d = new byte[1];
128 public void write(byte[] b, int off, int len) throws IOException {
129 byte[] b2 = new byte[64];
131 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
132 synchronized(FtdiChip.this) {
133 int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));