7 #include <libusb-1.0/libusb.h>
10 /* endianess related */
11 static inline unsigned int is_bigendian(void)
13 static volatile const unsigned int i = 1;
14 return *(volatile const char*) &i == 0;
17 static void write_uint32(unsigned char* buf, uint32_t ui)
19 if (!is_bigendian()) { // le -> le (don't swap)
20 buf[0] = ((unsigned char*) &ui)[0];
21 buf[1] = ((unsigned char*) &ui)[1];
22 buf[2] = ((unsigned char*) &ui)[2];
23 buf[3] = ((unsigned char*) &ui)[3];
25 buf[0] = ((unsigned char*) &ui)[3];
26 buf[1] = ((unsigned char*) &ui)[2];
27 buf[2] = ((unsigned char*) &ui)[1];
28 buf[3] = ((unsigned char*) &ui)[0];
32 static void write_uint16(unsigned char* buf, uint16_t ui)
34 if (!is_bigendian()) { // le -> le (don't swap)
35 buf[0] = ((unsigned char*) &ui)[0];
36 buf[1] = ((unsigned char*) &ui)[1];
38 buf[0] = ((unsigned char*) &ui)[1];
39 buf[1] = ((unsigned char*) &ui)[0];
43 static uint32_t read_uint32(const unsigned char *c, const int pt)
46 char *p = (char *) &ui;
48 if (!is_bigendian()) { // le -> le (don't swap)
62 static uint16_t read_uint16(const unsigned char *c, const int pt)
65 char *p = (char *) &ui;
67 if (!is_bigendian()) { // le -> le (don't swap)
77 /* libusb transport layer */
79 libusb_context* libusb_ctx = NULL;
83 libusb_device_handle* usb_handle;
84 struct libusb_transfer* req_trans;
85 struct libusb_transfer* rep_trans;
92 #define TRANS_FLAGS_IS_DONE (1 << 0)
93 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
94 volatile unsigned long flags;
97 static void on_trans_done(struct libusb_transfer* trans)
99 struct trans_ctx* const ctx = trans->user_data;
101 if (trans->status != LIBUSB_TRANSFER_COMPLETED)
102 ctx->flags |= TRANS_FLAGS_HAS_ERROR;
104 ctx->flags = TRANS_FLAGS_IS_DONE;
108 static int submit_wait(struct libusb_transfer* trans)
110 struct timeval start;
113 struct trans_ctx trans_ctx;
114 enum libusb_error error;
118 /* brief intrusion inside the libusb interface */
119 trans->callback = on_trans_done;
120 trans->user_data = &trans_ctx;
122 if ((error = libusb_submit_transfer(trans)))
124 printf("libusb_submit_transfer(%d)\n", error);
128 gettimeofday(&start, NULL);
130 while (!(trans_ctx.flags & TRANS_FLAGS_IS_DONE))
132 if (libusb_handle_events(NULL))
134 printf("libusb_handle_events()\n");
138 gettimeofday(&now, NULL);
139 timersub(&now, &start, &diff);
140 if (diff.tv_sec >= 3)
142 printf("libusb_handle_events() timeout\n");
150 static ssize_t send_recv
152 struct stlink_libusb* handle,
153 unsigned char* txbuf, size_t txsize,
154 unsigned char* rxbuf, size_t rxsize
157 /* note: txbuf and rxbuf can point to the same area */
159 libusb_fill_bulk_transfer
169 printf("submit_wait(req)\n");
171 if (submit_wait(handle->req_trans)) return -1;
173 /* read the response */
175 libusb_fill_bulk_transfer
185 printf("submit_wait(rep)\n");
187 if (submit_wait(handle->rep_trans)) return -1;
189 return handle->rep_trans->actual_length;
193 /* stlink layer independant interface */
197 TRANSPORT_TYPE_ZERO = 0,
199 TRANSPORT_TYPE_LIBSG,
200 #endif /* CONFIG_USE_LIBSG */
201 #if CONFIG_USE_LIBUSB
202 TRANSPORT_TYPE_LIBUSB,
203 #endif /* CONFIG_USE_LIBUSB */
204 TRANSPORT_TYPE_INVALID
209 enum transport_type tt;
212 #if CONFIG_USE_LIBUSB
213 struct stlink_libusb libusb;
214 #endif /* CONFIG_USE_LIBUSB */
217 #endif /* CONFIG_USE_LIBSG */
220 /* layer independant */
224 int stlink_initialize(enum transport_type tt)
228 #if CONFIG_USE_LIBUSB
229 case TRANSPORT_TYPE_LIBUSB:
231 if (libusb_ctx != NULL) return -1;
232 if (libusb_init(&libusb_ctx))
234 printf("libusb_init()\n");
239 #endif /* CONFIG_USE_LIBUSB */
247 void stlink_finalize(enum transport_type tt)
251 #if CONFIG_USE_LIBUSB
252 case TRANSPORT_TYPE_LIBUSB:
254 libusb_exit(libusb_ctx);
257 #endif /* CONFIG_USE_LIBUSB */
263 #if CONFIG_USE_LIBUSB
264 static int is_stlink_device(libusb_device* dev)
266 struct libusb_device_descriptor desc;
268 if (libusb_get_device_descriptor(dev, &desc))
271 printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
273 if (desc.idVendor != 0x0483)
276 if (desc.idProduct != 0x3748)
281 #endif /* CONFIG_USE_LIBUSB */
284 void stlink_close(struct stlink*);
286 struct stlink* stlink_quirk_open
287 (enum transport_type tt, const char *dev_name, const int verbose)
289 struct stlink* sl = NULL;
291 sl = malloc(sizeof(struct stlink));
292 if (sl == NULL) goto on_error;
298 #if CONFIG_USE_LIBUSB
299 case TRANSPORT_TYPE_LIBUSB:
301 struct stlink_libusb* const slu = &sl->transport.libusb;
305 libusb_device** devs = NULL;
310 count = libusb_get_device_list(libusb_ctx, &devs);
313 printf("libusb_get_device_list\n");
314 goto on_libusb_error;
317 for (i = 0; i < count; ++i)
320 if (is_stlink_device(dev)) break;
322 if (i == count) return NULL;
324 if (libusb_open(dev, &slu->usb_handle))
326 printf("libusb_open()\n");
327 goto on_libusb_error;
330 if (libusb_set_configuration(slu->usb_handle, 1))
332 printf("libusb_set_configuration()\n");
333 goto on_libusb_error;
336 if (libusb_claim_interface(slu->usb_handle, 0))
338 printf("libusb_claim_interface()\n");
339 goto on_libusb_error;
342 slu->req_trans = libusb_alloc_transfer(0);
343 if (slu->req_trans == NULL)
345 printf("libusb_alloc_transfer\n");
346 goto on_libusb_error;
349 slu->rep_trans = libusb_alloc_transfer(0);
350 if (slu->rep_trans == NULL)
352 printf("libusb_alloc_transfer\n");
353 goto on_libusb_error;
356 slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
357 slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
363 if (devs != NULL) libusb_free_device_list(devs, 1);
373 #endif /* CONFIG_USE_LIBUSB */
376 case transport_type_libsg:
380 #endif /* CONFIG_USE_LIBSG */
389 if (sl != NULL) free(sl);
393 void stlink_close(struct stlink *sl)
397 #if CONFIG_USE_LIBUSB
398 case TRANSPORT_TYPE_LIBUSB:
400 struct stlink_libusb* const handle = &sl->transport.libusb;
402 if (handle->req_trans != NULL)
403 libusb_free_transfer(handle->req_trans);
405 if (handle->rep_trans != NULL)
406 libusb_free_transfer(handle->rep_trans);
408 if (handle->usb_handle != NULL)
409 libusb_close(handle->usb_handle);
413 #endif /* CONFIG_USE_LIBUSB */
416 case TRANSPORT_TYPE_LIBSG:
420 #endif /* CONFIG_USE_LIBSG */
428 void stlink_version(struct stlink* sl)
432 #if CONFIG_USE_LIBUSB
433 case TRANSPORT_TYPE_LIBUSB:
435 struct stlink_libusb* const slu = &sl->transport.libusb;
438 unsigned char buf[64];
440 memset(buf, 0, sizeof(buf));
442 buf[0] = STLINK_GET_VERSION;
445 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
448 printf("[!] send_recv\n");
455 for (i = 0; i < size; ++i) printf("%02x", buf[i]);
462 #endif /* CONFIG_USE_LIBUSB */
468 int stlink_current_mode(struct stlink *sl)
474 #if CONFIG_USE_LIBUSB
475 case TRANSPORT_TYPE_LIBUSB:
477 struct stlink_libusb* const slu = &sl->transport.libusb;
480 unsigned char buf[64];
482 memset(buf, 0, sizeof(buf));
484 buf[0] = STLINK_GET_CURRENT_MODE;
486 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
489 printf("[!] send_recv\n");
496 printf("mode == 0x%x\n", mode);
501 #endif /* CONFIG_USE_LIBUSB */
509 void stlink_core_id(struct stlink *sl)
513 #if CONFIG_USE_LIBUSB
514 case TRANSPORT_TYPE_LIBUSB:
516 struct stlink_libusb* const slu = &sl->transport.libusb;
519 unsigned char buf[64];
521 memset(buf, 0, sizeof(buf));
523 buf[0] = STLINK_DEBUG_READCOREID;
525 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
528 printf("[!] send_recv\n");
532 sl->core_id = read_uint32(buf, 0);
535 printf("core_id == 0x%x\n", sl->core_id);
540 #endif /* CONFIG_USE_LIBUSB */
546 void stlink_status(struct stlink *sl)
550 #if CONFIG_USE_LIBUSB
551 case TRANSPORT_TYPE_LIBUSB:
553 struct stlink_libusb* const slu = &sl->transport.libusb;
556 unsigned char buf[64];
558 memset(buf, 0, sizeof(buf));
560 buf[0] = STLINK_DEBUG_GETSTATUS;
562 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
565 printf("[!] send_recv\n");
569 /* todo: stlink_core_stat */
572 printf("status == 0x%x\n", buf[0]);
577 #endif /* CONFIG_USE_LIBUSB */
583 void stlink_enter_swd_mode(struct stlink *sl)
587 void stlink_enter_jtag_mode(struct stlink *sl)
591 void stlink_exit_debug_mode(struct stlink *sl)
598 int main(int ac, char** av)
602 stlink_initialize(TRANSPORT_TYPE_LIBUSB);
603 sl = stlink_quirk_open(TRANSPORT_TYPE_LIBUSB, NULL, 0);
608 stlink_current_mode(sl);
612 stlink_finalize(TRANSPORT_TYPE_LIBUSB);