1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian *
3 * SimonQian@SimonQian.com *
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. *
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. *
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 ***************************************************************************/
24 #include "replacements.h"
32 #include "binarybuffer.h"
33 #include "time_support.h"
34 #include "breakpoints.h"
40 #include <sys/types.h>
44 #define AVR_JTAG_INS_LEN 4
47 int avr_register_commands(struct command_context_s *cmd_ctx);
49 /* forward declarations */
50 int avr_target_create(struct target_s *target, Jim_Interp *interp);
51 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
54 int avr_arch_state(struct target_s *target);
55 int avr_poll(target_t *target);
56 int avr_halt(target_t *target);
57 int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);
58 int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints);
60 int avr_assert_reset(target_t *target);
61 int avr_deassert_reset(target_t *target);
62 int avr_soft_reset_halt(struct target_s *target);
64 /* IR and DR functions */
65 int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);
66 int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);
68 int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);
69 int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti);
70 int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);
71 int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);
72 int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);
73 int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);
74 int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);
75 int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);
76 int mcu_execute_queue(void);
78 target_type_t avr_target =
83 .arch_state = avr_arch_state,
85 .target_request_data = NULL,
91 .assert_reset = avr_assert_reset,
92 .deassert_reset = avr_deassert_reset,
93 .soft_reset_halt = avr_soft_reset_halt,
95 .get_gdb_reg_list = avr_get_gdb_reg_list,
97 .read_memory = avr_read_memory,
98 .write_memory = avr_write_memory,
99 .bulk_write_memory = avr_bulk_write_memory,
100 .checksum_memory = avr_checksum_memory,
101 .blank_check_memory = avr_blank_check_memory,
103 .run_algorithm = avr_run_algorithm,
105 .add_breakpoint = avr_add_breakpoint,
106 .remove_breakpoint = avr_remove_breakpoint,
107 .add_watchpoint = avr_add_watchpoint,
108 .remove_watchpoint = avr_remove_watchpoint,
110 .register_commands = avr_register_commands,
111 .target_create = avr_target_create,
112 .init_target = avr_init_target,
115 .virt2phys = avr_virt2phys,
120 int avr_register_commands(struct command_context_s *cmd_ctx)
122 LOG_DEBUG("%s", __FUNCTION__);
126 int avr_target_create(struct target_s *target, Jim_Interp *interp)
128 avr_common_t *avr = calloc(1, sizeof(avr_common_t));
130 avr->jtag_info.tap = target->tap;
131 target->arch_info = avr;
136 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
138 LOG_DEBUG("%s", __FUNCTION__);
144 LOG_DEBUG("%s", __FUNCTION__);
148 int avr_arch_state(struct target_s *target)
150 LOG_DEBUG("%s", __FUNCTION__);
154 int avr_poll(target_t *target)
156 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
158 target->state = TARGET_HALTED;
161 LOG_DEBUG("%s", __FUNCTION__);
165 int avr_halt(target_t *target)
167 LOG_DEBUG("%s", __FUNCTION__);
171 int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
173 LOG_DEBUG("%s", __FUNCTION__);
177 int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
179 LOG_DEBUG("%s", __FUNCTION__);
183 int avr_assert_reset(target_t *target)
185 target->state = TARGET_RESET;
187 LOG_DEBUG("%s", __FUNCTION__);
191 int avr_deassert_reset(target_t *target)
193 target->state = TARGET_RUNNING;
195 LOG_DEBUG("%s", __FUNCTION__);
199 int avr_soft_reset_halt(struct target_s *target)
201 LOG_DEBUG("%s", __FUNCTION__);
205 int avr_jtag_senddat(jtag_tap_t *tap, u32* dr_in, u32 dr_out, int len)
207 return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
210 int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out)
212 return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
215 /* IR and DR functions */
216 int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti)
220 LOG_ERROR("invalid tap");
223 if (ir_len != tap->ir_length)
225 LOG_ERROR("invalid ir_len");
230 scan_field_t field[1];
233 field[0].num_bits = tap->ir_length;
234 field[0].out_value = ir_out;
235 field[0].in_value = ir_in;
236 field[0].in_check_value = NULL;
237 field[0].in_check_mask = NULL;
238 field[0].in_handler = NULL;
239 field[0].in_handler_priv = NULL;
240 jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
246 int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti)
250 LOG_ERROR("invalid tap");
255 scan_field_t field[1];
258 field[0].num_bits = dr_len;
259 field[0].out_value = dr_out;
260 field[0].in_value = dr_in;
261 field[0].in_check_value = NULL;
262 field[0].in_check_mask = NULL;
263 field[0].in_handler = NULL;
264 field[0].in_handler_priv = NULL;
265 jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
271 int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti)
275 LOG_ERROR("ir_len overflow, maxium is 8");
279 mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
284 int mcu_write_dr_u8(jtag_tap_t *tap, u8 *dr_in, u8 dr_out, int dr_len, int rti)
288 LOG_ERROR("dr_len overflow, maxium is 8");
292 mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
297 int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti)
301 LOG_ERROR("ir_len overflow, maxium is 16");
305 mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
310 int mcu_write_dr_u16(jtag_tap_t *tap, u16 *dr_in, u16 dr_out, int dr_len, int rti)
314 LOG_ERROR("dr_len overflow, maxium is 16");
318 mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
323 int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti)
327 LOG_ERROR("ir_len overflow, maxium is 32");
331 mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
336 int mcu_write_dr_u32(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int dr_len, int rti)
340 LOG_ERROR("dr_len overflow, maxium is 32");
344 mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
349 int mcu_execute_queue(void)
351 return jtag_execute_queue();