Merge remote-tracking branch 'mjb/altosui_mjb'
[fw/altos] / altosui / AltosFlash.java
1 /*
2  * Copyright © 2010 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 altosui;
19
20 import java.awt.event.*;
21 import javax.swing.*;
22 import java.io.*;
23
24 public class AltosFlash {
25         File            file;
26         FileInputStream input;
27         AltosHexfile    image;
28         JFrame          frame;
29         AltosDevice     debug_dongle;
30         AltosDebug      debug;
31         AltosRomconfig  rom_config;
32         ActionListener  listener;
33         boolean         aborted;
34
35         static final byte MOV_direct_data       = (byte) 0x75;
36         static final byte MOV_DPTR_data16       = (byte) 0x90;
37         static final byte MOV_A_data            = (byte) 0x74;
38         static final byte MOVX_atDPTR_A         = (byte) 0xf0;
39         static final byte MOVX_A_atDPTR         = (byte) 0xe0;
40         static final byte INC_DPTR              = (byte) 0xa3;
41         static final byte TRAP                  = (byte) 0xa5;
42
43         static final byte JB                    = (byte) 0x20;
44
45         static final byte MOV_A_direct          = (byte) 0xe5;
46         static final byte MOV_direct1_direct2   = (byte) 0x85;
47         static final byte MOV_direct_A          = (byte) 0xf5;
48         static final byte MOV_R0_data           = (byte) (0x78 | 0);
49         static final byte MOV_R1_data           = (byte) (0x78 | 1);
50         static final byte MOV_R2_data           = (byte) (0x78 | 2);
51         static final byte MOV_R3_data           = (byte) (0x78 | 3);
52         static final byte MOV_R4_data           = (byte) (0x78 | 4);
53         static final byte MOV_R5_data           = (byte) (0x78 | 5);
54         static final byte MOV_R6_data           = (byte) (0x78 | 6);
55         static final byte MOV_R7_data           = (byte) (0x78 | 7);
56         static final byte DJNZ_R0_rel           = (byte) (0xd8 | 0);
57         static final byte DJNZ_R1_rel           = (byte) (0xd8 | 1);
58         static final byte DJNZ_R2_rel           = (byte) (0xd8 | 2);
59         static final byte DJNZ_R3_rel           = (byte) (0xd8 | 3);
60         static final byte DJNZ_R4_rel           = (byte) (0xd8 | 4);
61         static final byte DJNZ_R5_rel           = (byte) (0xd8 | 5);
62         static final byte DJNZ_R6_rel           = (byte) (0xd8 | 6);
63         static final byte DJNZ_R7_rel           = (byte) (0xd8 | 7);
64
65         static final byte P1DIR                 = (byte) 0xFE;
66         static final byte P1                    = (byte) 0x90;
67
68         /* flash controller */
69         static final byte FWT                   = (byte) 0xAB;
70         static final byte FADDRL                = (byte) 0xAC;
71         static final byte FADDRH                = (byte) 0xAD;
72         static final byte FCTL                  = (byte) 0xAE;
73         static final byte FCTL_BUSY             = (byte) 0x80;
74         static final byte FCTL_BUSY_BIT         = (byte) 7;
75         static final byte FCTL_SWBSY            = (byte) 0x40;
76         static final byte FCTL_SWBSY_BIT        = (byte) 6;
77         static final byte FCTL_CONTRD           = (byte) 0x10;
78         static final byte FCTL_WRITE            = (byte) 0x02;
79         static final byte FCTL_ERASE            = (byte) 0x01;
80         static final byte FWDATA                = (byte) 0xAF;
81
82         static final byte ACC                   = (byte) 0xE0;
83
84         /* offsets within the flash_page program */
85         static final int FLASH_ADDR_HIGH        = 8;
86         static final int FLASH_ADDR_LOW         = 11;
87         static final int RAM_ADDR_HIGH          = 13;
88         static final int RAM_ADDR_LOW           = 14;
89         static final int FLASH_WORDS_HIGH       = 16;
90         static final int FLASH_WORDS_LOW        = 18;
91         static final int FLASH_TIMING           = 21;
92
93         /* sleep mode control */
94         static final int SLEEP                  = (byte) 0xbe;
95         static final int  SLEEP_USB_EN          = (byte) 0x80;
96         static final int  SLEEP_XOSC_STB        = (byte) 0x40;
97         static final int  SLEEP_HFRC_STB        = (byte) 0x20;
98         static final int  SLEEP_RST_MASK        = (byte) 0x18;
99         static final int   SLEEP_RST_POWERON    = (byte) 0x00;
100         static final int   SLEEP_RST_EXTERNAL   = (byte) 0x10;
101         static final int   SLEEP_RST_WATCHDOG   = (byte) 0x08;
102         static final int  SLEEP_OSC_PD          = (byte) 0x04;
103         static final int  SLEEP_MODE_MASK       = (byte) 0x03;
104         static final int   SLEEP_MODE_PM0       = (byte) 0x00;
105         static final int   SLEEP_MODE_PM1       = (byte) 0x01;
106         static final int   SLEEP_MODE_PM2       = (byte) 0x02;
107         static final int   SLEEP_MODE_PM3       = (byte) 0x03;
108
109         /* clock controller */
110         static final byte CLKCON                = (byte) 0xC6;
111         static final byte  CLKCON_OSC32K        = (byte) 0x80;
112         static final byte  CLKCON_OSC           = (byte) 0x40;
113         static final byte  CLKCON_TICKSPD       = (byte) 0x38;
114         static final byte  CLKCON_CLKSPD        = (byte) 0x07;
115
116         static final byte[] flash_page_proto = {
117
118                 MOV_direct_data, P1DIR, (byte) 0x02,
119                 MOV_direct_data, P1,    (byte) 0xFF,
120
121                 MOV_direct_data, FADDRH, 0,     /* FLASH_ADDR_HIGH */
122
123                 MOV_direct_data, FADDRL, 0,     /* FLASH_ADDR_LOW */
124
125                 MOV_DPTR_data16, 0, 0,          /* RAM_ADDR_HIGH, RAM_ADDR_LOW */
126
127                 MOV_R7_data, 0,                 /* FLASH_WORDS_HIGH */
128
129                 MOV_R6_data, 0,                 /* FLASH_WORDS_LOW */
130
131
132                 MOV_direct_data, FWT, 0x20,     /* FLASH_TIMING */
133
134                 MOV_direct_data, FCTL, FCTL_ERASE,
135 /* eraseWaitLoop: */
136                 MOV_A_direct,           FCTL,
137                 JB, ACC|FCTL_BUSY_BIT, (byte) 0xfb,
138
139                 MOV_direct_data, P1, (byte) 0xfd,
140
141                 MOV_direct_data, FCTL, FCTL_WRITE,
142 /* writeLoop: */
143                 MOV_R5_data, 2,
144 /* writeWordLoop: */
145                 MOVX_A_atDPTR,
146                 INC_DPTR,
147                 MOV_direct_A, FWDATA,
148                 DJNZ_R5_rel, (byte) 0xfa,               /* writeWordLoop */
149 /* writeWaitLoop: */
150                 MOV_A_direct, FCTL,
151                 JB, ACC|FCTL_SWBSY_BIT, (byte) 0xfb,    /* writeWaitLoop */
152                 DJNZ_R6_rel, (byte) 0xf1,               /* writeLoop */
153                 DJNZ_R7_rel, (byte) 0xef,                       /* writeLoop */
154
155                 MOV_direct_data, P1DIR, (byte) 0x00,
156                 MOV_direct_data, P1,    (byte) 0xFF,
157                 TRAP,
158         };
159
160         public byte[] make_flash_page(int flash_addr, int ram_addr, int byte_count) {
161                 int flash_word_addr = flash_addr >> 1;
162                 int flash_word_count = ((byte_count + 1) >> 1);
163
164                 byte[] flash_page = new byte[flash_page_proto.length];
165                 for (int i = 0; i < flash_page.length; i++)
166                         flash_page[i] = flash_page_proto[i];
167
168                 flash_page[FLASH_ADDR_HIGH]  = (byte) (flash_word_addr >> 8);
169                 flash_page[FLASH_ADDR_LOW]   = (byte) (flash_word_addr);
170                 flash_page[RAM_ADDR_HIGH]    = (byte) (ram_addr >> 8);
171                 flash_page[RAM_ADDR_LOW]     = (byte) (ram_addr);
172
173                 byte flash_words_low = (byte) (flash_word_count);
174                 byte flash_words_high = (byte) (flash_word_count >> 8);
175                 /* the flashing code has a minor 'bug' */
176                 if (flash_words_low != 0)
177                         flash_words_high++;
178
179                 flash_page[FLASH_WORDS_HIGH] = (byte) flash_words_high;
180                 flash_page[FLASH_WORDS_LOW]  = (byte) flash_words_low;
181                 return flash_page;
182         }
183
184         static byte[] set_clkcon_fast = {
185                 MOV_direct_data, CLKCON, 0x00
186         };
187
188         static byte[] get_sleep = {
189                 MOV_A_direct, SLEEP
190         };
191
192         public void clock_init() throws IOException, InterruptedException {
193                 if (debug != null) {
194                         debug.debug_instr(set_clkcon_fast);
195
196                         byte    status;
197                         for (int times = 0; times < 20; times++) {
198                                 Thread.sleep(1);
199                                 status = debug.debug_instr(get_sleep);
200                                 if ((status & SLEEP_XOSC_STB) != 0)
201                                         return;
202                         }
203                         throw new IOException("Failed to initialize target clock");
204                 }
205         }
206
207         void action(String in_s, int in_percent) {
208                 final String s = in_s;
209                 final int percent = in_percent;
210                 if (listener != null && !aborted) {
211                         Runnable r = new Runnable() {
212                                         public void run() {
213                                                 try {
214                                                         listener.actionPerformed(new ActionEvent(this,
215                                                                                                  percent,
216                                                                                                  s));
217                                                 } catch (Exception ex) {
218                                                 }
219                                         }
220                                 };
221                         SwingUtilities.invokeLater(r);
222                 }
223         }
224
225         void action(int part, int total) {
226                 int percent = 100 * part / total;
227                 action(String.format("%d/%d (%d%%)",
228                                      part, total, percent),
229                        percent);
230         }
231
232         void altos_run(int pc) throws IOException, InterruptedException {
233                 debug.set_pc(pc);
234                 int set_pc = debug.get_pc();
235                 if (pc != set_pc)
236                         throw new IOException("Failed to set target program counter");
237                 debug.resume();
238
239                 for (int times = 0; times < 20; times++) {
240                         byte status = debug.read_status();
241                         if ((status & AltosDebug.STATUS_CPU_HALTED) != 0)
242                                 return;
243                 }
244
245                 throw new IOException("Failed to execute program on target");
246         }
247
248         public void flash() {
249                 try {
250                         if (!check_rom_config())
251                                 throw new IOException("Invalid rom config settings");
252                         if (image.address + image.data.length > 0x8000)
253                                 throw new IOException(String.format("Flash image too long %d",
254                                                                     image.address +
255                                                                     image.data.length));
256                         if ((image.address & 0x3ff) != 0)
257                                 throw new IOException(String.format("Flash image must start on page boundary (is 0x%x)",
258                                                                     image.address));
259                         int ram_address = 0xf000;
260                         int flash_prog = 0xf400;
261
262                         /*
263                          * Store desired config values into image
264                          */
265                         rom_config.write(image);
266                         /*
267                          * Bring up the clock
268                          */
269                         clock_init();
270
271                         int remain = image.data.length;
272                         int flash_addr = image.address;
273                         int image_start = 0;
274
275                         action("start", 0);
276                         action(0, image.data.length);
277                         while (remain > 0 && !aborted) {
278                                 int this_time = remain;
279                                 if (this_time > 0x400)
280                                         this_time = 0x400;
281
282                                 if (debug != null) {
283                                         /* write the data */
284                                         debug.write_memory(ram_address, image.data,
285                                                            image_start, this_time);
286
287                                         /* write the flash program */
288                                         byte[] flash_page = make_flash_page(flash_addr,
289                                                                             ram_address,
290                                                                             this_time);
291                                         debug.write_memory(flash_prog, flash_page);
292
293                                         altos_run(flash_prog);
294                                         byte[] check = debug.read_memory(flash_addr, this_time);
295                                         for (int i = 0; i < this_time; i++)
296                                                 if (check[i] != image.data[image_start + i])
297                                                         throw new IOException(String.format("Flash write failed at 0x%x (%02x != %02x)",
298                                                                                             image.address + image_start + i,
299                                                                                             check[i], image.data[image_start + i]));
300                                 } else {
301                                         Thread.sleep(100);
302                                 }
303
304                                 remain -= this_time;
305                                 flash_addr += this_time;
306                                 image_start += this_time;
307
308                                 action(image.data.length - remain, image.data.length);
309                         }
310                         if (!aborted) {
311                                 action("done", 100);
312                                 if (debug != null) {
313                                         debug.set_pc(image.address);
314                                         debug.resume();
315                                 }
316                         }
317                         if (debug != null)
318                                 debug.close();
319                 } catch (IOException ie) {
320                         action(ie.getMessage(), -1);
321                         abort();
322                 } catch (InterruptedException ie) {
323                         abort();
324                 }
325         }
326
327         public void close() {
328                 if (debug != null)
329                         debug.close();
330         }
331
332         synchronized public void abort() {
333                 aborted = true;
334                 close();
335         }
336
337         public void addActionListener(ActionListener l) {
338                 listener = l;
339         }
340
341         public boolean check_rom_config() {
342                 if (debug == null)
343                         return true;
344                 if (rom_config == null)
345                         rom_config = debug.romconfig();
346                 return rom_config != null && rom_config.valid();
347         }
348
349         public void set_romconfig (AltosRomconfig romconfig) {
350                 rom_config = romconfig;
351         }
352
353         public AltosRomconfig romconfig() {
354                 if (!check_rom_config())
355                         return null;
356                 return rom_config;
357         }
358
359         public AltosFlash(File in_file, AltosDevice in_debug_dongle)
360                 throws IOException, FileNotFoundException, AltosSerialInUseException, InterruptedException {
361                 file = in_file;
362                 debug_dongle = in_debug_dongle;
363                 if (debug_dongle != null)
364                         debug = new AltosDebug(in_debug_dongle);
365                 input = new FileInputStream(file);
366                 image = new AltosHexfile(input);
367                 if (debug != null && !debug.check_connection()) {
368                         debug.close();
369                         throw new IOException("Debug port not connected");
370                 }
371         }
372 }