Remove redundant sys/types.h #include directives (now in types.h).
[openocd.git] / 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 "replacements.h"
25
26 #include "avrt.h"
27
28 #include "register.h"
29 #include "target.h"
30 #include "log.h"
31 #include "jtag.h"
32 #include "binarybuffer.h"
33 #include "time_support.h"
34 #include "breakpoints.h"
35 #include "fileio.h"
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <unistd.h>
41 #include <errno.h>
42
43 #define AVR_JTAG_INS_LEN 4
44
45 /* cli handling */
46 int avr_register_commands(struct command_context_s *cmd_ctx);
47
48 /* forward declarations */
49 int avr_target_create(struct target_s *target, Jim_Interp *interp);
50 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
51 int avr_quit(void);
52
53 int avr_arch_state(struct target_s *target);
54 int avr_poll(target_t *target);
55 int avr_halt(target_t *target);
56 int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);
57 int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints);
58
59 int avr_assert_reset(target_t *target);
60 int avr_deassert_reset(target_t *target);
61 int avr_soft_reset_halt(struct target_s *target);
62
63 /* IR and DR functions */
64 int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);
65 int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);
66
67 int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);
68 int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti);
69 int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);
70 int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);
71 int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);
72 int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);
73 int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);
74 int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);
75 int mcu_execute_queue(void);
76
77 target_type_t avr_target =
78 {
79 .name = "avr",
80
81 .poll = avr_poll,
82 .arch_state = avr_arch_state,
83
84 .target_request_data = NULL,
85
86 .halt = avr_halt,
87 .resume = avr_resume,
88 .step = avr_step,
89
90 .assert_reset = avr_assert_reset,
91 .deassert_reset = avr_deassert_reset,
92 .soft_reset_halt = avr_soft_reset_halt,
93 /*
94 .get_gdb_reg_list = avr_get_gdb_reg_list,
95
96 .read_memory = avr_read_memory,
97 .write_memory = avr_write_memory,
98 .bulk_write_memory = avr_bulk_write_memory,
99 .checksum_memory = avr_checksum_memory,
100 .blank_check_memory = avr_blank_check_memory,
101
102 .run_algorithm = avr_run_algorithm,
103
104 .add_breakpoint = avr_add_breakpoint,
105 .remove_breakpoint = avr_remove_breakpoint,
106 .add_watchpoint = avr_add_watchpoint,
107 .remove_watchpoint = avr_remove_watchpoint,
108 */
109 .register_commands = avr_register_commands,
110 .target_create = avr_target_create,
111 .init_target = avr_init_target,
112 .quit = avr_quit,
113 /*
114 .virt2phys = avr_virt2phys,
115 .mmu = avr_mmu
116 */
117 };
118
119 int avr_register_commands(struct command_context_s *cmd_ctx)
120 {
121 LOG_DEBUG("%s", __FUNCTION__);
122 return ERROR_OK;
123 }
124
125 int avr_target_create(struct target_s *target, Jim_Interp *interp)
126 {
127 avr_common_t *avr = calloc(1, sizeof(avr_common_t));
128
129 avr->jtag_info.tap = target->tap;
130 target->arch_info = avr;
131
132 return ERROR_OK;
133 }
134
135 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
136 {
137 LOG_DEBUG("%s", __FUNCTION__);
138 return ERROR_OK;
139 }
140
141 int avr_quit(void)
142 {
143 LOG_DEBUG("%s", __FUNCTION__);
144 return ERROR_OK;
145 }
146
147 int avr_arch_state(struct target_s *target)
148 {
149 LOG_DEBUG("%s", __FUNCTION__);
150 return ERROR_OK;
151 }
152
153 int avr_poll(target_t *target)
154 {
155 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
156 {
157 target->state = TARGET_HALTED;
158 }
159
160 LOG_DEBUG("%s", __FUNCTION__);
161 return ERROR_OK;
162 }
163
164 int avr_halt(target_t *target)
165 {
166 LOG_DEBUG("%s", __FUNCTION__);
167 return ERROR_OK;
168 }
169
170 int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
171 {
172 LOG_DEBUG("%s", __FUNCTION__);
173 return ERROR_OK;
174 }
175
176 int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
177 {
178 LOG_DEBUG("%s", __FUNCTION__);
179 return ERROR_OK;
180 }
181
182 int avr_assert_reset(target_t *target)
183 {
184 target->state = TARGET_RESET;
185
186 LOG_DEBUG("%s", __FUNCTION__);
187 return ERROR_OK;
188 }
189
190 int avr_deassert_reset(target_t *target)
191 {
192 target->state = TARGET_RUNNING;
193
194 LOG_DEBUG("%s", __FUNCTION__);
195 return ERROR_OK;
196 }
197
198 int avr_soft_reset_halt(struct target_s *target)
199 {
200 LOG_DEBUG("%s", __FUNCTION__);
201 return ERROR_OK;
202 }
203
204 int avr_jtag_senddat(jtag_tap_t *tap, u32* dr_in, u32 dr_out, int len)
205 {
206 return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
207 }
208
209 int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out)
210 {
211 return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
212 }
213
214 /* IR and DR functions */
215 int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti)
216 {
217 if (NULL == tap)
218 {
219 LOG_ERROR("invalid tap");
220 return ERROR_FAIL;
221 }
222 if (ir_len != tap->ir_length)
223 {
224 LOG_ERROR("invalid ir_len");
225 return ERROR_FAIL;
226 }
227
228 {
229 scan_field_t field[1];
230
231 field[0].tap = tap;
232 field[0].num_bits = tap->ir_length;
233 field[0].out_value = ir_out;
234 field[0].in_value = ir_in;
235 jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
236 }
237
238 return ERROR_OK;
239 }
240
241 int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti)
242 {
243 if (NULL == tap)
244 {
245 LOG_ERROR("invalid tap");
246 return ERROR_FAIL;
247 }
248
249 {
250 scan_field_t field[1];
251
252 field[0].tap = tap;
253 field[0].num_bits = dr_len;
254 field[0].out_value = dr_out;
255 field[0].in_value = dr_in;
256 jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
257 }
258
259 return ERROR_OK;
260 }
261
262 int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti)
263 {
264 if (ir_len > 8)
265 {
266 LOG_ERROR("ir_len overflow, maxium is 8");
267 return ERROR_FAIL;
268 }
269
270 mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
271
272 return ERROR_OK;
273 }
274
275 int mcu_write_dr_u8(jtag_tap_t *tap, u8 *dr_in, u8 dr_out, int dr_len, int rti)
276 {
277 if (dr_len > 8)
278 {
279 LOG_ERROR("dr_len overflow, maxium is 8");
280 return ERROR_FAIL;
281 }
282
283 mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
284
285 return ERROR_OK;
286 }
287
288 int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti)
289 {
290 if (ir_len > 16)
291 {
292 LOG_ERROR("ir_len overflow, maxium is 16");
293 return ERROR_FAIL;
294 }
295
296 mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
297
298 return ERROR_OK;
299 }
300
301 int mcu_write_dr_u16(jtag_tap_t *tap, u16 *dr_in, u16 dr_out, int dr_len, int rti)
302 {
303 if (dr_len > 16)
304 {
305 LOG_ERROR("dr_len overflow, maxium is 16");
306 return ERROR_FAIL;
307 }
308
309 mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
310
311 return ERROR_OK;
312 }
313
314 int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti)
315 {
316 if (ir_len > 32)
317 {
318 LOG_ERROR("ir_len overflow, maxium is 32");
319 return ERROR_FAIL;
320 }
321
322 mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
323
324 return ERROR_OK;
325 }
326
327 int mcu_write_dr_u32(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int dr_len, int rti)
328 {
329 if (dr_len > 32)
330 {
331 LOG_ERROR("dr_len overflow, maxium is 32");
332 return ERROR_FAIL;
333 }
334
335 mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
336
337 return ERROR_OK;
338 }
339
340 int mcu_execute_queue(void)
341 {
342 return jtag_execute_queue();
343 }