]> git.sur5r.net Git - openocd/blob - src/jtag/stlink/stlink_interface.c
stlink: enable connect under reset
[openocd] / src / jtag / stlink / stlink_interface.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 /* project specific includes */
26 #include <jtag/interface.h>
27 #include <transport/transport.h>
28 #include <helper/time_support.h>
29
30 #include <jtag/stlink/stlink_tcl.h>
31 #include <jtag/stlink/stlink_layout.h>
32 #include <jtag/stlink/stlink_transport.h>
33 #include <jtag/stlink/stlink_interface.h>
34
35 #include <target/target.h>
36
37 static struct stlink_interface_s stlink_if = { {0, 0, 0, 0, 0, 0}, 0, 0 };
38
39 int stlink_interface_open(enum stlink_transports tr)
40 {
41         LOG_DEBUG("stlink_interface_open");
42
43         /* set transport mode */
44         stlink_if.param.transport = tr;
45
46         int result = stlink_if.layout->open(&stlink_if);
47         if (result != ERROR_OK)
48                 return result;
49
50         return stlink_interface_init_reset();
51 }
52
53 int stlink_interface_init_target(struct target *t)
54 {
55         int res;
56
57         LOG_DEBUG("stlink_interface_init_target");
58
59         /* this is the interface for the current target and we
60          * can setup the private pointer in the tap structure
61          * if the interface match the tap idcode
62          */
63         res = stlink_if.layout->api->idcode(stlink_if.fd, &t->tap->idcode);
64
65         if (res != ERROR_OK)
66                 return res;
67
68         unsigned ii, limit = t->tap->expected_ids_cnt;
69         int found = 0;
70
71         for (ii = 0; ii < limit; ii++) {
72                 uint32_t expected = t->tap->expected_ids[ii];
73
74                 /* treat "-expected-id 0" as a "don't-warn" wildcard */
75                 if (!expected || (t->tap->idcode == expected)) {
76                         found = 1;
77                         break;
78                 }
79         }
80
81         if (found == 0) {
82                 LOG_ERROR("stlink_interface_init_target: target not found: idcode: 0x%08x",
83                                 t->tap->idcode);
84                 return ERROR_FAIL;
85         }
86
87         t->tap->priv = &stlink_if;
88         t->tap->hasidcode = 1;
89
90         return ERROR_OK;
91 }
92
93 static int stlink_interface_init(void)
94 {
95         LOG_DEBUG("stlink_interface_init");
96
97         /* here we can initialize the layout */
98         return stlink_layout_init(&stlink_if);
99 }
100
101 static int stlink_interface_quit(void)
102 {
103         LOG_DEBUG("stlink_interface_quit");
104
105         return ERROR_OK;
106 }
107
108 static int stlink_interface_speed(int speed)
109 {
110         LOG_DEBUG("stlink_interface_speed: ignore speed %d", speed);
111
112         return ERROR_OK;
113 }
114
115 static int stlink_speed_div(int speed, int *khz)
116 {
117         *khz = speed;
118         return ERROR_OK;
119 }
120
121 static int stlink_khz(int khz, int *jtag_speed)
122 {
123         *jtag_speed = khz;
124         return ERROR_OK;
125 }
126
127 static int stlink_interface_execute_queue(void)
128 {
129         LOG_DEBUG("stlink_interface_execute_queue: ignored");
130
131         return ERROR_OK;
132 }
133
134 int stlink_interface_init_reset(void)
135 {
136         enum reset_types jtag_reset_config = jtag_get_reset_config();
137
138         if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
139                 if (jtag_reset_config & RESET_SRST_NO_GATING) {
140                         jtag_add_reset(0, 1);
141                         stlink_if.layout->api->assert_srst(stlink_if.fd, 0);
142                 } else
143                         LOG_WARNING("\'srst_nogate\' reset_config option is required");
144         }
145
146         return ERROR_OK;
147 }
148
149 COMMAND_HANDLER(stlink_interface_handle_device_desc_command)
150 {
151         LOG_DEBUG("stlink_interface_handle_device_desc_command");
152
153         if (CMD_ARGC == 1) {
154                 stlink_if.param.device_desc = strdup(CMD_ARGV[0]);
155         } else {
156                 LOG_ERROR
157                     ("expected exactly one argument to stlink_device_desc <description>");
158         }
159
160         return ERROR_OK;
161 }
162
163 COMMAND_HANDLER(stlink_interface_handle_serial_command)
164 {
165         LOG_DEBUG("stlink_interface_handle_serial_command");
166
167         if (CMD_ARGC == 1) {
168                 stlink_if.param.serial = strdup(CMD_ARGV[0]);
169         } else {
170                 LOG_ERROR
171                     ("expected exactly one argument to stlink_serial <serial-number>");
172         }
173
174         return ERROR_OK;
175 }
176
177 COMMAND_HANDLER(stlink_interface_handle_layout_command)
178 {
179         LOG_DEBUG("stlink_interface_handle_layout_command");
180
181         if (CMD_ARGC != 1) {
182                 LOG_ERROR("Need exactly one argument to stlink_layout");
183                 return ERROR_COMMAND_SYNTAX_ERROR;
184         }
185
186         if (stlink_if.layout) {
187                 LOG_ERROR("already specified stlink_layout %s",
188                           stlink_if.layout->name);
189                 return (strcmp(stlink_if.layout->name, CMD_ARGV[0]) != 0)
190                     ? ERROR_FAIL : ERROR_OK;
191         }
192
193         for (const struct stlink_layout *l = stlink_layout_get_list(); l->name;
194              l++) {
195                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
196                         stlink_if.layout = l;
197                         return ERROR_OK;
198                 }
199         }
200
201         LOG_ERROR("No STLINK layout '%s' found", CMD_ARGV[0]);
202         return ERROR_FAIL;
203 }
204
205 COMMAND_HANDLER(stlink_interface_handle_vid_pid_command)
206 {
207         LOG_DEBUG("stlink_interface_handle_vid_pid_command");
208
209         if (CMD_ARGC != 2) {
210                 LOG_WARNING
211                     ("ignoring extra IDs in stlink_vid_pid (maximum is 1 pair)");
212                 return ERROR_COMMAND_SYNTAX_ERROR;
213         }
214
215         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], stlink_if.param.vid);
216         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_if.param.pid);
217
218         return ERROR_OK;
219 }
220
221 COMMAND_HANDLER(stlink_interface_handle_api_command)
222 {
223         if (CMD_ARGC != 1)
224                 return ERROR_COMMAND_SYNTAX_ERROR;
225
226         unsigned new_api;
227         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], new_api);
228         if ((new_api == 0) || (new_api > 2))
229                 return ERROR_COMMAND_SYNTAX_ERROR;
230
231         stlink_if.param.api = new_api;
232
233         return ERROR_OK;
234 }
235
236 static const struct command_registration stlink_interface_command_handlers[] = {
237         {
238          .name = "stlink_device_desc",
239          .handler = &stlink_interface_handle_device_desc_command,
240          .mode = COMMAND_CONFIG,
241          .help = "set the stlink device description of the STLINK device",
242          .usage = "description_string",
243          },
244         {
245          .name = "stlink_serial",
246          .handler = &stlink_interface_handle_serial_command,
247          .mode = COMMAND_CONFIG,
248          .help = "set the serial number of the STLINK device",
249          .usage = "serial_string",
250          },
251         {
252          .name = "stlink_layout",
253          .handler = &stlink_interface_handle_layout_command,
254          .mode = COMMAND_CONFIG,
255          .help = "set the layout of the STLINK to usb or sg",
256          .usage = "layout_name",
257          },
258         {
259          .name = "stlink_vid_pid",
260          .handler = &stlink_interface_handle_vid_pid_command,
261          .mode = COMMAND_CONFIG,
262          .help = "the vendor and product ID of the STLINK device",
263          .usage = "(vid pid)* ",
264          },
265          {
266          .name = "stlink_api",
267          .handler = &stlink_interface_handle_api_command,
268          .mode = COMMAND_CONFIG,
269          .help = "set the desired stlink api level",
270          .usage = "api version 1 or 2",
271          },
272         COMMAND_REGISTRATION_DONE
273 };
274
275 struct jtag_interface stlink_interface = {
276         .name = "stlink",
277         .supported = 0,
278         .commands = stlink_interface_command_handlers,
279         .transports = stlink_transports,
280         .init = stlink_interface_init,
281         .quit = stlink_interface_quit,
282         .speed = stlink_interface_speed,
283         .speed_div = stlink_speed_div,
284         .khz = stlink_khz,
285         .execute_queue = stlink_interface_execute_queue,
286 };