+
+ // 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)));
+ ftdiuart.getOutputStream().write((byte)dbits);
+ }
+
+ 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 {
+ for(int i=(numbits-1); i>=0; i--) {
+ boolean bit = (dat & (1<<i)) != 0;
+ data(bit);
+ clk(true);
+ clk(false);
+ }
+ }
+
+ // 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 {
+ ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
+ flush();
+ if (on) {
+ ftdiuart.dbus_mode(dmask);
+ flush();
+ }
+ }
+
+ 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 boolean con() throws IOException {
+ flush();
+ //dmask &= ~(1<<0);
+ ftdiuart.dbus_mode(dmask);
+ return (ftdiuart.readPins() & (1<<0)) != 0;
+ }
+
+ private boolean rcon() throws IOException {
+ flush();
+ dmask &= ~(1<<0);
+ ftdiuart.dbus_mode(dmask);
+ return (ftdiuart.readPins() & (1<<0)) != 0;
+ }
+ private void con(boolean on) throws IOException {
+ flush();
+ dmask |= (1<<0);
+ dbang(0, on);
+ ftdiuart.dbus_mode(dmask);
+ }
+
+ private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
+ private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }