]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/stlink_usb.c
64d3364076ae08fb353e3e360f28080db98e7168
[openocd] / src / jtag / drivers / stlink_usb.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   This code is based on https://github.com/texane/stlink                *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 /* project specific includes */
28 #include <helper/binarybuffer.h>
29 #include <jtag/interface.h>
30 #include <jtag/stlink/stlink_layout.h>
31 #include <jtag/stlink/stlink_transport.h>
32 #include <jtag/stlink/stlink_interface.h>
33 #include <target/target.h>
34
35 #include "libusb_common.h"
36
37 #define ENDPOINT_IN     0x80
38 #define ENDPOINT_OUT    0x00
39
40 #define STLINK_RX_EP    (1|ENDPOINT_IN)
41 #define STLINK_TX_EP    (2|ENDPOINT_OUT)
42 #define STLINK_CMD_SIZE (16)
43 #define STLINK_TX_SIZE  (4*128)
44 #define STLINK_RX_SIZE  (4*128)
45
46 /** */
47 struct stlink_usb_version {
48         /** */
49         int stlink;
50         /** */
51         int jtag;
52         /** */
53         int swim;
54 };
55
56 /** */
57 struct stlink_usb_handle_s {
58         /** */
59         struct jtag_libusb_device_handle *fd;
60         /** */
61         struct libusb_transfer *trans;
62         /** */
63         uint8_t txbuf[STLINK_TX_SIZE];
64         /** */
65         uint8_t rxbuf[STLINK_RX_SIZE];
66         /** */
67         enum stlink_transports transport;
68         /** */
69         struct stlink_usb_version version;
70         /** */
71         uint16_t vid;
72         /** */
73         uint16_t pid;
74         /** */
75         uint32_t sg_tag;
76 };
77
78 #define STLINK_OK                       0x80
79 #define STLINK_FALSE                    0x81
80 #define STLINK_CORE_RUNNING             0x80
81 #define STLINK_CORE_HALTED              0x81
82 #define STLINK_CORE_STAT_UNKNOWN        -1
83
84 #define STLINK_GET_VERSION              0xF1
85 #define STLINK_DEBUG_COMMAND            0xF2
86 #define STLINK_DFU_COMMAND              0xF3
87 #define STLINK_SWIM_COMMAND             0xF4
88 #define STLINK_GET_CURRENT_MODE         0xF5
89
90 #define STLINK_DEV_DFU_MODE             0x00
91 #define STLINK_DEV_MASS_MODE            0x01
92 #define STLINK_DEV_DEBUG_MODE           0x02
93 #define STLINK_DEV_SWIM_MODE            0x03
94 #define STLINK_DEV_UNKNOWN_MODE         -1
95
96 #define STLINK_DFU_EXIT                 0x07
97
98 #define STLINK_SWIM_ENTER               0x00
99 #define STLINK_SWIM_EXIT                0x01
100
101 #define STLINK_DEBUG_ENTER_JTAG         0x00
102 #define STLINK_DEBUG_GETSTATUS          0x01
103 #define STLINK_DEBUG_FORCEDEBUG         0x02
104 #define STLINK_DEBUG_RESETSYS           0x03
105 #define STLINK_DEBUG_READALLREGS        0x04
106 #define STLINK_DEBUG_READREG            0x05
107 #define STLINK_DEBUG_WRITEREG           0x06
108 #define STLINK_DEBUG_READMEM_32BIT      0x07
109 #define STLINK_DEBUG_WRITEMEM_32BIT     0x08
110 #define STLINK_DEBUG_RUNCORE            0x09
111 #define STLINK_DEBUG_STEPCORE           0x0a
112 #define STLINK_DEBUG_SETFP              0x0b
113 #define STLINK_DEBUG_READMEM_8BIT       0x0c
114 #define STLINK_DEBUG_WRITEMEM_8BIT      0x0d
115 #define STLINK_DEBUG_CLEARFP            0x0e
116 #define STLINK_DEBUG_WRITEDEBUGREG      0x0f
117
118 #define STLINK_DEBUG_ENTER_JTAG         0x00
119 #define STLINK_DEBUG_ENTER_SWD          0xa3
120
121 #define STLINK_DEBUG_ENTER              0x20
122 #define STLINK_DEBUG_EXIT               0x21
123 #define STLINK_DEBUG_READCOREID         0x22
124
125 /** */
126 enum stlink_mode {
127         STLINK_MODE_UNKNOWN = 0,
128         STLINK_MODE_DFU,
129         STLINK_MODE_MASS,
130         STLINK_MODE_DEBUG_JTAG,
131         STLINK_MODE_DEBUG_SWD,
132         STLINK_MODE_DEBUG_SWIM
133 };
134
135 /** */
136 static void stlink_usb_recv_v1_create_cmd(char *b, int s, uint32_t tag, uint32_t rxsize,
137                         uint8_t flag, uint8_t lun, uint8_t length)
138 {
139         int i = 0;
140
141         memset(b, 0x00, s);
142
143         /* fill the send buffer */
144         strcpy(b, "USBC");
145         i += 4;
146
147         buf_set_u32(b+i, 0, 32, tag);
148         i += 4;
149         buf_set_u32(b+i, 0, 32, rxsize);
150         i += 4;
151         b[i++] = flag;
152         b[i++] = lun;
153         b[i++] = length;
154 }
155
156 /** */
157 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
158                     int rxsize)
159 {
160         char sg_buffer[31];
161         struct stlink_usb_handle_s *h;
162
163         assert(handle != NULL);
164
165         h = (struct stlink_usb_handle_s *)handle;
166         h->sg_tag = (h->sg_tag + 1) & 1;
167
168         stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
169
170         memcpy(sg_buffer+15, txbuf, 10);
171
172         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
173                                    1000) != 31) {
174                 printf("send failed\n");
175                 return ERROR_FAIL;
176         }
177
178         return ERROR_OK;
179 }
180
181 #define REQUEST_SENSE           0x03
182 #define REQUEST_SENSE_LENGTH    18
183
184 /** */
185 static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
186 {
187         struct stlink_usb_handle_s *h;
188
189         assert(handle != NULL);
190
191         h = (struct stlink_usb_handle_s *)handle;
192
193         /* read status */
194         memset(sg_buffer, 0x00, len);
195
196         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
197                                 len, 1000) != len)
198                 return ERROR_FAIL;
199
200         uint32_t t1, t2;
201
202         t1 = buf_get_u32(sg_buffer+0, 0, 32);
203         t2 = buf_get_u32(sg_buffer+4, 0, 32);
204
205         /* check for USBS */
206         if (t1 != 0x53425355)
207                 return ERROR_FAIL;
208
209         return ERROR_OK;
210 }
211
212 /** */
213 static int stlink_usb_recv_v1_get_sense(void *handle)
214 {
215         struct stlink_usb_handle_s *h;
216         char cdb[16];
217         char sg_buffer[31];
218
219         assert(handle != NULL);
220
221         h = (struct stlink_usb_handle_s *)handle;
222         h->sg_tag = (h->sg_tag + 1) & 1;
223
224         cdb[0] = REQUEST_SENSE;
225         cdb[4] = REQUEST_SENSE_LENGTH;
226
227         stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
228                         0x00, 16);
229
230         memcpy(sg_buffer+15, cdb, 16);
231
232         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
233                                    1000) != 16)
234                 return ERROR_FAIL;
235
236         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)cdb,
237                                   16, 1000) != 16)
238                 return ERROR_FAIL;
239
240         if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
241                 return ERROR_FAIL;
242         /* check for sense */
243         if (sg_buffer[12] != 0)
244                 return ERROR_FAIL;
245
246         /* if (sense[0] != 0x70 && sense[0] != 0x71) */
247
248         return ERROR_OK;
249 }
250
251 /** */
252 static int stlink_usb_recv_v1(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
253                     int rxsize)
254 {
255         int err;
256         char sg_buffer[31];
257         struct stlink_usb_handle_s *h;
258
259         assert(handle != NULL);
260
261         h = (struct stlink_usb_handle_s *)handle;
262
263         err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
264
265         if (err != ERROR_OK)
266                 return err;
267
268         if (rxsize && rxbuf) {
269                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
270                                           rxsize, 1000) != rxsize) {
271                         LOG_DEBUG("jtag_libusb_bulk_read");
272                         return ERROR_FAIL;
273                 }
274         }
275
276         if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
277                 return ERROR_FAIL;
278         /* check for sense */
279         if (sg_buffer[12] == 1) {
280                 LOG_DEBUG("get sense");
281                 err = stlink_usb_recv_v1_get_sense(handle);
282         }
283         return err;
284 }
285
286 /** */
287 static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
288                     int rxsize)
289 {
290         struct stlink_usb_handle_s *h;
291
292         assert(handle != NULL);
293
294         h = (struct stlink_usb_handle_s *)handle;
295
296         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
297                                    1000) != txsize) {
298                 return ERROR_FAIL;
299         }
300         if (rxsize && rxbuf) {
301                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
302                                           rxsize, 1000) != rxsize) {
303                         return ERROR_FAIL;
304                 }
305         }
306         return ERROR_OK;
307 }
308
309 /** */
310 static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
311                     int rxsize)
312 {
313         struct stlink_usb_handle_s *h;
314
315         assert(handle != NULL);
316
317         h = (struct stlink_usb_handle_s *)handle;
318
319         if (h->version.stlink == 1) {
320                 return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
321         } else {
322                 if (txsize < STLINK_CMD_SIZE)
323                         txsize = STLINK_CMD_SIZE;
324                 return stlink_usb_recv_v2(handle, txbuf, txsize, rxbuf, rxsize);
325         }
326 }
327
328 /** */
329 static void stlink_usb_init_buffer(void *handle)
330 {
331         struct stlink_usb_handle_s *h;
332
333         assert(handle != NULL);
334
335         h = (struct stlink_usb_handle_s *)handle;
336
337         memset(h->txbuf, 0, STLINK_CMD_SIZE);
338 }
339
340 /** */
341 static int stlink_usb_version(void *handle)
342 {
343         int res;
344         uint16_t v;
345         struct stlink_usb_handle_s *h;
346
347         assert(handle != NULL);
348
349         h = (struct stlink_usb_handle_s *)handle;
350
351         stlink_usb_init_buffer(handle);
352
353         h->txbuf[0] = STLINK_GET_VERSION;
354
355         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
356
357         if (res != ERROR_OK)
358                 return res;
359
360         v = (h->rxbuf[0] << 8) | h->rxbuf[1];
361
362         h->version.stlink = (v >> 12) & 0x0f;
363         h->version.jtag = (v >> 6) & 0x3f;
364         h->version.swim = v & 0x3f;
365         h->vid = buf_get_u32(h->rxbuf, 16, 16);
366         h->pid = buf_get_u32(h->rxbuf, 32, 16);
367
368         LOG_DEBUG("STLINK v%d JTAG v%d SWIM v%d VID %04X PID %04X",
369                 h->version.stlink,
370                 h->version.jtag,
371                 h->version.swim,
372                 h->vid,
373                 h->pid);
374
375         return ERROR_OK;
376 }
377
378 /** */
379 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
380 {
381         int res;
382         struct stlink_usb_handle_s *h;
383
384         assert(handle != NULL);
385
386         h = (struct stlink_usb_handle_s *)handle;
387
388         stlink_usb_init_buffer(handle);
389
390         h->txbuf[0] = STLINK_GET_CURRENT_MODE;
391
392         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
393
394         if (res != ERROR_OK)
395                 return res;
396
397         *mode = h->rxbuf[0];
398
399         return ERROR_OK;
400 }
401
402 /** */
403 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
404 {
405         int res;
406         struct stlink_usb_handle_s *h;
407
408         assert(handle != NULL);
409
410         h = (struct stlink_usb_handle_s *)handle;
411
412         stlink_usb_init_buffer(handle);
413
414         switch (type) {
415                 case STLINK_MODE_DEBUG_JTAG:
416                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
417                         h->txbuf[1] = STLINK_DEBUG_ENTER;
418                         h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
419                         break;
420                 case STLINK_MODE_DEBUG_SWD:
421                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
422                         h->txbuf[1] = STLINK_DEBUG_ENTER;
423                         h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
424                         break;
425                 case STLINK_MODE_DEBUG_SWIM:
426                         h->txbuf[0] = STLINK_SWIM_COMMAND;
427                         h->txbuf[1] = STLINK_SWIM_ENTER;
428                         break;
429                 case STLINK_MODE_DFU:
430                 case STLINK_MODE_MASS:
431                 default:
432                         return ERROR_FAIL;
433         }
434
435         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
436         if (res != ERROR_OK)
437                 return res;
438
439         return ERROR_OK;
440 }
441
442 /** */
443 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
444 {
445         int res;
446         struct stlink_usb_handle_s *h;
447
448         assert(handle != NULL);
449
450         h = (struct stlink_usb_handle_s *)handle;
451
452         stlink_usb_init_buffer(handle);
453
454         switch (type) {
455                 case STLINK_MODE_DEBUG_JTAG:
456                 case STLINK_MODE_DEBUG_SWD:
457                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
458                         h->txbuf[1] = STLINK_DEBUG_EXIT;
459                         break;
460                 case STLINK_MODE_DEBUG_SWIM:
461                         h->txbuf[0] = STLINK_SWIM_COMMAND;
462                         h->txbuf[1] = STLINK_SWIM_EXIT;
463                         break;
464                 case STLINK_MODE_DFU:
465                         h->txbuf[0] = STLINK_DFU_COMMAND;
466                         h->txbuf[1] = STLINK_DFU_EXIT;
467                         break;
468                 case STLINK_MODE_MASS:
469                 default:
470                         return ERROR_FAIL;
471         }
472
473         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
474         if (res != ERROR_OK)
475                 return res;
476
477         return ERROR_OK;
478 }
479
480 /** */
481 static int stlink_usb_init_mode(void *handle)
482 {
483         int res;
484         uint8_t mode;
485         enum stlink_mode emode;
486         struct stlink_usb_handle_s *h;
487
488         assert(handle != NULL);
489
490         h = (struct stlink_usb_handle_s *)handle;
491
492         res = stlink_usb_current_mode(handle, &mode);
493
494         if (res != ERROR_OK)
495                 return res;
496
497         LOG_DEBUG("MODE: %02X", mode);
498
499         /* try to exit current mode */
500         switch (mode) {
501                 case STLINK_DEV_DFU_MODE:
502                         emode = STLINK_MODE_DFU;
503                         break;
504                 case STLINK_DEV_DEBUG_MODE:
505                         emode = STLINK_MODE_DEBUG_SWD;
506                         break;
507                 case STLINK_DEV_SWIM_MODE:
508                         emode = STLINK_MODE_DEBUG_SWIM;
509                         break;
510                 default:
511                         emode = STLINK_MODE_UNKNOWN;
512                         break;
513         }
514
515         if (emode != STLINK_MODE_UNKNOWN) {
516                 res = stlink_usb_mode_leave(handle, emode);
517
518                 if (res != ERROR_OK)
519                         return res;
520         }
521
522         res = stlink_usb_current_mode(handle, &mode);
523
524         if (res != ERROR_OK)
525                 return res;
526
527         LOG_DEBUG("MODE: %02X", mode);
528
529         /* set selected mode */
530         switch (h->transport) {
531                 case STLINK_TRANSPORT_SWD:
532                         emode = STLINK_MODE_DEBUG_SWD;
533                         break;
534                 case STLINK_TRANSPORT_JTAG:
535                         emode = STLINK_MODE_DEBUG_JTAG;
536                         break;
537                 case STLINK_TRANSPORT_SWIM:
538                         emode = STLINK_MODE_DEBUG_SWIM;
539                         break;
540                 default:
541                         emode = STLINK_MODE_UNKNOWN;
542                         break;
543         }
544
545         if (emode == STLINK_MODE_UNKNOWN) {
546                 LOG_ERROR("selected mode (transport) not supported");
547                 return ERROR_FAIL;
548         }
549
550         res = stlink_usb_mode_enter(handle, emode);
551
552         if (res != ERROR_OK)
553                 return res;
554
555         res = stlink_usb_current_mode(handle, &mode);
556
557         if (res != ERROR_OK)
558                 return res;
559
560         LOG_DEBUG("MODE: %02X", mode);
561
562         return ERROR_OK;
563 }
564
565 /** */
566 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
567 {
568         int res;
569         struct stlink_usb_handle_s *h;
570
571         assert(handle != NULL);
572
573         h = (struct stlink_usb_handle_s *)handle;
574
575         stlink_usb_init_buffer(handle);
576
577         h->txbuf[0] = STLINK_DEBUG_COMMAND;
578         h->txbuf[1] = STLINK_DEBUG_READCOREID;
579
580         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
581
582         if (res != ERROR_OK)
583                 return res;
584
585         *idcode = le_to_h_u32(h->rxbuf);
586
587         LOG_DEBUG("IDCODE: %08X", *idcode);
588
589         return ERROR_OK;
590 }
591
592 /** */
593 static enum target_state stlink_usb_state(void *handle)
594 {
595         int res;
596         struct stlink_usb_handle_s *h;
597
598         assert(handle != NULL);
599
600         h = (struct stlink_usb_handle_s *)handle;
601
602         stlink_usb_init_buffer(handle);
603
604         h->txbuf[0] = STLINK_DEBUG_COMMAND;
605         h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
606
607         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
608
609         if (res != ERROR_OK)
610                 return TARGET_UNKNOWN;
611
612         if (h->rxbuf[0] == STLINK_CORE_RUNNING)
613                 return TARGET_RUNNING;
614         if (h->rxbuf[0] == STLINK_CORE_HALTED)
615                 return TARGET_HALTED;
616
617         return TARGET_UNKNOWN;
618 }
619
620 /** */
621 static int stlink_usb_reset(void *handle)
622 {
623         int res;
624         struct stlink_usb_handle_s *h;
625
626         assert(handle != NULL);
627
628         h = (struct stlink_usb_handle_s *)handle;
629
630         stlink_usb_init_buffer(handle);
631
632         h->txbuf[0] = STLINK_DEBUG_COMMAND;
633         h->txbuf[1] = STLINK_DEBUG_RESETSYS;
634
635         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
636
637         if (res != ERROR_OK)
638                 return res;
639
640         LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
641
642         return ERROR_OK;
643 }
644
645 /** */
646 static int stlink_usb_run(void *handle)
647 {
648         int res;
649         struct stlink_usb_handle_s *h;
650
651         assert(handle != NULL);
652
653         h = (struct stlink_usb_handle_s *)handle;
654
655         stlink_usb_init_buffer(handle);
656
657         h->txbuf[0] = STLINK_DEBUG_COMMAND;
658         h->txbuf[1] = STLINK_DEBUG_RUNCORE;
659
660         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
661
662         if (res != ERROR_OK)
663                 return res;
664
665         return ERROR_OK;
666 }
667
668 /** */
669 static int stlink_usb_halt(void *handle)
670 {
671         int res;
672         struct stlink_usb_handle_s *h;
673
674         assert(handle != NULL);
675
676         h = (struct stlink_usb_handle_s *)handle;
677
678         stlink_usb_init_buffer(handle);
679
680         h->txbuf[0] = STLINK_DEBUG_COMMAND;
681         h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
682
683         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
684
685         if (res != ERROR_OK)
686                 return res;
687
688         return ERROR_OK;
689 }
690
691 /** */
692 static int stlink_usb_step(void *handle)
693 {
694         int res;
695         struct stlink_usb_handle_s *h;
696
697         assert(handle != NULL);
698
699         h = (struct stlink_usb_handle_s *)handle;
700
701         stlink_usb_init_buffer(handle);
702
703         h->txbuf[0] = STLINK_DEBUG_COMMAND;
704         h->txbuf[1] = STLINK_DEBUG_STEPCORE;
705
706         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
707
708         if (res != ERROR_OK)
709                 return res;
710
711         return ERROR_OK;
712 }
713
714 /** */
715 static int stlink_usb_read_regs(void *handle)
716 {
717         int res;
718         struct stlink_usb_handle_s *h;
719
720         assert(handle != NULL);
721
722         h = (struct stlink_usb_handle_s *)handle;
723
724         stlink_usb_init_buffer(handle);
725
726         h->txbuf[0] = STLINK_DEBUG_COMMAND;
727         h->txbuf[1] = STLINK_DEBUG_READALLREGS;
728
729         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
730
731         if (res != ERROR_OK)
732                 return res;
733
734         return ERROR_OK;
735 }
736
737 /** */
738 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
739 {
740         int res;
741         struct stlink_usb_handle_s *h;
742
743         assert(handle != NULL);
744
745         h = (struct stlink_usb_handle_s *)handle;
746
747         stlink_usb_init_buffer(handle);
748
749         h->txbuf[0] = STLINK_DEBUG_COMMAND;
750         h->txbuf[1] = STLINK_DEBUG_READREG;
751         h->txbuf[2] = num;
752
753         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
754
755         if (res != ERROR_OK)
756                 return res;
757
758         *val = le_to_h_u32(h->rxbuf);
759
760         return ERROR_OK;
761 }
762
763 /** */
764 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
765 {
766         int res;
767         struct stlink_usb_handle_s *h;
768
769         assert(handle != NULL);
770
771         h = (struct stlink_usb_handle_s *)handle;
772
773         stlink_usb_init_buffer(handle);
774
775         h->txbuf[0] = STLINK_DEBUG_COMMAND;
776         h->txbuf[1] = STLINK_DEBUG_WRITEREG;
777         h->txbuf[2] = num;
778         h_u32_to_le(h->txbuf + 3, val);
779
780         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
781
782         if (res != ERROR_OK)
783                 return res;
784
785         return ERROR_OK;
786 }
787
788 /** */
789 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
790                           uint8_t *buffer)
791 {
792         int res;
793         uint16_t read_len = len;
794         struct stlink_usb_handle_s *h;
795
796         assert(handle != NULL);
797
798         h = (struct stlink_usb_handle_s *)handle;
799
800         stlink_usb_init_buffer(handle);
801
802         h->txbuf[0] = STLINK_DEBUG_COMMAND;
803         h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
804         h_u32_to_le(h->txbuf + 2, addr);
805         h_u16_to_le(h->txbuf + 2 + 4, len);
806
807         /* we need to fix read length for single bytes */
808         if (read_len == 1)
809                 read_len++;
810
811         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
812
813         if (res != ERROR_OK)
814                 return res;
815
816         memcpy(buffer, h->rxbuf, len);
817
818         return ERROR_OK;
819 }
820
821 /** */
822 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
823                            const uint8_t *buffer)
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);
833
834         h->txbuf[0] = STLINK_DEBUG_COMMAND;
835         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
836         h_u32_to_le(h->txbuf + 2, addr);
837         h_u16_to_le(h->txbuf + 2 + 4, len);
838
839         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
840
841         if (res != ERROR_OK)
842                 return res;
843
844         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
845
846         if (res != ERROR_OK)
847                 return res;
848
849         return ERROR_OK;
850 }
851
852 /** */
853 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
854                           uint32_t *buffer)
855 {
856         int res;
857         struct stlink_usb_handle_s *h;
858
859         assert(handle != NULL);
860
861         h = (struct stlink_usb_handle_s *)handle;
862
863         stlink_usb_init_buffer(handle);
864
865         len *= 4;
866
867         h->txbuf[0] = STLINK_DEBUG_COMMAND;
868         h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
869         h_u32_to_le(h->txbuf + 2, addr);
870         h_u16_to_le(h->txbuf + 2 + 4, len);
871
872         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
873
874         if (res != ERROR_OK)
875                 return res;
876
877         memcpy(buffer, h->rxbuf, len);
878
879         return ERROR_OK;
880 }
881
882 /** */
883 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
884                            const uint32_t *buffer)
885 {
886         int res;
887         struct stlink_usb_handle_s *h;
888
889         assert(handle != NULL);
890
891         h = (struct stlink_usb_handle_s *)handle;
892
893         stlink_usb_init_buffer(handle);
894
895         len *= 4;
896
897         h->txbuf[0] = STLINK_DEBUG_COMMAND;
898         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
899         h_u32_to_le(h->txbuf + 2, addr);
900         h_u16_to_le(h->txbuf + 2 + 4, len);
901
902         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
903
904         if (res != ERROR_OK)
905                 return res;
906
907         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
908
909         if (res != ERROR_OK)
910                 return res;
911
912         return ERROR_OK;
913 }
914
915 /** */
916 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
917 {
918         int err;
919         struct stlink_usb_handle_s *h;
920
921         LOG_DEBUG("stlink_usb_open");
922
923         h = malloc(sizeof(struct stlink_usb_handle_s));
924
925         if (h == 0) {
926                 LOG_DEBUG("malloc failed");
927                 return ERROR_FAIL;
928         }
929
930         h->transport = param->transport;
931
932         const uint16_t vids[] = { param->vid, 0 };
933         const uint16_t pids[] = { param->pid, 0 };
934
935         LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
936                 param->vid, param->pid);
937
938         if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
939                 LOG_ERROR("open failed");
940                 return ERROR_FAIL;
941         }
942
943         jtag_libusb_set_configuration(h->fd, 0);
944
945         if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
946                 LOG_DEBUG("claim interface failed");
947                 return ERROR_FAIL;
948         }
949
950         /* wrap version for first read */
951         switch (param->pid) {
952                 case 0x3744:
953                         h->version.stlink = 1;
954                         break;
955                 default:
956                         h->version.stlink = 2;
957                         break;
958         }
959
960         /* get the device version */
961         err = stlink_usb_version(h);
962
963         if (err != ERROR_OK) {
964                 LOG_ERROR("read version failed");
965                 jtag_libusb_close(h->fd);
966                 free(h);
967                 return err;
968         }
969
970         /* compare usb vid/pid */
971         if ((param->vid != h->vid) || (param->pid != h->pid))
972                 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
973                         param->vid, param->pid,
974                         h->vid, h->pid);
975
976         /* check if mode is supported */
977         err = ERROR_OK;
978
979         switch (h->transport) {
980                 case STLINK_TRANSPORT_SWD:
981                 case STLINK_TRANSPORT_JTAG:
982                         if (h->version.jtag == 0)
983                                 err = ERROR_FAIL;
984                         break;
985                 case STLINK_TRANSPORT_SWIM:
986                         if (h->version.swim == 0)
987                                 err = ERROR_FAIL;
988                         break;
989                 default:
990                         err = ERROR_FAIL;
991                         break;
992         }
993
994         if (err != ERROR_OK) {
995                 LOG_ERROR("mode (transport) not supported by device");
996                 jtag_libusb_close(h->fd);
997                 free(h);
998                 return err;
999         }
1000
1001         err = stlink_usb_init_mode(h);
1002
1003         if (err != ERROR_OK) {
1004                 LOG_ERROR("init mode failed");
1005                 jtag_libusb_close(h->fd);
1006                 free(h);
1007                 return err;
1008         }
1009
1010         *fd = h;
1011
1012         return ERROR_OK;
1013 }
1014
1015 /** */
1016 static int stlink_usb_close(void *fd)
1017 {
1018         return ERROR_OK;
1019 }
1020
1021 /** */
1022 struct stlink_layout_api_s stlink_usb_layout_api = {
1023         /** */
1024         .open = stlink_usb_open,
1025         /** */
1026         .close = stlink_usb_close,
1027         /** */
1028         .idcode = stlink_usb_idcode,
1029         /** */
1030         .state = stlink_usb_state,
1031         /** */
1032         .reset = stlink_usb_reset,
1033         /** */
1034         .run = stlink_usb_run,
1035         /** */
1036         .halt = stlink_usb_halt,
1037         /** */
1038         .step = stlink_usb_step,
1039         /** */
1040         .read_regs = stlink_usb_read_regs,
1041         /** */
1042         .read_reg = stlink_usb_read_reg,
1043         /** */
1044         .write_reg = stlink_usb_write_reg,
1045         /** */
1046         .read_mem8 = stlink_usb_read_mem8,
1047         /** */
1048         .write_mem8 = stlink_usb_write_mem8,
1049         /** */
1050         .read_mem32 = stlink_usb_read_mem32,
1051         /** */
1052         .write_mem32 = stlink_usb_write_mem32,
1053 };