8 #include <libusb-1.0/libusb.h>
9 #include "stlink-common.h"
10 #include "stlink-usb.h"
12 enum SCSI_Generic_Direction {SG_DXFER_TO_DEV=0, SG_DXFER_FROM_DEV=0x80};
14 void _stlink_usb_close(stlink_t* sl) {
15 struct stlink_libusb * const handle = sl->backend_data;
16 // maybe we couldn't even get the usb device?
18 if (handle->req_trans != NULL)
19 libusb_free_transfer(handle->req_trans);
21 if (handle->rep_trans != NULL)
22 libusb_free_transfer(handle->rep_trans);
24 if (handle->usb_handle != NULL) {
25 libusb_close(handle->usb_handle);
28 libusb_exit(handle->libusb_ctx);
35 #define TRANS_FLAGS_IS_DONE (1 << 0)
36 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
37 volatile unsigned long flags;
40 static void on_trans_done(struct libusb_transfer * trans) {
41 struct trans_ctx * const ctx = trans->user_data;
43 if (trans->status != LIBUSB_TRANSFER_COMPLETED)
44 ctx->flags |= TRANS_FLAGS_HAS_ERROR;
46 ctx->flags |= TRANS_FLAGS_IS_DONE;
49 int submit_wait(struct stlink_libusb *slu, struct libusb_transfer * trans) {
53 struct trans_ctx trans_ctx;
54 enum libusb_error error;
58 /* brief intrusion inside the libusb interface */
59 trans->callback = on_trans_done;
60 trans->user_data = &trans_ctx;
62 if ((error = libusb_submit_transfer(trans))) {
63 printf("libusb_submit_transfer(%d)\n", error);
67 gettimeofday(&start, NULL);
69 while (trans_ctx.flags == 0) {
70 struct timeval timeout;
73 if (libusb_handle_events_timeout(slu->libusb_ctx, &timeout)) {
74 printf("libusb_handle_events()\n");
78 gettimeofday(&now, NULL);
79 timersub(&now, &start, &diff);
80 if (diff.tv_sec >= 3) {
81 printf("libusb_handle_events() timeout\n");
86 if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) {
87 printf("libusb_handle_events() | has_error\n");
94 ssize_t send_recv(struct stlink_libusb* handle, int terminate,
95 unsigned char* txbuf, size_t txsize,
96 unsigned char* rxbuf, size_t rxsize) {
97 /* note: txbuf and rxbuf can point to the same area */
100 libusb_fill_bulk_transfer(handle->req_trans, handle->usb_handle,
107 if (submit_wait(handle, handle->req_trans)) return -1;
112 /* read the response */
114 libusb_fill_bulk_transfer(handle->rep_trans, handle->usb_handle,
115 handle->ep_rep, rxbuf, rxsize, NULL, NULL, 0);
117 if (submit_wait(handle, handle->rep_trans)) return -1;
118 res = handle->rep_trans->actual_length;
120 if ((handle->protocoll == 1) && terminate) {
121 /* Read the SG reply */
122 unsigned char sg_buf[13];
123 libusb_fill_bulk_transfer
124 (handle->rep_trans, handle->usb_handle,
125 handle->ep_rep, sg_buf, 13, NULL, NULL, 0);
126 res = submit_wait(handle, handle->rep_trans);
127 handle->sg_transfer_idx++;
131 return handle->rep_trans->actual_length;
134 static inline int send_only
135 (struct stlink_libusb* handle, int terminate,
136 unsigned char* txbuf, size_t txsize) {
137 return send_recv(handle, terminate, txbuf, txsize, NULL, 0);
141 /* Search for a STLINK device, either any or teh one with the given PID
142 * Return the protocoll version
144 static int is_stlink_device(libusb_device * dev, uint16_t pid) {
145 struct libusb_device_descriptor desc;
148 if (libusb_get_device_descriptor(dev, &desc))
151 if (desc.idVendor != USB_ST_VID)
154 if ((desc.idProduct != USB_STLINK_32L_PID) &&
155 (desc.idProduct != USB_STLINK_PID ))
158 if(pid && (pid != desc.idProduct))
160 if (desc.idProduct == USB_STLINK_PID )
168 static int fill_command
169 (stlink_t * sl, enum SCSI_Generic_Direction dir, uint32_t len) {
170 struct stlink_libusb * const slu = sl->backend_data;
171 unsigned char* const cmd = sl->c_buf;
173 memset(cmd, 0, sizeof (sl->q_buf));
174 if(slu->protocoll == 1) {
179 write_uint32(&cmd[i], slu->sg_transfer_idx);
181 write_uint32(&cmd[i], len);
183 cmd[i++] = (dir == SG_DXFER_FROM_DEV)?0x80:0;
184 cmd[i++] = 0; /* Logical unit */
185 cmd[i++] = 0xa; /* Command length */
190 void _stlink_usb_version(stlink_t *sl) {
191 struct stlink_libusb * const slu = sl->backend_data;
192 unsigned char* const data = sl->q_buf;
193 unsigned char* const cmd = sl->c_buf;
195 uint32_t rep_len = 6;
196 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
198 cmd[i++] = STLINK_GET_VERSION;
201 size = send_recv(slu, 1, cmd, slu->cmd_len, data, 6);
203 printf("[!] send_recv\n");
208 void _stlink_usb_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
209 struct stlink_libusb * const slu = sl->backend_data;
210 unsigned char* const data = sl->q_buf;
211 unsigned char* const cmd = sl->c_buf;
213 int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
214 cmd[i++] = STLINK_DEBUG_COMMAND;
215 cmd[i++] = STLINK_DEBUG_WRITEMEM_32BIT;
216 write_uint32(&cmd[i], addr);
218 write_uint16(&cmd[i], len);
219 send_only(slu, 0, cmd, slu->cmd_len);
221 #if Q_BUF_LEN < UINT16_MAX
222 assert(len < sizeof(sl->q_buf)); // makes a compiler warning? always true?
224 assert((len & 3) == 0);
225 send_only(slu, 1, data, len);
228 void _stlink_usb_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
229 struct stlink_libusb * const slu = sl->backend_data;
230 unsigned char* const data = sl->q_buf;
231 unsigned char* const cmd = sl->c_buf;
233 int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
234 cmd[i++] = STLINK_DEBUG_COMMAND;
235 cmd[i++] = STLINK_DEBUG_WRITEMEM_8BIT;
236 write_uint32(&cmd[i], addr);
238 write_uint16(&cmd[i], len);
239 send_only(slu, 0, cmd, slu->cmd_len);
241 #if Q_BUF_LEN < UINT16_MAX
242 assert(len < sizeof(sl->q_buf)); // makes a compiler warning? always true?
244 send_only(slu, 1, data, len);
248 int _stlink_usb_current_mode(stlink_t * sl) {
249 struct stlink_libusb * const slu = sl->backend_data;
250 unsigned char* const cmd = sl->c_buf;
251 unsigned char* const data = sl->q_buf;
254 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
256 cmd[i++] = STLINK_GET_CURRENT_MODE;
257 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
259 printf("[!] send_recv\n");
265 void _stlink_usb_core_id(stlink_t * sl) {
266 struct stlink_libusb * const slu = sl->backend_data;
267 unsigned char* const cmd = sl->c_buf;
268 unsigned char* const data = sl->q_buf;
271 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
273 cmd[i++] = STLINK_DEBUG_COMMAND;
274 cmd[i++] = STLINK_DEBUG_READCOREID;
276 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
278 printf("[!] send_recv\n");
282 sl->core_id = read_uint32(data, 0);
285 void _stlink_usb_status(stlink_t * sl) {
286 struct stlink_libusb * const slu = sl->backend_data;
287 unsigned char* const data = sl->q_buf;
288 unsigned char* const cmd = sl->c_buf;
291 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
293 cmd[i++] = STLINK_DEBUG_COMMAND;
294 cmd[i++] = STLINK_DEBUG_GETSTATUS;
296 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
298 printf("[!] send_recv\n");
303 void _stlink_usb_force_debug(stlink_t *sl) {
304 struct stlink_libusb *slu = sl->backend_data;
305 unsigned char* const data = sl->q_buf;
306 unsigned char* const cmd = sl->c_buf;
309 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
311 cmd[i++] = STLINK_DEBUG_COMMAND;
312 cmd[i++] = STLINK_DEBUG_FORCEDEBUG;
313 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
315 printf("[!] send_recv\n");
320 void _stlink_usb_enter_swd_mode(stlink_t * sl) {
321 struct stlink_libusb * const slu = sl->backend_data;
322 unsigned char* const cmd = sl->c_buf;
324 int i = fill_command(sl, SG_DXFER_FROM_DEV, 0);
326 cmd[i++] = STLINK_DEBUG_COMMAND;
327 cmd[i++] = STLINK_DEBUG_ENTER;
328 cmd[i++] = STLINK_DEBUG_ENTER_SWD;
330 size = send_only(slu, 1, cmd, slu->cmd_len);
332 printf("[!] send_recv\n");
337 void _stlink_usb_exit_dfu_mode(stlink_t* sl) {
338 struct stlink_libusb * const slu = sl->backend_data;
339 unsigned char* const cmd = sl->c_buf;
341 int i = fill_command(sl, SG_DXFER_FROM_DEV, 0);
343 cmd[i++] = STLINK_DFU_COMMAND;
344 cmd[i++] = STLINK_DFU_EXIT;
346 size = send_only(slu, 1, cmd, slu->cmd_len);
348 printf("[!] send_recv\n");
354 * TODO - not convinced this does anything...
357 void _stlink_usb_reset(stlink_t * sl) {
358 struct stlink_libusb * const slu = sl->backend_data;
359 unsigned char* const data = sl->q_buf;
360 unsigned char* const cmd = sl->c_buf;
363 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
365 cmd[i++] = STLINK_DEBUG_COMMAND;
366 cmd[i++] = STLINK_DEBUG_RESETSYS;
368 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
370 printf("[!] send_recv\n");
376 void _stlink_usb_step(stlink_t* sl) {
377 struct stlink_libusb * const slu = sl->backend_data;
378 unsigned char* const data = sl->q_buf;
379 unsigned char* const cmd = sl->c_buf;
382 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
384 cmd[i++] = STLINK_DEBUG_COMMAND;
385 cmd[i++] = STLINK_DEBUG_STEPCORE;
387 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
389 printf("[!] send_recv\n");
395 * This seems to do a good job of restarting things from the beginning?
398 void _stlink_usb_run(stlink_t* sl) {
399 struct stlink_libusb * const slu = sl->backend_data;
400 unsigned char* const data = sl->q_buf;
401 unsigned char* const cmd = sl->c_buf;
404 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
406 cmd[i++] = STLINK_DEBUG_COMMAND;
407 cmd[i++] = STLINK_DEBUG_RUNCORE;
409 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
411 printf("[!] send_recv\n");
416 void _stlink_usb_exit_debug_mode(stlink_t *sl) {
417 struct stlink_libusb * const slu = sl->backend_data;
418 unsigned char* const cmd = sl->c_buf;
420 int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
422 cmd[i++] = STLINK_DEBUG_COMMAND;
423 cmd[i++] = STLINK_DEBUG_EXIT;
425 size = send_only(slu, 1, cmd, slu->cmd_len);
427 printf("[!] send_only\n");
432 void _stlink_usb_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
433 struct stlink_libusb * const slu = sl->backend_data;
434 unsigned char* const data = sl->q_buf;
435 unsigned char* const cmd = sl->c_buf;
437 int i = fill_command(sl, SG_DXFER_FROM_DEV, len);
439 #if Q_BUF_LEN < UINT16_MAX
440 assert(len < sizeof(sl->q_buf));
443 cmd[i++] = STLINK_DEBUG_COMMAND;
444 cmd[i++] = STLINK_DEBUG_READMEM_32BIT;
445 write_uint32(&cmd[i], addr);
447 /* windows usb logs show only one byte is used for length ... */
448 // Presumably, this is because usb transfers can't be 16 bits worth of bytes long...
450 cmd[i] = (uint8_t) len;
452 size = send_recv(slu, 1, cmd, slu->cmd_len, data, len);
454 printf("[!] send_recv\n");
458 sl->q_len = (size_t) size;
460 stlink_print_data(sl);
463 void _stlink_usb_read_all_regs(stlink_t *sl, reg *regp) {
464 struct stlink_libusb * const slu = sl->backend_data;
465 unsigned char* const cmd = sl->c_buf;
466 unsigned char* const data = sl->q_buf;
468 uint32_t rep_len = 84;
469 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
471 cmd[i++] = STLINK_DEBUG_COMMAND;
472 cmd[i++] = STLINK_DEBUG_READALLREGS;
473 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
475 printf("[!] send_recv\n");
478 sl->q_len = (size_t) size;
479 stlink_print_data(sl);
481 regp->r[i]= read_uint32(sl->q_buf, i*4);
482 regp->xpsr = read_uint32(sl->q_buf, 64);
483 regp->main_sp = read_uint32(sl->q_buf, 68);
484 regp->process_sp = read_uint32(sl->q_buf, 72);
485 regp->rw = read_uint32(sl->q_buf, 76);
486 regp->rw2 = read_uint32(sl->q_buf, 80);
490 DD(sl, "xpsr = 0x%08x\n", read_uint32(sl->q_buf, 64));
491 DD(sl, "main_sp = 0x%08x\n", read_uint32(sl->q_buf, 68));
492 DD(sl, "process_sp = 0x%08x\n", read_uint32(sl->q_buf, 72));
493 DD(sl, "rw = 0x%08x\n", read_uint32(sl->q_buf, 76));
494 DD(sl, "rw2 = 0x%08x\n", read_uint32(sl->q_buf, 80));
497 void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
498 struct stlink_libusb * const slu = sl->backend_data;
499 unsigned char* const data = sl->q_buf;
500 unsigned char* const cmd = sl->c_buf;
503 uint32_t rep_len = 4;
504 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
506 cmd[i++] = STLINK_DEBUG_COMMAND;
507 cmd[i++] = STLINK_DEBUG_READREG;
508 cmd[i++] = (uint8_t) r_idx;
509 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
511 printf("[!] send_recv\n");
514 sl->q_len = (size_t) size;
515 stlink_print_data(sl);
516 r = read_uint32(sl->q_buf, 0);
517 DD(sl, "r_idx (%2d) = 0x%08x\n", r_idx, r);
527 regp->process_sp = r;
530 regp->rw = r; /* XXX ?(primask, basemask etc.) */
533 regp->rw2 = r; /* XXX ?(primask, basemask etc.) */
540 void _stlink_usb_write_reg(stlink_t *sl, uint32_t reg, int idx) {
541 struct stlink_libusb * const slu = sl->backend_data;
542 unsigned char* const data = sl->q_buf;
543 unsigned char* const cmd = sl->c_buf;
545 uint32_t rep_len = 2;
546 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
548 cmd[i++] = STLINK_DEBUG_COMMAND;
549 cmd[i++] = STLINK_DEBUG_WRITEREG;
551 write_uint32(&cmd[i], reg);
552 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
554 printf("[!] send_recv\n");
557 sl->q_len = (size_t) size;
558 stlink_print_data(sl);
561 stlink_backend_t _stlink_usb_backend = {
563 _stlink_usb_exit_debug_mode,
564 _stlink_usb_enter_swd_mode,
565 NULL, // no enter_jtag_mode here...
566 _stlink_usb_exit_dfu_mode,
572 _stlink_usb_read_mem32,
573 _stlink_usb_write_mem32,
574 _stlink_usb_write_mem8,
575 _stlink_usb_read_all_regs,
576 _stlink_usb_read_reg,
577 _stlink_usb_write_reg,
579 _stlink_usb_current_mode,
580 _stlink_usb_force_debug
584 stlink_t* stlink_open_usb(const int verbose) {
586 struct stlink_libusb* slu = NULL;
588 libusb_device** devs = NULL;
593 char *iSerial = NULL;
595 sl = malloc(sizeof (stlink_t));
596 slu = malloc(sizeof (struct stlink_libusb));
597 if (sl == NULL) goto on_error;
598 if (slu == NULL) goto on_error;
599 memset(sl, 0, sizeof (stlink_t));
600 memset(slu, 0, sizeof (struct stlink_libusb));
602 sl->verbose = verbose;
603 sl->backend = &_stlink_usb_backend;
604 sl->backend_data = slu;
606 sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
608 /* flash memory settings */
609 sl->flash_base = STM32_FLASH_BASE;
610 sl->flash_size = STM32_FLASH_SIZE;
611 sl->flash_pgsz = STM32_FLASH_PGSZ;
614 sl->sys_base = STM32_SYSTEM_BASE;
615 sl->sys_size = STM32_SYSTEM_SIZE;
617 /* sram memory settings */
618 sl->sram_base = STM32_SRAM_BASE;
619 sl->sram_size = STM32L_SRAM_SIZE;
621 if (libusb_init(&(slu->libusb_ctx))) {
622 fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
626 count = libusb_get_device_list(slu->libusb_ctx, &devs);
628 printf("libusb_get_device_list\n");
629 goto on_libusb_error;
632 for (i = 0; i < count; ++i) {
634 slu->protocoll = is_stlink_device(dev, 0);
635 if (slu->protocoll > 0) break;
637 if (i == count) goto on_libusb_error;
639 if (libusb_open(dev, &(slu->usb_handle))) {
640 printf("libusb_open()\n");
641 goto on_libusb_error;
645 unsigned char serial[256];
646 struct libusb_device_descriptor desc;
649 r = libusb_get_device_descriptor(dev, &desc);
651 printf("Can't get descriptor to match Iserial\n");
652 goto on_libusb_error;
654 r = libusb_get_string_descriptor_ascii
655 (slu->usb_handle, desc.iSerialNumber, serial, 256);
657 printf("Can't get Serialnumber to match Iserial\n");
658 goto on_libusb_error;
660 if (strcmp((char*)serial, iSerial)) {
661 printf("Mismatch in serial numbers, dev %s vs given %s\n",
663 goto on_libusb_error;
667 if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
670 r = libusb_detach_kernel_driver(slu->usb_handle, 0);
672 printf("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
673 goto on_libusb_error;
676 libusb_reset_device(slu->usb_handle);
678 if (libusb_get_configuration(slu->usb_handle, &config)) {
679 /* this may fail for a previous configured device */
680 printf("libusb_get_configuration()\n");
681 goto on_libusb_error;
685 printf("setting new configuration (%d -> 1)\n", config);
686 if (libusb_set_configuration(slu->usb_handle, 1)) {
687 /* this may fail for a previous configured device */
688 printf("libusb_set_configuration()\n");
689 goto on_libusb_error;
693 if (libusb_claim_interface(slu->usb_handle, 0)) {
694 printf("libusb_claim_interface()\n");
695 goto on_libusb_error;
698 slu->req_trans = libusb_alloc_transfer(0);
699 if (slu->req_trans == NULL) {
700 printf("libusb_alloc_transfer\n");
701 goto on_libusb_error;
704 slu->rep_trans = libusb_alloc_transfer(0);
705 if (slu->rep_trans == NULL) {
706 printf("libusb_alloc_transfer\n");
707 goto on_libusb_error;
710 slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
711 slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
713 /* libusb_reset_device(slu->usb_handle); */
714 slu->sg_transfer_idx = 0;
715 slu->cmd_len = (slu->protocoll == 1)? STLINK_SG_SIZE: STLINK_CMD_SIZE;
718 stlink_exit_dfu_mode(sl);
724 libusb_free_device_list(devs, 1);
736 if (sl != NULL) free(sl);
737 if (slu != NULL) free(slu);