Add AT45DBxx1D driver
[fw/altos] / ao-tools / lib / ccdbg-command.c
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 #include "ccdbg.h"
20
21 uint8_t
22 ccdbg_chip_erase(struct ccdbg *dbg)
23 {
24         return ccdbg_cmd_write_read8(dbg, CC_CHIP_ERASE, NULL, 0);
25 }
26
27 uint8_t
28 ccdbg_wr_config(struct ccdbg *dbg, uint8_t config)
29 {
30         return ccdbg_cmd_write_read8(dbg, CC_WR_CONFIG, &config, 1);
31 }
32
33 uint8_t
34 ccdbg_rd_config(struct ccdbg *dbg)
35 {
36         return ccdbg_cmd_write_read8(dbg, CC_RD_CONFIG, NULL, 0);
37 }
38
39 uint16_t
40 ccdbg_get_pc(struct ccdbg *dbg)
41 {
42         uint16_t        pc1, pc2;
43
44         pc1 = ccdbg_cmd_write_read16(dbg, CC_GET_PC, NULL, 0);
45         pc2 = ccdbg_cmd_write_read16(dbg, CC_GET_PC, NULL, 0);
46         if (pc1 != pc2)
47                 fprintf (stderr, "Invalid pc %04x != %04x\n",
48                          pc1, pc2);
49         return pc2;
50 }
51
52 uint8_t
53 ccdbg_read_status(struct ccdbg *dbg)
54 {
55         return ccdbg_cmd_write_read8(dbg, CC_READ_STATUS, NULL, 0);
56 }
57
58 uint8_t
59 ccdbg_set_hw_brkpnt(struct ccdbg *dbg, uint8_t number, uint8_t enable, uint16_t addr)
60 {
61         uint8_t data[3];
62
63         data[0] = (number << 3) | (enable << 2);
64         data[1] = (addr >> 8);
65         data[2] = addr;
66         return ccdbg_cmd_write_read8(dbg, CC_SET_HW_BRKPNT, data, 3);
67 }
68
69 uint8_t
70 ccdbg_halt(struct ccdbg *dbg)
71 {
72         return ccdbg_cmd_write_read8(dbg, CC_HALT, NULL, 0);
73 }
74
75 uint8_t
76 ccdbg_resume(struct ccdbg *dbg)
77 {
78         return ccdbg_cmd_write_read8(dbg, CC_RESUME, NULL, 0);
79 }
80
81 uint8_t
82 ccdbg_debug_instr(struct ccdbg *dbg, uint8_t *instr, int nbytes)
83 {
84         return ccdbg_cmd_write_read8(dbg, CC_DEBUG_INSTR(nbytes), instr, nbytes);
85 }
86
87 void
88 ccdbg_debug_instr_discard(struct ccdbg *dbg, uint8_t *instr, int nbytes)
89 {
90         static uint8_t  discard;
91         ccdbg_cmd_write_queue8(dbg, CC_DEBUG_INSTR(nbytes),
92                                instr, nbytes, &discard);
93 }
94
95 void
96 ccdbg_debug_instr_queue(struct ccdbg *dbg, uint8_t *instr, int nbytes,
97                         uint8_t *reply)
98 {
99         return ccdbg_cmd_write_queue8(dbg, CC_DEBUG_INSTR(nbytes),
100                                       instr, nbytes, reply);
101 }
102
103 uint8_t
104 ccdbg_step_instr(struct ccdbg *dbg)
105 {
106         return ccdbg_cmd_write_read8(dbg, CC_STEP_INSTR, NULL, 0);
107 }
108
109 uint8_t
110 ccdbg_step_replace(struct ccdbg *dbg, uint8_t *instr, int nbytes)
111 {
112         return ccdbg_cmd_write_read8(dbg, CC_STEP_REPLACE(nbytes), instr, nbytes);
113 }
114
115 uint16_t
116 ccdbg_get_chip_id(struct ccdbg *dbg)
117 {
118         return ccdbg_cmd_write_read16(dbg, CC_GET_CHIP_ID, NULL, 0);
119 }
120
121 /*
122  * Execute a sequence of instructions
123  */
124 uint8_t
125 ccdbg_execute(struct ccdbg *dbg, uint8_t *inst)
126 {
127         uint8_t status = 0;
128         while(inst[0] != 0) {
129                 uint8_t len = inst[0];
130                 int i;
131                 ccdbg_debug(CC_DEBUG_INSTRUCTIONS, "\t%02x", inst[1]);
132                 for (i = 0; i < len - 1; i++)
133                         ccdbg_debug(CC_DEBUG_INSTRUCTIONS, " %02x", inst[i+2]);
134                 ccdbg_debug_instr_queue(dbg, inst+1, len, &status);
135                 for (; i < 3; i++)
136                         ccdbg_debug(CC_DEBUG_INSTRUCTIONS, "   ");
137                 ccdbg_debug(CC_DEBUG_INSTRUCTIONS, " -> %02x\n", status);
138                 inst += len + 1;
139         }
140         ccdbg_sync(dbg);
141         return status;
142 }
143
144 static uint8_t jump_mem[] = {
145         3, LJMP, 0xf0, 0x00,
146 #define PC_HIGH 2
147 #define PC_LOW  3
148         0
149 };
150
151 uint8_t
152 ccdbg_set_pc(struct ccdbg *dbg, uint16_t pc)
153 {
154         jump_mem[PC_HIGH] = pc >> 8;
155         jump_mem[PC_LOW] = pc & 0xff;
156         return ccdbg_execute(dbg, jump_mem);
157 }
158
159 uint8_t
160 ccdbg_execute_hex_image(struct ccdbg *dbg, struct hex_image *image)
161 {
162         uint16_t pc;
163         uint8_t status;
164
165         if (image->address < 0xf000) {
166                 fprintf(stderr, "Cannot execute program starting at 0x%04x\n", image->address);
167                 return -1;
168         }
169         ccdbg_write_hex_image(dbg, image, 0);
170         ccdbg_set_pc(dbg, image->address);
171         pc = ccdbg_get_pc(dbg);
172         ccdbg_debug(CC_DEBUG_EXECUTE, "pc starts at 0x%04x\n", pc);
173         status = ccdbg_resume(dbg);
174         ccdbg_debug(CC_DEBUG_EXECUTE, "resume status: 0x%02x\n", status);
175         return 0;
176 }