1 package com.atmel.fpslic;
6 * Exposes the FpslicRaw interface of an FPSLIC wired to an FTDI USB-UART.
8 public class FpslicRawUsb implements FpslicRaw {
10 private FtdiUart ftdiuart;
22 public FpslicRawUsb() throws IOException {
23 this(new FtdiUart(0x6666, 0x3133, 1500 * 1000));
25 public FpslicRawUsb(FtdiUart ftdiuart) throws IOException {
26 this.ftdiuart = ftdiuart;
30 void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
32 protected int dbits = 0;
33 protected synchronized void dbang(int bit, boolean val) throws IOException {
34 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
35 ftdiuart.getOutputStream().write((byte)dbits);
38 public void reset() throws IOException {
54 ftdiuart.dbus_mode(dmask);
61 //try { Thread.sleep(500); } catch (Exception e) { }
65 try { Thread.sleep(500); } catch (Exception e) { }
66 if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
70 try { Thread.sleep(500); } catch (Exception e) { }
71 if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
76 void config(boolean bit) throws IOException { config(bit?1:0, 1); }
77 void config(int dat) throws IOException { config(dat, 8); }
78 void config(int dat, int numbits) throws IOException {
79 for(int i=(numbits-1); i>=0; i--) {
80 boolean bit = (dat & (1<<i)) != 0;
87 // tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
88 // we can pull it down (assert reset) from uart-mode, or we can
89 // let it float upward from either mode.
90 void reset(boolean on) throws IOException {
91 ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
94 ftdiuart.dbus_mode(dmask);
99 void avrrst(boolean on) throws IOException { dbang(7, on); }
100 void clk(boolean on) throws IOException { dbang(6, on); }
101 void data(boolean on) throws IOException { dbang(5, on); }
103 boolean initErr() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
105 boolean con() throws IOException {
108 ftdiuart.dbus_mode(dmask);
109 return (ftdiuart.readPins() & (1<<0)) != 0;
111 boolean rcon() throws IOException {
114 ftdiuart.dbus_mode(dmask);
115 return (ftdiuart.readPins() & (1<<0)) != 0;
117 void con(boolean on) throws IOException {
121 ftdiuart.dbus_mode(dmask);
124 public OutputStream getConfigStream() throws IOException {
128 return new OutputStream() {
130 public void write(int in) throws IOException {
132 for(int i=7; i>=0; i--) {
133 config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
136 public void write(byte[] b, int off, int len) throws IOException {
137 for(int i=off; i<off+len; i++)
140 public void flush() throws IOException {
141 FpslicRawUsb.this.flush();
144 public void close() throws IOException {
147 throw new RuntimeException("initialization failed at " + bytes);
148 for(int i=0; i<100; i++) {
151 throw new RuntimeException("initialization failed at " + bytes);
152 try { Thread.sleep(20); } catch (Exception e) { }
156 try { Thread.sleep(100); } catch (Exception e) { }
158 ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
163 public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
164 public InputStream getInputStream() { return ftdiuart.getInputStream(); }
167 static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
168 static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
169 public void selfTest() throws Exception {
177 config(Integer.parseInt("10110111", 2), 8);
180 try { Thread.sleep(100); } catch (Exception e) { }
182 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
185 try { Thread.sleep(100); } catch (Exception e) { }
191 config(Integer.parseInt("10110111", 2), 8);
194 try { Thread.sleep(100); } catch (Exception e) { }
196 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
199 try { Thread.sleep(100); } catch (Exception e) { }
205 config(Integer.parseInt("11110111", 2), 8);
208 try { Thread.sleep(100); } catch (Exception e) { }
210 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));