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 {
38 ftdiuart.dbus_mode(dmask);
42 ftdiuart.dbus_mode(dmask);
45 try { Thread.sleep(500); } catch (Exception e) { }
46 if (initPin()) throw new RuntimeException("INIT was still high after pulling RESET low");
49 try { Thread.sleep(500); } catch (Exception e) { }
50 if (!initPin()) throw new RuntimeException("INIT was still low after releasing RESET");
56 public OutputStream getConfigStream() throws IOException {
58 return new OutputStream() {
61 public void write(int in) throws IOException {
62 for(int i=7; i>=0; i--) {
64 sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
67 public void write(byte[] b, int off, int len) throws IOException {
68 for(int i=off; i<off+len; i++)
71 public void flush() throws IOException {
72 FpslicRawUsb.this.flush();
74 public void close() throws IOException {
78 // turn off the CON pin we've been pulling low...
80 ftdiuart.dbus_mode(dmask);
84 throw new RuntimeException("initialization failed at " + bytes);
85 for(int i=0; i<100; i++) {
88 throw new RuntimeException("initialization failed at " + bytes);
89 try { Thread.sleep(20); } catch (Exception e) { }
93 // switching to uart mode will implicitly release AVRRST
96 ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
101 public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
102 public InputStream getInputStream() { return ftdiuart.getInputStream(); }
104 public void selfTest() throws Exception {
106 System.out.print("smoke check: ");
110 sendConfigBits(Integer.parseInt("00000000", 2), 8);
111 sendConfigBits(Integer.parseInt("10110111", 2), 8);
114 try { Thread.sleep(100); } catch (Exception e) { }
116 System.out.print((pin ? green(" [pass]") : red(" [FAIL]")));
118 // preamble shifted one bit earlier than it should be
120 sendConfigBits(Integer.parseInt("0000000", 2), 7);
121 sendConfigBits(Integer.parseInt("10110111", 2), 8);
122 sendConfigBits(0, 2);
124 try { Thread.sleep(100); } catch (Exception e) { }
126 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
128 // preamble shifted one bit later than it should be
130 sendConfigBits(Integer.parseInt("000000000", 2), 9);
131 sendConfigBits(Integer.parseInt("10110111", 2), 8);
132 //sendConfigBits(0, 1);
134 try { Thread.sleep(100); } catch (Exception e) { }
136 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
138 // plain 'ol bogus preamble
140 sendConfigBits(Integer.parseInt("00000000", 2), 8);
141 sendConfigBits(Integer.parseInt("11110111", 2), 8);
142 sendConfigBits(0, 1);
144 try { Thread.sleep(100); } catch (Exception e) { }
146 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
148 System.out.println();
151 // Private //////////////////////////////////////////////////////////////////////////////
153 private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
155 private int dbits = 0;
156 private void setDBusLine() throws IOException {
157 ftdiuart.getOutputStream().write((byte)dbits);
159 private void clearDBusLines() throws IOException {
163 private void setDBusLine(int bit, boolean val) throws IOException {
164 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
168 private void sendConfigBits(int dat, int numbits) throws IOException {
169 for(int i=(numbits-1); i>=0; i--) {
170 boolean bit = (dat & (1<<i)) != 0;
173 dbits &= ~(1<<6); // let the clock fall with the next data bit, whenever it goes out
177 // tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
178 // we can pull it down (assert reset) from uart-mode, or we can
179 // let it float upward from either mode.
180 private void resetPin(boolean on) throws IOException {
181 ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
184 ftdiuart.dbus_mode(dmask);
189 private void avrrstPin(boolean on) throws IOException { setDBusLine(7, on); }
190 private void cclkPin(boolean on) throws IOException { setDBusLine(6, on); }
191 private void configDataPin(boolean on) throws IOException { setDBusLine(5, on); }
192 private boolean initPin() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
194 private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
195 private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }