1 package com.atmel.fpslic;
5 public class FpslicRawUsb extends FtdiUart implements FpslicRaw {
17 public FpslicRawUsb() throws IOException {
18 super(0x6666, 0x3133, 1500 * 1000);
22 void flush() throws IOException { getOutputStream().flush(); }
24 protected int dbits = 0;
25 protected synchronized void dbang(int bit, boolean val) throws IOException {
26 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
28 getOutputStream().write((byte)dbits);
29 } catch (IOException e) { throw new RuntimeException(e); }
32 public void reset() throws IOException {
55 //try { Thread.sleep(500); } catch (Exception e) { }
59 try { Thread.sleep(500); } catch (Exception e) { }
60 if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
64 try { Thread.sleep(500); } catch (Exception e) { }
65 if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
70 void config(boolean bit) throws IOException { config(bit?1:0, 1); }
71 void config(int dat) throws IOException { config(dat, 8); }
72 void config(int dat, int numbits) throws IOException {
73 for(int i=(numbits-1); i>=0; i--) {
74 boolean bit = (dat & (1<<i)) != 0;
81 // tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
82 // we can pull it down (assert reset) from uart-mode, or we can
83 // let it float upward from either mode.
84 void reset(boolean on) throws IOException {
85 uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
93 void avrrst(boolean on) throws IOException { dbang(7, on); }
94 void clk(boolean on) throws IOException { dbang(6, on); }
95 void data(boolean on) throws IOException { dbang(5, on); }
97 boolean initErr() throws IOException { flush(); return (readPins() & (1<<4))!=0; }
99 boolean con() throws IOException {
103 return (readPins() & (1<<0)) != 0;
105 boolean rcon() throws IOException {
109 return (readPins() & (1<<0)) != 0;
111 void con(boolean on) throws IOException {
118 public OutputStream getConfigStream() throws IOException {
122 return new OutputStream() {
124 public void write(int in) throws IOException {
126 for(int i=7; i>=0; i--) {
127 config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
130 public void write(byte[] b, int off, int len) throws IOException {
131 for(int i=off; i<off+len; i++)
134 public void flush() throws IOException {
135 FpslicRawUsb.this.flush();
138 public void close() throws IOException {
141 throw new RuntimeException("initialization failed at " + bytes);
142 for(int i=0; i<100; i++) {
145 throw new RuntimeException("initialization failed at " + bytes);
146 try { Thread.sleep(20); } catch (Exception e) { }
150 try { Thread.sleep(100); } catch (Exception e) { }
152 uart_and_cbus_mode(1<<1, 1<<1);
157 static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
158 static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
159 public void selfTest() throws Exception {
160 FpslicRawUsb d = this;
168 d.config(Integer.parseInt("10110111", 2), 8);
171 try { Thread.sleep(100); } catch (Exception e) { }
173 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
176 try { Thread.sleep(100); } catch (Exception e) { }
182 d.config(Integer.parseInt("10110111", 2), 8);
185 try { Thread.sleep(100); } catch (Exception e) { }
187 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
190 try { Thread.sleep(100); } catch (Exception e) { }
196 d.config(Integer.parseInt("11110111", 2), 8);
199 try { Thread.sleep(100); } catch (Exception e) { }
201 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));