Piotr Esden-Tempski <piotr@esden.net> Mac OS X compile fixes
[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 "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 <sys/types.h>
41 #include <unistd.h>
42 #include <errno.h>
43
44 #define AVR_JTAG_INS_LEN                                                        4
45
46 /* cli handling */
47 int avr_register_commands(struct command_context_s *cmd_ctx);
48
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);
52 int avr_quit(void);
53
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);
59
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);
63
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);
67
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);
77
78 target_type_t avr_target =
79 {
80         .name = "avr",
81
82         .poll = avr_poll,
83         .arch_state = avr_arch_state,
84
85         .target_request_data = NULL,
86
87         .halt = avr_halt,
88         .resume = avr_resume,
89         .step = avr_step,
90
91         .assert_reset = avr_assert_reset,
92         .deassert_reset = avr_deassert_reset,
93         .soft_reset_halt = avr_soft_reset_halt,
94 /*
95         .get_gdb_reg_list = avr_get_gdb_reg_list,
96
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,
102
103         .run_algorithm = avr_run_algorithm,
104
105         .add_breakpoint = avr_add_breakpoint,
106         .remove_breakpoint = avr_remove_breakpoint,
107         .add_watchpoint = avr_add_watchpoint,
108         .remove_watchpoint = avr_remove_watchpoint,
109 */
110         .register_commands = avr_register_commands,
111         .target_create = avr_target_create,
112         .init_target = avr_init_target,
113         .quit = avr_quit,
114 /*
115         .virt2phys = avr_virt2phys,
116         .mmu = avr_mmu
117 */
118 };
119
120 int avr_register_commands(struct command_context_s *cmd_ctx)
121 {
122         LOG_DEBUG("%s", __FUNCTION__);
123         return ERROR_OK;
124 }
125
126 int avr_target_create(struct target_s *target, Jim_Interp *interp)
127 {
128         avr_common_t *avr = calloc(1, sizeof(avr_common_t));
129         
130         avr->jtag_info.tap = target->tap;
131         target->arch_info = avr;
132         
133         return ERROR_OK;
134 }
135
136 int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
137 {
138         LOG_DEBUG("%s", __FUNCTION__);
139         return ERROR_OK;
140 }
141
142 int avr_quit(void)
143 {
144         LOG_DEBUG("%s", __FUNCTION__);
145         return ERROR_OK;
146 }
147
148 int avr_arch_state(struct target_s *target)
149 {
150         LOG_DEBUG("%s", __FUNCTION__);
151         return ERROR_OK;
152 }
153
154 int avr_poll(target_t *target)
155 {
156         if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
157         {
158                 target->state = TARGET_HALTED;
159         }
160         
161         LOG_DEBUG("%s", __FUNCTION__);
162         return ERROR_OK;
163 }
164
165 int avr_halt(target_t *target)
166 {
167         LOG_DEBUG("%s", __FUNCTION__);
168         return ERROR_OK;
169 }
170
171 int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
172 {
173         LOG_DEBUG("%s", __FUNCTION__);
174         return ERROR_OK;
175 }
176
177 int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
178 {
179         LOG_DEBUG("%s", __FUNCTION__);
180         return ERROR_OK;
181 }
182
183 int avr_assert_reset(target_t *target)
184 {
185         target->state = TARGET_RESET;
186         
187         LOG_DEBUG("%s", __FUNCTION__);
188         return ERROR_OK;
189 }
190
191 int avr_deassert_reset(target_t *target)
192 {
193         target->state = TARGET_RUNNING;
194         
195         LOG_DEBUG("%s", __FUNCTION__);
196         return ERROR_OK;
197 }
198
199 int avr_soft_reset_halt(struct target_s *target)
200 {
201         LOG_DEBUG("%s", __FUNCTION__);
202         return ERROR_OK;
203 }
204
205 int avr_jtag_senddat(jtag_tap_t *tap, u32* dr_in, u32 dr_out, int len)
206 {
207         return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
208 }
209
210 int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out)
211 {
212         return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
213 }
214
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)
217 {
218         if (NULL == tap)
219         {
220                 LOG_ERROR("invalid tap");
221                 return ERROR_FAIL;
222         }
223         if (ir_len != tap->ir_length)
224         {
225                 LOG_ERROR("invalid ir_len");
226                 return ERROR_FAIL;
227         }
228         
229         {
230                 scan_field_t field[1];
231                 
232                 field[0].tap = tap;
233                 field[0].num_bits = tap->ir_length;
234                 field[0].out_value = ir_out;
235                 field[0].out_mask = NULL;
236                 field[0].in_value = ir_in;
237                 field[0].in_check_value = NULL;
238                 field[0].in_check_mask = NULL;
239                 field[0].in_handler = NULL;
240                 field[0].in_handler_priv = NULL;
241                 jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
242         }
243         
244         return ERROR_OK;
245 }
246
247 int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti)
248 {
249         if (NULL == tap)
250         {
251                 LOG_ERROR("invalid tap");
252                 return ERROR_FAIL;
253         }
254         
255         {
256                 scan_field_t field[1];
257                 
258                 field[0].tap = tap;
259                 field[0].num_bits = dr_len;
260                 field[0].out_value = dr_out;
261                 field[0].out_mask = NULL;
262                 field[0].in_value = dr_in;
263                 field[0].in_check_value = NULL;
264                 field[0].in_check_mask = NULL;
265                 field[0].in_handler = NULL;
266                 field[0].in_handler_priv = NULL;
267                 jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
268         }
269         
270         return ERROR_OK;
271 }
272
273 int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti)
274 {
275         if (ir_len > 8)
276         {
277                 LOG_ERROR("ir_len overflow, maxium is 8");
278                 return ERROR_FAIL;
279         }
280         
281         mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
282         
283         return ERROR_OK;
284 }
285
286 int mcu_write_dr_u8(jtag_tap_t *tap, u8 *dr_in, u8 dr_out, int dr_len, int rti)
287 {
288         if (dr_len > 8)
289         {
290                 LOG_ERROR("dr_len overflow, maxium is 8");
291                 return ERROR_FAIL;
292         }
293         
294         mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
295         
296         return ERROR_OK;
297 }
298
299 int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti)
300 {
301         if (ir_len > 16)
302         {
303                 LOG_ERROR("ir_len overflow, maxium is 16");
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_u16(jtag_tap_t *tap, u16 *dr_in, u16 dr_out, int dr_len, int rti)
313 {
314         if (dr_len > 16)
315         {
316                 LOG_ERROR("dr_len overflow, maxium is 16");
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_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti)
326 {
327         if (ir_len > 32)
328         {
329                 LOG_ERROR("ir_len overflow, maxium is 32");
330                 return ERROR_FAIL;
331         }
332         
333         mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
334         
335         return ERROR_OK;
336 }
337
338 int mcu_write_dr_u32(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int dr_len, int rti)
339 {
340         if (dr_len > 32)
341         {
342                 LOG_ERROR("dr_len overflow, maxium is 32");
343                 return ERROR_FAIL;
344         }
345         
346         mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
347         
348         return ERROR_OK;
349 }
350
351 int mcu_execute_queue(void)
352 {
353         return jtag_execute_queue();
354 }