I programmed on the device a couple of Vendor requests so i know for
sure what does it answers.
I also programmed the comunication in C with libusb 1.0 and with pyusb.
PyUSB gives different error messages (always -1 Input/Output Error)
than the errors with libusb 1.0 in C.
Also here is a working example in C but the same in python with pyusb
doesnt work.
Please check if it is correctly programmed for pyusb:
==============================================================================
=== Example-libusb.c
==============================================================================
#include <stdio.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_IN)
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE
| LIBUSB_ENDPOINT_OUT)
//=============================================================================
// Constantes para la comunicacion con el Firmware.
#define USB_REQ_GPIO 15
#define REQUEST_LINE 0
#define LIBUSB_DEBUG 3
libusb_device_handle* usb_find_and_open(int vendorID, int productID);
int main()
{
const unsigned char rawVid[2] = {0x51, 0x04};
const unsigned char rawPid[2] = {0x10, 0x34};
int vendorID, productID;
libusb_device_handle* usbdev;
unsigned char buffer[8+1]={0};
int rc, wValue;
vendorID = rawVid[1] * 256 + rawVid[0];
productID = rawPid[1] * 256 + rawPid[0];
printf("Buscando: VID=%x, PID=%x\n",vendorID,productID);
if ( libusb_init(NULL) != 0 ) {
fprintf(stderr, "failed to init libusb\n");
return 0;
}
//libusb_set_debug(NULL,3);
usbdev = usb_find_and_open(vendorID, productID);
if (usbdev == NULL)
{
fprintf(stderr, "failed to find device\n");
libusb_exit(NULL);
return 0;
}
printf("Encontrado.\n");
if ( libusb_detach_kernel_driver(usbdev, 0) !=0 ){
fprintf(stderr, "failed to detach kernel driver, already
detached?\n");
//libusb_close(usbdev);
//libusb_exit(NULL);
//return 0;
}
else
printf("Kernel driver detached.\n");
if ( libusb_claim_interface(usbdev, 0) !=0 ){
fprintf(stderr, "failed to claim interface\n");
libusb_close(usbdev);
libusb_exit(NULL);
return 0;
}
rc = libusb_control_transfer(usbdev, CTRL_IN, REQUEST_LINE, 0, 0,
buffer, 8, 1000 );
if ( rc < 0){
fprintf(stderr, "failed to transfer a few bytes, failed with
error %d\n", rc);
libusb_release_interface(usbdev, 0);
libusb_close(usbdev);
libusb_exit(NULL);
return 0;
}
buffer[rc] = '\0';
printf("Lectura realizada REQUEST_LINE correctamente. Transferido
%d bytes: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
rc, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5],
buffer[6], buffer[7]);
rc = libusb_control_transfer(usbdev, CTRL_IN, USB_REQ_GPIO, 0, 0,
buffer, 8, 1000 );
if ( rc < 0){
fprintf(stderr, "failed to transfer a few bytes, failed with
error %d\n", rc);
libusb_release_interface(usbdev, 0);
libusb_close(usbdev);
libusb_exit(NULL);
return 0;
}
buffer[rc] = '\0';
printf("Lectura realizada USB_REQ_GPIO correctamente. Transferido
%d bytes: 0x%02x\n", rc, buffer[0]);
wValue = 0x00FF;
rc = libusb_control_transfer(usbdev, CTRL_OUT, USB_REQ_GPIO,
wValue, 0, 0, 0, 1000 );
if ( rc < 0){
fprintf(stderr, "failed to transfer a few bytes, failed with
error %d\n", rc);
libusb_release_interface(usbdev, 0);
libusb_close(usbdev);
libusb_exit(NULL);
return 0;
}
/*if ( rc!=1 ){
fprintf(stderr, "failed to transfer safelly a few bytes\n");
libusb_release_interface(usbdev, 0);
libusb_close(usbdev);
libusb_exit(NULL);
return 0;
}*/
printf("Escritura realizada USB_REQ_GPIO correctamente. Estado:
0x%02x\n", wValue);
/*
libusb_release_interface(usbdev, 0);
if ( libusb_attach_kernel_driver(usbdev, 0) !=0 ){
fprintf(stderr, "failed to attach kernel driver\n");
//libusb_close(usbdev);
//libusb_exit(NULL);
//return 0;
}
else
printf("Kernel driver reattached.\n");*/
libusb_close(usbdev);
libusb_exit(NULL);
return 0;
}
libusb_device_handle* usb_find_and_open(int vendorID, int productID)
{
libusb_device_handle *handle;
libusb_device **list;
size_t i = 0;
size_t cnt;
int err = 0;
char buffer[60];
int len=0;
cnt = libusb_get_device_list(NULL, &list);
if (cnt < 0){
fprintf(stderr, "failed to get device list\n");
}
for (i = 0; i < cnt; i++)
{
libusb_device *dev = list[i];
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "failed to get device descriptor\n");
continue;
}
if ( ! (vendorID==0
|| desc.idVendor == vendorID) )
continue;
if ( ! (productID == 0
|| desc.idProduct == productID) )
continue;
printf("--> VID=%x, PID=%x\n",vendorID,productID);
err = libusb_open(dev, &handle);
if (err){
fprintf(stderr, "failed to open device\n");
continue;
}
len = buffer[0] = 0;
if(desc.iManufacturer > 0){
len = libusb_get_string_descriptor_ascii(handle,
desc.iManufacturer, (unsigned char*)buffer, sizeof(buffer));
if(len < 0)
fprintf(stderr, "cannot query manufacturer for
VID=0x%04x PID=0x%04x\n", vendorID, productID);
}
else { libusb_close(handle);
continue;
}
printf("--> vendor=%s\n",buffer);
len = buffer[0] = 0;
if(desc.iProduct > 0){
len = libusb_get_string_descriptor_ascii(handle,
desc.iProduct, (unsigned char*)buffer, sizeof(buffer));
if(len < 0)
fprintf(stderr, "cannot query product for VID=0x%04x
PID=0x%04x\n", vendorID, productID);
}
else { libusb_close(handle);
continue;
}
printf("--> product=%s\n",buffer);
libusb_free_device_list(list, 1);
return handle;
}
libusb_free_device_list(list, 1);
return NULL;
}
=================================================================================
==== PyUSB-example.py
=================================================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import usb.core
import usb.util
USB_REQ_GPIO = 15
REQUEST_LINE = 0
TI_VENDOR_ID = 0x0451
TI_3410_PRODUCT_ID = 0x3410
# Configuration Descriptors IDs
# Very important to know if the firmware was loaded
TI_BOOT_CONFIG = 1
TI_ACTIVE_CONFIG = 2
if __name__ == '__main__':
# find our device
dev = usb.core.find(idVendor=TI_VENDOR_ID, idProduct=TI_3410_PRODUCT_ID)
# was it found?
if dev is None:
raise ValueError('Device not found')
interface = dev.get_interface_altsetting()
if dev.is_kernel_driver_active(interface.bInterfaceNumber):
dev.detach_kernel_driver(interface.bInterfaceNumber)
# Setting configuration
if dev.bNumConfigurations == 1:
raise "ERROR: The firmware was not yet uploaded"
elif dev.get_active_configuration().bConfigurationValue == 1:
dev.set_configuration(TI_ACTIVE_CONFIG)
else:
print "Configuration already set in ", TI_ACTIVE_CONFIG
interface = dev.get_interface_altsetting()
if dev.is_kernel_driver_active(interface.bInterfaceNumber):
dev.detach_kernel_driver(interface.bInterfaceNumber)
print "Going for reading: REQUEST_LINE "
requesttype = (usb.util.CTRL_TYPE_VENDOR |
usb.util.CTRL_RECIPIENT_DEVICE | usb.util.CTRL_IN)
command = REQUEST_LINE
value = 0
moduleid = 0 #USBCTRL_UART1_PORT#USBCTRL_RAM_PORT
msg = 0
print dev.ctrl_transfer(bmRequestType=requesttype,
bRequest=command,
wValue=value,
wIndex=moduleid,
data_or_wLength=msg,
timeout=1000)
print "Going for reading: USB_REQ_GPIO "
requesttype = (usb.util.CTRL_TYPE_VENDOR |
usb.util.CTRL_RECIPIENT_DEVICE | usb.util.CTRL_IN)
command = USB_REQ_GPIO
value = 0
moduleid = 0 #USBCTRL_UART1_PORT#USBCTRL_RAM_PORT
msg = 0
print dev.ctrl_transfer(bmRequestType=requesttype,
bRequest=command,
wValue=value,
wIndex=moduleid,
data_or_wLength=msg,
timeout=1000)
print "Going for writing: USB_REQ_GPIO "
requesttype = (usb.util.CTRL_TYPE_VENDOR |
usb.util.CTRL_RECIPIENT_DEVICE | usb.util.CTRL_OUT)
command = USB_REQ_GPIO
value = 0xFF
moduleid = 0 #USBCTRL_UART1_PORT#USBCTRL_RAM_PORT
msg = 0
print dev.ctrl_transfer(bmRequestType=requesttype,
bRequest=command,
wValue=value,
wIndex=moduleid,
data_or_wLength=msg,
timeout=1000)
#try:
#xdata_set_register(dev,UART1_MCR, MCR_DTR | MCR_RTS, 0xFF)
#except usb.core.USBError as (a,b,c,d):
# print "ERROR: Control Transfer", (a,b,c)
#print "reattaching"
#try:
# dev.attach_kernel_driver(interface.bInterfaceNumber)
#except usb.core.USBError as e:
# print "ERROR: Reattaching driver ", e
2010/6/24 Xiaofan Chen <[email protected]>:
> On Fri, Jun 25, 2010 at 1:44 AM, Diego Jacobi <[email protected]> wrote:
>> I cant get ctrol_transfer to work in anyway. It will always give an error.
>> Calling it even without payload:
>>
>> I readed all of the pertinent source code of pyusb until the call to
>> libusb, and I myself cant find what the problem is.
>
> All I can say is that you will have to know how the device works
> before using libusb or pyusb. You can not just send any control
> message and expect it to work.
>
> If you suspect that pyusb has a bug, then you can try to use libusb
> directly. Once it works there, you can come back to use pyusb.
>
>
>
> --
> Xiaofan http://mcuee.blogspot.com
>
> ------------------------------------------------------------------------------
> ThinkGeek and WIRED's GeekDad team up for the Ultimate
> GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
> lucky parental unit. See the prize list and enter to win:
> http://p.sf.net/sfu/thinkgeek-promo
> _______________________________________________
> pyusb-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pyusb-users
>
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
pyusb-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyusb-users