1 package edu.berkeley.obits.device.atmel;
5 public class ChipImpl implements Chip {
7 private SWIGTYPE_p_ftdi_context context;
10 public void doReset() {
20 try { Thread.sleep(200); } catch (Exception e) { }
23 try { Thread.sleep(200); } catch (Exception e) { }
30 public void porte(int pin, boolean b) {
31 porte = (~(1<<pin)) | (b ? (1<<pin) : 0);
39 context = example.new_ftdi_context();
41 int result = example.ftdi_init(context);
43 throw new RuntimeException("ftdi_initErr() returned " + result);
45 result = example.ftdi_usb_open(context, 0x6666, 0x3133);
47 throw new RuntimeException("ftdi_usb_open() returned " + result);
49 result = example.ftdi_set_baudrate(context, 750 * 1000);
51 throw new RuntimeException("ftdi_set_baudrate() returned " + result);
53 result = example.ftdi_set_line_property(context, 8, 0, 0);
55 throw new RuntimeException("ftdi_set_baudrate() returned " + result);
60 public synchronized int readPins() {
61 byte[] b = new byte[1];
62 int result = example.ftdi_read_pins(context, b);
64 throw new RuntimeException("ftdi_read_pins() returned " + result);
67 ByteArrayOutputStream baos = new ByteArrayOutputStream();
69 byte[] bytes = baos.toByteArray();
70 baos = new ByteArrayOutputStream();
71 dbang(bytes, bytes.length);
73 public boolean buffered = false;
75 private static int mask =
82 private static int dmask =
94 public synchronized void purge() {
95 example.ftdi_usb_purge_buffers(context);
97 int result = example.ftdi_setflowctrl(context, (1 << 8));
99 throw new RuntimeException("ftdi_setflowcontrol() returned " +
102 public synchronized void uart() {
103 int result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
105 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
107 result = example.ftdi_setflowctrl(context, (1 << 8));
109 throw new RuntimeException("ftdi_setflowcontrol() returned " +
113 public synchronized void dbangmode() {
114 int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
116 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
119 private synchronized void cbangmode() {
120 int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
122 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
125 private int dbits = 0;
126 private synchronized void dbang(int bit, boolean val) {
127 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
129 baos.write((byte)dbits);
135 private synchronized void dbang(byte by) {
136 byte[] b = new byte[1];
138 int result = example.ftdi_write_data(context, b, 1);
140 throw new RuntimeException("ftdi_write_data() returned " + result);
143 private synchronized void dbang(byte[] b, int len) {
144 example.ftdi_write_data(context, b, len);
149 public void buffered() { buffered = true; }
150 public void buffered(boolean buf) { buffered = buf; }
151 public void config(boolean bit) { config(bit?1:0, 1); }
152 public void config(int dat) { config(dat, 8); }
153 public void config(int dat, int numbits) {
154 for(int i=(numbits-1); i>=0; i--) {
155 boolean bit = (dat & (1<<i)) != 0;
162 public void reset(boolean on) {
163 bits = on ? (1<<1) : 0;
167 public void avrrst(boolean on) { dbang(7, on); }
168 public boolean initErr() { return (readPins() & (1<<4))!=0; }
169 public void clk(boolean on) { dbang(6, on); }
170 public void data(boolean on) { dbang(5, on); }
172 public boolean con() {
177 boolean ret = (readPins() & (1<<0)) != 0;
186 return (readPins() & (1<<0)) != 0;
189 public void con(boolean on) {
193 bits = on ? (1<<0) : 0;
205 // UART comm pair //////////////////////////////////////////////////////////////////////////////
208 private OutputStream os = new ChipOutputStream();
209 private InputStream is = new ChipInputStream();
210 public OutputStream getOutputStream() { return os; }
211 public InputStream getInputStream() { return is; }
213 public class ChipInputStream extends InputStream {
214 public int available() throws IOException {
218 public long skip(long l) throws IOException {
219 throw new RuntimeException("not supported");
221 public int read() throws IOException {
222 System.out.println("read()");
223 byte[] b = new byte[1];
226 result = read(b, 0, 1);
228 throw new IOException("ftdi_read_pins() returned " + result);
231 public int read(byte[] b, int off, int len) throws IOException {
232 // FIXME: blocking reads?
235 if (len==0) return 0;
236 byte[] b0 = new byte[len];
237 synchronized(ChipImpl.this) {
238 result = example.ftdi_read_data(context, b0, len);
241 throw new IOException("ftdi_read_pins() returned " + result);
243 System.arraycopy(b0, 0, b, off, result);
246 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
251 public class ChipOutputStream extends OutputStream {
252 public void write(int b) throws IOException {
253 byte[] d = new byte[1];
257 public void write(byte[] b, int off, int len) throws IOException {
258 byte[] b2 = new byte[64];
260 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
261 synchronized(ChipImpl.this) {
262 int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
264 throw new IOException("ftdi_write_data() returned " + result);