Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[fw/altos] / ao-tools / altosui / AltosFlash.java
index 0f92d6e7bf2ffb82d27fa31aa0fde7cb240e73d5..3af25c23457b696529ea8a2eb19fc1a8bb06278a 100644 (file)
@@ -28,8 +28,6 @@ import java.text.*;
 import java.util.prefs.*;
 import java.util.concurrent.LinkedBlockingQueue;
 
-import altosui.AltosHexfile;
-
 public class AltosFlash {
        File            file;
        FileInputStream input;
@@ -202,9 +200,13 @@ public class AltosFlash {
                debug.debug_instr(set_clkcon_fast);
 
                byte    status;
-               do {
+               for (int times = 0; times < 20; times++) {
+                       Thread.sleep(1);
                        status = debug.debug_instr(get_sleep);
-               } while ((status & SLEEP_XOSC_STB) == 0);
+                       if ((status & SLEEP_XOSC_STB) != 0)
+                               return;
+               }
+               throw new IOException("Failed to initialize target clock");
        }
 
        void action(String s, int percent) {
@@ -221,6 +223,22 @@ public class AltosFlash {
                       percent);
        }
 
+       void run(int pc) throws IOException, InterruptedException {
+               debug.set_pc(pc);
+               int set_pc = debug.get_pc();
+               if (pc != set_pc)
+                       throw new IOException("Failed to set target program counter");
+               debug.resume();
+
+               for (int times = 0; times < 20; times++) {
+                       byte status = debug.read_status();
+                       if ((status & AltosDebug.STATUS_CPU_HALTED) != 0)
+                               return;
+               }
+
+               throw new IOException("Failed to execute program on target");
+       }
+
        public void flash() throws IOException, FileNotFoundException, InterruptedException {
                if (!check_rom_config())
                        throw new IOException("Invalid rom config settings");
@@ -247,6 +265,7 @@ public class AltosFlash {
                int flash_addr = image.address;
                int image_start = 0;
 
+               action("start", 0);
                action(0, image.data.length);
                while (remain > 0 && !aborted) {
                        int this_time = remain;
@@ -263,16 +282,7 @@ public class AltosFlash {
                                                            this_time);
                        debug.write_memory(flash_prog, flash_page);
 
-                       debug.set_pc(flash_prog);
-                       int pc = debug.get_pc();
-                       debug.resume();
-                       Thread.sleep(100);
-                       for (int times = 0; times < 10; times++) {
-                               byte status = debug.read_status();
-                               if ((status & AltosDebug.STATUS_CPU_HALTED) != 0)
-                                       break;
-                               Thread.sleep(100);
-                       }
+                       run(flash_prog);
 
                        byte[] check = debug.read_memory(flash_addr, this_time);
                        for (int i = 0; i < this_time; i++)
@@ -314,20 +324,21 @@ public class AltosFlash {
        }
 
        public AltosRomconfig romconfig() {
+               if (!check_rom_config())
+                       return null;
                return rom_config;
        }
 
-       public void open() throws IOException, FileNotFoundException, InterruptedException {
+       public AltosFlash(File in_file, AltosDevice in_debug_dongle)
+               throws IOException, FileNotFoundException, AltosSerialInUseException, InterruptedException {
+               file = in_file;
+               debug_dongle = in_debug_dongle;
+               debug = new AltosDebug(in_debug_dongle);
                input = new FileInputStream(file);
                image = new AltosHexfile(input);
-               debug.open(debug_dongle);
-               if (!debug.check_connection())
+               if (!debug.check_connection()) {
+                       debug.close();
                        throw new IOException("Debug port not connected");
-       }
-
-       public AltosFlash(File in_file, AltosDevice in_debug_dongle) {
-               file = in_file;
-               debug_dongle = in_debug_dongle;
-               debug = new AltosDebug();
+               }
        }
 }
\ No newline at end of file