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");
66 static ssize_t send_recv
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 if (submit_wait(handle->rep_trans)) return -1;
100 return handle->rep_trans->actual_length;
104 /* stlink layer independant interface */
108 TRANSPORT_TYPE_ZERO = 0,
110 TRANSPORT_TYPE_LIBSG,
111 #endif /* CONFIG_USE_LIBSG */
112 #if CONFIG_USE_LIBUSB
113 TRANSPORT_TYPE_LIBUSB,
114 #endif /* CONFIG_USE_LIBUSB */
115 TRANSPORT_TYPE_INVALID
120 enum transport_type tt;
123 #if CONFIG_USE_LIBUSB
124 struct stlink_libusb libusb;
125 #endif /* CONFIG_USE_LIBUSB */
128 #endif /* CONFIG_USE_LIBSG */
132 int stlink_initialize(enum transport_type tt)
136 #if CONFIG_USE_LIBUSB
137 case TRANSPORT_TYPE_LIBUSB:
139 if (libusb_ctx != NULL) return -1;
140 if (libusb_init(&libusb_ctx))
142 printf("libusb_init()\n");
147 #endif /* CONFIG_USE_LIBUSB */
155 void stlink_finalize(enum transport_type tt)
159 #if CONFIG_USE_LIBUSB
160 case TRANSPORT_TYPE_LIBUSB:
162 libusb_exit(libusb_ctx);
165 #endif /* CONFIG_USE_LIBUSB */
171 #if CONFIG_USE_LIBUSB
172 static int is_stlink_device(libusb_device* dev)
174 struct libusb_device_descriptor desc;
176 if (libusb_get_device_descriptor(dev, &desc))
179 printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
181 if (desc.idVendor != 0x0483)
184 if (desc.idProduct != 0x3748)
189 #endif /* CONFIG_USE_LIBUSB */
192 void stlink_close(struct stlink*);
194 struct stlink* stlink_quirk_open
195 (enum transport_type tt, const char *dev_name, const int verbose)
197 struct stlink* sl = NULL;
199 sl = malloc(sizeof(struct stlink));
200 if (sl == NULL) goto on_error;
206 #if CONFIG_USE_LIBUSB
207 case TRANSPORT_TYPE_LIBUSB:
209 struct stlink_libusb* const slu = &sl->transport.libusb;
213 libusb_device** devs = NULL;
218 count = libusb_get_device_list(libusb_ctx, &devs);
221 printf("libusb_get_device_list\n");
222 goto on_libusb_error;
225 for (i = 0; i < count; ++i)
228 if (is_stlink_device(dev)) break;
230 if (i == count) return NULL;
232 if (libusb_open(dev, &slu->usb_handle))
234 printf("libusb_open()\n");
235 goto on_libusb_error;
238 if (libusb_set_configuration(slu->usb_handle, 1))
240 printf("libusb_set_configuration()\n");
241 goto on_libusb_error;
244 if (libusb_claim_interface(slu->usb_handle, 0))
246 printf("libusb_claim_interface()\n");
247 goto on_libusb_error;
250 slu->req_trans = libusb_alloc_transfer(0);
251 if (slu->req_trans == NULL)
253 printf("libusb_alloc_transfer\n");
254 goto on_libusb_error;
257 slu->rep_trans = libusb_alloc_transfer(0);
258 if (slu->rep_trans == NULL)
260 printf("libusb_alloc_transfer\n");
261 goto on_libusb_error;
264 slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
265 slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
271 if (devs != NULL) libusb_free_device_list(devs, 1);
281 #endif /* CONFIG_USE_LIBUSB */
284 case transport_type_libsg:
288 #endif /* CONFIG_USE_LIBSG */
297 if (sl != NULL) free(sl);
301 void stlink_close(struct stlink *sl)
305 #if CONFIG_USE_LIBUSB
306 case TRANSPORT_TYPE_LIBUSB:
308 struct stlink_libusb* const handle = &sl->transport.libusb;
310 if (handle->req_trans != NULL)
311 libusb_free_transfer(handle->req_trans);
313 if (handle->rep_trans != NULL)
314 libusb_free_transfer(handle->rep_trans);
316 if (handle->usb_handle != NULL)
317 libusb_close(handle->usb_handle);
321 #endif /* CONFIG_USE_LIBUSB */
324 case TRANSPORT_TYPE_LIBSG:
328 #endif /* CONFIG_USE_LIBSG */
336 void stlink_version(struct stlink* sl)
340 #if CONFIG_USE_LIBUSB
341 case TRANSPORT_TYPE_LIBUSB:
343 struct stlink_libusb* const slu = &sl->transport.libusb;
347 unsigned char buf[64];
349 for (i = 0; i < sizeof(buf); ++i) buf[i] = 0;
354 size = send_recv(slu, buf, sizeof(buf), buf, sizeof(buf));
357 printf("[!] send_recv\n");
361 for (i = 0; i < size; ++i) printf("%02x", buf[i]);
366 #endif /* CONFIG_USE_LIBUSB */
372 int stlink_current_mode(struct stlink *sl)
377 void stlink_enter_swd_mode(struct stlink *sl)
381 void stlink_enter_jtag_mode(struct stlink *sl)
385 void stlink_exit_debug_mode(struct stlink *sl)
389 void stlink_core_id(struct stlink *sl)
393 void stlink_status(struct stlink *sl)
400 int main(int ac, char** av)
404 stlink_initialize(TRANSPORT_TYPE_LIBUSB);
405 sl = stlink_quirk_open(TRANSPORT_TYPE_LIBUSB, NULL, 0);
410 stlink_current_mode(sl);
413 stlink_finalize(TRANSPORT_TYPE_LIBUSB);