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     protected SWIGTYPE_p_ftdi_context context;
7     protected int bits = 0;
8     public FtdiChip() {
9         context = example.new_ftdi_context();
10
11         int result = example.ftdi_init(context);
12         if (result != 0)
13             throw new RuntimeException("ftdi_initErr() returned " + result);
14
15         result = example.ftdi_usb_open(context, 0x6666, 0x3133);
16         if (result != 0)
17             throw new RuntimeException("ftdi_usb_open() returned " + result);
18
19         int result = example.ftdi_set_baudrate(context, 750 * 1000);
20         if (result != 0)
21             throw new RuntimeException("ftdi_set_baudrate() returned " + result);
22
23         result = example.ftdi_set_line_property(context, 8, 0, 0);
24         if (result != 0)
25             throw new RuntimeException("ftdi_set_baudrate() returned " + result);
26     }
27
28     public synchronized int readPins() {
29         byte[] b = new byte[1];
30         int result = example.ftdi_read_pins(context, b);
31         if (result != 0)
32             throw new RuntimeException("ftdi_read_pins() returned " + result);
33         return b[0];
34     }
35     ByteArrayOutputStream baos = new ByteArrayOutputStream();
36     public void flush() {
37         byte[] bytes = baos.toByteArray();
38         baos = new ByteArrayOutputStream();
39         dbang(bytes, bytes.length);
40     }
41     public boolean buffered = false;
42
43     protected static int mask =
44         (1<<0) |
45         (1<<1)// |
46         //(1<<2) |
47         //(1<<3)
48         ;
49
50     protected static int dmask =
51         //(1<<0) |
52
53         (1<<1) |
54         (1<<2) |
55
56         //(1<<3) |
57         //(1<<4) |
58         (1<<5) |
59         (1<<6) |
60         (1<<7);
61
62     public synchronized void purge() {
63         example.ftdi_usb_purge_buffers(context);
64
65         int result = example.ftdi_setflowctrl(context, (1 << 8));
66         if (result != 0)
67             throw new RuntimeException("ftdi_setflowcontrol() returned " +
68                                        result);
69     }
70     public synchronized void uart() {
71
72         result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
73         if (result != 0)
74             throw new RuntimeException("ftdi_set_bitmode() returned " + result);
75
76         result = example.ftdi_setflowctrl(context, (1 << 8));
77         if (result != 0)
78             throw new RuntimeException("ftdi_setflowcontrol() returned " +
79                                        result);
80
81     }
82     public synchronized void dbangmode() {
83         int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
84         if (result != 0)
85             throw new RuntimeException("ftdi_set_bitmode() returned " + result);
86     }
87
88     protected synchronized void cbangmode() {
89         int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
90         if (result != 0)
91             throw new RuntimeException("ftdi_set_bitmode() returned " + result);
92     }
93
94     protected int dbits = 0;
95     protected synchronized void dbang(int bit, boolean val) {
96         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
97         if (buffered) {
98             baos.write((byte)dbits);
99         } else {
100             dbang((byte)dbits);
101         }
102     }
103     int write = 0;
104     protected synchronized void dbang(byte by) {
105         byte[] b = new byte[1];
106         b[0] = by;
107         int result = example.ftdi_write_data(context, b, 1);
108         if (result != 1)
109             throw new RuntimeException("ftdi_write_data() returned " + result);
110     }
111     int queued = 0;
112     protected synchronized void dbang(byte[] b, int len) {
113         example.ftdi_write_data(context, b, len);
114     }
115 }