8 #include <libusb-1.0/libusb.h>
9 #include "stlink-common.h"
10 #include "stlink-usb.h"
12 void _stlink_usb_close(stlink_t* sl) {
13 struct stlink_libusb * const handle = sl->backend_data;
14 // maybe we couldn't even get the usb device?
16 if (handle->req_trans != NULL)
17 libusb_free_transfer(handle->req_trans);
19 if (handle->rep_trans != NULL)
20 libusb_free_transfer(handle->rep_trans);
22 if (handle->usb_handle != NULL)
23 libusb_close(handle->usb_handle);
25 libusb_exit(handle->libusb_ctx);
32 #define TRANS_FLAGS_IS_DONE (1 << 0)
33 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
34 volatile unsigned long flags;
37 static void on_trans_done(struct libusb_transfer * trans) {
38 struct trans_ctx * const ctx = trans->user_data;
40 if (trans->status != LIBUSB_TRANSFER_COMPLETED)
41 ctx->flags |= TRANS_FLAGS_HAS_ERROR;
43 ctx->flags |= TRANS_FLAGS_IS_DONE;
46 int submit_wait(struct stlink_libusb *slu, struct libusb_transfer * trans) {
50 struct trans_ctx trans_ctx;
51 enum libusb_error error;
55 /* brief intrusion inside the libusb interface */
56 trans->callback = on_trans_done;
57 trans->user_data = &trans_ctx;
59 if ((error = libusb_submit_transfer(trans))) {
60 printf("libusb_submit_transfer(%d)\n", error);
64 gettimeofday(&start, NULL);
66 while (trans_ctx.flags == 0) {
67 struct timeval timeout;
70 if (libusb_handle_events_timeout(slu->libusb_ctx, &timeout)) {
71 printf("libusb_handle_events()\n");
75 gettimeofday(&now, NULL);
76 timersub(&now, &start, &diff);
77 if (diff.tv_sec >= 3) {
78 printf("libusb_handle_events() timeout\n");
83 if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) {
84 printf("libusb_handle_events() | has_error\n");
91 ssize_t send_recv(struct stlink_libusb* handle,
92 unsigned char* txbuf, size_t txsize,
93 unsigned char* rxbuf, size_t rxsize) {
94 /* note: txbuf and rxbuf can point to the same area */
96 libusb_fill_bulk_transfer(handle->req_trans, handle->usb_handle,
103 printf("submit_wait(req)\n");
105 if (submit_wait(handle, handle->req_trans)) return -1;
108 if (rxsize == 0) return 0;
110 /* read the response */
112 libusb_fill_bulk_transfer(handle->rep_trans, handle->usb_handle,
113 handle->ep_rep, rxbuf, rxsize, NULL, NULL, 0);
115 printf("submit_wait(rep)\n");
117 if (submit_wait(handle, handle->rep_trans)) return -1;
119 return handle->rep_trans->actual_length;
122 static inline int send_only
123 (struct stlink_libusb* handle, unsigned char* txbuf, size_t txsize) {
124 return send_recv(handle, txbuf, txsize, NULL, 0);
128 // KARL - fixme, common code! (or, one per backend)
129 // candidate for common code...
132 static int is_stlink_device(libusb_device * dev) {
133 struct libusb_device_descriptor desc;
135 if (libusb_get_device_descriptor(dev, &desc))
138 printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
140 if (desc.idVendor != USB_ST_VID)
143 if (desc.idProduct != USB_STLINK_32L_PID)
149 void _stlink_usb_version(stlink_t *sl) {
150 struct stlink_libusb * const slu = sl->backend_data;
151 unsigned char* const buf = sl->q_buf;
154 memset(buf, 0, sizeof (sl->q_buf));
155 buf[0] = STLINK_GET_VERSION;
158 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
160 printf("[!] send_recv\n");
167 for (i = 0; i < size; ++i) printf("%02x", buf[i]);
173 void _stlink_usb_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
174 DD(sl, "oops! no write32 support yet, wanted to write %d bytes to %#x\n",
178 void _stlink_usb_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
179 DD(sl, "oops! no write8 support yet, wanted to write %d bytes to %#x\n",
184 int _stlink_usb_current_mode(stlink_t * sl) {
185 struct stlink_libusb * const slu = sl->backend_data;
186 unsigned char* const buf = sl->q_buf;
188 memset(buf, 0, sizeof (sl->q_buf));
189 buf[0] = STLINK_GET_CURRENT_MODE;
190 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
192 printf("[!] send_recv\n");
198 void _stlink_usb_core_id(stlink_t * sl) {
199 struct stlink_libusb * const slu = sl->backend_data;
200 unsigned char* const buf = sl->q_buf;
203 memset(buf, 0, sizeof (sl->q_buf));
204 buf[0] = STLINK_DEBUG_COMMAND;
205 buf[1] = STLINK_DEBUG_READCOREID;
207 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
209 printf("[!] send_recv\n");
213 sl->core_id = read_uint32(buf, 0);
216 void _stlink_usb_status(stlink_t * sl) {
217 struct stlink_libusb * const slu = sl->backend_data;
218 unsigned char* const buf = sl->q_buf;
221 memset(buf, 0, sizeof (sl->q_buf));
223 buf[0] = STLINK_DEBUG_COMMAND;
224 buf[1] = STLINK_DEBUG_GETSTATUS;
226 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
228 printf("[!] send_recv\n");
232 /* todo: stlink_core_stat */
234 // FIXME - decode into sl->core_stat
236 printf("status == 0x%x\n", buf[0]);
241 void _stlink_usb_force_debug(stlink_t *sl) {
242 struct stlink_libusb *slu = sl->backend_data;
243 unsigned char* const buf = sl->q_buf;
246 memset(buf, 0, sizeof (sl->q_buf));
248 buf[0] = STLINK_DEBUG_COMMAND;
249 buf[1] = STLINK_DEBUG_FORCEDEBUG;
250 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
252 printf("[!] send_recv\n");
258 void _stlink_usb_enter_swd_mode(stlink_t * sl) {
259 struct stlink_libusb * const slu = sl->backend_data;
260 unsigned char* const buf = sl->q_buf;
263 memset(buf, 0, sizeof (sl->q_buf));
265 buf[0] = STLINK_DEBUG_COMMAND;
266 buf[1] = STLINK_SWD_ENTER;
267 buf[2] = STLINK_DEBUG_ENTER_SWD;
269 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
271 printf("[!] send_recv\n");
276 void _stlink_usb_exit_dfu_mode(stlink_t* sl) {
277 struct stlink_libusb * const slu = sl->backend_data;
278 unsigned char* const buf = sl->q_buf;
281 memset(buf, 0, sizeof (sl->q_buf));
282 buf[0] = STLINK_DFU_COMMAND;
283 buf[1] = STLINK_DFU_EXIT;
285 size = send_only(slu, buf, 16);
287 printf("[!] send_recv\n");
293 * TODO - not convinced this does anything...
296 void _stlink_usb_reset(stlink_t * sl) {
297 struct stlink_libusb * const slu = sl->backend_data;
298 unsigned char* const buf = sl->q_buf;
301 memset(buf, 0, sizeof (sl->q_buf));
302 buf[0] = STLINK_DEBUG_COMMAND;
303 buf[1] = STLINK_DEBUG_RESETSYS;
305 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
307 printf("[!] send_recv\n");
313 void _stlink_usb_step(stlink_t* sl) {
314 struct stlink_libusb * const slu = sl->backend_data;
315 unsigned char* const buf = sl->q_buf;
318 memset(buf, 0, sizeof (sl->q_buf));
319 buf[0] = STLINK_DEBUG_COMMAND;
320 buf[1] = STLINK_DEBUG_STEPCORE;
322 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
324 printf("[!] send_recv\n");
330 * This seems to do a good job of restarting things from the beginning?
333 void _stlink_usb_run(stlink_t* sl) {
334 struct stlink_libusb * const slu = sl->backend_data;
335 unsigned char* const buf = sl->q_buf;
338 memset(buf, 0, sizeof (sl->q_buf));
339 buf[0] = STLINK_DEBUG_COMMAND;
340 buf[1] = STLINK_DEBUG_RUNCORE;
342 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
344 printf("[!] send_recv\n");
350 void _stlink_usb_exit_debug_mode(stlink_t *sl) {
351 struct stlink_libusb * const slu = sl->backend_data;
352 unsigned char* const buf = sl->q_buf;
355 memset(buf, 0, sizeof (sl->q_buf));
356 buf[0] = STLINK_DEBUG_COMMAND;
357 buf[1] = STLINK_DEBUG_EXIT;
359 size = send_only(slu, buf, 16);
361 printf("[!] send_only\n");
366 void _stlink_usb_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
367 struct stlink_libusb * const slu = sl->backend_data;
368 unsigned char* const buf = sl->q_buf;
371 /* assume len < sizeof(sl->q_buf) */
372 assert(len < sizeof(sl->q_buf)); // makes a compiler warning? always true?
374 memset(buf, 0, sizeof (sl->q_buf));
375 buf[0] = STLINK_DEBUG_COMMAND;
376 buf[1] = STLINK_DEBUG_READMEM_32BIT;
377 write_uint32(buf + 2, addr);
378 /* windows usb logs show only one byte is used for length ... */
379 // Presumably, this is because usb transfers can't be 16 bits worth of bytes long...
381 buf[6] = (uint8_t) len;
383 size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
385 printf("[!] send_recv\n");
389 sl->q_len = (size_t) size;
391 stlink_print_data(sl);
394 void _stlink_usb_read_all_regs(stlink_t *sl, reg *regp) {
395 DD(sl, "oops! read_all_regs not implemented for USB!\n");
398 void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
399 DD(sl, "oops! read_reg not implemented for USB! Wanted to read reg %d\n",
403 void _stlink_usb_write_reg(stlink_t *sl, uint32_t reg, int idx) {
404 DD(sl, "oops! write_reg not implemented for USB! Wanted to write %#x to %d\n",
410 stlink_backend_t _stlink_usb_backend = {
412 _stlink_usb_exit_debug_mode,
413 _stlink_usb_enter_swd_mode,
414 NULL, // no enter_jtag_mode here...
415 _stlink_usb_exit_dfu_mode,
421 _stlink_usb_read_mem32,
422 _stlink_usb_write_mem32,
423 _stlink_usb_write_mem8,
424 _stlink_usb_read_all_regs,
425 _stlink_usb_read_reg,
426 _stlink_usb_write_reg,
428 _stlink_usb_current_mode,
429 _stlink_usb_force_debug
433 stlink_t* stlink_open_usb(const char *dev_name, const int verbose) {
435 struct stlink_libusb* slu = NULL;
437 sl = malloc(sizeof (stlink_t));
438 slu = malloc(sizeof (struct stlink_libusb));
439 if (sl == NULL) goto on_error;
440 if (slu == NULL) goto on_error;
442 sl->verbose = verbose;
444 if (slu->libusb_ctx != NULL) {
445 fprintf(stderr, "reopening with an existing context? undefined behaviour!\n");
448 if (libusb_init(&(slu->libusb_ctx))) {
449 fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
456 libusb_device** devs = NULL;
462 count = libusb_get_device_list(slu->libusb_ctx, &devs);
464 printf("libusb_get_device_list\n");
465 goto on_libusb_error;
468 for (i = 0; i < count; ++i) {
470 if (is_stlink_device(dev)) break;
472 if (i == count) return NULL;
474 if (libusb_open(dev, &(slu->usb_handle))) {
475 printf("libusb_open()\n");
476 goto on_libusb_error;
479 if (libusb_get_configuration(slu->usb_handle, &config)) {
480 /* this may fail for a previous configured device */
481 printf("libusb_get_configuration()\n");
482 goto on_libusb_error;
486 printf("setting new configuration (%d -> 1)\n", config);
487 if (libusb_set_configuration(slu->usb_handle, 1)) {
488 /* this may fail for a previous configured device */
489 printf("libusb_set_configuration()\n");
490 goto on_libusb_error;
494 if (libusb_claim_interface(slu->usb_handle, 0)) {
495 printf("libusb_claim_interface()\n");
496 goto on_libusb_error;
499 slu->req_trans = libusb_alloc_transfer(0);
500 if (slu->req_trans == NULL) {
501 printf("libusb_alloc_transfer\n");
502 goto on_libusb_error;
505 slu->rep_trans = libusb_alloc_transfer(0);
506 if (slu->rep_trans == NULL) {
507 printf("libusb_alloc_transfer\n");
508 goto on_libusb_error;
511 slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
512 slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
514 /* libusb_reset_device(slu->usb_handle); */
521 libusb_free_device_list(devs, 1);
522 fprintf(stderr, "freed libusb device list\n");
530 sl->backend = &_stlink_usb_backend;
531 sl->backend_data = slu;
536 if (sl != NULL) free(sl);
537 if (slu != NULL) free(slu);