• AVR Freaks

Jan Axelson's WinUSB example under Linux with libusb

Page: < 1234 Showing page 4 of 4
Author
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2010/02/03 08:01:36 (permalink)
0
For the latest Stack v2.5 based example, some minor modification is necessary. But I am facing some stability issues. I will check again.

Edit: solved by uncommenting the reset line.


#include <errno.h>

#include <signal.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.h>



#include <libusb-1.0/libusb.h>



#define VERSION "0.1.0"



#define VENDOR_ID 0x0925



#define PRODUCT_ID 0x1456

#define WINUSB_REQUEST_1 0x01

#define WINUSB_REQUEST_2 0x02

#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_INTERFACE)

#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_INTERFACE)

const static int PACKET_CTRL_LEN=2;

const static int PACKET_INT_LEN=2;

const static int PACKET_BULK_LEN=64;

const static int INTERFACE=0;

const static int ENDPOINT_INT_IN=0x82; /* endpoint 0x81 address for IN */

const static int ENDPOINT_INT_OUT=0x02; /* endpoint 1 address for OUT */

const static int ENDPOINT_BULK_IN=0x81; /* endpoint 0x81 address for IN */

const static int ENDPOINT_BULK_OUT=0x01; /* endpoint 1 address for OUT */

const static int TIMEOUT=5000; /* timeout in ms */





void bad(const char *why) {

fprintf(stderr,"Fatal error> %s\n",why);

exit(17);

}





static struct libusb_device_handle *devh = NULL;







static int find_lvr_winusb(void)

{

devh = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);

return devh ? 0 : -EIO;

}





static int test_control_transfer(void)

{

int r,i;

char answer[PACKET_CTRL_LEN];

char question[PACKET_CTRL_LEN];

for (i=0;i<PACKET_CTRL_LEN; i++) question[i]=0x20+i;



r = libusb_control_transfer(devh,CTRL_OUT,WINUSB_REQUEST_1,2, 0,question, PACKET_CTRL_LEN,TIMEOUT);

if (r < 0) {

fprintf(stderr, "Control Out error %d\n", r);

return r;

}

r = libusb_control_transfer(devh,CTRL_IN,WINUSB_REQUEST_2,2,0, answer,PACKET_CTRL_LEN, TIMEOUT);

if (r < 0) {

fprintf(stderr, "Control IN error %d\n", r);

return r;

}

for(i = 0;i < PACKET_CTRL_LEN; i++) {

if(i%8 == 0)

printf("\n");

printf("%02x, %02x; ",question[i],answer[i]);

}

printf("\n");

printf("\n");



return 0;

}





static int test_interrupt_transfer(void)
{
int r,i;
int transferred;
char answer[PACKET_INT_LEN];
char question[PACKET_INT_LEN];
for (i=0;i<PACKET_INT_LEN; i++) question[i]=i;

r = libusb_interrupt_transfer(devh, ENDPOINT_INT_OUT, question, PACKET_INT_LEN,
&transferred,TIMEOUT);
if (r < 0) {
fprintf(stderr, "Interrupt write error %d\n", r);
return r;
}
r = libusb_interrupt_transfer(devh, ENDPOINT_INT_IN, answer,PACKET_INT_LEN,
&transferred, TIMEOUT);
if (r < 0) {
fprintf(stderr, "Interrupt read error %d\n", r);
return r;
}
if (transferred < PACKET_INT_LEN) {
fprintf(stderr, "Interrupt transfer short read (%d)\n", r);
return -1;
}

for(i = 0;i < PACKET_INT_LEN; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");

printf("\n");

return 0;
}


static int test_bulk_transfer(void)
{
int r,i;
int transferred;
char answer[PACKET_BULK_LEN];
char question[PACKET_BULK_LEN];
for (i=0;i<PACKET_BULK_LEN; i++) question[i]=i;

r = libusb_bulk_transfer(devh, ENDPOINT_BULK_OUT, question, PACKET_BULK_LEN,
&transferred,TIMEOUT);
if (r < 0) {
fprintf(stderr, "Bulk write error %d\n", r);
return r;
}
r = libusb_bulk_transfer(devh, ENDPOINT_BULK_IN, answer,PACKET_BULK_LEN,
&transferred, TIMEOUT);
if (r < 0) {
fprintf(stderr, "Bulk read error %d\n", r);
return r;
}
if (transferred < PACKET_BULK_LEN) {
fprintf(stderr, "Bulk transfer short read (%d)\n", r);
return -1;
}

for(i = 0;i < PACKET_BULK_LEN; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");

printf("\n");

return 0;
}



int main(void)

{

int r = 1;



r = libusb_init(NULL);

if (r < 0) {

fprintf(stderr, "failed to initialise libusb\n");

exit(1);

}



r = find_lvr_winusb();

if (r < 0) {

fprintf(stderr, "Could not find/open device\n");

goto out;

}

printf("Successfully find the LVR WINUSB device\n");

r = libusb_set_configuration(devh, 1);

if (r < 0) {

fprintf(stderr, "libusb_set_configuration error %d\n", r);

goto out;

}

printf("Successfully set usb configuration 1\n");

r = libusb_claim_interface(devh, 0);

if (r < 0) {

fprintf(stderr, "libusb_claim_interface error %d\n", r);

goto out;

}

printf("Successfully claimed interface\n");


printf("Testing control transfer: ");
test_control_transfer();

printf("Testing interrupt transfer: ");
test_interrupt_transfer();
printf("Testing bulk transfer: ");

test_bulk_transfer();


libusb_release_interface(devh, 0);

out:

//This is somehow necessary, without it there seems to be a problem.

libusb_reset_device(devh);

libusb_close(devh);

libusb_exit(NULL);

return r >= 0 ? r : -r;



}



mcuee@ubuntu:~/Desktop/build/libusb1/examples/winusb_new/lvrWinusb$ sudo ./libusb1_LVR_WinUSB2
Successfully find the LVR WINUSB device
Successfully set usb configuration 1
Successfully claimed interface
Testing control transfer:
20, 20; 21, 21;

Testing interrupt transfer:
00, 00; 01, 01;

Testing bulk transfer:
00, 00; 01, 01; 02, 02; 03, 03; 04, 04; 05, 05; 06, 06; 07, 07;
08, 08; 09, 09; 0a, 0a; 0b, 0b; 0c, 0c; 0d, 0d; 0e, 0e; 0f, 0f;
10, 10; 11, 11; 12, 12; 13, 13; 14, 14; 15, 15; 16, 16; 17, 17;
18, 18; 19, 19; 1a, 1a; 1b, 1b; 1c, 1c; 1d, 1d; 1e, 1e; 1f, 1f;
20, 20; 21, 21; 22, 22; 23, 23; 24, 24; 25, 25; 26, 26; 27, 27;
28, 28; 29, 29; 2a, 2a; 2b, 2b; 2c, 2c; 2d, 2d; 2e, 2e; 2f, 2f;
30, 30; 31, 31; 32, 32; 33, 33; 34, 34; 35, 35; 36, 36; 37, 37;
38, 38; 39, 39; 3a, 3a; 3b, 3b; 3c, 3c; 3d, 3d; 3e, 3e; 3f, 3f;
post edited by xiaofan - 2010/02/04 04:58:25

  USB_Links and libusb
#61
xiaofan
Super Member
  • Total Posts : 6247
  • Reward points : 0
  • Joined: 2005/04/14 07:05:25
  • Location: Singapore
  • Status: offline
RE: Jan Axelson`s WinUSB example under Linux with libusb 2010/02/04 05:01:32 (permalink)
0
Functional same as the above but with libusb 0.1.


#include <usb.h>
#include <stdio.h>



#define VERSION "0.1.0"

#define VENDOR_ID 0x0925

#define PRODUCT_ID 0x1456

#define INTERFACE 0
#define WINUSB_REQUEST_1 0x01

#define WINUSB_REQUEST_2 0x02
#define LIBUSB_REQUEST_TYPE_VENDOR (0x02 << 5)
#define LIBUSB_RECIPIENT_INTERFACE 0x01
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | USB_ENDPOINT_IN | LIBUSB_RECIPIENT_INTERFACE)

#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | USB_ENDPOINT_OUT | LIBUSB_RECIPIENT_INTERFACE)
const static int reqCtlLen=2;

const static int reqIntLen=2;
const static int reqBulkLen=64;
const static int endpoint_Int_in=0x82; /* endpoint 0x81 address for IN */
const static int endpoint_Int_out=0x02; /* endpoint 1 address for OUT */
const static int endpoint_Bulk_in=0x81; /* endpoint 0x81 address for IN */
const static int endpoint_Bulk_out=0x01; /* endpoint 1 address for OUT */
const static int timeout=5000; /* timeout in ms */


void bad(const char *why) {
fprintf(stderr,"Fatal error> %s\n",why);
exit(17);
}


usb_dev_handle *find_lvr_winusb();



usb_dev_handle* setup_libusb_access() {

usb_dev_handle *lvr_winusb;

usb_set_debug(255);

usb_init();

usb_find_busses();

usb_find_devices();



if(!(lvr_winusb = find_lvr_winusb())) {

printf("Couldn't find the USB device, Exiting\n");

return NULL;

}

if (usb_set_configuration(lvr_winusb, 1) < 0) {

printf("Could not set configuration 1 : \n");

return NULL;

}

if (usb_claim_interface(lvr_winusb, INTERFACE) < 0) {

printf("Could not claim interface: \n");

return NULL;

}

return lvr_winusb;

}



usb_dev_handle *find_lvr_winusb()

{

struct usb_bus *bus;

struct usb_device *dev;



for (bus = usb_busses; bus; bus = bus->next) {

for (dev = bus->devices; dev; dev = dev->next) {

if (dev->descriptor.idVendor == VENDOR_ID &&

dev->descriptor.idProduct == PRODUCT_ID ) {

usb_dev_handle *handle;

printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", VENDOR_ID, PRODUCT_ID);

if (!(handle = usb_open(dev))) {

printf("Could not open USB device\n");

return NULL;

}

return handle;

}

}

}

return NULL;

}



static int test_control_transfer(usb_dev_handle *dev)
{
int r,i;
char answer[reqCtlLen];
char question[reqCtlLen];
for (i=0;i<reqCtlLen; i++) question[i]=0x20+i;

r = usb_control_msg(dev,CTRL_OUT,WINUSB_REQUEST_1,2, 0,question, reqCtlLen,timeout);
if (r < 0) {
fprintf(stderr, "Control Out error %d\n", r);
return r;
}
r = usb_control_msg(dev,CTRL_IN,WINUSB_REQUEST_2,2,0, answer,reqCtlLen,timeout);
if (r < 0) {
fprintf(stderr, "Control IN error %d\n", r);
return r;
}
for(i = 0;i < reqCtlLen; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");

return 0;
}

void test_interrupt_transfer(usb_dev_handle *dev)

{

int r,i;
char answer[reqIntLen];
char question[reqIntLen];
for (i=0;i<reqIntLen; i++) question[i]=0x40+i;
r = usb_interrupt_write(dev, endpoint_Int_out, question, reqIntLen, timeout);
if( r < 0 )
{
perror("USB interrupt write"); bad("USB write failed");
}
r = usb_interrupt_read(dev, endpoint_Int_in, answer, reqIntLen, timeout);
if( r != reqIntLen )
{
perror("USB interrupt read"); bad("USB read failed");
}
// for (i=0;i<reqIntLen; i++) printf("%i, %i, \n",question[i],answer[i]);
for(i = 0;i < reqIntLen; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");

}


void test_bulk_transfer(usb_dev_handle *dev)

{

int r,i;
char answer[reqBulkLen];
char question[reqBulkLen];
for (i=0;i<reqBulkLen; i++) question[i]=i;
r = usb_bulk_write(dev, endpoint_Bulk_out, question, reqBulkLen, timeout);
if( r < 0 )
{
perror("USB bulk write"); bad("USB write failed");
}
r = usb_bulk_read(dev, endpoint_Bulk_in, answer, reqBulkLen, timeout);
if( r != reqBulkLen )
{
perror("USB bulk read"); bad("USB read failed");
}
// for (i=0;i<reqBulkLen;i++) printf("%i, %i, \n",question[i],answer[i]);
for(i = 0;i < reqBulkLen; i++) {
if(i%8 == 0)
printf("\n");
printf("%02x, %02x; ",question[i],answer[i]);
}
printf("\n");
}



int main( int argc, char **argv)

{

usb_dev_handle *lvr_winusb;

if ((lvr_winusb = setup_libusb_access()) == NULL) {

exit(-1);

}

printf("Testing control transfer: ");
test_control_transfer(lvr_winusb);
printf("Testing interrupt transfer: ");
test_interrupt_transfer(lvr_winusb);
printf("Testing bulk transfer: ");

test_bulk_transfer(lvr_winusb);



usb_release_interface(lvr_winusb, 0);
usb_reset(lvr_winusb);

usb_close(lvr_winusb);



return 0;

}


  USB_Links and libusb
#62
Page: < 1234 Showing page 4 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5