Last active
August 29, 2015 14:00
-
-
Save YuuichiAkagawa/e74d7b12e5e37f0d2e2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <libusb-1.0/libusb.h> | |
#include <unistd.h> | |
#define MBED_VENDOR_ID 0x1f00 | |
#define MBED_PRODUCT_ID 0x2012 | |
#define IF_CDCACM 0 | |
#define IF_CDCDATA 1 | |
#define BUFFER 1024 | |
#define EPIN 0x82 | |
#define EPOUT 0x02 | |
#define ACM_CTRL_DTR 0x01 | |
#define ACM_CTRL_RTS 0x02 | |
/* - set line encoding: here 9600 8N1 | |
* 9600 = 0x2580 ~> 0x80, 0x25 in little endian | |
*/ | |
unsigned char encoding[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }; | |
unsigned char gBuffer[BUFFER]; | |
int main() | |
{ | |
libusb_context *ctx = NULL; | |
libusb_device **devs; | |
libusb_device *dev; | |
libusb_device_handle* handle; | |
struct libusb_device_descriptor desc; | |
struct libusb_config_descriptor *config; | |
static int xfered; | |
int err; | |
int if_num; | |
if(libusb_init(&ctx) < 0){ | |
printf("libusb_init failed\n"); | |
return 1; | |
} | |
libusb_set_debug(ctx, 3); | |
if((handle = libusb_open_device_with_vid_pid(ctx, MBED_VENDOR_ID, MBED_PRODUCT_ID)) == NULL) { | |
printf("mbed not found.\n"); | |
return(1); | |
} | |
//detach kernel driver(cdc_acm) and clime it. | |
//if0:cdc acm | |
//if1:cdc data | |
for (if_num = 0; if_num < 2; if_num++) { | |
if (libusb_kernel_driver_active(handle, if_num)){ | |
libusb_detach_kernel_driver(handle, if_num); | |
} | |
err = libusb_claim_interface(handle, if_num); | |
if(err != 0 ){ | |
printf("libusb_clain_inteface failed.(%d)\n", err); | |
} | |
} | |
/* | |
err = libusb_set_interface_alt_setting(handle, IF_CDCDATA, 0); | |
if(err != 0 ){ | |
printf("libusb_set_interface_alt_setting failed.(%d)\n", err); | |
} | |
*/ | |
err = libusb_control_transfer(handle, 0x21, 0x22, ACM_CTRL_DTR | ACM_CTRL_RTS, IF_CDCACM, NULL, 0, 0); | |
err = libusb_control_transfer(handle, 0x21, 0x20, 0, IF_CDCACM, encoding, sizeof(encoding), 0); | |
printf("Begin Bulk xfer\n"); | |
while(1){ | |
err = libusb_bulk_transfer(handle, EPIN, gBuffer, BUFFER, &xfered, 100); | |
if(err == LIBUSB_ERROR_TIMEOUT){ | |
continue; | |
} | |
if(err == 0){ | |
printf("%d bytes rcvd : %02X %02X\n", xfered, gBuffer[0], gBuffer[1]); | |
break; | |
}else{ | |
printf("libusb_bulk_transfer failed.(%d)\n", err); | |
} | |
} | |
//release and close USB | |
err = libusb_release_interface (handle, IF_CDCDATA); | |
err = libusb_release_interface (handle, IF_CDCDATA); | |
libusb_close(handle); | |
libusb_exit(ctx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment