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