(1<<7);
ftdiuart.dbus_mode(dmask);
- avrrst(false);
- clk(false);
- data(false);
-
+ clearDBusLines();
flush();
- dmask |= (1<<0);
- dbang(0, false);
ftdiuart.dbus_mode(dmask);
- reset(false);
+ resetPin(false);
try { Thread.sleep(500); } catch (Exception e) { }
- if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
+ if (initPin()) throw new RuntimeException("INIT was still high after pulling RESET low");
- reset(true);
+ resetPin(true);
try { Thread.sleep(500); } catch (Exception e) { }
- if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
+ if (!initPin()) throw new RuntimeException("INIT was still low after releasing RESET");
- config(0,2);
+ sendConfigBits(0,2);
flush();
}
public void write(int in) throws IOException {
for(int i=7; i>=0; i--) {
bits++;
- config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
+ sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
}
}
public void write(byte[] b, int off, int len) throws IOException {
public void close() throws IOException {
flush();
- if (!initErr())
+
+ // turn off the CON pin we've been pulling low...
+ dmask &= ~(1<<0);
+ ftdiuart.dbus_mode(dmask);
+ flush();
+
+ if (!initPin())
throw new RuntimeException("initialization failed at " + bytes);
for(int i=0; i<100; i++) {
flush();
- if (!initErr())
+ if (!initPin())
throw new RuntimeException("initialization failed at " + bytes);
try { Thread.sleep(20); } catch (Exception e) { }
- config(0,1);
+ sendConfigBits(0,1);
}
- // turn off the CON pin we've been pulling low...
- dmask &= ~(1<<0);
- ftdiuart.dbus_mode(dmask);
-
-
- avrrst(false);
- try { Thread.sleep(100); } catch (Exception e) { }
+ // switching to uart mode will implicitly release AVRRST
+ avrrstPin(false);
ftdiuart.purge();
ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
}
public void selfTest() throws Exception {
boolean pin;
+ System.out.print("smoke check: ");
+
+ // correct preamble
+ getConfigStream();
+ sendConfigBits(Integer.parseInt("00000000", 2), 8);
+ sendConfigBits(Integer.parseInt("10110111", 2), 8);
+ sendConfigBits(0,1);
+ flush();
+ try { Thread.sleep(100); } catch (Exception e) { }
+ pin = initPin();
+ System.out.print((pin ? green(" [pass]") : red(" [FAIL]")));
+ // preamble shifted one bit earlier than it should be
getConfigStream();
- config(Integer.parseInt("00000000", 2), 8);
- config(Integer.parseInt("10110111", 2), 8);
- config(0,1);
+ sendConfigBits(Integer.parseInt("0000000", 2), 7);
+ sendConfigBits(Integer.parseInt("10110111", 2), 8);
+ sendConfigBits(0, 2);
flush();
try { Thread.sleep(100); } catch (Exception e) { }
- pin = initErr();
- System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
+ pin = initPin();
+ System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
+ // preamble shifted one bit later than it should be
getConfigStream();
- config(Integer.parseInt("0000000", 2), 7);
- config(Integer.parseInt("10110111", 2), 8);
- config(0, 2);
+ sendConfigBits(Integer.parseInt("000000000", 2), 9);
+ sendConfigBits(Integer.parseInt("10110111", 2), 8);
+ //sendConfigBits(0, 1);
flush();
try { Thread.sleep(100); } catch (Exception e) { }
- pin = initErr();
- System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
+ pin = initPin();
+ System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
+ // plain 'ol bogus preamble
getConfigStream();
- config(Integer.parseInt("00000000", 2), 8);
- config(Integer.parseInt("11110111", 2), 8);
- config(0, 1);
+ sendConfigBits(Integer.parseInt("00000000", 2), 8);
+ sendConfigBits(Integer.parseInt("11110111", 2), 8);
+ sendConfigBits(0, 1);
flush();
try { Thread.sleep(100); } catch (Exception e) { }
- pin = initErr();
- System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
+ pin = initPin();
+ System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
+
+ System.out.println();
}
// Private //////////////////////////////////////////////////////////////////////////////
private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
private int dbits = 0;
- private void dbang(int bit, boolean val) throws IOException {
- dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
+ private void setDBusLine() throws IOException {
ftdiuart.getOutputStream().write((byte)dbits);
}
+ private void clearDBusLines() throws IOException {
+ dbits = 0;
+ setDBusLine();
+ }
+ private void setDBusLine(int bit, boolean val) throws IOException {
+ dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
+ setDBusLine();
+ }
- private void config(boolean bit) throws IOException { config(bit?1:0, 1); }
- private void config(int dat) throws IOException { config(dat, 8); }
- private void config(int dat, int numbits) throws IOException {
+ private void sendConfigBits(int dat, int numbits) throws IOException {
for(int i=(numbits-1); i>=0; i--) {
boolean bit = (dat & (1<<i)) != 0;
- data(bit);
- clk(true);
+ configDataPin(bit);
+ cclkPin(true);
dbits &= ~(1<<6); // let the clock fall with the next data bit, whenever it goes out
}
}
// tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
// we can pull it down (assert reset) from uart-mode, or we can
// let it float upward from either mode.
- private void reset(boolean on) throws IOException {
+ private void resetPin(boolean on) throws IOException {
ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
flush();
if (on) {
}
}
- private void avrrst(boolean on) throws IOException { dbang(7, on); }
- private void clk(boolean on) throws IOException { dbang(6, on); }
- private void data(boolean on) throws IOException { dbang(5, on); }
- private boolean initErr() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
+ private void avrrstPin(boolean on) throws IOException { setDBusLine(7, on); }
+ private void cclkPin(boolean on) throws IOException { setDBusLine(6, on); }
+ private void configDataPin(boolean on) throws IOException { setDBusLine(5, on); }
+ private boolean initPin() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
--- /dev/null
+package org.ibex.util;
+import java.io.*;
+
+public class ProgressInputStream extends FilterInputStream {
+
+ private int size = -1;
+ private int bytes = 0;
+ private String title;
+
+ public ProgressInputStream(String title, InputStream o) { this(title, o, -1); }
+
+ public ProgressInputStream(String title, InputStream o, int size) {
+ super(o);
+ this.size = size;
+ this.title = title;
+ }
+
+ public int read() throws IOException {
+ int ret = super.read();
+ if (ret != -1) bytes++;
+ return ret;
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ int ret = super.read(b, off, len);
+ if (ret != -1) bytes += ret;
+ update();
+ return ret;
+ }
+
+ private void update() {
+ System.out.print("\r \r");
+ System.out.print(title);
+ if (size != -1) {
+ int frac = (100 * bytes) / size;
+ String fracs = frac+"";
+ while(fracs.length()<3) fracs = " "+fracs;
+ System.out.print(" ");
+ System.out.print("\033[32m");
+ System.out.print(fracs+"%");
+ System.out.print("\033[0m");
+ }
+ System.out.print(" ");
+ System.out.print(bytes);
+ System.out.print(" bytes ");
+ }
+
+ public void close() throws IOException {
+ super.close();
+ bytes = size;
+ update();
+ System.out.println();
+ }
+}