]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/stlink_usb.c
stlink-v1: code cleanup
[openocd] / src / jtag / drivers / stlink_usb.c
1 /***************************************************************************
2  *   Copyright (C) 2011-2012 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 enum stlink_jtag_api_version {
47         STLINK_JTAG_API_V1 = 0,
48         STLINK_JTAG_API_V2,
49 };
50
51 /** */
52 struct stlink_usb_version {
53         /** */
54         int stlink;
55         /** */
56         int jtag;
57         /** */
58         int swim;
59         /** highest supported jtag api version */
60         enum stlink_jtag_api_version jtag_api_max;
61 };
62
63 /** */
64 struct stlink_usb_handle_s {
65         /** */
66         struct jtag_libusb_device_handle *fd;
67         /** */
68         struct libusb_transfer *trans;
69         /** */
70         uint8_t txbuf[STLINK_TX_SIZE];
71         /** */
72         uint8_t rxbuf[STLINK_RX_SIZE];
73         /** */
74         enum stlink_transports transport;
75         /** */
76         struct stlink_usb_version version;
77         /** */
78         uint16_t vid;
79         /** */
80         uint16_t pid;
81         /** */
82         uint32_t sg_tag;
83         /** this is the currently used jtag api */
84         enum stlink_jtag_api_version jtag_api;
85 };
86
87 #define STLINK_DEBUG_ERR_OK                     0x80
88 #define STLINK_DEBUG_ERR_FAULT                  0x81
89 #define STLINK_CORE_RUNNING                     0x80
90 #define STLINK_CORE_HALTED                      0x81
91 #define STLINK_CORE_STAT_UNKNOWN                -1
92
93 #define STLINK_GET_VERSION                      0xF1
94 #define STLINK_DEBUG_COMMAND                    0xF2
95 #define STLINK_DFU_COMMAND                      0xF3
96 #define STLINK_SWIM_COMMAND                     0xF4
97 #define STLINK_GET_CURRENT_MODE                 0xF5
98
99 #define STLINK_DEV_DFU_MODE                     0x00
100 #define STLINK_DEV_MASS_MODE                    0x01
101 #define STLINK_DEV_DEBUG_MODE                   0x02
102 #define STLINK_DEV_SWIM_MODE                    0x03
103 #define STLINK_DEV_BOOTLOADER_MODE              0x04
104 #define STLINK_DEV_UNKNOWN_MODE                 -1
105
106 #define STLINK_DFU_EXIT                         0x07
107
108 #define STLINK_SWIM_ENTER                       0x00
109 #define STLINK_SWIM_EXIT                        0x01
110
111 #define STLINK_DEBUG_ENTER_JTAG                 0x00
112 #define STLINK_DEBUG_GETSTATUS                  0x01
113 #define STLINK_DEBUG_FORCEDEBUG                 0x02
114 #define STLINK_DEBUG_APIV1_RESETSYS             0x03
115 #define STLINK_DEBUG_APIV1_READALLREGS          0x04
116 #define STLINK_DEBUG_APIV1_READREG              0x05
117 #define STLINK_DEBUG_APIV1_WRITEREG             0x06
118 #define STLINK_DEBUG_READMEM_32BIT              0x07
119 #define STLINK_DEBUG_WRITEMEM_32BIT             0x08
120 #define STLINK_DEBUG_RUNCORE                    0x09
121 #define STLINK_DEBUG_STEPCORE                   0x0a
122 #define STLINK_DEBUG_APIV1_SETFP                0x0b
123 #define STLINK_DEBUG_READMEM_8BIT               0x0c
124 #define STLINK_DEBUG_WRITEMEM_8BIT              0x0d
125 #define STLINK_DEBUG_APIV1_CLEARFP              0x0e
126 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG        0x0f
127 #define STLINK_DEBUG_APIV1_SETWATCHPOINT        0x10
128
129 #define STLINK_DEBUG_ENTER_JTAG                 0x00
130 #define STLINK_DEBUG_ENTER_SWD                  0xa3
131
132 #define STLINK_DEBUG_APIV1_ENTER                0x20
133 #define STLINK_DEBUG_EXIT                       0x21
134 #define STLINK_DEBUG_READCOREID                 0x22
135
136 #define STLINK_DEBUG_APIV2_ENTER                0x30
137 #define STLINK_DEBUG_APIV2_READ_IDCODES         0x31
138 #define STLINK_DEBUG_APIV2_RESETSYS             0x32
139 #define STLINK_DEBUG_APIV2_READREG              0x33
140 #define STLINK_DEBUG_APIV2_WRITEREG             0x34
141
142 #define STLINK_DEBUG_APIV2_READALLREGS          0x3A
143
144 #define STLINK_DEBUG_APIV2_DRIVE_NRST           0x3C
145
146 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW       0x00
147 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH      0x01
148 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE     0x02
149
150 /** */
151 enum stlink_mode {
152         STLINK_MODE_UNKNOWN = 0,
153         STLINK_MODE_DFU,
154         STLINK_MODE_MASS,
155         STLINK_MODE_DEBUG_JTAG,
156         STLINK_MODE_DEBUG_SWD,
157         STLINK_MODE_DEBUG_SWIM
158 };
159
160 /** */
161 static int stlink_usb_xfer_v1_send_cmd(void *handle, const uint8_t *cmd, int cmdsize, int ep, int size)
162 {
163         uint8_t sg_buffer[31];
164         struct stlink_usb_handle_s *h;
165
166         assert(handle != NULL);
167         assert(cmdsize <= 16);
168
169         h = (struct stlink_usb_handle_s *)handle;
170         h->sg_tag = (h->sg_tag + 1) & 1; /* seriously? */
171
172         memset(sg_buffer, 0, sizeof(sg_buffer));
173
174         h_u32_to_le(sg_buffer, 0x43425355); /* USBC */
175         h_u32_to_le(&sg_buffer[4], h->sg_tag);
176         h_u32_to_le(&sg_buffer[8], size);
177
178         sg_buffer[12] = (ep == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
179         /* sg_buffer[13] = 0; */
180         sg_buffer[14] = (uint8_t)cmdsize;
181
182         memcpy(&sg_buffer[15], cmd, cmdsize);
183
184         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, sizeof(sg_buffer),
185                                    1000) != sizeof(sg_buffer)) {
186                 LOG_DEBUG("send failed\n");
187                 return ERROR_FAIL;
188         }
189
190         return ERROR_OK;
191 }
192
193 /** */
194 static int stlink_usb_xfer_v1_get_status(void *handle, uint8_t *sg_buffer, int len)
195 {
196         struct stlink_usb_handle_s *h;
197
198         assert(handle != NULL);
199
200         h = (struct stlink_usb_handle_s *)handle;
201
202         /* read status */
203         memset(sg_buffer, 0, len);
204
205         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
206                                 len, 1000) != len)
207                 return ERROR_FAIL;
208
209         uint32_t t1 = le_to_h_u32(sg_buffer+0);
210         /* uint32_t t2 = le_to_h_u32(sg_buffer+4); */
211
212         /* check for USBS */
213         if (t1 != 0x53425355)
214                 return ERROR_FAIL;
215
216         return ERROR_OK;
217 }
218
219 /** */
220 static int stlink_usb_xfer_rw(void *handle, int ep, uint8_t *buf, int size)
221 {
222         struct stlink_usb_handle_s *h;
223
224         h = (struct stlink_usb_handle_s *)handle;
225
226         if (!size)
227                 return ERROR_OK;
228
229         if (ep == STLINK_RX_EP) {
230                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
231                                           size, 1000) != size) {
232                         return ERROR_FAIL;
233                 }
234         } else {
235                 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
236                                           size, 1000) != size) {
237                         return ERROR_FAIL;
238                 }
239         }
240
241         return ERROR_OK;
242 }
243
244 /**
245  * http://en.wikipedia.org/wiki/SCSI_Request_Sense_Command
246  */
247 static int stlink_usb_xfer_v1_get_sense(void *handle)
248 {
249         int err;
250
251         uint8_t cdb[STLINK_CMD_SIZE];
252         uint8_t sense[18];
253         uint8_t status[13];
254
255         assert(handle != NULL);
256
257         memset(cdb, 0, sizeof(cdb));
258
259         cdb[0] = 0x03;
260         cdb[4] = sizeof(sense);
261
262         err = stlink_usb_xfer_v1_send_cmd(handle, cdb, sizeof(cdb), STLINK_RX_EP, sizeof(sense));
263
264         if (err != ERROR_OK)
265                 return err;
266
267         err = stlink_usb_xfer_rw(handle, STLINK_RX_EP, sense, sizeof(sense));
268
269         if (err != ERROR_OK)
270                 return err;
271
272         err = stlink_usb_xfer_v1_get_status(handle, status, sizeof(status));
273
274         if (err != ERROR_OK)
275                 return err;
276
277         /* check for sense */
278         if (status[12] != 0)
279                 return ERROR_FAIL;
280
281         /* if (sense[0] != 0x70 && sense[0] != 0x71) */
282
283         return err;
284 }
285
286 /** */
287 static int stlink_usb_xfer_v1_check_status(void *handle)
288 {
289         int err;
290         uint8_t sg_buffer[13];
291
292         err = stlink_usb_xfer_v1_get_status(handle, sg_buffer, sizeof(sg_buffer));
293
294         if (err != ERROR_OK)
295                 return err;
296
297         /* check for sense */
298         if (sg_buffer[12] == 1) {
299                 LOG_DEBUG("get sense");
300
301                 err = stlink_usb_xfer_v1_get_sense(handle);
302         }
303
304         return err;
305 }
306
307 /** */
308 static int stlink_usb_xfer_v1(void *handle, const uint8_t *cmd, int cmdsize, int ep,
309                               uint8_t *buf, int size)
310 {
311         int err;
312
313         assert(handle != NULL);
314
315         err = stlink_usb_xfer_v1_send_cmd(handle, cmd, cmdsize, ep, size);
316
317         if (err != ERROR_OK)
318                 return err;
319
320         err = stlink_usb_xfer_rw(handle, ep, buf, size);
321
322         if (err != ERROR_OK)
323                 return err;
324
325         return stlink_usb_xfer_v1_check_status(handle);
326 }
327
328 /** */
329 static int stlink_usb_xfer_v2(void *handle, const uint8_t *cmd, int cmdsize, int ep,
330                               uint8_t *buf, int size)
331 {
332         struct stlink_usb_handle_s *h;
333
334         assert(handle != NULL);
335
336         h = (struct stlink_usb_handle_s *)handle;
337
338         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)cmd, cmdsize,
339                                    1000) != cmdsize) {
340                 return ERROR_FAIL;
341         }
342
343         return stlink_usb_xfer_rw(handle, ep, buf, size);
344 }
345
346 /** */
347 static int stlink_usb_xfer(void *handle, const uint8_t *cmd, int cmdsize, int ep,
348                               uint8_t *buf, int size)
349 {
350         struct stlink_usb_handle_s *h;
351
352         assert(handle != NULL);
353         assert(cmdsize == STLINK_CMD_SIZE);
354
355         h = (struct stlink_usb_handle_s *)handle;
356
357         if (h->version.stlink == 1) {
358                 return stlink_usb_xfer_v1(handle, cmd, cmdsize, ep, buf, size);
359         } else {
360                 return stlink_usb_xfer_v2(handle, cmd, cmdsize, ep, buf, size);
361         }
362 }
363
364 /** */
365 static int stlink_usb_recv(void *handle, const uint8_t *cmd, int cmdsize, uint8_t *rxbuf,
366                     int rxsize)
367 {
368         return stlink_usb_xfer(handle, cmd, cmdsize, STLINK_RX_EP, rxbuf, rxsize);
369 }
370
371 /** */
372 static void stlink_usb_init_buffer(void *handle)
373 {
374         struct stlink_usb_handle_s *h;
375
376         assert(handle != NULL);
377
378         h = (struct stlink_usb_handle_s *)handle;
379
380         memset(h->txbuf, 0, STLINK_TX_SIZE);
381         memset(h->rxbuf, 0, STLINK_RX_SIZE);
382 }
383
384 static const char * const stlink_usb_error_msg[] = {
385         "unknown"
386 };
387
388 /** */
389 static int stlink_usb_error_check(void *handle)
390 {
391         int res;
392         const char *err_msg = 0;
393         struct stlink_usb_handle_s *h;
394
395         assert(handle != NULL);
396
397         h = (struct stlink_usb_handle_s *)handle;
398
399         /* TODO: no error checking yet on api V1 */
400         if (h->jtag_api == STLINK_JTAG_API_V1)
401                 h->rxbuf[0] = STLINK_DEBUG_ERR_OK;
402
403         switch (h->rxbuf[0]) {
404                 case STLINK_DEBUG_ERR_OK:
405                         res = ERROR_OK;
406                         break;
407                 case STLINK_DEBUG_ERR_FAULT:
408                 default:
409                         err_msg = stlink_usb_error_msg[0];
410                         res = ERROR_FAIL;
411                         break;
412         }
413
414         if (res != ERROR_OK)
415                 LOG_DEBUG("status error: %d ('%s')", h->rxbuf[0], err_msg);
416
417         return res;
418 }
419
420 /** */
421 static int stlink_usb_version(void *handle)
422 {
423         int res;
424         uint16_t v;
425         struct stlink_usb_handle_s *h;
426
427         assert(handle != NULL);
428
429         h = (struct stlink_usb_handle_s *)handle;
430
431         stlink_usb_init_buffer(handle);
432
433         h->txbuf[0] = STLINK_GET_VERSION;
434
435         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
436
437         if (res != ERROR_OK)
438                 return res;
439
440         v = (h->rxbuf[0] << 8) | h->rxbuf[1];
441
442         h->version.stlink = (v >> 12) & 0x0f;
443         h->version.jtag = (v >> 6) & 0x3f;
444         h->version.swim = v & 0x3f;
445         h->vid = buf_get_u32(h->rxbuf, 16, 16);
446         h->pid = buf_get_u32(h->rxbuf, 32, 16);
447
448         /* set the supported jtag api version
449          * V1 doesn't support API V2 at all
450          * V2 support API V2 since JTAG V13
451          */
452         if ((h->version.stlink == 2) && (h->version.jtag > 12))
453                 h->version.jtag_api_max = STLINK_JTAG_API_V2;
454         else
455                 h->version.jtag_api_max = STLINK_JTAG_API_V1;
456
457         LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
458                 h->version.stlink,
459                 h->version.jtag,
460                 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
461                 h->version.swim,
462                 h->vid,
463                 h->pid);
464
465         return ERROR_OK;
466 }
467
468 /** */
469 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
470 {
471         int res;
472         struct stlink_usb_handle_s *h;
473
474         assert(handle != NULL);
475
476         h = (struct stlink_usb_handle_s *)handle;
477
478         stlink_usb_init_buffer(handle);
479
480         h->txbuf[0] = STLINK_GET_CURRENT_MODE;
481
482         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
483
484         if (res != ERROR_OK)
485                 return res;
486
487         *mode = h->rxbuf[0];
488
489         return ERROR_OK;
490 }
491
492 /** */
493 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
494 {
495         int res;
496         int rx_size = 0;
497         struct stlink_usb_handle_s *h;
498
499         assert(handle != NULL);
500
501         h = (struct stlink_usb_handle_s *)handle;
502
503         stlink_usb_init_buffer(handle);
504
505         switch (type) {
506                 case STLINK_MODE_DEBUG_JTAG:
507                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
508                         if (h->jtag_api == STLINK_JTAG_API_V1)
509                                 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
510                         else
511                                 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
512                         h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
513                         break;
514                 case STLINK_MODE_DEBUG_SWD:
515                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
516                         if (h->jtag_api == STLINK_JTAG_API_V1)
517                                 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
518                         else
519                                 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
520                         h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
521                         break;
522                 case STLINK_MODE_DEBUG_SWIM:
523                         h->txbuf[0] = STLINK_SWIM_COMMAND;
524                         h->txbuf[1] = STLINK_SWIM_ENTER;
525                         break;
526                 case STLINK_MODE_DFU:
527                 case STLINK_MODE_MASS:
528                 default:
529                         return ERROR_FAIL;
530         }
531
532         /* on api V2 we are able the read the latest command
533          * status
534          * TODO: we need the test on api V1 too
535          */
536         if (h->jtag_api == STLINK_JTAG_API_V2)
537                 rx_size = 2;
538
539         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, rx_size);
540
541         if (res != ERROR_OK)
542                 return res;
543
544         res = stlink_usb_error_check(h);
545
546         return res;
547 }
548
549 /** */
550 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
551 {
552         int res;
553         struct stlink_usb_handle_s *h;
554
555         assert(handle != NULL);
556
557         h = (struct stlink_usb_handle_s *)handle;
558
559         stlink_usb_init_buffer(handle);
560
561         switch (type) {
562                 case STLINK_MODE_DEBUG_JTAG:
563                 case STLINK_MODE_DEBUG_SWD:
564                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
565                         h->txbuf[1] = STLINK_DEBUG_EXIT;
566                         break;
567                 case STLINK_MODE_DEBUG_SWIM:
568                         h->txbuf[0] = STLINK_SWIM_COMMAND;
569                         h->txbuf[1] = STLINK_SWIM_EXIT;
570                         break;
571                 case STLINK_MODE_DFU:
572                         h->txbuf[0] = STLINK_DFU_COMMAND;
573                         h->txbuf[1] = STLINK_DFU_EXIT;
574                         break;
575                 case STLINK_MODE_MASS:
576                 default:
577                         return ERROR_FAIL;
578         }
579
580         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
581
582         if (res != ERROR_OK)
583                 return res;
584
585         return ERROR_OK;
586 }
587
588 /** */
589 static int stlink_usb_init_mode(void *handle)
590 {
591         int res;
592         uint8_t mode;
593         enum stlink_mode emode;
594         struct stlink_usb_handle_s *h;
595
596         assert(handle != NULL);
597
598         h = (struct stlink_usb_handle_s *)handle;
599
600         res = stlink_usb_current_mode(handle, &mode);
601
602         if (res != ERROR_OK)
603                 return res;
604
605         LOG_DEBUG("MODE: %02X", mode);
606
607         /* try to exit current mode */
608         switch (mode) {
609                 case STLINK_DEV_DFU_MODE:
610                         emode = STLINK_MODE_DFU;
611                         break;
612                 case STLINK_DEV_DEBUG_MODE:
613                         emode = STLINK_MODE_DEBUG_SWD;
614                         break;
615                 case STLINK_DEV_SWIM_MODE:
616                         emode = STLINK_MODE_DEBUG_SWIM;
617                         break;
618                 case STLINK_DEV_BOOTLOADER_MODE:
619                 case STLINK_DEV_MASS_MODE:
620                 default:
621                         emode = STLINK_MODE_UNKNOWN;
622                         break;
623         }
624
625         if (emode != STLINK_MODE_UNKNOWN) {
626                 res = stlink_usb_mode_leave(handle, emode);
627
628                 if (res != ERROR_OK)
629                         return res;
630         }
631
632         res = stlink_usb_current_mode(handle, &mode);
633
634         if (res != ERROR_OK)
635                 return res;
636
637         LOG_DEBUG("MODE: %02X", mode);
638
639         /* set selected mode */
640         switch (h->transport) {
641                 case STLINK_TRANSPORT_SWD:
642                         emode = STLINK_MODE_DEBUG_SWD;
643                         break;
644                 case STLINK_TRANSPORT_JTAG:
645                         emode = STLINK_MODE_DEBUG_JTAG;
646                         break;
647                 case STLINK_TRANSPORT_SWIM:
648                         emode = STLINK_MODE_DEBUG_SWIM;
649                         break;
650                 default:
651                         emode = STLINK_MODE_UNKNOWN;
652                         break;
653         }
654
655         if (emode == STLINK_MODE_UNKNOWN) {
656                 LOG_ERROR("selected mode (transport) not supported");
657                 return ERROR_FAIL;
658         }
659
660         res = stlink_usb_mode_enter(handle, emode);
661
662         if (res != ERROR_OK)
663                 return res;
664
665         res = stlink_usb_current_mode(handle, &mode);
666
667         if (res != ERROR_OK)
668                 return res;
669
670         LOG_DEBUG("MODE: %02X", mode);
671
672         return ERROR_OK;
673 }
674
675 /** */
676 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
677 {
678         int res;
679         struct stlink_usb_handle_s *h;
680
681         assert(handle != NULL);
682
683         h = (struct stlink_usb_handle_s *)handle;
684
685         stlink_usb_init_buffer(handle);
686
687         h->txbuf[0] = STLINK_DEBUG_COMMAND;
688         h->txbuf[1] = STLINK_DEBUG_READCOREID;
689
690         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
691
692         if (res != ERROR_OK)
693                 return res;
694
695         *idcode = le_to_h_u32(h->rxbuf);
696
697         LOG_DEBUG("IDCODE: %08X", *idcode);
698
699         return ERROR_OK;
700 }
701
702 /** */
703 static enum target_state stlink_usb_state(void *handle)
704 {
705         int res;
706         struct stlink_usb_handle_s *h;
707
708         assert(handle != NULL);
709
710         h = (struct stlink_usb_handle_s *)handle;
711
712         if (h->jtag_api == STLINK_JTAG_API_V2)
713                 return TARGET_UNKNOWN;
714
715         stlink_usb_init_buffer(handle);
716
717         h->txbuf[0] = STLINK_DEBUG_COMMAND;
718         h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
719
720         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
721
722         if (res != ERROR_OK)
723                 return TARGET_UNKNOWN;
724
725         if (h->rxbuf[0] == STLINK_CORE_RUNNING)
726                 return TARGET_RUNNING;
727         if (h->rxbuf[0] == STLINK_CORE_HALTED)
728                 return TARGET_HALTED;
729
730         return TARGET_UNKNOWN;
731 }
732
733 /** */
734 static int stlink_usb_reset(void *handle)
735 {
736         int res;
737         struct stlink_usb_handle_s *h;
738
739         assert(handle != NULL);
740
741         h = (struct stlink_usb_handle_s *)handle;
742
743         stlink_usb_init_buffer(handle);
744
745         h->txbuf[0] = STLINK_DEBUG_COMMAND;
746
747         if (h->jtag_api == STLINK_JTAG_API_V1)
748                 h->txbuf[1] = STLINK_DEBUG_APIV1_RESETSYS;
749         else
750                 h->txbuf[1] = STLINK_DEBUG_APIV2_RESETSYS;
751
752         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
753
754         if (res != ERROR_OK)
755                 return res;
756
757         LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
758
759         return ERROR_OK;
760 }
761
762 /** */
763 static int stlink_usb_run(void *handle)
764 {
765         int res;
766         struct stlink_usb_handle_s *h;
767
768         assert(handle != NULL);
769
770         h = (struct stlink_usb_handle_s *)handle;
771
772         if (h->jtag_api == STLINK_JTAG_API_V2)
773                 return ERROR_FAIL;
774
775         stlink_usb_init_buffer(handle);
776
777         h->txbuf[0] = STLINK_DEBUG_COMMAND;
778         h->txbuf[1] = STLINK_DEBUG_RUNCORE;
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_halt(void *handle)
790 {
791         int res;
792         struct stlink_usb_handle_s *h;
793
794         assert(handle != NULL);
795
796         h = (struct stlink_usb_handle_s *)handle;
797
798         if (h->jtag_api == STLINK_JTAG_API_V2)
799                 return ERROR_FAIL;
800
801         stlink_usb_init_buffer(handle);
802
803         h->txbuf[0] = STLINK_DEBUG_COMMAND;
804         h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
805
806         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
807
808         if (res != ERROR_OK)
809                 return res;
810
811         return ERROR_OK;
812 }
813
814 /** */
815 static int stlink_usb_step(void *handle)
816 {
817         int res;
818         struct stlink_usb_handle_s *h;
819
820         assert(handle != NULL);
821
822         h = (struct stlink_usb_handle_s *)handle;
823
824         if (h->jtag_api == STLINK_JTAG_API_V2)
825                 return ERROR_FAIL;
826
827         stlink_usb_init_buffer(handle);
828
829         h->txbuf[0] = STLINK_DEBUG_COMMAND;
830         h->txbuf[1] = STLINK_DEBUG_STEPCORE;
831
832         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
833
834         if (res != ERROR_OK)
835                 return res;
836
837         return ERROR_OK;
838 }
839
840 /** */
841 static int stlink_usb_read_regs(void *handle)
842 {
843         int res;
844         struct stlink_usb_handle_s *h;
845
846         assert(handle != NULL);
847
848         h = (struct stlink_usb_handle_s *)handle;
849
850         stlink_usb_init_buffer(handle);
851
852         h->txbuf[0] = STLINK_DEBUG_COMMAND;
853         if (h->jtag_api == STLINK_JTAG_API_V1)
854                 h->txbuf[1] = STLINK_DEBUG_APIV1_READALLREGS;
855         else
856                 h->txbuf[1] = STLINK_DEBUG_APIV2_READALLREGS;
857
858         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
859
860         if (res != ERROR_OK)
861                 return res;
862
863         return ERROR_OK;
864 }
865
866 /** */
867 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
868 {
869         int res;
870         struct stlink_usb_handle_s *h;
871
872         assert(handle != NULL);
873
874         h = (struct stlink_usb_handle_s *)handle;
875
876         stlink_usb_init_buffer(handle);
877
878         h->txbuf[0] = STLINK_DEBUG_COMMAND;
879         if (h->jtag_api == STLINK_JTAG_API_V1)
880                 h->txbuf[1] = STLINK_DEBUG_APIV1_READREG;
881         else
882                 h->txbuf[1] = STLINK_DEBUG_APIV2_READREG;
883         h->txbuf[2] = num;
884
885         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
886
887         if (res != ERROR_OK)
888                 return res;
889
890         *val = le_to_h_u32(h->rxbuf);
891
892         return ERROR_OK;
893 }
894
895 /** */
896 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
897 {
898         int res;
899         struct stlink_usb_handle_s *h;
900
901         assert(handle != NULL);
902
903         h = (struct stlink_usb_handle_s *)handle;
904
905         stlink_usb_init_buffer(handle);
906
907         h->txbuf[0] = STLINK_DEBUG_COMMAND;
908         if (h->jtag_api == STLINK_JTAG_API_V1)
909                 h->txbuf[1] = STLINK_DEBUG_APIV1_WRITEREG;
910         else
911                 h->txbuf[1] = STLINK_DEBUG_APIV2_WRITEREG;
912         h->txbuf[2] = num;
913         h_u32_to_le(h->txbuf + 3, val);
914
915         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
916
917         if (res != ERROR_OK)
918                 return res;
919
920         return ERROR_OK;
921 }
922
923 /** */
924 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
925                           uint8_t *buffer)
926 {
927         int res;
928         uint16_t read_len = len;
929         struct stlink_usb_handle_s *h;
930
931         assert(handle != NULL);
932
933         h = (struct stlink_usb_handle_s *)handle;
934
935         stlink_usb_init_buffer(handle);
936
937         h->txbuf[0] = STLINK_DEBUG_COMMAND;
938         h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
939         h_u32_to_le(h->txbuf + 2, addr);
940         h_u16_to_le(h->txbuf + 2 + 4, len);
941
942         /* we need to fix read length for single bytes */
943         if (read_len == 1)
944                 read_len++;
945
946         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
947
948         if (res != ERROR_OK)
949                 return res;
950
951         memcpy(buffer, h->rxbuf, len);
952
953         return ERROR_OK;
954 }
955
956 /** */
957 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
958                            const uint8_t *buffer)
959 {
960         int res;
961         struct stlink_usb_handle_s *h;
962
963         assert(handle != NULL);
964
965         h = (struct stlink_usb_handle_s *)handle;
966
967         stlink_usb_init_buffer(handle);
968
969         h->txbuf[0] = STLINK_DEBUG_COMMAND;
970         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
971         h_u32_to_le(h->txbuf + 2, addr);
972         h_u16_to_le(h->txbuf + 2 + 4, len);
973
974         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
975
976         if (res != ERROR_OK)
977                 return res;
978
979         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
980
981         if (res != ERROR_OK)
982                 return res;
983
984         return ERROR_OK;
985 }
986
987 /** */
988 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
989                           uint32_t *buffer)
990 {
991         int res;
992         struct stlink_usb_handle_s *h;
993
994         assert(handle != NULL);
995
996         h = (struct stlink_usb_handle_s *)handle;
997
998         stlink_usb_init_buffer(handle);
999
1000         len *= 4;
1001
1002         h->txbuf[0] = STLINK_DEBUG_COMMAND;
1003         h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
1004         h_u32_to_le(h->txbuf + 2, addr);
1005         h_u16_to_le(h->txbuf + 2 + 4, len);
1006
1007         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
1008
1009         if (res != ERROR_OK)
1010                 return res;
1011
1012         memcpy(buffer, h->rxbuf, len);
1013
1014         return ERROR_OK;
1015 }
1016
1017 /** */
1018 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1019                            const uint32_t *buffer)
1020 {
1021         int res;
1022         struct stlink_usb_handle_s *h;
1023
1024         assert(handle != NULL);
1025
1026         h = (struct stlink_usb_handle_s *)handle;
1027
1028         stlink_usb_init_buffer(handle);
1029
1030         len *= 4;
1031
1032         h->txbuf[0] = STLINK_DEBUG_COMMAND;
1033         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
1034         h_u32_to_le(h->txbuf + 2, addr);
1035         h_u16_to_le(h->txbuf + 2 + 4, len);
1036
1037         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
1038
1039         if (res != ERROR_OK)
1040                 return res;
1041
1042         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
1043
1044         if (res != ERROR_OK)
1045                 return res;
1046
1047         return ERROR_OK;
1048 }
1049
1050 /** */
1051 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
1052 {
1053         int err;
1054         struct stlink_usb_handle_s *h;
1055
1056         LOG_DEBUG("stlink_usb_open");
1057
1058         h = malloc(sizeof(struct stlink_usb_handle_s));
1059
1060         if (h == 0) {
1061                 LOG_DEBUG("malloc failed");
1062                 return ERROR_FAIL;
1063         }
1064
1065         h->transport = param->transport;
1066
1067         const uint16_t vids[] = { param->vid, 0 };
1068         const uint16_t pids[] = { param->pid, 0 };
1069
1070         LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
1071                 param->vid, param->pid);
1072
1073         if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1074                 LOG_ERROR("open failed");
1075                 return ERROR_FAIL;
1076         }
1077
1078         jtag_libusb_set_configuration(h->fd, 0);
1079
1080         if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1081                 LOG_DEBUG("claim interface failed");
1082                 return ERROR_FAIL;
1083         }
1084
1085         /* wrap version for first read */
1086         switch (param->pid) {
1087                 case 0x3744:
1088                         h->version.stlink = 1;
1089                         break;
1090                 default:
1091                         h->version.stlink = 2;
1092                         break;
1093         }
1094
1095         /* get the device version */
1096         err = stlink_usb_version(h);
1097
1098         if (err != ERROR_OK) {
1099                 LOG_ERROR("read version failed");
1100                 jtag_libusb_close(h->fd);
1101                 free(h);
1102                 return err;
1103         }
1104
1105         /* compare usb vid/pid */
1106         if ((param->vid != h->vid) || (param->pid != h->pid))
1107                 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1108                         param->vid, param->pid,
1109                         h->vid, h->pid);
1110
1111         /* check if mode is supported */
1112         err = ERROR_OK;
1113
1114         switch (h->transport) {
1115                 case STLINK_TRANSPORT_SWD:
1116                 case STLINK_TRANSPORT_JTAG:
1117                         if (h->version.jtag == 0)
1118                                 err = ERROR_FAIL;
1119                         break;
1120                 case STLINK_TRANSPORT_SWIM:
1121                         if (h->version.swim == 0)
1122                                 err = ERROR_FAIL;
1123                         break;
1124                 default:
1125                         err = ERROR_FAIL;
1126                         break;
1127         }
1128
1129         if (err != ERROR_OK) {
1130                 LOG_ERROR("mode (transport) not supported by device");
1131                 jtag_libusb_close(h->fd);
1132                 free(h);
1133                 return err;
1134         }
1135
1136         /* set the used jtag api */
1137         h->jtag_api = STLINK_JTAG_API_V1;
1138
1139         /* initialize the debug hardware */
1140         err = stlink_usb_init_mode(h);
1141
1142         if (err != ERROR_OK) {
1143                 LOG_ERROR("init mode failed");
1144                 jtag_libusb_close(h->fd);
1145                 free(h);
1146                 return err;
1147         }
1148
1149         *fd = h;
1150
1151         return ERROR_OK;
1152 }
1153
1154 /** */
1155 static int stlink_usb_close(void *fd)
1156 {
1157         return ERROR_OK;
1158 }
1159
1160 /** */
1161 struct stlink_layout_api_s stlink_usb_layout_api = {
1162         /** */
1163         .open = stlink_usb_open,
1164         /** */
1165         .close = stlink_usb_close,
1166         /** */
1167         .idcode = stlink_usb_idcode,
1168         /** */
1169         .state = stlink_usb_state,
1170         /** */
1171         .reset = stlink_usb_reset,
1172         /** */
1173         .run = stlink_usb_run,
1174         /** */
1175         .halt = stlink_usb_halt,
1176         /** */
1177         .step = stlink_usb_step,
1178         /** */
1179         .read_regs = stlink_usb_read_regs,
1180         /** */
1181         .read_reg = stlink_usb_read_reg,
1182         /** */
1183         .write_reg = stlink_usb_write_reg,
1184         /** */
1185         .read_mem8 = stlink_usb_read_mem8,
1186         /** */
1187         .write_mem8 = stlink_usb_write_mem8,
1188         /** */
1189         .read_mem32 = stlink_usb_read_mem32,
1190         /** */
1191         .write_mem32 = stlink_usb_write_mem32,
1192 };