1 package edu.berkeley.obits.device.atmel;
5 public class ChipImpl extends FtdiChip implements Chip {
12 public void doReset() {
22 try { Thread.sleep(200); } catch (Exception e) { }
25 try { Thread.sleep(200); } catch (Exception e) { }
32 public void porte(int pin, boolean b) {
33 porte = (~(1<<pin)) | (b ? (1<<pin) : 0);
43 public void buffered() { buffered = true; }
44 public void buffered(boolean buf) { buffered = buf; }
45 public void config(boolean bit) { config(bit?1:0, 1); }
46 public void config(int dat) { config(dat, 8); }
47 public void config(int dat, int numbits) {
48 for(int i=(numbits-1); i>=0; i--) {
49 boolean bit = (dat & (1<<i)) != 0;
56 public void reset(boolean on) {
57 bits = on ? (1<<1) : 0;
61 public void avrrst(boolean on) { dbang(7, on); }
62 public boolean initErr() { return (readPins() & (1<<4))!=0; }
63 public void clk(boolean on) { dbang(6, on); }
64 public void data(boolean on) { dbang(5, on); }
66 public boolean con() {
71 boolean ret = (readPins() & (1<<0)) != 0;
80 return (readPins() & (1<<0)) != 0;
83 public void con(boolean on) {
87 bits = on ? (1<<0) : 0;
99 // UART comm pair //////////////////////////////////////////////////////////////////////////////
102 private OutputStream os = new ChipOutputStream();
103 private InputStream is = new ChipInputStream();
104 public OutputStream getOutputStream() { return os; }
105 public InputStream getInputStream() { return is; }
107 public class ChipInputStream extends InputStream {
108 public int available() throws IOException {
112 public long skip(long l) throws IOException {
113 throw new RuntimeException("not supported");
115 public int read() throws IOException {
116 System.out.println("read()");
117 byte[] b = new byte[1];
120 result = read(b, 0, 1);
122 throw new IOException("ftdi_read_pins() returned " + result);
125 public int read(byte[] b, int off, int len) throws IOException {
126 // FIXME: blocking reads?
129 if (len==0) return 0;
130 byte[] b0 = new byte[len];
131 synchronized(ChipImpl.this) {
132 result = example.ftdi_read_data(context, b0, len);
135 throw new IOException("ftdi_read_pins() returned " + result);
137 System.arraycopy(b0, 0, b, off, result);
140 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
145 public class ChipOutputStream extends OutputStream {
146 public void write(int b) throws IOException {
147 byte[] d = new byte[1];
151 public void write(byte[] b, int off, int len) throws IOException {
152 byte[] b2 = new byte[64];
154 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
155 synchronized(ChipImpl.this) {
156 int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
158 throw new IOException("ftdi_write_data() returned " + result);