checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / ChipImpl.java
1 package edu.berkeley.obits.device.atmel;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 public class ChipImpl extends FtdiChip implements Chip {
6
7     public ChipImpl() {
8         super();
9         doReset();
10     }
11
12     public void doReset() {
13         dbangmode();
14         clk(false);
15         data(false);
16
17         con(false);
18         flush();
19         buffered(false);
20         reset(false);
21         //avrrst(false);
22         try { Thread.sleep(200); } catch (Exception e) { }
23         reset(true);
24         //avrrst(true);
25         try { Thread.sleep(200); } catch (Exception e) { }
26
27         dmask &= ~(1<<7);
28         dbangmode();
29     }
30
31     int porte = 0;
32     public void porte(int pin, boolean b) {
33         porte = (~(1<<pin)) | (b ? (1<<pin) : 0);
34         if (pin==4) {
35             dbang(2, b);
36             flush();
37         }
38     }
39
40
41     //
42
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;
50             data(bit);
51             clk(true);
52             clk(false);
53         }
54     }
55
56     public void reset(boolean on) {
57         bits = on ? (1<<1) : 0;
58         cbangmode();
59         //dbang(0, on);
60     }
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); }
65
66     public boolean con() {
67
68         /*
69         mask &= ~(1<<0);
70         cbangmode();
71         boolean ret = (readPins() & (1<<0)) != 0;
72         dbangmode();
73         return ret;
74         */
75
76
77
78         dmask &= ~(1<<0);
79         dbangmode();
80         return (readPins() & (1<<0)) != 0;
81
82     }
83     public void con(boolean on) {
84
85         /*
86         mask |= (1<<0);
87         bits = on ? (1<<0) : 0;
88         cbangmode();
89         */
90
91
92         dmask |= (1<<0);
93         dbangmode();
94         dbang(0, on);
95
96     }
97
98
99     // UART comm pair //////////////////////////////////////////////////////////////////////////////
100
101
102     private OutputStream os = new ChipOutputStream();
103     private InputStream  is = new ChipInputStream();
104     public OutputStream getOutputStream() { return os; }
105     public InputStream  getInputStream() { return is; }
106     
107     public class ChipInputStream extends InputStream {
108         public int available() throws IOException {
109             // FIXME
110             return 0;
111         }
112         public long skip(long l) throws IOException {
113             throw new RuntimeException("not supported");
114         }
115         public int read() throws IOException {
116             System.out.println("read()");
117             byte[] b = new byte[1];
118             int result = 0;
119             while(result==0)
120                 result = read(b, 0, 1);
121             if (result==-1)
122                 throw new IOException("ftdi_read_pins() returned " + result);
123             return b[0] & 0xff;
124         }
125         public int read(byte[] b, int off, int len) throws IOException {
126             // FIXME: blocking reads?
127             int result = 0;
128             while(true) {
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);
133                     }
134                     if (result == -1)
135                         throw new IOException("ftdi_read_pins() returned " + result);
136                     if (result>0) {
137                         System.arraycopy(b0, 0, b, off, result);
138                         return result;
139                     }
140                 try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
141             }
142         }
143     }
144
145     public class ChipOutputStream extends OutputStream {
146         public void write(int b) throws IOException {
147             byte[] d = new byte[1];
148             d[0] = (byte)b;
149             write(d, 0, 1);
150         }
151         public void write(byte[] b, int off, int len) throws IOException {
152             byte[] b2 = new byte[64];
153             while(len > 0) {
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));
157                     if (result < 0)
158                         throw new IOException("ftdi_write_data() returned " + result);
159                     off += result;
160                     len -= result;
161                 }
162             }
163         }
164     }
165
166 }