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