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 /* The STLink doesn't seem to evaluate the sequence number */
128 handle->sg_transfer_idx++;
132 return handle->rep_trans->actual_length;
135 static inline int send_only
136 (struct stlink_libusb* handle, int terminate,
137 unsigned char* txbuf, size_t txsize) {
138 return send_recv(handle, terminate, txbuf, txsize, NULL, 0);
142 /* Search for a STLINK device, either any or teh one with the given PID
143 * Return the protocoll version
145 static int is_stlink_device(libusb_device * dev, uint16_t pid) {
146 struct libusb_device_descriptor desc;
149 if (libusb_get_device_descriptor(dev, &desc))
152 if (desc.idVendor != USB_ST_VID)
155 if ((desc.idProduct != USB_STLINK_32L_PID) &&
156 (desc.idProduct != USB_STLINK_PID ))
159 if(pid && (pid != desc.idProduct))
161 if (desc.idProduct == USB_STLINK_PID )
169 static int fill_command
170 (stlink_t * sl, enum SCSI_Generic_Direction dir, uint32_t len) {
171 struct stlink_libusb * const slu = sl->backend_data;
172 unsigned char* const cmd = sl->c_buf;
174 memset(cmd, 0, sizeof (sl->q_buf));
175 if(slu->protocoll == 1) {
180 write_uint32(&cmd[i], slu->sg_transfer_idx);
182 write_uint32(&cmd[i], len);
184 cmd[i++] = (dir == SG_DXFER_FROM_DEV)?0x80:0;
185 cmd[i++] = 0; /* Logical unit */
186 cmd[i++] = 0xa; /* Command length */
191 void _stlink_usb_version(stlink_t *sl) {
192 struct stlink_libusb * const slu = sl->backend_data;
193 unsigned char* const data = sl->q_buf;
194 unsigned char* const cmd = sl->c_buf;
196 uint32_t rep_len = 6;
197 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
199 cmd[i++] = STLINK_GET_VERSION;
202 size = send_recv(slu, 1, cmd, slu->cmd_len, data, 6);
204 printf("[!] send_recv\n");
209 void _stlink_usb_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
210 struct stlink_libusb * const slu = sl->backend_data;
211 unsigned char* const data = sl->q_buf;
212 unsigned char* const cmd = sl->c_buf;
214 int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
215 cmd[i++] = STLINK_DEBUG_COMMAND;
216 cmd[i++] = STLINK_DEBUG_WRITEMEM_32BIT;
217 write_uint32(&cmd[i], addr);
219 write_uint16(&cmd[i], len);
220 send_only(slu, 0, cmd, slu->cmd_len);
222 #if Q_BUF_LEN < UINT16_MAX
223 assert(len < sizeof(sl->q_buf)); // makes a compiler warning? always true?
225 assert((len & 3) == 0);
226 send_only(slu, 1, data, len);
229 void _stlink_usb_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
230 struct stlink_libusb * const slu = sl->backend_data;
231 unsigned char* const data = sl->q_buf;
232 unsigned char* const cmd = sl->c_buf;
234 int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
235 cmd[i++] = STLINK_DEBUG_COMMAND;
236 cmd[i++] = STLINK_DEBUG_WRITEMEM_8BIT;
237 write_uint32(&cmd[i], addr);
239 write_uint16(&cmd[i], len);
240 send_only(slu, 0, cmd, slu->cmd_len);
242 #if Q_BUF_LEN < UINT16_MAX
243 assert(len < sizeof(sl->q_buf)); // makes a compiler warning? always true?
245 send_only(slu, 1, data, len);
249 int _stlink_usb_current_mode(stlink_t * sl) {
250 struct stlink_libusb * const slu = sl->backend_data;
251 unsigned char* const cmd = sl->c_buf;
252 unsigned char* const data = sl->q_buf;
255 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
257 cmd[i++] = STLINK_GET_CURRENT_MODE;
258 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
260 printf("[!] send_recv\n");
266 void _stlink_usb_core_id(stlink_t * sl) {
267 struct stlink_libusb * const slu = sl->backend_data;
268 unsigned char* const cmd = sl->c_buf;
269 unsigned char* const data = sl->q_buf;
272 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
274 cmd[i++] = STLINK_DEBUG_COMMAND;
275 cmd[i++] = STLINK_DEBUG_READCOREID;
277 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
279 printf("[!] send_recv\n");
283 sl->core_id = read_uint32(data, 0);
286 void _stlink_usb_status(stlink_t * sl) {
287 struct stlink_libusb * const slu = sl->backend_data;
288 unsigned char* const data = sl->q_buf;
289 unsigned char* const cmd = sl->c_buf;
292 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
294 cmd[i++] = STLINK_DEBUG_COMMAND;
295 cmd[i++] = STLINK_DEBUG_GETSTATUS;
297 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
299 printf("[!] send_recv\n");
304 void _stlink_usb_force_debug(stlink_t *sl) {
305 struct stlink_libusb *slu = sl->backend_data;
306 unsigned char* const data = sl->q_buf;
307 unsigned char* const cmd = sl->c_buf;
310 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
312 cmd[i++] = STLINK_DEBUG_COMMAND;
313 cmd[i++] = STLINK_DEBUG_FORCEDEBUG;
314 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
316 printf("[!] send_recv\n");
321 void _stlink_usb_enter_swd_mode(stlink_t * sl) {
322 struct stlink_libusb * const slu = sl->backend_data;
323 unsigned char* const cmd = sl->c_buf;
325 int i = fill_command(sl, SG_DXFER_FROM_DEV, 0);
327 cmd[i++] = STLINK_DEBUG_COMMAND;
328 cmd[i++] = STLINK_DEBUG_ENTER;
329 cmd[i++] = STLINK_DEBUG_ENTER_SWD;
331 size = send_only(slu, 1, cmd, slu->cmd_len);
333 printf("[!] send_recv\n");
338 void _stlink_usb_exit_dfu_mode(stlink_t* sl) {
339 struct stlink_libusb * const slu = sl->backend_data;
340 unsigned char* const cmd = sl->c_buf;
342 int i = fill_command(sl, SG_DXFER_FROM_DEV, 0);
344 cmd[i++] = STLINK_DFU_COMMAND;
345 cmd[i++] = STLINK_DFU_EXIT;
347 size = send_only(slu, 1, cmd, slu->cmd_len);
349 printf("[!] send_recv\n");
355 * TODO - not convinced this does anything...
358 void _stlink_usb_reset(stlink_t * sl) {
359 struct stlink_libusb * const slu = sl->backend_data;
360 unsigned char* const data = sl->q_buf;
361 unsigned char* const cmd = sl->c_buf;
364 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
366 cmd[i++] = STLINK_DEBUG_COMMAND;
367 cmd[i++] = STLINK_DEBUG_RESETSYS;
369 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
371 printf("[!] send_recv\n");
377 void _stlink_usb_step(stlink_t* sl) {
378 struct stlink_libusb * const slu = sl->backend_data;
379 unsigned char* const data = sl->q_buf;
380 unsigned char* const cmd = sl->c_buf;
383 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
385 cmd[i++] = STLINK_DEBUG_COMMAND;
386 cmd[i++] = STLINK_DEBUG_STEPCORE;
388 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
390 printf("[!] send_recv\n");
396 * This seems to do a good job of restarting things from the beginning?
399 void _stlink_usb_run(stlink_t* sl) {
400 struct stlink_libusb * const slu = sl->backend_data;
401 unsigned char* const data = sl->q_buf;
402 unsigned char* const cmd = sl->c_buf;
405 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
407 cmd[i++] = STLINK_DEBUG_COMMAND;
408 cmd[i++] = STLINK_DEBUG_RUNCORE;
410 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
412 printf("[!] send_recv\n");
417 void _stlink_usb_exit_debug_mode(stlink_t *sl) {
418 struct stlink_libusb * const slu = sl->backend_data;
419 unsigned char* const cmd = sl->c_buf;
421 int i = fill_command(sl, SG_DXFER_TO_DEV, 0);
423 cmd[i++] = STLINK_DEBUG_COMMAND;
424 cmd[i++] = STLINK_DEBUG_EXIT;
426 size = send_only(slu, 1, cmd, slu->cmd_len);
428 printf("[!] send_only\n");
433 void _stlink_usb_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
434 struct stlink_libusb * const slu = sl->backend_data;
435 unsigned char* const data = sl->q_buf;
436 unsigned char* const cmd = sl->c_buf;
438 int i = fill_command(sl, SG_DXFER_FROM_DEV, len);
440 #if Q_BUF_LEN < UINT16_MAX
441 assert(len < sizeof(sl->q_buf));
444 cmd[i++] = STLINK_DEBUG_COMMAND;
445 cmd[i++] = STLINK_DEBUG_READMEM_32BIT;
446 write_uint32(&cmd[i], addr);
448 /* windows usb logs show only one byte is used for length ... */
449 // Presumably, this is because usb transfers can't be 16 bits worth of bytes long...
451 cmd[i] = (uint8_t) len;
453 size = send_recv(slu, 1, cmd, slu->cmd_len, data, len);
455 printf("[!] send_recv\n");
459 sl->q_len = (size_t) size;
461 stlink_print_data(sl);
464 void _stlink_usb_read_all_regs(stlink_t *sl, reg *regp) {
465 struct stlink_libusb * const slu = sl->backend_data;
466 unsigned char* const cmd = sl->c_buf;
467 unsigned char* const data = sl->q_buf;
469 uint32_t rep_len = 84;
470 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
472 cmd[i++] = STLINK_DEBUG_COMMAND;
473 cmd[i++] = STLINK_DEBUG_READALLREGS;
474 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
476 printf("[!] send_recv\n");
479 sl->q_len = (size_t) size;
480 stlink_print_data(sl);
482 regp->r[i]= read_uint32(sl->q_buf, i*4);
483 regp->xpsr = read_uint32(sl->q_buf, 64);
484 regp->main_sp = read_uint32(sl->q_buf, 68);
485 regp->process_sp = read_uint32(sl->q_buf, 72);
486 regp->rw = read_uint32(sl->q_buf, 76);
487 regp->rw2 = read_uint32(sl->q_buf, 80);
491 DD(sl, "xpsr = 0x%08x\n", read_uint32(sl->q_buf, 64));
492 DD(sl, "main_sp = 0x%08x\n", read_uint32(sl->q_buf, 68));
493 DD(sl, "process_sp = 0x%08x\n", read_uint32(sl->q_buf, 72));
494 DD(sl, "rw = 0x%08x\n", read_uint32(sl->q_buf, 76));
495 DD(sl, "rw2 = 0x%08x\n", read_uint32(sl->q_buf, 80));
498 void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
499 struct stlink_libusb * const slu = sl->backend_data;
500 unsigned char* const data = sl->q_buf;
501 unsigned char* const cmd = sl->c_buf;
504 uint32_t rep_len = 4;
505 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
507 cmd[i++] = STLINK_DEBUG_COMMAND;
508 cmd[i++] = STLINK_DEBUG_READREG;
509 cmd[i++] = (uint8_t) r_idx;
510 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
512 printf("[!] send_recv\n");
515 sl->q_len = (size_t) size;
516 stlink_print_data(sl);
517 r = read_uint32(sl->q_buf, 0);
518 DD(sl, "r_idx (%2d) = 0x%08x\n", r_idx, r);
528 regp->process_sp = r;
531 regp->rw = r; /* XXX ?(primask, basemask etc.) */
534 regp->rw2 = r; /* XXX ?(primask, basemask etc.) */
541 void _stlink_usb_write_reg(stlink_t *sl, uint32_t reg, int idx) {
542 struct stlink_libusb * const slu = sl->backend_data;
543 unsigned char* const data = sl->q_buf;
544 unsigned char* const cmd = sl->c_buf;
546 uint32_t rep_len = 2;
547 int i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);
549 cmd[i++] = STLINK_DEBUG_COMMAND;
550 cmd[i++] = STLINK_DEBUG_WRITEREG;
552 write_uint32(&cmd[i], reg);
553 size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
555 printf("[!] send_recv\n");
558 sl->q_len = (size_t) size;
559 stlink_print_data(sl);
562 stlink_backend_t _stlink_usb_backend = {
564 _stlink_usb_exit_debug_mode,
565 _stlink_usb_enter_swd_mode,
566 NULL, // no enter_jtag_mode here...
567 _stlink_usb_exit_dfu_mode,
573 _stlink_usb_read_mem32,
574 _stlink_usb_write_mem32,
575 _stlink_usb_write_mem8,
576 _stlink_usb_read_all_regs,
577 _stlink_usb_read_reg,
578 _stlink_usb_write_reg,
580 _stlink_usb_current_mode,
581 _stlink_usb_force_debug
585 stlink_t* stlink_open_usb(const int verbose) {
587 struct stlink_libusb* slu = NULL;
589 libusb_device** devs = NULL;
594 char *iSerial = NULL;
596 sl = malloc(sizeof (stlink_t));
597 slu = malloc(sizeof (struct stlink_libusb));
598 if (sl == NULL) goto on_error;
599 if (slu == NULL) goto on_error;
600 memset(sl, 0, sizeof (stlink_t));
601 memset(slu, 0, sizeof (struct stlink_libusb));
603 sl->verbose = verbose;
604 sl->backend = &_stlink_usb_backend;
605 sl->backend_data = slu;
607 sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
609 /* flash memory settings */
610 sl->flash_base = STM32_FLASH_BASE;
611 sl->flash_size = STM32_FLASH_SIZE;
612 sl->flash_pgsz = STM32_FLASH_PGSZ;
615 sl->sys_base = STM32_SYSTEM_BASE;
616 sl->sys_size = STM32_SYSTEM_SIZE;
618 /* sram memory settings */
619 sl->sram_base = STM32_SRAM_BASE;
620 sl->sram_size = STM32L_SRAM_SIZE;
622 if (libusb_init(&(slu->libusb_ctx))) {
623 fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
627 count = libusb_get_device_list(slu->libusb_ctx, &devs);
629 printf("libusb_get_device_list\n");
630 goto on_libusb_error;
633 for (i = 0; i < count; ++i) {
635 slu->protocoll = is_stlink_device(dev, 0);
636 if (slu->protocoll > 0) break;
638 if (i == count) goto on_libusb_error;
640 if (libusb_open(dev, &(slu->usb_handle))) {
641 printf("libusb_open()\n");
642 goto on_libusb_error;
646 unsigned char serial[256];
647 struct libusb_device_descriptor desc;
650 r = libusb_get_device_descriptor(dev, &desc);
652 printf("Can't get descriptor to match Iserial\n");
653 goto on_libusb_error;
655 r = libusb_get_string_descriptor_ascii
656 (slu->usb_handle, desc.iSerialNumber, serial, 256);
658 printf("Can't get Serialnumber to match Iserial\n");
659 goto on_libusb_error;
661 if (strcmp((char*)serial, iSerial)) {
662 printf("Mismatch in serial numbers, dev %s vs given %s\n",
664 goto on_libusb_error;
668 if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
671 r = libusb_detach_kernel_driver(slu->usb_handle, 0);
673 printf("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
674 goto on_libusb_error;
677 libusb_reset_device(slu->usb_handle);
679 if (libusb_get_configuration(slu->usb_handle, &config)) {
680 /* this may fail for a previous configured device */
681 printf("libusb_get_configuration()\n");
682 goto on_libusb_error;
686 printf("setting new configuration (%d -> 1)\n", config);
687 if (libusb_set_configuration(slu->usb_handle, 1)) {
688 /* this may fail for a previous configured device */
689 printf("libusb_set_configuration()\n");
690 goto on_libusb_error;
694 if (libusb_claim_interface(slu->usb_handle, 0)) {
695 printf("libusb_claim_interface()\n");
696 goto on_libusb_error;
699 slu->req_trans = libusb_alloc_transfer(0);
700 if (slu->req_trans == NULL) {
701 printf("libusb_alloc_transfer\n");
702 goto on_libusb_error;
705 slu->rep_trans = libusb_alloc_transfer(0);
706 if (slu->rep_trans == NULL) {
707 printf("libusb_alloc_transfer\n");
708 goto on_libusb_error;
711 slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
712 slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
714 /* libusb_reset_device(slu->usb_handle); */
715 slu->sg_transfer_idx = 0;
716 slu->cmd_len = (slu->protocoll == 1)? STLINK_SG_SIZE: STLINK_CMD_SIZE;
719 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
720 printf("-- exit_dfu_mode\n");
721 stlink_exit_dfu_mode(sl);
728 libusb_free_device_list(devs, 1);
740 if (sl != NULL) free(sl);
741 if (slu != NULL) free(slu);