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, 1000 * 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 synchronized 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 private OutputStream os = new ChipOutputStream();
64 private InputStream is = new ChipInputStream();
66 public OutputStream getOutputStream() {
69 public InputStream getInputStream() {
70 //example.ftdi_read_data_set_chunksize(context, 32);
74 public class ChipInputStream extends InputStream {
75 public int available() throws IOException {
79 public long skip(long l) throws IOException {
80 throw new RuntimeException("not supported");
82 public int read() throws IOException {
83 System.out.println("read()");
84 byte[] b = new byte[1];
87 result = read(b, 0, 1);
89 throw new IOException("ftdi_read_pins() returned " + result);
92 public int read(byte[] b, int off, int len) throws IOException {
93 // FIXME: blocking reads?
97 byte[] b0 = new byte[len];
98 synchronized(ChipImpl.this) {
99 result = example.ftdi_read_data(context, b0, len);
102 throw new IOException("ftdi_read_pins() returned " + result);
104 System.arraycopy(b0, 0, b, off, result);
107 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); }
112 public class ChipOutputStream extends OutputStream {
113 public void write(int b) throws IOException {
114 byte[] d = new byte[1];
118 public void write(byte[] b, int off, int len) throws IOException {
119 byte[] b2 = new byte[64];
121 System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
122 synchronized(ChipImpl.this) {
123 int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
125 throw new IOException("ftdi_write_data() returned " + result);
133 ByteArrayOutputStream baos = new ByteArrayOutputStream();
134 protected void flush() {
135 byte[] bytes = baos.toByteArray();
136 baos = new ByteArrayOutputStream();
137 dbang(bytes, bytes.length);
139 public boolean buffered = false;
140 public void buffered() { buffered = true; }
141 public void buffered(boolean buf) { buffered = buf; }
142 public void config(int dat) { config(dat, 8); }
143 public void config(int dat, int numbits) {
144 for(int i=(numbits-1); i>=0; i--) {
145 boolean bit = (dat & (1<<i)) != 0;
152 public void reset(boolean on) {
153 bits = on ? (1<<1) : 0;
157 public void avrrst(boolean on) {
160 public boolean initErr() { return (readPins() & (1<<4))!=0; }
161 public void clk(boolean on) { dbang(6, on); }
162 public void data(boolean on) { dbang(5, on); }
164 public boolean con() {
169 boolean ret = (readPins() & (1<<0)) != 0;
178 return (readPins() & (1<<0)) != 0;
181 public void con(boolean on) {
185 bits = on ? (1<<0) : 0;
196 private static int mask =
203 private static int dmask =
215 public synchronized void purge() {
216 example.ftdi_usb_purge_buffers(context);
218 int result = example.ftdi_setflowctrl(context, (1 << 8));
220 throw new RuntimeException("ftdi_setflowcontrol() returned " +
223 public synchronized void uart() {
224 int result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
226 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
228 result = example.ftdi_setflowctrl(context, (1 << 8));
230 throw new RuntimeException("ftdi_setflowcontrol() returned " +
234 public synchronized void dbangmode() {
235 int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
237 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
240 private synchronized void cbangmode() {
241 int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
243 throw new RuntimeException("ftdi_set_bitmode() returned " + result);
246 private int dbits = 0;
247 private synchronized void dbang(int bit, boolean val) {
248 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
250 baos.write((byte)dbits);
256 private synchronized void dbang(byte by) {
257 byte[] b = new byte[1];
259 int result = example.ftdi_write_data(context, b, 1);
261 throw new RuntimeException("ftdi_write_data() returned " + result);
264 private synchronized void dbang(byte[] b, int len) {
265 example.ftdi_write_data(context, b, len);