6 #include <libusb-1.0/libusb.h>
9 /* endianess related */
10 static inline unsigned int is_bigendian(void)
12 static volatile const unsigned int i = 1;
13 return *(volatile const char*) &i == 0;
16 static void write_uint32(unsigned char* buf, uint32_t ui)
18 if (!is_bigendian()) { // le -> le (don't swap)
19 buf[0] = ((unsigned char*) &ui)[0];
20 buf[1] = ((unsigned char*) &ui)[1];
21 buf[2] = ((unsigned char*) &ui)[2];
22 buf[3] = ((unsigned char*) &ui)[3];
24 buf[0] = ((unsigned char*) &ui)[3];
25 buf[1] = ((unsigned char*) &ui)[2];
26 buf[2] = ((unsigned char*) &ui)[1];
27 buf[3] = ((unsigned char*) &ui)[0];
31 static void write_uint16(unsigned char* buf, uint16_t ui)
33 if (!is_bigendian()) { // le -> le (don't swap)
34 buf[0] = ((unsigned char*) &ui)[0];
35 buf[1] = ((unsigned char*) &ui)[1];
37 buf[0] = ((unsigned char*) &ui)[1];
38 buf[1] = ((unsigned char*) &ui)[0];
42 static uint32_t read_uint32(const unsigned char *c, const int pt)
45 char *p = (char *) &ui;
47 if (!is_bigendian()) { // le -> le (don't swap)
61 static uint16_t read_uint16(const unsigned char *c, const int pt)
64 char *p = (char *) &ui;
66 if (!is_bigendian()) { // le -> le (don't swap)
76 /* libusb transport layer */
78 libusb_context* libusb_ctx = NULL;
82 libusb_device_handle* usb_handle;
83 struct libusb_transfer* req_trans;
84 struct libusb_transfer* rep_trans;
91 #define TRANS_FLAGS_IS_DONE (1 << 0)
92 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
93 volatile unsigned long flags;
96 static void on_trans_done(struct libusb_transfer* trans)
98 struct trans_ctx* const ctx = trans->user_data;
100 if (trans->status != LIBUSB_TRANSFER_COMPLETED)
101 ctx->flags |= TRANS_FLAGS_HAS_ERROR;
103 ctx->flags = TRANS_FLAGS_IS_DONE;
107 static int submit_wait(struct libusb_transfer* trans)
109 struct trans_ctx trans_ctx;
110 enum libusb_error error;
114 /* brief intrusion inside the libusb interface */
115 trans->callback = on_trans_done;
116 trans->user_data = &trans_ctx;
118 if ((error = libusb_submit_transfer(trans)))
120 printf("libusb_submit_transfer(%d)\n", error);
124 while (!(trans_ctx.flags & TRANS_FLAGS_IS_DONE))
126 if (libusb_handle_events(NULL))
128 printf("libusb_handle_events()\n");
136 static ssize_t send_recv
138 struct stlink_libusb* handle,
139 unsigned char* txbuf, size_t txsize,
140 unsigned char* rxbuf, size_t rxsize
143 /* note: txbuf and rxbuf can point to the same area */
145 libusb_fill_bulk_transfer
155 printf("submit_wait(req)\n");
157 if (submit_wait(handle->req_trans)) return -1;
159 /* read the response */
161 libusb_fill_bulk_transfer
171 printf("submit_wait(rep)\n");
173 if (submit_wait(handle->rep_trans)) return -1;
175 return handle->rep_trans->actual_length;
179 /* stlink layer independant interface */
183 TRANSPORT_TYPE_ZERO = 0,
185 TRANSPORT_TYPE_LIBSG,
186 #endif /* CONFIG_USE_LIBSG */
187 #if CONFIG_USE_LIBUSB
188 TRANSPORT_TYPE_LIBUSB,
189 #endif /* CONFIG_USE_LIBUSB */
190 TRANSPORT_TYPE_INVALID
195 enum transport_type tt;
198 #if CONFIG_USE_LIBUSB
199 struct stlink_libusb libusb;
200 #endif /* CONFIG_USE_LIBUSB */
203 #endif /* CONFIG_USE_LIBSG */
206 /* layer independant */
210 int stlink_initialize(enum transport_type tt)
214 #if CONFIG_USE_LIBUSB
215 case TRANSPORT_TYPE_LIBUSB:
217 if (libusb_ctx != NULL) return -1;
218 if (libusb_init(&libusb_ctx))
220 printf("libusb_init()\n");
225 #endif /* CONFIG_USE_LIBUSB */
233 void stlink_finalize(enum transport_type tt)
237 #if CONFIG_USE_LIBUSB
238 case TRANSPORT_TYPE_LIBUSB:
240 libusb_exit(libusb_ctx);
243 #endif /* CONFIG_USE_LIBUSB */
249 #if CONFIG_USE_LIBUSB
250 static int is_stlink_device(libusb_device* dev)
252 struct libusb_device_descriptor desc;
254 if (libusb_get_device_descriptor(dev, &desc))
257 printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
259 if (desc.idVendor != 0x0483)
262 if (desc.idProduct != 0x3748)
267 #endif /* CONFIG_USE_LIBUSB */
270 void stlink_close(struct stlink*);
272 struct stlink* stlink_quirk_open
273 (enum transport_type tt, const char *dev_name, const int verbose)
275 struct stlink* sl = NULL;
277 sl = malloc(sizeof(struct stlink));
278 if (sl == NULL) goto on_error;
284 #if CONFIG_USE_LIBUSB
285 case TRANSPORT_TYPE_LIBUSB:
287 struct stlink_libusb* const slu = &sl->transport.libusb;
291 libusb_device** devs = NULL;
296 count = libusb_get_device_list(libusb_ctx, &devs);
299 printf("libusb_get_device_list\n");
300 goto on_libusb_error;
303 for (i = 0; i < count; ++i)
306 if (is_stlink_device(dev)) break;
308 if (i == count) return NULL;
310 if (libusb_open(dev, &slu->usb_handle))
312 printf("libusb_open()\n");
313 goto on_libusb_error;
316 if (libusb_set_configuration(slu->usb_handle, 1))
318 printf("libusb_set_configuration()\n");
319 goto on_libusb_error;
322 if (libusb_claim_interface(slu->usb_handle, 0))
324 printf("libusb_claim_interface()\n");
325 goto on_libusb_error;
328 slu->req_trans = libusb_alloc_transfer(0);
329 if (slu->req_trans == NULL)
331 printf("libusb_alloc_transfer\n");
332 goto on_libusb_error;
335 slu->rep_trans = libusb_alloc_transfer(0);
336 if (slu->rep_trans == NULL)
338 printf("libusb_alloc_transfer\n");
339 goto on_libusb_error;
342 slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
343 slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
349 if (devs != NULL) libusb_free_device_list(devs, 1);
359 #endif /* CONFIG_USE_LIBUSB */
362 case transport_type_libsg:
366 #endif /* CONFIG_USE_LIBSG */
375 if (sl != NULL) free(sl);
379 void stlink_close(struct stlink *sl)
383 #if CONFIG_USE_LIBUSB
384 case TRANSPORT_TYPE_LIBUSB:
386 struct stlink_libusb* const handle = &sl->transport.libusb;
388 if (handle->req_trans != NULL)
389 libusb_free_transfer(handle->req_trans);
391 if (handle->rep_trans != NULL)
392 libusb_free_transfer(handle->rep_trans);
394 if (handle->usb_handle != NULL)
395 libusb_close(handle->usb_handle);
399 #endif /* CONFIG_USE_LIBUSB */
402 case TRANSPORT_TYPE_LIBSG:
406 #endif /* CONFIG_USE_LIBSG */
414 void stlink_version(struct stlink* sl)
418 #if CONFIG_USE_LIBUSB
419 case TRANSPORT_TYPE_LIBUSB:
421 struct stlink_libusb* const slu = &sl->transport.libusb;
424 unsigned char buf[64];
426 memset(buf, 0, sizeof(buf));
428 buf[0] = STLINK_GET_VERSION;
431 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
434 printf("[!] send_recv\n");
441 for (i = 0; i < size; ++i) printf("%02x", buf[i]);
448 #endif /* CONFIG_USE_LIBUSB */
454 int stlink_current_mode(struct stlink *sl)
460 #if CONFIG_USE_LIBUSB
461 case TRANSPORT_TYPE_LIBUSB:
463 struct stlink_libusb* const slu = &sl->transport.libusb;
466 unsigned char buf[64];
468 memset(buf, 0, sizeof(buf));
470 buf[0] = STLINK_GET_CURRENT_MODE;
472 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
475 printf("[!] send_recv\n");
482 printf("mode == 0x%x\n", mode);
487 #endif /* CONFIG_USE_LIBUSB */
495 void stlink_core_id(struct stlink *sl)
499 #if CONFIG_USE_LIBUSB
500 case TRANSPORT_TYPE_LIBUSB:
502 struct stlink_libusb* const slu = &sl->transport.libusb;
505 unsigned char buf[64];
507 memset(buf, 0, sizeof(buf));
509 buf[0] = STLINK_DEBUG_READCOREID;
511 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
514 printf("[!] send_recv\n");
518 sl->core_id = read_uint32(buf, 0);
521 printf("core_id == 0x%x\n", sl->core_id);
526 #endif /* CONFIG_USE_LIBUSB */
532 void stlink_status(struct stlink *sl)
536 #if CONFIG_USE_LIBUSB
537 case TRANSPORT_TYPE_LIBUSB:
539 struct stlink_libusb* const slu = &sl->transport.libusb;
542 unsigned char buf[64];
544 memset(buf, 0, sizeof(buf));
546 buf[0] = STLINK_DEBUG_GETSTATUS;
548 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
551 printf("[!] send_recv\n");
555 /* todo: stlink_core_stat */
558 printf("status == 0x%x\n", buf[0]);
563 #endif /* CONFIG_USE_LIBUSB */
569 void stlink_enter_swd_mode(struct stlink *sl)
573 void stlink_enter_jtag_mode(struct stlink *sl)
577 void stlink_exit_debug_mode(struct stlink *sl)
584 int main(int ac, char** av)
588 stlink_initialize(TRANSPORT_TYPE_LIBUSB);
589 sl = stlink_quirk_open(TRANSPORT_TYPE_LIBUSB, NULL, 0);
594 stlink_current_mode(sl);
598 stlink_finalize(TRANSPORT_TYPE_LIBUSB);