1 package edu.berkeley.obits.device.atmel;
5 public class ChipImpl extends Chip {
7 public void doReset() {
16 public void int3(boolean on) { throw new RuntimeException("not connected"); }
17 public boolean porte(int pin) { throw new RuntimeException("not connected"); }
20 public void porte(int pin, boolean b) {
21 porte = (~(1<<pin)) | (b ? (1<<pin) : 0);
28 public void config(boolean bit) { config(bit?1:0, 1); }
30 private SWIGTYPE_p_ftdi_context context;
34 context = example.new_ftdi_context();
36 int result = example.ftdi_init(context);
38 throw new RuntimeException("ftdi_initErr() returned " + result);
40 result = example.ftdi_usb_open(context, 0x6666, 0x3133);
42 throw new RuntimeException("ftdi_usb_open() returned " + result);
44 //result = example.ftdi_set_baudrate(context, 750 * 1000);
45 result = example.ftdi_set_baudrate(context, 750 * 1000);
47 throw new RuntimeException("ftdi_set_baudrate() returned " + result);
48 result = example.ftdi_set_line_property(context, 8, 0, 0);
50 throw new RuntimeException("ftdi_set_baudrate() returned " + result);
55 public int readPins() {
56 byte[] b = new byte[1];
57 int result = example.ftdi_read_pins(context, b);
59 throw new RuntimeException("ftdi_read_pins() returned " + result);
63 public void write(int out) {
64 byte[] b = new byte[1];
68 result = example.ftdi_write_data(context, b, 1);
71 private OutputStream os = new ChipOutputStream();
72 private InputStream is = new ChipInputStream();
74 public OutputStream getOutputStream() {
75 //example.ftdi_write_data_set_chunksize(context, 32);
78 public InputStream getInputStream() { return is; }
80 public class ChipInputStream extends InputStream {
81 public int available() throws IOException {
85 public long skip(long l) throws IOException {
86 throw new RuntimeException("not supported");
88 public int read() throws IOException {
89 System.out.println("read()");
90 byte[] b = new byte[1];
93 result = read(b, 0, 1);
95 throw new IOException("ftdi_read_pins() returned " + result);
98 public int read(byte[] b, int off, int len) throws IOException {
99 System.out.println("read("+off+","+len+")");
100 // FIXME: blocking reads?
103 if (len==0) return 0;
104 synchronized(ChipImpl.this) {
105 byte[] b0 = new byte[len];
106 result = example.ftdi_read_data(context, b0, len);
108 throw new IOException("ftdi_read_pins() returned " + result);
110 System.arraycopy(b0, 0, b, off, result);
111 System.out.println(" return " + result);
120 public class ChipOutputStream extends OutputStream {
121 public void write(int b) throws IOException {
122 byte[] d = new byte[1];
126 public void write(byte[] b, int off, int len) throws IOException {
127 byte[] b2 = new byte[64];
129 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
130 synchronized(ChipImpl.this) {
131 int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
133 throw new IOException("ftdi_write_data() returned " + result);
142 byte[] b = new byte[1];
145 result = example.ftdi_read_data(context, b, 1);
147 throw new RuntimeException("ftdi_read_pins() returned " + result);
148 return (b[0] & 0xff);
151 ByteArrayOutputStream baos = new ByteArrayOutputStream();
152 public void flush() {
153 byte[] bytes = baos.toByteArray();
154 baos = new ByteArrayOutputStream();
155 dbang(bytes, bytes.length);
157 public boolean buffered = false;
158 public void buffered() { buffered = true; }
159 public void buffered(boolean buf) { buffered = buf; }
160 public void config(int dat) { config(dat, 8); }
161 public void config(int dat, int numbits) {
162 for(int i=(numbits-1); i>=0; i--) {
163 boolean bit = (dat & (1<<i)) != 0;
170 public void reset(boolean on) {
171 bits = on ? (1<<1) : 0;
175 public void avrrst(boolean on) {
178 public boolean initErr() { return (readPins() & (1<<4))!=0; }
179 public void clk(boolean on) { dbang(6, on); }
180 public void data(boolean on) { dbang(5, on); }
182 public boolean con() {
187 boolean ret = (readPins() & (1<<0)) != 0;
196 return (readPins() & (1<<0)) != 0;
199 public void con(boolean on) {
203 bits = on ? (1<<0) : 0;
214 private static int mask =
221 private static int dmask =
233 public void purge() {
234 example.ftdi_usb_purge_buffers(context);
237 int result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
239 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
241 result = example.ftdi_setflowctrl(context, 1 << 8);
243 throw new RuntimeException("ftdi_setflowcontrol() returned " +
247 public void dbangmode() {
248 int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
250 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
253 private void cbangmode() {
254 int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
256 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
259 private int dbits = 0;
260 private void dbang(int bit, boolean val) {
261 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
263 baos.write((byte)dbits);
269 private void dbang(byte by) {
270 byte[] b = new byte[1];
272 int result = example.ftdi_write_data(context, b, 1);
274 throw new RuntimeException("ftdi_write_data() returned " + result);
277 private void dbang(byte[] b, int len) {
278 example.ftdi_write_data(context, b, len);