Switch to using internal cc1111 temperature sensor
[fw/altos] / ao-tools / lib / ccdbg.h
1 /*
2  * Copyright © 2008 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _CCDBG_H_
20 #define _CCDBG_H_
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
33 #include "ccdbg-debug.h"
34 #include "cc-bitbang.h"
35 #include "cc-usb.h"
36
37 /* 8051 instructions
38  */
39 #define NOP                     0x00
40 #define MOV_direct_data         0x75
41 #define LJMP                    0x02
42 #define MOV_Rn_data(n)          (0x78 | (n))
43 #define DJNZ_Rn_rel(n)          (0xd8 | (n))
44 #define MOV_A_direct            0xe5
45 #define MOV_direct1_direct2     0x85
46 #define MOV_direct_A            0xf5
47 #define MOV_DPTR_data16         0x90
48 #define MOV_A_data              0x74
49 #define MOVX_atDPTR_A           0xf0
50 #define MOVX_A_atDPTR           0xe0
51 #define INC_DPTR                0xa3
52 #define TRAP                    0xa5
53 #define SJMP                    0x80
54 #define JB                      0x20
55
56 /* 8051 special function registers
57  */
58
59 #define SFR_P0                  0x80
60 #define SFR_SP                  0x81
61 #define SFR_DPL0                0x82
62 #define SFR_DPH0                0x83
63 #define SFR_DPL1                0x84
64 #define SFR_DPH1                0x85
65
66 /* flash controller */
67 #define FWT                     0xAB
68 #define FADDRL                  0xAC
69 #define FADDRH                  0xAD
70 #define FCTL                    0xAE
71 # define FCTL_BUSY              0x80
72 # define FCTL_BUSY_BIT          7
73 # define FCTL_SWBSY             0x40
74 # define FCTL_SWBSY_BIT         6
75 # define FCTL_CONTRD            0x10
76 # define FCTL_WRITE             0x02
77 # define FCTL_ERASE             0x01
78 #define FWDATA                  0xAF
79
80 #define SLEEP                   0xBE
81
82 /* clock controller */
83 #define CLKCON                  0xC6
84 #define  CLKCON_OSC32K          0x80
85 #define  CLKCON_OSC             0x40
86 #define  CLKCON_TICKSPD         0x38
87 #define  CLKCON_CLKSPD          0x07
88
89 /* I/O pins */
90 #define P0                      0x80
91 #define P1                      0x90
92 #define P2                      0xA0
93 #define P0DIR                   0xFD
94 #define P1DIR                   0xFE
95 #define P2DIR                   0xFF
96
97 /* Bit-addressable accumulator */
98 #define ACC(bit)                (0xE0 | (bit))
99
100 /* Bit-addressable status word */
101 #define PSW(bit)                (0xD0 | (bit))
102
103 struct ccdbg {
104         struct cc_bitbang       *bb;
105         struct cc_usb           *usb;
106         struct hex_image        *rom;
107 };
108
109 /* Intel hex file format data
110  */
111 struct hex_record {
112         uint8_t length;
113         uint16_t address;
114         uint8_t type;
115         uint8_t checksum;
116         uint8_t data[0];
117 };
118
119 struct hex_file {
120         int                     nrecord;
121         struct hex_record       *records[0];
122 };
123
124 struct hex_image {
125         uint16_t        address;
126         uint16_t        length;
127         uint8_t         data[0];
128 };
129
130 #define CC_STATE_ACC    0x1
131 #define CC_STATE_PSW    0x2
132 #define CC_STATE_DP     0x4
133
134 #define CC_STATE_NSFR   5
135
136 struct ccstate {
137         uint16_t        mask;
138         uint8_t         acc;
139         uint8_t         sfr[CC_STATE_NSFR];
140 };
141
142 #define HEX_RECORD_NORMAL               0x00
143 #define HEX_RECORD_EOF                  0x01
144 #define HEX_RECORD_EXTENDED_ADDRESS     0x02
145
146 /* CC1111 debug port commands
147  */
148 #define CC_CHIP_ERASE           0x14
149
150 #define CC_WR_CONFIG            0x1d
151 #define CC_RD_CONFIG            0x24
152 # define CC_CONFIG_TIMERS_OFF           (1 << 3)
153 # define CC_CONFIG_DMA_PAUSE            (1 << 2)
154 # define CC_CONFIG_TIMER_SUSPEND        (1 << 1)
155 # define CC_SET_FLASH_INFO_PAGE         (1 << 0)
156
157 #define CC_GET_PC               0x28
158 #define CC_READ_STATUS          0x34
159 # define CC_STATUS_CHIP_ERASE_DONE      (1 << 7)
160 # define CC_STATUS_PCON_IDLE            (1 << 6)
161 # define CC_STATUS_CPU_HALTED           (1 << 5)
162 # define CC_STATUS_POWER_MODE_0         (1 << 4)
163 # define CC_STATUS_HALT_STATUS          (1 << 3)
164 # define CC_STATUS_DEBUG_LOCKED         (1 << 2)
165 # define CC_STATUS_OSCILLATOR_STABLE    (1 << 1)
166 # define CC_STATUS_STACK_OVERFLOW       (1 << 0)
167
168 #define CC_SET_HW_BRKPNT        0x3b
169 # define CC_HW_BRKPNT_N(n)      ((n) << 3)
170 # define CC_HW_BRKPNT_N_MASK    (0x3 << 3)
171 # define CC_HW_BRKPNT_ENABLE    (1 << 2)
172
173 #define CC_HALT                 0x44
174 #define CC_RESUME               0x4c
175 #define CC_DEBUG_INSTR(n)       (0x54|(n))
176 #define CC_STEP_INSTR           0x5c
177 #define CC_STEP_REPLACE(n)      (0x64|(n))
178 #define CC_GET_CHIP_ID          0x68
179
180 /* ccdbg-command.c */
181 void
182 ccdbg_debug_mode(struct ccdbg *dbg);
183
184 void
185 ccdbg_reset(struct ccdbg *dbg);
186
187 uint8_t
188 ccdbg_chip_erase(struct ccdbg *dbg);
189
190 uint8_t
191 ccdbg_wr_config(struct ccdbg *dbg, uint8_t config);
192
193 uint8_t
194 ccdbg_rd_config(struct ccdbg *dbg);
195
196 uint16_t
197 ccdbg_get_pc(struct ccdbg *dbg);
198
199 uint8_t
200 ccdbg_read_status(struct ccdbg *dbg);
201
202 uint8_t
203 ccdbg_set_hw_brkpnt(struct ccdbg *dbg, uint8_t number, uint8_t enable, uint16_t addr);
204
205 uint8_t
206 ccdbg_halt(struct ccdbg *dbg);
207
208 uint8_t
209 ccdbg_resume(struct ccdbg *dbg);
210
211 uint8_t
212 ccdbg_debug_instr(struct ccdbg *dbg, uint8_t *instr, int nbytes);
213
214 void
215 ccdbg_debug_instr_discard(struct ccdbg *dbg, uint8_t *instr, int nbytes);
216
217 void
218 ccdbg_debug_instr_queue(struct ccdbg *dbg, uint8_t *instr, int nbytes,
219                         uint8_t *reply);
220
221 uint8_t
222 ccdbg_step_instr(struct ccdbg *dbg);
223
224 uint8_t
225 ccdbg_step_replace(struct ccdbg *dbg, uint8_t *instr, int nbytes);
226
227 uint16_t
228 ccdbg_get_chip_id(struct ccdbg *dbg);
229
230 uint8_t
231 ccdbg_execute(struct ccdbg *dbg, uint8_t *inst);
232
233 uint8_t
234 ccdbg_set_pc(struct ccdbg *dbg, uint16_t pc);
235
236 uint8_t
237 ccdbg_execute_hex_image(struct ccdbg *dbg, struct hex_image *image);
238
239 /* ccdbg-flash.c */
240 uint8_t
241 ccdbg_flash_hex_image(struct ccdbg *dbg, struct hex_image *image);
242
243 /* ccdbg-hex.c */
244 struct hex_file *
245 ccdbg_hex_file_read(FILE *file, char *name);
246
247 void
248 ccdbg_hex_file_free(struct hex_file *hex);
249
250 struct hex_image *
251 ccdbg_hex_image_create(struct hex_file *hex);
252
253 void
254 ccdbg_hex_image_free(struct hex_image *image);
255
256 int
257 ccdbg_hex_image_equal(struct hex_image *a, struct hex_image *b);
258
259 /* ccdbg-io.c */
260 struct ccdbg *
261 ccdbg_open(char *tty);
262
263 void
264 ccdbg_close(struct ccdbg *dbg);
265
266 void
267 ccdbg_cmd_write(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len);
268
269 uint8_t
270 ccdbg_cmd_write_read8(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len);
271
272 void
273 ccdbg_cmd_write_queue8(struct ccdbg *dbg, uint8_t cmd,
274                        uint8_t *data, int len, uint8_t *reply);
275
276 uint16_t
277 ccdbg_cmd_write_read16(struct ccdbg *dbg, uint8_t cmd, uint8_t *data, int len);
278
279 void
280 ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes);
281
282 void
283 ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes);
284
285 void
286 ccdbg_sync(struct ccdbg *dbg);
287
288 /* ccdbg-manual.c */
289
290 void
291 ccdbg_manual(struct ccdbg *dbg, FILE *input);
292
293 /* ccdbg-memory.c */
294 uint8_t
295 ccdbg_write_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes);
296
297 uint8_t
298 ccdbg_read_memory(struct ccdbg *dbg, uint16_t addr, uint8_t *bytes, int nbytes);
299
300 uint8_t
301 ccdbg_write_uint8(struct ccdbg *dbg, uint16_t addr, uint8_t byte);
302
303 uint8_t
304 ccdbg_write_hex_image(struct ccdbg *dbg, struct hex_image *image, uint16_t offset);
305
306 struct hex_image *
307 ccdbg_read_hex_image(struct ccdbg *dbg, uint16_t address, uint16_t length);
308
309 uint8_t
310 ccdbg_read_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes);
311
312 uint8_t
313 ccdbg_write_sfr(struct ccdbg *dbg, uint8_t addr, uint8_t *bytes, int nbytes);
314
315 /* ccdbg-rom.c */
316 uint8_t
317 ccdbg_set_rom(struct ccdbg *dbg, struct hex_image *rom);
318
319 uint8_t
320 ccdbg_rom_contains(struct ccdbg *dbg, uint16_t addr, int nbytes);
321
322 uint8_t
323 ccdbg_rom_replace_xmem(struct ccdbg *dbg,
324                        uint16_t addrp, uint8_t *bytesp, int nbytes);
325
326 /* ccdbg-state.c */
327 uint8_t
328 ccdbg_state_save(struct ccdbg *dbg, struct ccstate *state, unsigned int mask);
329
330 uint8_t
331 ccdbg_state_restore(struct ccdbg *dbg, struct ccstate *state);
332
333 void
334 ccdbg_state_replace_xmem(struct ccdbg *dbg, struct ccstate *state,
335                          uint16_t addr, uint8_t *bytes, int nbytes);
336
337 void
338 ccdbg_state_replace_sfr(struct ccdbg *dbg, struct ccstate *state,
339                         uint8_t addr, uint8_t *bytes, int nbytes);
340
341 #endif /* _CCDBG_H_ */