4 #include <libusb-1.0/libusb.h>
6 /* libusb transport layer */
8 libusb_context* libusb_ctx = NULL;
12 libusb_device_handle* usb_handle;
13 struct libusb_transfer* req_trans;
14 struct libusb_transfer* rep_trans;
21 #define TRANS_FLAGS_IS_DONE (1 << 0)
22 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
23 volatile unsigned long flags;
26 static void on_trans_done(struct libusb_transfer* trans)
28 struct trans_ctx* const ctx = trans->user_data;
30 if (trans->status != LIBUSB_TRANSFER_COMPLETED)
31 ctx->flags |= TRANS_FLAGS_HAS_ERROR;
33 ctx->flags = TRANS_FLAGS_IS_DONE;
37 static int submit_wait(struct libusb_transfer* trans)
39 struct trans_ctx trans_ctx;
40 enum libusb_error error;
44 /* brief intrusion inside the libusb interface */
45 trans->callback = on_trans_done;
46 trans->user_data = &trans_ctx;
48 if ((error = libusb_submit_transfer(trans)))
50 printf("libusb_submit_transfer(%d)\n", error);
54 while (!(trans_ctx.flags & TRANS_FLAGS_IS_DONE))
56 if (libusb_handle_events(NULL))
58 printf("libusb_handle_events()\n");
68 struct stlink_libusb* handle,
69 unsigned char* txbuf, size_t txsize,
70 unsigned char* rxbuf, size_t rxsize
73 /* note: txbuf and rxbuf can point to the same area */
75 libusb_fill_bulk_transfer
85 if (submit_wait(handle->req_trans)) return -1;
87 /* read the response */
89 libusb_fill_bulk_transfer
99 return submit_wait(handle->rep_trans);
103 /* stlink layer independant interface */
107 TRANSPORT_TYPE_ZERO = 0,
109 TRANSPORT_TYPE_LIBSG,
110 #endif /* CONFIG_USE_LIBSG */
111 #if CONFIG_USE_LIBUSB
112 TRANSPORT_TYPE_LIBUSB,
113 #endif /* CONFIG_USE_LIBUSB */
114 TRANSPORT_TYPE_INVALID
119 enum transport_type tt;
122 #if CONFIG_USE_LIBUSB
123 struct stlink_libusb libusb;
124 #endif /* CONFIG_USE_LIBUSB */
127 #endif /* CONFIG_USE_LIBSG */
131 int stlink_initialize(enum transport_type tt)
135 #if CONFIG_USE_LIBUSB
136 case TRANSPORT_TYPE_LIBUSB:
138 if (libusb_ctx != NULL) return -1;
139 if (libusb_init(&libusb_ctx))
141 printf("libusb_init()\n");
146 #endif /* CONFIG_USE_LIBUSB */
154 void stlink_finalize(enum transport_type tt)
158 #if CONFIG_USE_LIBUSB
159 case TRANSPORT_TYPE_LIBUSB:
161 libusb_exit(libusb_ctx);
164 #endif /* CONFIG_USE_LIBUSB */
170 #if CONFIG_USE_LIBUSB
171 static int is_stlink_device(libusb_device* dev)
173 struct libusb_device_descriptor desc;
175 if (libusb_get_device_descriptor(dev, &desc))
178 printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
180 if (desc.idVendor != 0x0483)
183 if (desc.idProduct != 0x3748)
188 #endif /* CONFIG_USE_LIBUSB */
191 void stlink_close(struct stlink*);
193 struct stlink* stlink_quirk_open
194 (enum transport_type tt, const char *dev_name, const int verbose)
196 struct stlink* sl = NULL;
198 sl = malloc(sizeof(struct stlink));
199 if (sl == NULL) goto on_error;
205 #if CONFIG_USE_LIBUSB
206 case TRANSPORT_TYPE_LIBUSB:
208 struct stlink_libusb* const slu = &sl->transport.libusb;
212 libusb_device** devs = NULL;
217 count = libusb_get_device_list(libusb_ctx, &devs);
220 printf("libusb_get_device_list\n");
221 goto on_libusb_error;
224 for (i = 0; i < count; ++i)
227 if (is_stlink_device(dev)) break;
229 if (i == count) return NULL;
231 if (libusb_open(dev, &slu->usb_handle))
233 printf("libusb_open()\n");
234 goto on_libusb_error;
237 if (libusb_set_configuration(slu->usb_handle, 1))
239 printf("libusb_set_configuration()\n");
240 goto on_libusb_error;
243 if (libusb_claim_interface(slu->usb_handle, 0))
245 printf("libusb_claim_interface()\n");
246 goto on_libusb_error;
249 slu->req_trans = libusb_alloc_transfer(0);
250 if (slu->req_trans == NULL)
252 printf("libusb_alloc_transfer\n");
253 goto on_libusb_error;
256 slu->rep_trans = libusb_alloc_transfer(0);
257 if (slu->rep_trans == NULL)
259 printf("libusb_alloc_transfer\n");
260 goto on_libusb_error;
263 slu->ep_req = 1 /* ep req */ | LIBUSB_ENDPOINT_OUT;
264 slu->ep_rep = 2 /* ep rep */ | LIBUSB_ENDPOINT_IN;
270 if (devs != NULL) libusb_free_device_list(devs, 1);
280 #endif /* CONFIG_USE_LIBUSB */
283 case transport_type_libsg:
287 #endif /* CONFIG_USE_LIBSG */
296 if (sl != NULL) free(sl);
300 void stlink_close(struct stlink *sl)
304 #if CONFIG_USE_LIBUSB
305 case TRANSPORT_TYPE_LIBUSB:
307 struct stlink_libusb* const handle = &sl->transport.libusb;
309 if (handle->req_trans != NULL)
310 libusb_free_transfer(handle->req_trans);
312 if (handle->rep_trans != NULL)
313 libusb_free_transfer(handle->rep_trans);
315 if (handle->usb_handle != NULL)
316 libusb_close(handle->usb_handle);
320 #endif /* CONFIG_USE_LIBUSB */
323 case TRANSPORT_TYPE_LIBSG:
327 #endif /* CONFIG_USE_LIBSG */
335 void stlink_version(struct stlink* sl)
339 #if CONFIG_USE_LIBUSB
340 case TRANSPORT_TYPE_LIBUSB:
342 struct stlink_libusb* const slu = &sl->transport.libusb;
345 unsigned char buf[6];
347 for (i = 0; i < sizeof(buf); ++i) buf[i] = 0;
350 if (send_recv(slu, buf, sizeof(buf), buf, sizeof(buf)) == -1)
352 printf("[!] send_recv\n");
356 for (i = 0; i < 6; ++i) printf("%02x", buf[i]);
361 #endif /* CONFIG_USE_LIBUSB */
367 int stlink_current_mode(struct stlink *sl)
372 void stlink_enter_swd_mode(struct stlink *sl)
376 void stlink_enter_jtag_mode(struct stlink *sl)
380 void stlink_exit_debug_mode(struct stlink *sl)
384 void stlink_core_id(struct stlink *sl)
388 void stlink_status(struct stlink *sl)
395 int main(int ac, char** av)
399 stlink_initialize(TRANSPORT_TYPE_LIBUSB);
400 sl = stlink_quirk_open(TRANSPORT_TYPE_LIBUSB, NULL, 0);
405 stlink_current_mode(sl);
408 stlink_finalize(TRANSPORT_TYPE_LIBUSB);