d3e6282bbf44672a7e25e53ab76f51432c93dffc
[fw/openocd] / src / target / avrt.c
1 /***************************************************************************
2  *   Copyright (C) 2009 by Simon Qian                                      *
3  *   SimonQian@SimonQian.com                                               *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "avrt.h"
25 #include "target.h"
26 #include "target_type.h"
27
28
29 #define AVR_JTAG_INS_LEN        4
30
31 /* cli handling */
32 int avr_register_commands(struct command_context_s *cmd_ctx);
33
34 /* forward declarations */
35 int avr_target_create(struct target_s *target, Jim_Interp *interp);
36 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
37
38 int avr_arch_state(struct target_s *target);
39 int avr_poll(target_t *target);
40 int avr_halt(target_t *target);
41 int avr_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution);
42 int avr_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints);
43
44 int avr_assert_reset(target_t *target);
45 int avr_deassert_reset(target_t *target);
46 int avr_soft_reset_halt(struct target_s *target);
47
48 /* IR and DR functions */
49 int avr_jtag_sendinstr(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out);
50 int avr_jtag_senddat(jtag_tap_t *tap, uint32_t *dr_in, uint32_t dr_out, int len);
51
52 int mcu_write_ir(jtag_tap_t *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
53 int mcu_write_dr(jtag_tap_t *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti);
54 int mcu_write_ir_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
55 int mcu_write_dr_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int dr_len, int rti);
56 int mcu_write_ir_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti);
57 int mcu_write_dr_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int dr_len, int rti);
58 int mcu_write_ir_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti);
59 int mcu_write_dr_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
60 int mcu_execute_queue(void);
61
62 target_type_t avr_target =
63 {
64         .name = "avr",
65
66         .poll = avr_poll,
67         .arch_state = avr_arch_state,
68
69         .target_request_data = NULL,
70
71         .halt = avr_halt,
72         .resume = avr_resume,
73         .step = avr_step,
74
75         .assert_reset = avr_assert_reset,
76         .deassert_reset = avr_deassert_reset,
77         .soft_reset_halt = avr_soft_reset_halt,
78 /*
79         .get_gdb_reg_list = avr_get_gdb_reg_list,
80
81         .read_memory = avr_read_memory,
82         .write_memory = avr_write_memory,
83         .bulk_write_memory = avr_bulk_write_memory,
84         .checksum_memory = avr_checksum_memory,
85         .blank_check_memory = avr_blank_check_memory,
86
87         .run_algorithm = avr_run_algorithm,
88
89         .add_breakpoint = avr_add_breakpoint,
90         .remove_breakpoint = avr_remove_breakpoint,
91         .add_watchpoint = avr_add_watchpoint,
92         .remove_watchpoint = avr_remove_watchpoint,
93 */
94         .register_commands = avr_register_commands,
95         .target_create = avr_target_create,
96         .init_target = avr_init_target,
97 /*
98         .virt2phys = avr_virt2phys,
99         .mmu = avr_mmu
100 */
101 };
102
103 int avr_register_commands(struct command_context_s *cmd_ctx)
104 {
105         LOG_DEBUG("%s", __FUNCTION__);
106         return ERROR_OK;
107 }
108
109 int avr_target_create(struct target_s *target, Jim_Interp *interp)
110 {
111         avr_common_t *avr = calloc(1, sizeof(avr_common_t));
112
113         avr->jtag_info.tap = target->tap;
114         target->arch_info = avr;
115
116         return ERROR_OK;
117 }
118
119 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
120 {
121         LOG_DEBUG("%s", __FUNCTION__);
122         return ERROR_OK;
123 }
124
125 int avr_arch_state(struct target_s *target)
126 {
127         LOG_DEBUG("%s", __FUNCTION__);
128         return ERROR_OK;
129 }
130
131 int avr_poll(target_t *target)
132 {
133         if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
134         {
135                 target->state = TARGET_HALTED;
136         }
137
138         LOG_DEBUG("%s", __FUNCTION__);
139         return ERROR_OK;
140 }
141
142 int avr_halt(target_t *target)
143 {
144         LOG_DEBUG("%s", __FUNCTION__);
145         return ERROR_OK;
146 }
147
148 int avr_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
149 {
150         LOG_DEBUG("%s", __FUNCTION__);
151         return ERROR_OK;
152 }
153
154 int avr_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
155 {
156         LOG_DEBUG("%s", __FUNCTION__);
157         return ERROR_OK;
158 }
159
160 int avr_assert_reset(target_t *target)
161 {
162         target->state = TARGET_RESET;
163
164         LOG_DEBUG("%s", __FUNCTION__);
165         return ERROR_OK;
166 }
167
168 int avr_deassert_reset(target_t *target)
169 {
170         target->state = TARGET_RUNNING;
171
172         LOG_DEBUG("%s", __FUNCTION__);
173         return ERROR_OK;
174 }
175
176 int avr_soft_reset_halt(struct target_s *target)
177 {
178         LOG_DEBUG("%s", __FUNCTION__);
179         return ERROR_OK;
180 }
181
182 int avr_jtag_senddat(jtag_tap_t *tap, uint32_t* dr_in, uint32_t dr_out, int len)
183 {
184         return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
185 }
186
187 int avr_jtag_sendinstr(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out)
188 {
189         return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
190 }
191
192 /* IR and DR functions */
193 int mcu_write_ir(jtag_tap_t *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti)
194 {
195         if (NULL == tap)
196         {
197                 LOG_ERROR("invalid tap");
198                 return ERROR_FAIL;
199         }
200         if (ir_len != tap->ir_length)
201         {
202                 LOG_ERROR("invalid ir_len");
203                 return ERROR_FAIL;
204         }
205
206         {
207                 scan_field_t field[1];
208
209                 field[0].tap = tap;
210                 field[0].num_bits = tap->ir_length;
211                 field[0].out_value = ir_out;
212                 field[0].in_value = ir_in;
213                 jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, jtag_set_end_state(TAP_IDLE));
214         }
215
216         return ERROR_OK;
217 }
218
219 int mcu_write_dr(jtag_tap_t *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti)
220 {
221         if (NULL == tap)
222         {
223                 LOG_ERROR("invalid tap");
224                 return ERROR_FAIL;
225         }
226
227         {
228                 scan_field_t field[1];
229
230                 field[0].tap = tap;
231                 field[0].num_bits = dr_len;
232                 field[0].out_value = dr_out;
233                 field[0].in_value = dr_in;
234                 jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, jtag_set_end_state(TAP_IDLE));
235         }
236
237         return ERROR_OK;
238 }
239
240 int mcu_write_ir_u8(jtag_tap_t *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti)
241 {
242         if (ir_len > 8)
243         {
244                 LOG_ERROR("ir_len overflow, maxium is 8");
245                 return ERROR_FAIL;
246         }
247
248         mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
249
250         return ERROR_OK;
251 }
252
253 int mcu_write_dr_u8(jtag_tap_t *tap, uint8_t *dr_in, uint8_t dr_out, int dr_len, int rti)
254 {
255         if (dr_len > 8)
256         {
257                 LOG_ERROR("dr_len overflow, maxium is 8");
258                 return ERROR_FAIL;
259         }
260
261         mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
262
263         return ERROR_OK;
264 }
265
266 int mcu_write_ir_u16(jtag_tap_t *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti)
267 {
268         if (ir_len > 16)
269         {
270                 LOG_ERROR("ir_len overflow, maxium is 16");
271                 return ERROR_FAIL;
272         }
273
274         mcu_write_ir(tap, (uint8_t*)ir_in, (uint8_t*)&ir_out, ir_len, rti);
275
276         return ERROR_OK;
277 }
278
279 int mcu_write_dr_u16(jtag_tap_t *tap, uint16_t *dr_in, uint16_t dr_out, int dr_len, int rti)
280 {
281         if (dr_len > 16)
282         {
283                 LOG_ERROR("dr_len overflow, maxium is 16");
284                 return ERROR_FAIL;
285         }
286
287         mcu_write_dr(tap, (uint8_t*)dr_in, (uint8_t*)&dr_out, dr_len, rti);
288
289         return ERROR_OK;
290 }
291
292 int mcu_write_ir_u32(jtag_tap_t *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti)
293 {
294         if (ir_len > 32)
295         {
296                 LOG_ERROR("ir_len overflow, maxium is 32");
297                 return ERROR_FAIL;
298         }
299
300         mcu_write_ir(tap, (uint8_t*)ir_in, (uint8_t*)&ir_out, ir_len, rti);
301
302         return ERROR_OK;
303 }
304
305 int mcu_write_dr_u32(jtag_tap_t *tap, uint32_t *dr_in, uint32_t dr_out, int dr_len, int rti)
306 {
307         if (dr_len > 32)
308         {
309                 LOG_ERROR("dr_len overflow, maxium is 32");
310                 return ERROR_FAIL;
311         }
312
313         mcu_write_dr(tap, (uint8_t*)dr_in, (uint8_t*)&dr_out, dr_len, rti);
314
315         return ERROR_OK;
316 }
317
318 int mcu_execute_queue(void)
319 {
320         return jtag_execute_queue();
321 }