1 package edu.berkeley.obits.device.atmel;
5 public class FtdiChip {
6 protected SWIGTYPE_p_ftdi_context context;
7 protected int bits = 0;
9 context = example.new_ftdi_context();
11 int result = example.ftdi_init(context);
13 throw new RuntimeException("ftdi_initErr() returned " + result);
15 result = example.ftdi_usb_open(context, 0x6666, 0x3133);
17 throw new RuntimeException("ftdi_usb_open() returned " + result);
19 int result = example.ftdi_set_baudrate(context, 750 * 1000);
21 throw new RuntimeException("ftdi_set_baudrate() returned " + result);
23 result = example.ftdi_set_line_property(context, 8, 0, 0);
25 throw new RuntimeException("ftdi_set_baudrate() returned " + result);
28 public synchronized int readPins() {
29 byte[] b = new byte[1];
30 int result = example.ftdi_read_pins(context, b);
32 throw new RuntimeException("ftdi_read_pins() returned " + result);
35 ByteArrayOutputStream baos = new ByteArrayOutputStream();
37 byte[] bytes = baos.toByteArray();
38 baos = new ByteArrayOutputStream();
39 dbang(bytes, bytes.length);
41 public boolean buffered = false;
43 protected static int mask =
50 protected static int dmask =
62 public synchronized void purge() {
63 example.ftdi_usb_purge_buffers(context);
65 int result = example.ftdi_setflowctrl(context, (1 << 8));
67 throw new RuntimeException("ftdi_setflowcontrol() returned " +
70 public synchronized void uart() {
72 result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
74 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
76 result = example.ftdi_setflowctrl(context, (1 << 8));
78 throw new RuntimeException("ftdi_setflowcontrol() returned " +
82 public synchronized void dbangmode() {
83 int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
85 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
88 protected synchronized void cbangmode() {
89 int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
91 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
94 protected int dbits = 0;
95 protected synchronized void dbang(int bit, boolean val) {
96 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
98 baos.write((byte)dbits);
104 protected synchronized void dbang(byte by) {
105 byte[] b = new byte[1];
107 int result = example.ftdi_write_data(context, b, 1);
109 throw new RuntimeException("ftdi_write_data() returned " + result);
112 protected synchronized void dbang(byte[] b, int len) {
113 example.ftdi_write_data(context, b, len);