cortex_m: Fix single stepping will not return to debug mode sometimes
[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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "avrt.h"
26 #include "target.h"
27 #include "target_type.h"
28
29 #define AVR_JTAG_INS_LEN        4
30
31 /* forward declarations */
32 static int avr_target_create(struct target *target, Jim_Interp *interp);
33 static int avr_init_target(struct command_context *cmd_ctx, struct target *target);
34
35 static int avr_arch_state(struct target *target);
36 static int avr_poll(struct target *target);
37 static int avr_halt(struct target *target);
38 static int avr_resume(struct target *target, int current, uint32_t address,
39                 int handle_breakpoints, int debug_execution);
40 static int avr_step(struct target *target, int current, uint32_t address,
41                 int handle_breakpoints);
42
43 static int avr_assert_reset(struct target *target);
44 static int avr_deassert_reset(struct target *target);
45 static int avr_soft_reset_halt(struct target *target);
46
47 /* IR and DR functions */
48 static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
49 static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti);
50 static int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
51 static int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
52
53 struct target_type avr_target = {
54         .name = "avr",
55
56         .poll = avr_poll,
57         .arch_state = avr_arch_state,
58
59         .target_request_data = NULL,
60
61         .halt = avr_halt,
62         .resume = avr_resume,
63         .step = avr_step,
64
65         .assert_reset = avr_assert_reset,
66         .deassert_reset = avr_deassert_reset,
67         .soft_reset_halt = avr_soft_reset_halt,
68 /*
69         .get_gdb_reg_list = avr_get_gdb_reg_list,
70
71         .read_memory = avr_read_memory,
72         .write_memory = avr_write_memory,
73         .bulk_write_memory = avr_bulk_write_memory,
74         .checksum_memory = avr_checksum_memory,
75         .blank_check_memory = avr_blank_check_memory,
76
77         .run_algorithm = avr_run_algorithm,
78
79         .add_breakpoint = avr_add_breakpoint,
80         .remove_breakpoint = avr_remove_breakpoint,
81         .add_watchpoint = avr_add_watchpoint,
82         .remove_watchpoint = avr_remove_watchpoint,
83 */
84         .target_create = avr_target_create,
85         .init_target = avr_init_target,
86 };
87
88 static int avr_target_create(struct target *target, Jim_Interp *interp)
89 {
90         struct avr_common *avr = calloc(1, sizeof(struct avr_common));
91
92         avr->jtag_info.tap = target->tap;
93         target->arch_info = avr;
94
95         return ERROR_OK;
96 }
97
98 static int avr_init_target(struct command_context *cmd_ctx, struct target *target)
99 {
100         LOG_DEBUG("%s", __func__);
101         return ERROR_OK;
102 }
103
104 static int avr_arch_state(struct target *target)
105 {
106         LOG_DEBUG("%s", __func__);
107         return ERROR_OK;
108 }
109
110 static int avr_poll(struct target *target)
111 {
112         if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
113                 target->state = TARGET_HALTED;
114
115         LOG_DEBUG("%s", __func__);
116         return ERROR_OK;
117 }
118
119 static int avr_halt(struct target *target)
120 {
121         LOG_DEBUG("%s", __func__);
122         return ERROR_OK;
123 }
124
125 static int avr_resume(struct target *target, int current, uint32_t address,
126                 int handle_breakpoints, int debug_execution)
127 {
128         LOG_DEBUG("%s", __func__);
129         return ERROR_OK;
130 }
131
132 static int avr_step(struct target *target, int current, uint32_t address, int handle_breakpoints)
133 {
134         LOG_DEBUG("%s", __func__);
135         return ERROR_OK;
136 }
137
138 static int avr_assert_reset(struct target *target)
139 {
140         target->state = TARGET_RESET;
141
142         LOG_DEBUG("%s", __func__);
143         return ERROR_OK;
144 }
145
146 static int avr_deassert_reset(struct target *target)
147 {
148         target->state = TARGET_RUNNING;
149
150         LOG_DEBUG("%s", __func__);
151         return ERROR_OK;
152 }
153
154 static int avr_soft_reset_halt(struct target *target)
155 {
156         LOG_DEBUG("%s", __func__);
157         return ERROR_OK;
158 }
159
160 int avr_jtag_senddat(struct jtag_tap *tap, uint32_t* dr_in, uint32_t dr_out,
161                 int len)
162 {
163         return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
164 }
165
166 int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
167 {
168         return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
169 }
170
171 /* IR and DR functions */
172 static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out,
173                 int ir_len, int rti)
174 {
175         if (NULL == tap) {
176                 LOG_ERROR("invalid tap");
177                 return ERROR_FAIL;
178         }
179         if (ir_len != tap->ir_length) {
180                 LOG_ERROR("invalid ir_len");
181                 return ERROR_FAIL;
182         }
183
184         {
185                 jtag_add_plain_ir_scan(tap->ir_length, ir_out, ir_in, TAP_IDLE);
186         }
187
188         return ERROR_OK;
189 }
190
191 static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out,
192                 int dr_len, int rti)
193 {
194         if (NULL == tap) {
195                 LOG_ERROR("invalid tap");
196                 return ERROR_FAIL;
197         }
198
199         {
200                 jtag_add_plain_dr_scan(dr_len, dr_out, dr_in, TAP_IDLE);
201         }
202
203         return ERROR_OK;
204 }
205
206 static int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in,
207                 uint8_t ir_out, int ir_len, int rti)
208 {
209         if (ir_len > 8) {
210                 LOG_ERROR("ir_len overflow, maxium is 8");
211                 return ERROR_FAIL;
212         }
213
214         mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
215
216         return ERROR_OK;
217 }
218
219 static int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *dr_in,
220                 uint32_t dr_out, int dr_len, int rti)
221 {
222         if (dr_len > 32) {
223                 LOG_ERROR("dr_len overflow, maxium is 32");
224                 return ERROR_FAIL;
225         }
226
227         mcu_write_dr(tap, (uint8_t *)dr_in, (uint8_t *)&dr_out, dr_len, rti);
228
229         return ERROR_OK;
230 }
231
232 int mcu_execute_queue(void)
233 {
234         return jtag_execute_queue();
235 }