]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/stlink_usb.c
stlink: print target voltage if supported
[openocd] / src / jtag / drivers / stlink_usb.c
1 /***************************************************************************
2  *   Copyright (C) 2011-2012 by Mathias Kuester                            *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   Copyright (C) 2012 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
8  *   This code is based on https://github.com/texane/stlink                *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /* project specific includes */
31 #include <helper/binarybuffer.h>
32 #include <jtag/interface.h>
33 #include <jtag/hla/hla_layout.h>
34 #include <jtag/hla/hla_transport.h>
35 #include <jtag/hla/hla_interface.h>
36 #include <target/target.h>
37
38 #include <target/cortex_m.h>
39
40 #include "libusb_common.h"
41
42 #define ENDPOINT_IN  0x80
43 #define ENDPOINT_OUT 0x00
44
45 #define STLINK_NULL_EP     0
46 #define STLINK_RX_EP       (1|ENDPOINT_IN)
47 #define STLINK_TX_EP       (2|ENDPOINT_OUT)
48 #define STLINK_SG_SIZE     (31)
49 #define STLINK_DATA_SIZE   (4*128)
50 #define STLINK_CMD_SIZE_V2 (16)
51 #define STLINK_CMD_SIZE_V1 (10)
52
53 enum stlink_jtag_api_version {
54         STLINK_JTAG_API_V1 = 1,
55         STLINK_JTAG_API_V2,
56 };
57
58 /** */
59 struct stlink_usb_version {
60         /** */
61         int stlink;
62         /** */
63         int jtag;
64         /** */
65         int swim;
66         /** highest supported jtag api version */
67         enum stlink_jtag_api_version jtag_api_max;
68 };
69
70 /** */
71 struct stlink_usb_handle_s {
72         /** */
73         struct jtag_libusb_device_handle *fd;
74         /** */
75         struct libusb_transfer *trans;
76         /** */
77         uint8_t cmdbuf[STLINK_SG_SIZE];
78         /** */
79         uint8_t cmdidx;
80         /** */
81         uint8_t direction;
82         /** */
83         uint8_t databuf[STLINK_DATA_SIZE];
84         /** */
85         enum hl_transports transport;
86         /** */
87         struct stlink_usb_version version;
88         /** */
89         uint16_t vid;
90         /** */
91         uint16_t pid;
92         /** this is the currently used jtag api */
93         enum stlink_jtag_api_version jtag_api;
94 };
95
96 #define STLINK_DEBUG_ERR_OK            0x80
97 #define STLINK_DEBUG_ERR_FAULT         0x81
98 #define STLINK_SWD_AP_WAIT             0x10
99 #define STLINK_SWD_DP_WAIT             0x14
100
101 #define STLINK_CORE_RUNNING            0x80
102 #define STLINK_CORE_HALTED             0x81
103 #define STLINK_CORE_STAT_UNKNOWN       -1
104
105 #define STLINK_GET_VERSION             0xF1
106 #define STLINK_DEBUG_COMMAND           0xF2
107 #define STLINK_DFU_COMMAND             0xF3
108 #define STLINK_SWIM_COMMAND            0xF4
109 #define STLINK_GET_CURRENT_MODE        0xF5
110 #define STLINK_GET_TARGET_VOLTAGE      0xF7
111
112 #define STLINK_DEV_DFU_MODE            0x00
113 #define STLINK_DEV_MASS_MODE           0x01
114 #define STLINK_DEV_DEBUG_MODE          0x02
115 #define STLINK_DEV_SWIM_MODE           0x03
116 #define STLINK_DEV_BOOTLOADER_MODE     0x04
117 #define STLINK_DEV_UNKNOWN_MODE        -1
118
119 #define STLINK_DFU_EXIT                0x07
120
121 #define STLINK_SWIM_ENTER              0x00
122 #define STLINK_SWIM_EXIT               0x01
123
124 #define STLINK_DEBUG_ENTER_JTAG            0x00
125 #define STLINK_DEBUG_GETSTATUS             0x01
126 #define STLINK_DEBUG_FORCEDEBUG            0x02
127 #define STLINK_DEBUG_APIV1_RESETSYS        0x03
128 #define STLINK_DEBUG_APIV1_READALLREGS     0x04
129 #define STLINK_DEBUG_APIV1_READREG         0x05
130 #define STLINK_DEBUG_APIV1_WRITEREG        0x06
131 #define STLINK_DEBUG_READMEM_32BIT         0x07
132 #define STLINK_DEBUG_WRITEMEM_32BIT        0x08
133 #define STLINK_DEBUG_RUNCORE               0x09
134 #define STLINK_DEBUG_STEPCORE              0x0a
135 #define STLINK_DEBUG_APIV1_SETFP           0x0b
136 #define STLINK_DEBUG_READMEM_8BIT          0x0c
137 #define STLINK_DEBUG_WRITEMEM_8BIT         0x0d
138 #define STLINK_DEBUG_APIV1_CLEARFP         0x0e
139 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG   0x0f
140 #define STLINK_DEBUG_APIV1_SETWATCHPOINT   0x10
141
142 #define STLINK_DEBUG_ENTER_JTAG            0x00
143 #define STLINK_DEBUG_ENTER_SWD             0xa3
144
145 #define STLINK_DEBUG_APIV1_ENTER           0x20
146 #define STLINK_DEBUG_EXIT                  0x21
147 #define STLINK_DEBUG_READCOREID            0x22
148
149 #define STLINK_DEBUG_APIV2_ENTER           0x30
150 #define STLINK_DEBUG_APIV2_READ_IDCODES    0x31
151 #define STLINK_DEBUG_APIV2_RESETSYS        0x32
152 #define STLINK_DEBUG_APIV2_READREG         0x33
153 #define STLINK_DEBUG_APIV2_WRITEREG        0x34
154 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG   0x35
155 #define STLINK_DEBUG_APIV2_READDEBUGREG    0x36
156
157 #define STLINK_DEBUG_APIV2_READALLREGS     0x3A
158 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
159 #define STLINK_DEBUG_APIV2_DRIVE_NRST      0x3C
160
161 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW   0x00
162 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH  0x01
163 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
164
165 /** */
166 enum stlink_mode {
167         STLINK_MODE_UNKNOWN = 0,
168         STLINK_MODE_DFU,
169         STLINK_MODE_MASS,
170         STLINK_MODE_DEBUG_JTAG,
171         STLINK_MODE_DEBUG_SWD,
172         STLINK_MODE_DEBUG_SWIM
173 };
174
175 #define REQUEST_SENSE        0x03
176 #define REQUEST_SENSE_LENGTH 18
177
178 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
179
180 /** */
181 static int stlink_usb_xfer_v1_get_status(void *handle)
182 {
183         struct stlink_usb_handle_s *h;
184
185         assert(handle != NULL);
186
187         h = (struct stlink_usb_handle_s *)handle;
188
189         /* read status */
190         memset(h->cmdbuf, 0, STLINK_SG_SIZE);
191
192         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
193                                 13, 1000) != 13)
194                 return ERROR_FAIL;
195
196         uint32_t t1;
197
198         t1 = buf_get_u32(h->cmdbuf, 0, 32);
199
200         /* check for USBS */
201         if (t1 != 0x53425355)
202                 return ERROR_FAIL;
203         /*
204          * CSW status:
205          * 0 success
206          * 1 command failure
207          * 2 phase error
208          */
209         if (h->cmdbuf[12] != 0)
210                 return ERROR_FAIL;
211
212         return ERROR_OK;
213 }
214
215 /** */
216 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
217 {
218         struct stlink_usb_handle_s *h;
219
220         assert(handle != NULL);
221
222         h = (struct stlink_usb_handle_s *)handle;
223
224         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
225                                    1000) != cmdsize) {
226                 return ERROR_FAIL;
227         }
228
229         if (h->direction == STLINK_TX_EP && size) {
230                 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
231                                           size, 1000) != size) {
232                         LOG_DEBUG("bulk write failed");
233                         return ERROR_FAIL;
234                 }
235         } else if (h->direction == STLINK_RX_EP && size) {
236                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
237                                           size, 1000) != size) {
238                         LOG_DEBUG("bulk read failed");
239                         return ERROR_FAIL;
240                 }
241         }
242
243         return ERROR_OK;
244 }
245
246 /** */
247 static int stlink_usb_xfer_v1_get_sense(void *handle)
248 {
249         int res;
250         struct stlink_usb_handle_s *h;
251
252         assert(handle != NULL);
253
254         h = (struct stlink_usb_handle_s *)handle;
255
256         stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
257
258         h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
259         h->cmdbuf[h->cmdidx++] = 0;
260         h->cmdbuf[h->cmdidx++] = 0;
261         h->cmdbuf[h->cmdidx++] = 0;
262         h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
263
264         res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
265
266         if (res != ERROR_OK)
267                 return res;
268
269         if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
270                 return ERROR_FAIL;
271
272         return ERROR_OK;
273 }
274
275 /** */
276 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
277 {
278         int err, cmdsize = STLINK_CMD_SIZE_V2;
279         struct stlink_usb_handle_s *h;
280
281         assert(handle != NULL);
282
283         h = (struct stlink_usb_handle_s *)handle;
284
285         if (h->version.stlink == 1)
286                 cmdsize = STLINK_SG_SIZE;
287
288         err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
289
290         if (err != ERROR_OK)
291                 return err;
292
293         if (h->version.stlink == 1) {
294                 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
295                         /* check csw status */
296                         if (h->cmdbuf[12] == 1) {
297                                 LOG_DEBUG("get sense");
298                                 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
299                                         return ERROR_FAIL;
300                         }
301                         return ERROR_FAIL;
302                 }
303         }
304
305         return ERROR_OK;
306 }
307
308 /** */
309 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
310 {
311         struct stlink_usb_handle_s *h;
312
313         h = (struct stlink_usb_handle_s *)handle;
314
315         /* fill the send buffer */
316         strcpy((char *)h->cmdbuf, "USBC");
317         h->cmdidx += 4;
318         /* csw tag not used */
319         h->cmdidx += 4;
320         buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
321         h->cmdidx += 4;
322         h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
323         h->cmdbuf[h->cmdidx++] = 0; /* lun */
324         h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
325 }
326
327 /** */
328 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
329 {
330         struct stlink_usb_handle_s *h;
331
332         h = (struct stlink_usb_handle_s *)handle;
333
334         h->direction = direction;
335
336         h->cmdidx = 0;
337
338         memset(h->cmdbuf, 0, STLINK_SG_SIZE);
339         memset(h->databuf, 0, STLINK_DATA_SIZE);
340
341         if (h->version.stlink == 1)
342                 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
343 }
344
345 static const char * const stlink_usb_error_msg[] = {
346         "unknown"
347 };
348
349 /** */
350 static int stlink_usb_error_check(void *handle)
351 {
352         int res;
353         const char *err_msg = 0;
354         struct stlink_usb_handle_s *h;
355
356         assert(handle != NULL);
357
358         h = (struct stlink_usb_handle_s *)handle;
359
360         /* TODO: no error checking yet on api V1 */
361         if (h->jtag_api == STLINK_JTAG_API_V1)
362                 h->databuf[0] = STLINK_DEBUG_ERR_OK;
363
364         switch (h->databuf[0]) {
365                 case STLINK_DEBUG_ERR_OK:
366                         res = ERROR_OK;
367                         break;
368                 case STLINK_DEBUG_ERR_FAULT:
369                 default:
370                         err_msg = stlink_usb_error_msg[0];
371                         res = ERROR_FAIL;
372                         break;
373         }
374
375         if (res != ERROR_OK)
376                 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
377
378         return res;
379 }
380
381 /** */
382 static int stlink_usb_version(void *handle)
383 {
384         int res;
385         uint16_t v;
386         struct stlink_usb_handle_s *h;
387
388         assert(handle != NULL);
389
390         h = (struct stlink_usb_handle_s *)handle;
391
392         stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
393
394         h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
395
396         res = stlink_usb_xfer(handle, h->databuf, 6);
397
398         if (res != ERROR_OK)
399                 return res;
400
401         v = (h->databuf[0] << 8) | h->databuf[1];
402
403         h->version.stlink = (v >> 12) & 0x0f;
404         h->version.jtag = (v >> 6) & 0x3f;
405         h->version.swim = v & 0x3f;
406         h->vid = buf_get_u32(h->databuf, 16, 16);
407         h->pid = buf_get_u32(h->databuf, 32, 16);
408
409         /* set the supported jtag api version
410          * API V2 is supported since JTAG V11
411          */
412         if (h->version.jtag >= 11)
413                 h->version.jtag_api_max = STLINK_JTAG_API_V2;
414         else
415                 h->version.jtag_api_max = STLINK_JTAG_API_V1;
416
417         LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
418                 h->version.stlink,
419                 h->version.jtag,
420                 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
421                 h->version.swim,
422                 h->vid,
423                 h->pid);
424
425         return ERROR_OK;
426 }
427
428 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
429 {
430         struct stlink_usb_handle_s *h;
431         uint32_t adc_results[2];
432
433         h = (struct stlink_usb_handle_s *)handle;
434
435         /* only supported by stlink/v2 and for firmware >= 13 */
436         if (h->version.stlink == 1 || h->version.jtag < 13)
437                 return ERROR_COMMAND_NOTFOUND;
438
439         stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
440
441         h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
442
443         int result = stlink_usb_xfer(handle, h->databuf, 8);
444
445         if (result != ERROR_OK)
446                 return result;
447
448         /* convert result */
449         adc_results[0] = le_to_h_u32(h->databuf);
450         adc_results[1] = le_to_h_u32(h->databuf + 4);
451
452         *target_voltage = 0;
453
454         if (adc_results[0])
455                 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
456
457         LOG_INFO("Target voltage: %f", (double)*target_voltage);
458
459         return ERROR_OK;
460 }
461
462 /** */
463 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
464 {
465         int res;
466         struct stlink_usb_handle_s *h;
467
468         assert(handle != NULL);
469
470         h = (struct stlink_usb_handle_s *)handle;
471
472         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
473
474         h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
475
476         res = stlink_usb_xfer(handle, h->databuf, 2);
477
478         if (res != ERROR_OK)
479                 return res;
480
481         *mode = h->databuf[0];
482
483         return ERROR_OK;
484 }
485
486 /** */
487 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
488 {
489         int res;
490         int rx_size = 0;
491         struct stlink_usb_handle_s *h;
492
493         assert(handle != NULL);
494
495         h = (struct stlink_usb_handle_s *)handle;
496
497         /* on api V2 we are able the read the latest command
498          * status
499          * TODO: we need the test on api V1 too
500          */
501         if (h->jtag_api == STLINK_JTAG_API_V2)
502                 rx_size = 2;
503
504         stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
505
506         switch (type) {
507                 case STLINK_MODE_DEBUG_JTAG:
508                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
509                         if (h->jtag_api == STLINK_JTAG_API_V1)
510                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
511                         else
512                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
513                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
514                         break;
515                 case STLINK_MODE_DEBUG_SWD:
516                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
517                         if (h->jtag_api == STLINK_JTAG_API_V1)
518                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
519                         else
520                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
521                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
522                         break;
523                 case STLINK_MODE_DEBUG_SWIM:
524                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
525                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
526                         break;
527                 case STLINK_MODE_DFU:
528                 case STLINK_MODE_MASS:
529                 default:
530                         return ERROR_FAIL;
531         }
532
533         res = stlink_usb_xfer(handle, h->databuf, rx_size);
534
535         if (res != ERROR_OK)
536                 return res;
537
538         res = stlink_usb_error_check(h);
539
540         return res;
541 }
542
543 /** */
544 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
545 {
546         int res;
547         struct stlink_usb_handle_s *h;
548
549         assert(handle != NULL);
550
551         h = (struct stlink_usb_handle_s *)handle;
552
553         stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
554
555         switch (type) {
556                 case STLINK_MODE_DEBUG_JTAG:
557                 case STLINK_MODE_DEBUG_SWD:
558                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
559                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
560                         break;
561                 case STLINK_MODE_DEBUG_SWIM:
562                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
563                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
564                         break;
565                 case STLINK_MODE_DFU:
566                         h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
567                         h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
568                         break;
569                 case STLINK_MODE_MASS:
570                 default:
571                         return ERROR_FAIL;
572         }
573
574         res = stlink_usb_xfer(handle, 0, 0);
575
576         if (res != ERROR_OK)
577                 return res;
578
579         return ERROR_OK;
580 }
581
582 /** */
583 static int stlink_usb_init_mode(void *handle)
584 {
585         int res;
586         uint8_t mode;
587         enum stlink_mode emode;
588         struct stlink_usb_handle_s *h;
589
590         assert(handle != NULL);
591
592         h = (struct stlink_usb_handle_s *)handle;
593
594         res = stlink_usb_current_mode(handle, &mode);
595
596         if (res != ERROR_OK)
597                 return res;
598
599         LOG_DEBUG("MODE: 0x%02X", mode);
600
601         /* try to exit current mode */
602         switch (mode) {
603                 case STLINK_DEV_DFU_MODE:
604                         emode = STLINK_MODE_DFU;
605                         break;
606                 case STLINK_DEV_DEBUG_MODE:
607                         emode = STLINK_MODE_DEBUG_SWD;
608                         break;
609                 case STLINK_DEV_SWIM_MODE:
610                         emode = STLINK_MODE_DEBUG_SWIM;
611                         break;
612                 case STLINK_DEV_BOOTLOADER_MODE:
613                 case STLINK_DEV_MASS_MODE:
614                 default:
615                         emode = STLINK_MODE_UNKNOWN;
616                         break;
617         }
618
619         if (emode != STLINK_MODE_UNKNOWN) {
620                 res = stlink_usb_mode_leave(handle, emode);
621
622                 if (res != ERROR_OK)
623                         return res;
624         }
625
626         res = stlink_usb_current_mode(handle, &mode);
627
628         if (res != ERROR_OK)
629                 return res;
630
631         /* we check the target voltage here as an aid to debugging connection problems.
632          * the stlink requires the target Vdd to be connected for reliable debugging.
633          * this cmd is supported in all modes except DFU
634          */
635         if (mode != STLINK_DEV_DFU_MODE) {
636
637                 float target_voltage;
638
639                 /* check target voltage (if supported) */
640                 res = stlink_usb_check_voltage(h, &target_voltage);
641
642                 if (res != ERROR_OK) {
643                         if (res != ERROR_COMMAND_NOTFOUND)
644                                 LOG_ERROR("voltage check failed");
645                         /* attempt to continue as it is not a catastrophic failure */
646                 } else {
647                         /* check for a sensible target voltage, operating range is 1.65-5.5v
648                          * according to datasheet */
649                         if (target_voltage < 1.5)
650                                 LOG_ERROR("target voltage may be too low for reliable debugging");
651                 }
652         }
653
654         LOG_DEBUG("MODE: 0x%02X", mode);
655
656         /* set selected mode */
657         switch (h->transport) {
658                 case HL_TRANSPORT_SWD:
659                         emode = STLINK_MODE_DEBUG_SWD;
660                         break;
661                 case HL_TRANSPORT_JTAG:
662                         emode = STLINK_MODE_DEBUG_JTAG;
663                         break;
664                 case HL_TRANSPORT_SWIM:
665                         emode = STLINK_MODE_DEBUG_SWIM;
666                         break;
667                 default:
668                         emode = STLINK_MODE_UNKNOWN;
669                         break;
670         }
671
672         if (emode == STLINK_MODE_UNKNOWN) {
673                 LOG_ERROR("selected mode (transport) not supported");
674                 return ERROR_FAIL;
675         }
676
677         res = stlink_usb_mode_enter(handle, emode);
678
679         if (res != ERROR_OK)
680                 return res;
681
682         res = stlink_usb_current_mode(handle, &mode);
683
684         if (res != ERROR_OK)
685                 return res;
686
687         LOG_DEBUG("MODE: 0x%02X", mode);
688
689         return ERROR_OK;
690 }
691
692 /** */
693 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
694 {
695         int res;
696         struct stlink_usb_handle_s *h;
697
698         assert(handle != NULL);
699
700         h = (struct stlink_usb_handle_s *)handle;
701
702         stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
703
704         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
705         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
706
707         res = stlink_usb_xfer(handle, h->databuf, 4);
708
709         if (res != ERROR_OK)
710                 return res;
711
712         *idcode = le_to_h_u32(h->databuf);
713
714         LOG_DEBUG("IDCODE: 0x%08X", *idcode);
715
716         return ERROR_OK;
717 }
718
719 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
720 {
721         struct stlink_usb_handle_s *h;
722         int res;
723
724         assert(handle != NULL);
725
726         h = (struct stlink_usb_handle_s *)handle;
727
728         stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
729
730         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
731         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
732         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
733         h->cmdidx += 4;
734
735         res = stlink_usb_xfer(handle, h->databuf, 8);
736
737         if (res != ERROR_OK)
738                 return res;
739
740         *val = le_to_h_u32(h->databuf + 4);
741
742         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
743 }
744
745 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
746 {
747         int res;
748         struct stlink_usb_handle_s *h;
749
750         assert(handle != NULL);
751
752         h = (struct stlink_usb_handle_s *)handle;
753
754         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
755
756         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
757         if (h->jtag_api == STLINK_JTAG_API_V1)
758                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
759         else
760                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
761         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
762         h->cmdidx += 4;
763         h_u32_to_le(h->cmdbuf+h->cmdidx, val);
764         h->cmdidx += 4;
765
766         res = stlink_usb_xfer(handle, h->databuf, 2);
767
768         if (res != ERROR_OK)
769                 return res;
770
771         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
772 }
773
774 static enum target_state stlink_usb_v2_get_status(void *handle)
775 {
776         int result;
777         uint32_t status;
778
779         result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
780         if  (result != ERROR_OK)
781                 return TARGET_UNKNOWN;
782
783         if (status & S_HALT)
784                 return TARGET_HALTED;
785         else if (status & S_RESET_ST)
786                 return TARGET_RESET;
787
788         return TARGET_RUNNING;
789 }
790
791 /** */
792 static enum target_state stlink_usb_state(void *handle)
793 {
794         int res;
795         struct stlink_usb_handle_s *h;
796
797         assert(handle != NULL);
798
799         h = (struct stlink_usb_handle_s *)handle;
800
801         if (h->jtag_api == STLINK_JTAG_API_V2)
802                 return stlink_usb_v2_get_status(handle);
803
804         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
805
806         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
807         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
808
809         res = stlink_usb_xfer(handle, h->databuf, 2);
810
811         if (res != ERROR_OK)
812                 return TARGET_UNKNOWN;
813
814         if (h->databuf[0] == STLINK_CORE_RUNNING)
815                 return TARGET_RUNNING;
816         if (h->databuf[0] == STLINK_CORE_HALTED)
817                 return TARGET_HALTED;
818
819         return TARGET_UNKNOWN;
820 }
821
822 /** */
823 static int stlink_usb_reset(void *handle)
824 {
825         int res;
826         struct stlink_usb_handle_s *h;
827
828         assert(handle != NULL);
829
830         h = (struct stlink_usb_handle_s *)handle;
831
832         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
833
834         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
835
836         if (h->jtag_api == STLINK_JTAG_API_V1)
837                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
838         else
839                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
840
841         res = stlink_usb_xfer(handle, h->databuf, 2);
842
843         if (res != ERROR_OK)
844                 return res;
845
846         LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
847
848         /* the following is not a error under swd (using hardware srst), so return success */
849         if (h->databuf[0] == STLINK_SWD_AP_WAIT || h->databuf[0] == STLINK_SWD_DP_WAIT)
850                 return ERROR_OK;
851
852         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
853 }
854
855 static int stlink_usb_assert_srst(void *handle, int srst)
856 {
857         int res;
858         struct stlink_usb_handle_s *h;
859
860         assert(handle != NULL);
861
862         h = (struct stlink_usb_handle_s *)handle;
863
864         if (h->jtag_api == STLINK_JTAG_API_V1)
865                 return ERROR_COMMAND_NOTFOUND;
866
867         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
868
869         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
870         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
871         h->cmdbuf[h->cmdidx++] = srst;
872
873         res = stlink_usb_xfer(handle, h->databuf, 2);
874
875         if (res != ERROR_OK)
876                 return res;
877
878         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
879 }
880
881 /** */
882 static int stlink_usb_run(void *handle)
883 {
884         int res;
885         struct stlink_usb_handle_s *h;
886
887         assert(handle != NULL);
888
889         h = (struct stlink_usb_handle_s *)handle;
890
891         if (h->jtag_api == STLINK_JTAG_API_V2)
892                 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
893
894         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
895
896         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
897         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
898
899         res = stlink_usb_xfer(handle, h->databuf, 2);
900
901         if (res != ERROR_OK)
902                 return res;
903
904         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
905 }
906
907 /** */
908 static int stlink_usb_halt(void *handle)
909 {
910         int res;
911         struct stlink_usb_handle_s *h;
912
913         assert(handle != NULL);
914
915         h = (struct stlink_usb_handle_s *)handle;
916
917         if (h->jtag_api == STLINK_JTAG_API_V2)
918                 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
919
920         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
921
922         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
923         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
924
925         res = stlink_usb_xfer(handle, h->databuf, 2);
926
927         if (res != ERROR_OK)
928                 return res;
929
930         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
931 }
932
933 /** */
934 static int stlink_usb_step(void *handle)
935 {
936         int res;
937         struct stlink_usb_handle_s *h;
938
939         assert(handle != NULL);
940
941         h = (struct stlink_usb_handle_s *)handle;
942
943         if (h->jtag_api == STLINK_JTAG_API_V2) {
944                 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
945                  * that the cortex-m3 currently does. */
946                 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
947                 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
948                 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
949         }
950
951         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
952
953         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
954         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
955
956         res = stlink_usb_xfer(handle, h->databuf, 2);
957
958         if (res != ERROR_OK)
959                 return res;
960
961         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
962 }
963
964 /** */
965 static int stlink_usb_read_regs(void *handle)
966 {
967         int res;
968         struct stlink_usb_handle_s *h;
969
970         assert(handle != NULL);
971
972         h = (struct stlink_usb_handle_s *)handle;
973
974         stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
975
976         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
977         if (h->jtag_api == STLINK_JTAG_API_V1)
978                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
979         else
980                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
981
982         res = stlink_usb_xfer(handle, h->databuf, 84);
983
984         if (res != ERROR_OK)
985                 return res;
986
987         return ERROR_OK;
988 }
989
990 /** */
991 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
992 {
993         int res;
994         struct stlink_usb_handle_s *h;
995
996         assert(handle != NULL);
997
998         h = (struct stlink_usb_handle_s *)handle;
999
1000         stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1001
1002         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1003         if (h->jtag_api == STLINK_JTAG_API_V1)
1004                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1005         else
1006                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1007         h->cmdbuf[h->cmdidx++] = num;
1008
1009         res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1010
1011         if (res != ERROR_OK)
1012                 return res;
1013
1014         if (h->jtag_api == STLINK_JTAG_API_V1)
1015                 *val = le_to_h_u32(h->databuf);
1016         else {
1017                 *val = le_to_h_u32(h->databuf + 4);
1018                 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1019         }
1020
1021         return ERROR_OK;
1022 }
1023
1024 /** */
1025 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1026 {
1027         int res;
1028         struct stlink_usb_handle_s *h;
1029
1030         assert(handle != NULL);
1031
1032         h = (struct stlink_usb_handle_s *)handle;
1033
1034         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1035
1036         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1037         if (h->jtag_api == STLINK_JTAG_API_V1)
1038                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1039         else
1040                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1041         h->cmdbuf[h->cmdidx++] = num;
1042         h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1043         h->cmdidx += 4;
1044
1045         res = stlink_usb_xfer(handle, h->databuf, 2);
1046
1047         if (res != ERROR_OK)
1048                 return res;
1049
1050         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1051 }
1052
1053 static int stlink_usb_get_rw_status(void *handle)
1054 {
1055         int res;
1056         struct stlink_usb_handle_s *h;
1057
1058         assert(handle != NULL);
1059
1060         h = (struct stlink_usb_handle_s *)handle;
1061
1062         if (h->jtag_api == STLINK_JTAG_API_V1)
1063                 return ERROR_OK;
1064
1065         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1066
1067         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1068         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1069
1070         res = stlink_usb_xfer(handle, h->databuf, 2);
1071
1072         if (res != ERROR_OK)
1073                 return res;
1074
1075         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1076 }
1077
1078 /** */
1079 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1080                           uint8_t *buffer)
1081 {
1082         int res;
1083         uint16_t read_len = len;
1084         struct stlink_usb_handle_s *h;
1085
1086         assert(handle != NULL);
1087
1088         h = (struct stlink_usb_handle_s *)handle;
1089
1090         stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1091
1092         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1093         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1094         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1095         h->cmdidx += 4;
1096         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1097         h->cmdidx += 2;
1098
1099         /* we need to fix read length for single bytes */
1100         if (read_len == 1)
1101                 read_len++;
1102
1103         res = stlink_usb_xfer(handle, h->databuf, read_len);
1104
1105         if (res != ERROR_OK)
1106                 return res;
1107
1108         memcpy(buffer, h->databuf, len);
1109
1110         return stlink_usb_get_rw_status(handle);
1111 }
1112
1113 /** */
1114 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1115                            const uint8_t *buffer)
1116 {
1117         int res;
1118         struct stlink_usb_handle_s *h;
1119
1120         assert(handle != NULL);
1121
1122         h = (struct stlink_usb_handle_s *)handle;
1123
1124         stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1125
1126         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1127         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1128         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1129         h->cmdidx += 4;
1130         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1131         h->cmdidx += 2;
1132
1133         res = stlink_usb_xfer(handle, buffer, len);
1134
1135         if (res != ERROR_OK)
1136                 return res;
1137
1138         return stlink_usb_get_rw_status(handle);
1139 }
1140
1141 /** */
1142 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1143                           uint8_t *buffer)
1144 {
1145         int res;
1146         struct stlink_usb_handle_s *h;
1147
1148         assert(handle != NULL);
1149
1150         h = (struct stlink_usb_handle_s *)handle;
1151
1152         len *= 4;
1153
1154         stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1155
1156         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1157         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1158         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1159         h->cmdidx += 4;
1160         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1161         h->cmdidx += 2;
1162
1163         res = stlink_usb_xfer(handle, h->databuf, len);
1164
1165         if (res != ERROR_OK)
1166                 return res;
1167
1168         memcpy(buffer, h->databuf, len);
1169
1170         return stlink_usb_get_rw_status(handle);
1171 }
1172
1173 /** */
1174 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1175                            const uint8_t *buffer)
1176 {
1177         int res;
1178         struct stlink_usb_handle_s *h;
1179
1180         assert(handle != NULL);
1181
1182         h = (struct stlink_usb_handle_s *)handle;
1183
1184         len *= 4;
1185
1186         stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1187
1188         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1189         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1190         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1191         h->cmdidx += 4;
1192         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1193         h->cmdidx += 2;
1194
1195         res = stlink_usb_xfer(handle, buffer, len);
1196
1197         if (res != ERROR_OK)
1198                 return res;
1199
1200         return stlink_usb_get_rw_status(handle);
1201 }
1202
1203 /** */
1204 static int stlink_usb_close(void *fd)
1205 {
1206         struct stlink_usb_handle_s *h;
1207
1208         h = (struct stlink_usb_handle_s *)fd;
1209
1210         if (h->fd)
1211                 jtag_libusb_close(h->fd);
1212
1213         free(fd);
1214
1215         return ERROR_OK;
1216 }
1217
1218 /** */
1219 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1220 {
1221         int err;
1222         struct stlink_usb_handle_s *h;
1223         enum stlink_jtag_api_version api;
1224
1225         LOG_DEBUG("stlink_usb_open");
1226
1227         h = calloc(1, sizeof(struct stlink_usb_handle_s));
1228
1229         if (h == 0) {
1230                 LOG_DEBUG("malloc failed");
1231                 return ERROR_FAIL;
1232         }
1233
1234         h->transport = param->transport;
1235
1236         /* set max read/write buffer size in bytes */
1237         param->max_buffer = 512;
1238
1239         const uint16_t vids[] = { param->vid, 0 };
1240         const uint16_t pids[] = { param->pid, 0 };
1241
1242         LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1243                 param->vid, param->pid);
1244
1245         if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1246                 LOG_ERROR("open failed");
1247                 goto error_open;
1248         }
1249
1250         jtag_libusb_set_configuration(h->fd, 0);
1251
1252         if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1253                 LOG_DEBUG("claim interface failed");
1254                 goto error_open;
1255         }
1256
1257         /* wrap version for first read */
1258         switch (param->pid) {
1259                 case 0x3744:
1260                         h->version.stlink = 1;
1261                         break;
1262                 default:
1263                         h->version.stlink = 2;
1264                         break;
1265         }
1266
1267         /* get the device version */
1268         err = stlink_usb_version(h);
1269
1270         if (err != ERROR_OK) {
1271                 LOG_ERROR("read version failed");
1272                 goto error_open;
1273         }
1274
1275         /* compare usb vid/pid */
1276         if ((param->vid != h->vid) || (param->pid != h->pid))
1277                 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1278                         param->vid, param->pid,
1279                         h->vid, h->pid);
1280
1281         /* check if mode is supported */
1282         err = ERROR_OK;
1283
1284         switch (h->transport) {
1285                 case HL_TRANSPORT_SWD:
1286                 case HL_TRANSPORT_JTAG:
1287                         if (h->version.jtag == 0)
1288                                 err = ERROR_FAIL;
1289                         break;
1290                 case HL_TRANSPORT_SWIM:
1291                         if (h->version.swim == 0)
1292                                 err = ERROR_FAIL;
1293                         break;
1294                 default:
1295                         err = ERROR_FAIL;
1296                         break;
1297         }
1298
1299         if (err != ERROR_OK) {
1300                 LOG_ERROR("mode (transport) not supported by device");
1301                 goto error_open;
1302         }
1303
1304         api = h->version.jtag_api_max;
1305
1306         /* check that user has not requested certain api version
1307          * and if they have check it is supported */
1308         if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1309                 api = param->api;
1310                 LOG_INFO("using stlink api v%d", api);
1311         }
1312
1313         /* set the used jtag api, this will default to the newest supported version */
1314         h->jtag_api = api;
1315
1316         /* initialize the debug hardware */
1317         err = stlink_usb_init_mode(h);
1318
1319         if (err != ERROR_OK) {
1320                 LOG_ERROR("init mode failed");
1321                 goto error_open;
1322         }
1323
1324         *fd = h;
1325
1326         return ERROR_OK;
1327
1328 error_open:
1329         stlink_usb_close(h);
1330
1331         return ERROR_FAIL;
1332 }
1333
1334 /** */
1335 struct hl_layout_api_s stlink_usb_layout_api = {
1336         /** */
1337         .open = stlink_usb_open,
1338         /** */
1339         .close = stlink_usb_close,
1340         /** */
1341         .idcode = stlink_usb_idcode,
1342         /** */
1343         .state = stlink_usb_state,
1344         /** */
1345         .reset = stlink_usb_reset,
1346         /** */
1347         .assert_srst = stlink_usb_assert_srst,
1348         /** */
1349         .run = stlink_usb_run,
1350         /** */
1351         .halt = stlink_usb_halt,
1352         /** */
1353         .step = stlink_usb_step,
1354         /** */
1355         .read_regs = stlink_usb_read_regs,
1356         /** */
1357         .read_reg = stlink_usb_read_reg,
1358         /** */
1359         .write_reg = stlink_usb_write_reg,
1360         /** */
1361         .read_mem8 = stlink_usb_read_mem8,
1362         /** */
1363         .write_mem8 = stlink_usb_write_mem8,
1364         /** */
1365         .read_mem32 = stlink_usb_read_mem32,
1366         /** */
1367         .write_mem32 = stlink_usb_write_mem32,
1368         /** */
1369         .write_debug_reg = stlink_usb_write_debug_reg
1370 };