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