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