bff2418c9475d803c72f82f8043d07f727a85f74
[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(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out);
50 int avr_jtag_senddat(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out, int len);
51
52 int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
53 int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti);
54 int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
55 int mcu_write_dr_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int dr_len, int rti);
56 int mcu_write_ir_u16(struct jtag_tap *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti);
57 int mcu_write_dr_u16(struct jtag_tap *tap, uint16_t *ir_in, uint16_t ir_out, int dr_len, int rti);
58 int mcu_write_ir_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti);
59 int mcu_write_dr_u32(struct jtag_tap *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
99 int avr_register_commands(struct command_context_s *cmd_ctx)
100 {
101         LOG_DEBUG("%s", __FUNCTION__);
102         return ERROR_OK;
103 }
104
105 int avr_target_create(struct target_s *target, Jim_Interp *interp)
106 {
107         avr_common_t *avr = calloc(1, sizeof(avr_common_t));
108
109         avr->jtag_info.tap = target->tap;
110         target->arch_info = avr;
111
112         return ERROR_OK;
113 }
114
115 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
116 {
117         LOG_DEBUG("%s", __FUNCTION__);
118         return ERROR_OK;
119 }
120
121 int avr_arch_state(struct target_s *target)
122 {
123         LOG_DEBUG("%s", __FUNCTION__);
124         return ERROR_OK;
125 }
126
127 int avr_poll(target_t *target)
128 {
129         if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
130         {
131                 target->state = TARGET_HALTED;
132         }
133
134         LOG_DEBUG("%s", __FUNCTION__);
135         return ERROR_OK;
136 }
137
138 int avr_halt(target_t *target)
139 {
140         LOG_DEBUG("%s", __FUNCTION__);
141         return ERROR_OK;
142 }
143
144 int avr_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
145 {
146         LOG_DEBUG("%s", __FUNCTION__);
147         return ERROR_OK;
148 }
149
150 int avr_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
151 {
152         LOG_DEBUG("%s", __FUNCTION__);
153         return ERROR_OK;
154 }
155
156 int avr_assert_reset(target_t *target)
157 {
158         target->state = TARGET_RESET;
159
160         LOG_DEBUG("%s", __FUNCTION__);
161         return ERROR_OK;
162 }
163
164 int avr_deassert_reset(target_t *target)
165 {
166         target->state = TARGET_RUNNING;
167
168         LOG_DEBUG("%s", __FUNCTION__);
169         return ERROR_OK;
170 }
171
172 int avr_soft_reset_halt(struct target_s *target)
173 {
174         LOG_DEBUG("%s", __FUNCTION__);
175         return ERROR_OK;
176 }
177
178 int avr_jtag_senddat(struct jtag_tap *tap, uint32_t* dr_in, uint32_t dr_out, int len)
179 {
180         return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
181 }
182
183 int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
184 {
185         return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
186 }
187
188 /* IR and DR functions */
189 int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti)
190 {
191         if (NULL == tap)
192         {
193                 LOG_ERROR("invalid tap");
194                 return ERROR_FAIL;
195         }
196         if (ir_len != tap->ir_length)
197         {
198                 LOG_ERROR("invalid ir_len");
199                 return ERROR_FAIL;
200         }
201
202         {
203                 scan_field_t field[1];
204
205                 field[0].tap = tap;
206                 field[0].num_bits = tap->ir_length;
207                 field[0].out_value = ir_out;
208                 field[0].in_value = ir_in;
209                 jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, jtag_set_end_state(TAP_IDLE));
210         }
211
212         return ERROR_OK;
213 }
214
215 int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti)
216 {
217         if (NULL == tap)
218         {
219                 LOG_ERROR("invalid tap");
220                 return ERROR_FAIL;
221         }
222
223         {
224                 scan_field_t field[1];
225
226                 field[0].tap = tap;
227                 field[0].num_bits = dr_len;
228                 field[0].out_value = dr_out;
229                 field[0].in_value = dr_in;
230                 jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, jtag_set_end_state(TAP_IDLE));
231         }
232
233         return ERROR_OK;
234 }
235
236 int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti)
237 {
238         if (ir_len > 8)
239         {
240                 LOG_ERROR("ir_len overflow, maxium is 8");
241                 return ERROR_FAIL;
242         }
243
244         mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
245
246         return ERROR_OK;
247 }
248
249 int mcu_write_dr_u8(struct jtag_tap *tap, uint8_t *dr_in, uint8_t dr_out, int dr_len, int rti)
250 {
251         if (dr_len > 8)
252         {
253                 LOG_ERROR("dr_len overflow, maxium is 8");
254                 return ERROR_FAIL;
255         }
256
257         mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
258
259         return ERROR_OK;
260 }
261
262 int mcu_write_ir_u16(struct jtag_tap *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti)
263 {
264         if (ir_len > 16)
265         {
266                 LOG_ERROR("ir_len overflow, maxium is 16");
267                 return ERROR_FAIL;
268         }
269
270         mcu_write_ir(tap, (uint8_t*)ir_in, (uint8_t*)&ir_out, ir_len, rti);
271
272         return ERROR_OK;
273 }
274
275 int mcu_write_dr_u16(struct jtag_tap *tap, uint16_t *dr_in, uint16_t dr_out, int dr_len, int rti)
276 {
277         if (dr_len > 16)
278         {
279                 LOG_ERROR("dr_len overflow, maxium is 16");
280                 return ERROR_FAIL;
281         }
282
283         mcu_write_dr(tap, (uint8_t*)dr_in, (uint8_t*)&dr_out, dr_len, rti);
284
285         return ERROR_OK;
286 }
287
288 int mcu_write_ir_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti)
289 {
290         if (ir_len > 32)
291         {
292                 LOG_ERROR("ir_len overflow, maxium is 32");
293                 return ERROR_FAIL;
294         }
295
296         mcu_write_ir(tap, (uint8_t*)ir_in, (uint8_t*)&ir_out, ir_len, rti);
297
298         return ERROR_OK;
299 }
300
301 int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out, int dr_len, int rti)
302 {
303         if (dr_len > 32)
304         {
305                 LOG_ERROR("dr_len overflow, maxium is 32");
306                 return ERROR_FAIL;
307         }
308
309         mcu_write_dr(tap, (uint8_t*)dr_in, (uint8_t*)&dr_out, dr_len, rti);
310
311         return ERROR_OK;
312 }
313
314 int mcu_execute_queue(void)
315 {
316         return jtag_execute_queue();
317 }