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