altos: Time out reading packet data from cc1120 after 100ms
[fw/altos] / altosui / AltosDebug.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.io.*;
21
22 public class AltosDebug extends AltosSerial {
23
24         public static final byte WR_CONFIG =            0x1d;
25         public static final byte RD_CONFIG =            0x24;
26         public static final byte CONFIG_TIMERS_OFF =            (1 << 3);
27         public static final byte CONFIG_DMA_PAUSE =             (1 << 2);
28         public static final byte CONFIG_TIMER_SUSPEND =         (1 << 1);
29         public static final byte SET_FLASH_INFO_PAGE =          (1 << 0);
30
31         public static final byte GET_PC =               0x28;
32         public static final byte READ_STATUS =          0x34;
33         public static final byte STATUS_CHIP_ERASE_DONE =       (byte) (1 << 7);
34         public static final byte STATUS_PCON_IDLE =             (1 << 6);
35         public static final byte STATUS_CPU_HALTED =            (1 << 5);
36         public static final byte STATUS_POWER_MODE_0 =          (1 << 4);
37         public static final byte STATUS_HALT_STATUS =           (1 << 3);
38         public static final byte STATUS_DEBUG_LOCKED =          (1 << 2);
39         public static final byte STATUS_OSCILLATOR_STABLE =     (1 << 1);
40         public static final byte STATUS_STACK_OVERFLOW =        (1 << 0);
41
42         public static final byte SET_HW_BRKPNT =        0x3b;
43         public static byte       HW_BRKPNT_N(byte n)    { return (byte) ((n) << 3); }
44         public static final byte HW_BRKPNT_N_MASK =             (0x3 << 3);
45         public static final byte HW_BRKPNT_ENABLE =             (1 << 2);
46
47         public static final byte HALT =                 0x44;
48         public static final byte RESUME =               0x4c;
49         public static       byte DEBUG_INSTR(byte n)    { return (byte) (0x54|(n)); }
50         public static final byte STEP_INSTR =           0x5c;
51         public static        byte STEP_REPLACE(byte n)  { return  (byte) (0x64|(n)); }
52         public static final byte GET_CHIP_ID =          0x68;
53
54
55         boolean debug_mode;
56
57         void ensure_debug_mode() {
58                 if (!debug_mode) {
59                         printf("D\n");
60                         try {
61                                 flush_input();
62                         } catch (InterruptedException ie) {
63                         }
64                         debug_mode = true;
65                 }
66         }
67
68         void dump_memory(String header, int address, byte[] bytes, int start, int len) {
69                 System.out.printf("%s\n", header);
70                 for (int j = 0; j < len; j++) {
71                         if ((j & 15) == 0) {
72                                 if (j != 0)
73                                         System.out.printf("\n");
74                                 System.out.printf ("%04x:", address + j);
75                         }
76                         System.out.printf(" %02x", bytes[start + j]);
77                 }
78                 System.out.printf("\n");
79         }
80
81         /*
82          * Write target memory
83          */
84         public void write_memory(int address, byte[] bytes, int start, int len) {
85                 ensure_debug_mode();
86 //              dump_memory("write_memory", address, bytes, start, len);
87                 printf("O %x %x\n", len, address);
88                 for (int i = 0; i < len; i++)
89                         printf("%02x", bytes[start + i]);
90         }
91
92         public void write_memory(int address, byte[] bytes) {
93                 write_memory(address, bytes, 0, bytes.length);
94         }
95
96         /*
97          * Read target memory
98          */
99         public byte[] read_memory(int address, int length)
100                 throws IOException, InterruptedException {
101                 byte[]  data = new byte[length];
102
103                 flush_input();
104                 ensure_debug_mode();
105                 printf("I %x %x\n", length, address);
106                 int i = 0;
107                 int start = 0;
108                 while (i < length) {
109                         String  line = get_reply().trim();
110                         if (!Altos.ishex(line) || line.length() % 2 != 0)
111                                 throw new IOException(
112                                         String.format
113                                         ("Invalid reply \"%s\"", line));
114                         int this_time = line.length() / 2;
115                         for (int j = 0; j < this_time; j++)
116                                 data[start + j] = (byte) ((Altos.fromhex(line.charAt(j*2)) << 4) +
117                                                   Altos.fromhex(line.charAt(j*2+1)));
118                         start += this_time;
119                         i += this_time;
120                 }
121 //              dump_memory("read_memory", address, data, 0, length);
122
123                 return data;
124         }
125
126         /*
127          * Write raw bytes to the debug link using the 'P' command
128          */
129         public void write_bytes(byte[] bytes) throws IOException {
130                 int i = 0;
131                 ensure_debug_mode();
132                 while (i < bytes.length) {
133                         int this_time = bytes.length - i;
134                         if (this_time > 8)
135                                 this_time = 0;
136                         printf("P");
137                         for (int j = 0; j < this_time; j++)
138                                 printf(" %02x", bytes[i+j]);
139                         printf("\n");
140                         i += this_time;
141                 }
142         }
143
144         public void write_byte(byte b) throws IOException {
145                 byte[] bytes = { b };
146                 write_bytes(bytes);
147         }
148
149         /*
150          * Read raw bytes from the debug link using the 'G' command
151          */
152         public byte[] read_bytes(int length)
153                 throws IOException, InterruptedException {
154
155                 flush_input();
156                 ensure_debug_mode();
157                 printf("G %x\n", length);
158                 int i = 0;
159                 byte[] data = new byte[length];
160                 while (i < length) {
161                         String line = get_reply();
162
163                         if (line == null)
164                                 throw new IOException("Timeout in read_bytes");
165                         line = line.trim();
166                         String tokens[] = line.split("\\s+");
167                         for (int j = 0; j < tokens.length; j++) {
168                                 if (!Altos.ishex(tokens[j]) ||
169                                     tokens[j].length() != 2)
170                                         throw new IOException(
171                                                 String.format
172                                                 ("Invalid read_bytes reply \"%s\"", line));
173                                 try {
174                                         if (i + j >= length)
175                                                 throw new IOException(
176                                                         String.format
177                                                         ("Invalid read_bytes reply \"%s\"", line));
178                                         else
179                                                 data[i + j] = (byte) Integer.parseInt(tokens[j], 16);
180                                 } catch (NumberFormatException ne) {
181                                         throw new IOException(
182                                                 String.format
183                                                 ("Invalid read_bytes reply \"%s\"", line));
184                                 }
185                         }
186                         i += tokens.length;
187                 }
188                 return data;
189         }
190
191         public byte read_byte() throws IOException, InterruptedException {
192                 return read_bytes(1)[0];
193         }
194
195         public byte debug_instr(byte[] instruction) throws IOException, InterruptedException {
196                 byte[] command = new byte[1 + instruction.length];
197                 command[0] = DEBUG_INSTR((byte) instruction.length);
198                 for (int i = 0; i < instruction.length; i++)
199                         command[i+1] = instruction[i];
200                 write_bytes(command);
201                 return read_byte();
202         }
203
204         public byte resume() throws IOException, InterruptedException {
205                 write_byte(RESUME);
206                 return read_byte();
207         }
208
209         public int read_uint16() throws IOException, InterruptedException {
210                 byte[] d = read_bytes(2);
211                 return ((int) (d[0] & 0xff) << 8) | (d[1] & 0xff);
212         }
213
214         public int read_uint8()  throws IOException, InterruptedException {
215                 byte[] d = read_bytes(1);
216                 return (int) (d[0] & 0xff);
217         }
218
219         public int get_chip_id() throws IOException, InterruptedException {
220                 write_byte(GET_CHIP_ID);
221                 return read_uint16();
222         }
223
224         public int get_pc() throws IOException, InterruptedException {
225                 write_byte(GET_PC);
226                 return read_uint16();
227         }
228
229         public byte read_status() throws IOException, InterruptedException {
230                 write_byte(READ_STATUS);
231                 return read_byte();
232         }
233
234         static final byte LJMP                  = 0x02;
235
236         public void set_pc(int pc) throws IOException, InterruptedException {
237                 byte high = (byte) (pc >> 8);
238                 byte low = (byte) pc;
239                 byte[] jump_mem = { LJMP, high, low };
240                 debug_instr(jump_mem);
241         }
242
243         public boolean check_connection() throws IOException, InterruptedException {
244                 byte reply = read_status();
245                 if ((reply & STATUS_CHIP_ERASE_DONE) == 0)
246                         return false;
247                 if ((reply & STATUS_PCON_IDLE) != 0)
248                         return false;
249                 if ((reply & STATUS_POWER_MODE_0) == 0)
250                         return false;
251                 return true;
252         }
253
254         public AltosRomconfig romconfig() {
255                 try {
256                         byte[] bytes = read_memory(0xa0, 10);
257                         return new AltosRomconfig(bytes, 0);
258                 } catch (IOException ie) {
259                 } catch (InterruptedException ie) {
260                 }
261                 return new AltosRomconfig();
262         }
263
264         /*
265          * Reset target
266          */
267         public void reset() {
268                 printf ("R\n");
269         }
270
271         public AltosDebug (AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
272                 super(in_device);
273         }
274 }