altos/test: Compute and plot tilt based on GPS track
[fw/altos] / altoslib / AltosSelfFlash.java
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_2;
19
20 import java.io.*;
21
22 public class AltosSelfFlash {
23         File                    file;
24         FileInputStream         input;
25         AltosHexfile            image;
26         AltosLink               link;
27         boolean                 aborted;
28         AltosFlashListener      listener;
29         byte[]                  read_block, write_block;
30
31         void action(String s, int percent) {
32                 if (listener != null && !aborted)
33                         listener.position(s, percent);
34         }
35
36         void action(int part, int total) {
37                 int percent = 100 * part / total;
38                 action(String.format("%d/%d (%d%%)",
39                                      part, total, percent),
40                        percent);
41         }
42
43         void read_block(long addr) {
44                 link.printf("R %x\n", addr);
45                 
46         }
47
48         void read_memory(long addr, int len) {
49         }
50                 
51         void write_memory(long addr, byte[] data, int start, int len) {
52                 
53         }
54
55         void reboot() {
56         }
57
58         public void flash() {
59                 try {
60                         int remain = image.data.length;
61                         long flash_addr = image.address;
62                         int image_start = 0;
63
64                         action("start", 0);
65                         action(0, image.data.length);
66                         while (remain > 0 && !aborted) {
67                                 int this_time = remain;
68                                 if (this_time > 0x100)
69                                         this_time = 0x100;
70
71                                 if (link != null) {
72                                         /* write the data */
73                                         write_memory(flash_addr, image.data, image_start, this_time);
74
75                                         byte[] check = read_memory(flash_addr, this_time);
76                                         for (int i = 0; i < this_time; i++)
77                                                 if (check[i] != image.data[image_start + i])
78                                                         throw new IOException(String.format("Flash write failed at 0x%x (%02x != %02x)",
79                                                                                             image.address + image_start + i,
80                                                                                             check[i], image.data[image_start + i]));
81                                 } else {
82                                         Thread.sleep(100);
83                                 }
84
85                                 remain -= this_time;
86                                 flash_addr += this_time;
87                                 image_start += this_time;
88
89                                 action(image.data.length - remain, image.data.length);
90                         }
91                         if (!aborted) {
92                                 action("done", 100);
93                                 if (link != null) {
94                                         reboot();
95                                 }
96                         }
97                         if (link != null)
98                                 link.close();
99                 } catch (IOException ie) {
100                         action(ie.getMessage(), -1);
101                         abort();
102                 } catch (InterruptedException ie) {
103                         abort();
104                 }
105         }
106
107         public void close() {
108                 if (link != null)
109                         link.close();
110         }
111
112         synchronized public void abort() {
113                 aborted = true;
114                 close();
115         }
116
117         public boolean check_rom_config() {
118                 if (link == null)
119                         return true;
120                 if (rom_config == null)
121                         rom_config = debug.romconfig();
122                 return rom_config != null && rom_config.valid();
123         }
124
125         public void set_romconfig (AltosRomconfig romconfig) {
126                 rom_config = romconfig;
127         }
128
129         public AltosRomconfig romconfig() {
130                 if (!check_rom_config())
131                         return null;
132                 return rom_config;
133         }
134
135         public AltosFlash(File file, AltosLink link, AltosFlashListener listener)
136                 throws IOException, FileNotFoundException, InterruptedException {
137                 this.file = file;
138                 this.link = link;
139                 this.listener = listener;
140                 this.read_block = new byte[256];
141                 this.write_block = new byte[256];
142                 input = new FileInputStream(file);
143                 image = new AltosHexfile(input);
144                 if (link != null) {
145                         debug.close();
146                         throw new IOException("Debug port not connected");
147                 }
148         }
149 }