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(FtdiUart ftdiuart) throws IOException {
23 this.ftdiuart = ftdiuart;
27 public void reset() throws IOException {
43 ftdiuart.dbus_mode(dmask);
50 //try { Thread.sleep(500); } catch (Exception e) { }
54 try { Thread.sleep(500); } catch (Exception e) { }
55 if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
59 try { Thread.sleep(500); } catch (Exception e) { }
60 if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
65 public OutputStream getConfigStream() throws IOException {
71 return new OutputStream() {
74 public void write(int in) throws IOException {
75 for(int i=7; i>=0; i--) {
76 config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
79 public void write(byte[] b, int off, int len) throws IOException {
80 for(int i=off; i<off+len; i++)
83 public void flush() throws IOException {
84 FpslicRawUsb.this.flush();
87 public void close() throws IOException {
90 throw new RuntimeException("initialization failed at " + bytes);
91 for(int i=0; i<100; i++) {
94 throw new RuntimeException("initialization failed at " + bytes);
95 try { Thread.sleep(20); } catch (Exception e) { }
99 try { Thread.sleep(100); } catch (Exception e) { }
101 ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
106 public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
107 public InputStream getInputStream() { return ftdiuart.getInputStream(); }
109 private void preamble() throws IOException {
113 public void selfTest() throws Exception {
117 config(Integer.parseInt("10110111", 2), 8);
120 try { Thread.sleep(100); } catch (Exception e) { }
122 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
125 try { Thread.sleep(100); } catch (Exception e) { }
131 config(Integer.parseInt("10110111", 2), 8);
134 try { Thread.sleep(100); } catch (Exception e) { }
136 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
139 try { Thread.sleep(100); } catch (Exception e) { }
141 config(Integer.parseInt("11110111", 2), 8);
144 try { Thread.sleep(100); } catch (Exception e) { }
146 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
149 // Private //////////////////////////////////////////////////////////////////////////////
151 private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
153 private int dbits = 0;
154 private void dbang(int bit, boolean val) throws IOException {
155 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
156 ftdiuart.getOutputStream().write((byte)dbits);
159 private void config(boolean bit) throws IOException { config(bit?1:0, 1); }
160 private void config(int dat) throws IOException { config(dat, 8); }
161 private void config(int dat, int numbits) throws IOException {
162 for(int i=(numbits-1); i>=0; i--) {
163 boolean bit = (dat & (1<<i)) != 0;
170 // tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
171 // we can pull it down (assert reset) from uart-mode, or we can
172 // let it float upward from either mode.
173 private void reset(boolean on) throws IOException {
174 ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
177 ftdiuart.dbus_mode(dmask);
182 private void avrrst(boolean on) throws IOException { dbang(7, on); }
183 private void clk(boolean on) throws IOException { dbang(6, on); }
184 private void data(boolean on) throws IOException { dbang(5, on); }
185 private boolean initErr() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
187 private boolean con() throws IOException {
190 ftdiuart.dbus_mode(dmask);
191 return (ftdiuart.readPins() & (1<<0)) != 0;
194 private boolean rcon() throws IOException {
197 ftdiuart.dbus_mode(dmask);
198 return (ftdiuart.readPins() & (1<<0)) != 0;
200 private void con(boolean on) throws IOException {
204 ftdiuart.dbus_mode(dmask);
207 private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
208 private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }