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