• AVR Freaks

Jan Axelson's WinUSB example under Linux with libusb

Page: < 1234 > Showing page 2 of 4
Author
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/18 02:37:33 (permalink)
0
With libusb it work great !
Thank you very much guys! :)

I'll do some other tests then i'll publish the entire qt example class

Sandro
#21
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/21 04:04:03 (permalink)
0
Ok... i did many tests under linux and windows
Initially i tried to use original winusb driver under windows but without success. Actually mingw haven't any type of header for winusb so i downloaded microsoft ddk and write my own winusb header... gcc link succesfully the file but when it try ti call "winusb_initialize" the application crash (it seem not try to load winusb.dll ... i don't know why).
So i choose for a simpler way. Libusb under windows. It worked easily with the same linux code.

In my test i had again problems reading from device. My solution is an usb_reset during the initialization process (only on linux systems)... now the system work right every time (but is it the right way?)

This is my code for qt 4.5 .. it crosscompile well on linux and windows systems.

qusb.h
#ifndef QUSB_H
#define QUSB_H

#include <QObject>
#include <QtGlobal>

#include <usb.h>

class QUSB : public QObject
{
private:
const static int INTERFACE = 0;
const static int CONFIGURATION = 1;
const static int ENDPOINT_IN = 0x81;
const static int ENDPOINT_OUT = 0x01;
const static int TIMEOUT = 5000;

bool m_IsOpened;
usb_dev_handle *m_UsbDevice;

bool FindDevice (quint16 VendorId, quint16 ProductId);

public:
QUSB();

bool Open (quint16 VendorId, quint16 ProductId);
void Close ();
bool Write (char *packet, quint8 size);
bool Read (char *packet, quint8 size);
};

#endif // QUSB_H



qusb.cpp
#include "qusb.h"

QUSB::QUSB()
{
this->m_IsOpened = false;
}

bool QUSB::FindDevice(quint16 VendorId, quint16 ProductId)
{
struct usb_bus *bus;
struct usb_device *dev;

qDebug ("Scanning for device with VendorId = 0x%04x and ProductId = 0x%04x", VendorId, ProductId);
usb_find_busses ();
usb_find_devices ();

for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == VendorId && dev->descriptor.idProduct == ProductId)
{
qDebug ("Device found");
if (!(this->m_UsbDevice = usb_open(dev)))
{
qCritical ("Could not open USB device");
return false;
}
this->m_IsOpened = true;
return true;
}
}
}

qDebug ("Cannot find specified device");
return false;
}

bool QUSB::Open (quint16 VendorId, quint16 ProductId)
{
if (this->m_IsOpened) this->Close ();

qDebug ("Initializing LibUSB");
usb_init ();
//usb_set_debug (255);

if (!this->FindDevice (VendorId, ProductId)) return false;

#ifndef Q_OS_WIN32
if (usb_reset (this->m_UsbDevice))
{
qCritical ("Cannot reset usb connection.");
this->Close ();
return false;
}
qDebug ("Resetting usb connection.");
#endif

if (usb_set_configuration (this->m_UsbDevice, this->CONFIGURATION) < 0)
{
qCritical ("Cannot set configuration 1.");
this->Close ();
return false;
}
qDebug ("Configuration 1 setted");

if (usb_claim_interface (this->m_UsbDevice, this->INTERFACE) < 0)
{
qCritical ("Cannot claim interface.");
this->Close ();
return false;
}
qDebug ("Interface claimed");

return true;
}

bool QUSB::Write (char *packet, quint8 size)
{
if (!this->m_IsOpened)
{
qCritical ("Usb connection not opened");
return false;
}

if (usb_bulk_write (this->m_UsbDevice, this->ENDPOINT_OUT, packet, (int)size, this->TIMEOUT) < 0)
{
qCritical ("Bulk xfer write failed");
return false;
}
return true;
}

bool QUSB::Read (char *packet, quint8 size)
{
if (!this->m_IsOpened)
{
qCritical ("Usb connection not opened");
return false;
}

if (usb_bulk_read (this->m_UsbDevice, this->ENDPOINT_IN, packet, (int)size, this->TIMEOUT) < 0)
{
qCritical ("Bulk xfer read failed");
return false;
}
return true;
}

void QUSB::Close ()
{
qDebug ("Closing LibUSB");
usb_release_interface (this->m_UsbDevice, this->INTERFACE);
usb_close (this->m_UsbDevice);
this->m_IsOpened = false;
}


Project note:
for link with libusb
LIBS += -lusb

probabilly on windows you need to have a custom path for libusb
windows:INCLUDEPATH += C:\Programmi\LibUSB-Win32\include
windows:LIBS += -L"C:\Programmi\LibUSB-Win32\lib\gcc"
#22
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/23 23:58:23 (permalink)
0
Hi all...Sorry for my basic english but i'm italian...I have some problems with USB communication.I use libUSB.I must communicate with a PIC18f2550.I created a c program.This program can recognized the pic but when i try to communicate something an error happen...Every operation that i try the libusb function return -1 (error).I post c code and i hope that someone can help me.Thanks.

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

struct usb_bus *temp_bus;
struct usb_device *comm_device;
struct usb_dev_handle *comm_handle;


void init_lib()
{
printf("\nInitializing lib_usb...\n");
usb_init();
printf("Searching usb_busses...\n");
int num_bus=usb_find_busses();
printf("Searching usb_devices...\n");
int num_dev=(usb_find_devices())-num_bus;
printf("BUSSES FOUND : ");
printf("%d",num_bus);
printf("\n");
printf("DEVICES FOUND : ");
printf("%d",num_dev);
printf("\n");
}

struct usb_device *find_device(int idVendor,int idProduct)
{
puts("\nSearching device...");
temp_bus=usb_get_busses();
struct usb_device *device;
while(temp_bus->next!=NULL)
{
struct usb_device *temp_device;
struct usb_device_descriptor temp_desc;

temp_device=temp_bus->devices;

while(temp_device->next!=NULL)
{
temp_desc=temp_device->descriptor;
if(temp_desc.idVendor==idVendor && temp_desc.idProduct==idProduct)
{
comm_device=temp_device;
puts("\nDevice found...");
}
temp_device=temp_device->next;
}
temp_bus=temp_bus->next;
}
return comm_device;
}

int main()
{
init_lib();
comm_device=find_device(0x1222,0x0007);
comm_handle=usb_open(comm_device);

/*char array[2];
array[0]='a';
array[1]='b';
int num=usb_bulk_write(comm_handle,0xFF,array,2,1500);
printf("%d",num);


int num=usb_set_configuration(comm_handle,0x156);
printf("%d",num);*/


return 0;
}
#23
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 01:22:28 (permalink)
0
Hi Marco,
before write data you have to load configuration (are you sure 0x156 is the right configuration?) and claim the inteface (see xiofan examples).
Try also with the function
usb_set_debug (255);

it enable a verbose debug from libusb and it print more informations
#24
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 05:55:30 (permalink)
0
Thanks skaman.I thougth that was necessary to set configuration but i'dont know how set this configuration...i don't know what fields compose the integer of usb_set_configuration function...where can i find some example?can you post some links?thank you very much...
#25
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 06:08:52 (permalink)
0
The example are all in this thread. Xiofan published many examples (about openusb and libusb) with many notes.
You can also see my last example about libusb ont QT (the post before your first post)

About configuration and interface i suppose you can use this values

const static int INTERFACE = 0;
const static int CONFIGURATION = 1;

and

usb_set_configuration (this->m_UsbDevice, this->CONFIGURATION)
usb_claim_interface (this->m_UsbDevice, this->INTERFACE)

this values works right with microchip winusb examples and microchip libusb example.
You can also see the latest microchip framework (2.5)... inside can find a libusb example for windows and linux

Regards

Sandro
#26
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 06:36:25 (permalink)
0
Sei italiano?Cosi riesco a spiegarti meglio il mio problema...
Are you italian?
#27
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 06:51:07 (permalink)
0
Yes... i'm italian... but i think english is preferred by other users ... so everyone can read and understand our discussion
(it's not important a good english grammatically corrected... try to explain as better you can)
#28
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:08:30 (permalink)
0
yeah...of course...sorry...i have try with your suggestions but it doesen't work...if i use the function usb_set_configuration(my_handle,0) it return always -1 (error).
PIC always transferr a char ('c') and with my c program i must read it and display it...i don't understand how must be the int CONFIGURATION...In official documentation i don't find everything.I don't know what is int CONFIGURATION (speed,data length....???)
#29
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:15:08 (permalink)
0
Have you tried with usb_set_configuration(my_handle,1) ?
Are you using an example firmware from microchip?
#30
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:19:27 (permalink)
0
You can also use lsusb -v for discover informations about you usb device

Bus 002 Device 002: ID 04d8:0204 Microchip Technology, Inc. 
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x04d8 Microchip Technology, Inc.
idProduct 0x0204
bcdDevice 0.00
iManufacturer 1 Microchip Technology Inc.
iProduct 2 Microchip Libusb Example Device
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 32
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 255 Vendor Specific Protocol
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Device Status: 0x0001
Self Powered
#31
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:20:14 (permalink)
0
yes i tried with int conf = 1.It return always -1.On pic there is a my firware created with mikroC HID tool
#32
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:30:56 (permalink)
0
i don't know how HID work.... but you can check this libraries http://libhid.alioth.debian.org/
#33
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:31:56 (permalink)
0
with lsusb i found my device:

Bus 002 Device 002: ID 1222:0007
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x1222
idProduct 0x0007
bcdDevice 0.01
iManufacturer 1
iProduct 2
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 41
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 0 No Subclass
bInterfaceProtocol 0 None
iInterface 0
HID Device Descriptor:
bLength 9
bDescriptorType 33
bcdHID 1.01
bCountryCode 0 Not supported
bNumDescriptors 1
bDescriptorType 34 Report
wDescriptorLength 47
Report Descriptors:
** UNAVAILABLE **
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
can't get device qualifier: Operation not permitted
can't get debug descriptor: Operation not permitted
cannot read device status, Operation not permitted (1)
#34
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 07:57:26 (permalink)
0
In a normal flow you have to do:
- find device
- open device
- set configuration
- claim interface
- read/write operations
- release interface
- close device

For stability reason i changed it into:
- find device
- open device
- reset usb device (only on linux)
- set configuration
- claim interface
- read/write operations
- release interface
- close device

Try to do exact this flow and enable libusb debug.... if it still not work print here your exact code and debug output
#35
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 08:11:26 (permalink)
0
ok...but usb_set_configuration and also usb_reset return -1...in this way i can't do anything...why this function return always -1?
#36
skaman
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2008/11/25 03:56:21
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 08:28:33 (permalink)
0
Without any information i can't help you... try to put here your exact (and actual) source code and the application output (with libusb debug active)
#37
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/24 08:49:31 (permalink)
0
On pc this program runs:
<ccode>
 
#include<usb.h>
#include<stdio.h>

struct usb_bus *temp_bus;
struct usb_device *comm_device;
struct usb_dev_handle *comm_handle;


void init_lib()
{
printf("\nInitializing lib_usb...\n");
usb_init();
printf("Searching usb_busses...\n");
int num_bus=usb_find_busses();
printf("Searching usb_devices...\n");
int num_dev=(usb_find_devices())-num_bus;
printf("BUSSES FOUND : ");
printf("%d",num_bus);
printf("\n");
printf("DEVICES FOUND : ");
printf("%d",num_dev);
printf("\n");
}

struct usb_device *find_device(int idVendor,int idProduct)
{
puts("\nSearching device...");
temp_bus=usb_get_busses();
struct usb_device *device;
while(temp_bus->next!=NULL)
{
struct usb_device *temp_device;
struct usb_device_descriptor temp_desc;

temp_device=temp_bus->devices;

while(temp_device->next!=NULL)
{
temp_desc=temp_device->descriptor;
if(temp_desc.idVendor==idVendor && temp_desc.idProduct==idProduct)
{
comm_device=temp_device;
puts("\nDevice found...");
}
temp_device=temp_device->next;
}
temp_bus=temp_bus->next;
}
return comm_device;
}

int main()
{
init_lib();
comm_device=find_device(0x1222,0x0007);
comm_handle=usb_open(comm_device);

usb_set_debug(255);
 
int num=usb_set_configuration(comm_handle,1);
if(num<0){printf("Error");}
 
/*
 
int num=usb_reset(comm_handle);
if(num<0){printf("Error");}
 
*/

return 0;
}
 
I post also the firmware in mikroC project including all of file used (*.c , *.h ....)
#38
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 2009/08/24 22:00:35 (permalink)
0
If you run libusb under Linux with your HID device, the kernel hid driver will claim the device. Therefore the program will fail. You need to use the non-portable libhid function under Linux.
http://libusb.sourceforge.net/doc/ref.nonportable.html

usb_get_driver_np -- Get driver name bound to interface
usb_detach_kernel_driver_np -- Detach kernel driver from interface

  USB_Links and libusb
#39
MarcoPICXXX
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2009/08/23 23:50:41
  • Location: 0
  • Status: offline
RE: Jan Axelson's WinUSB example under Linux with libusb 2009/08/25 00:20:44 (permalink)
0
xiaofan thank you very much (thank you also scaman).I try your hints and i hope...
#40
Page: < 1234 > Showing page 2 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5