Hi,
I am new to PyUSB and libusb-win32. I am trying to send data to my HID
device to turn LED ON/OFF along with some characters to display on LCD
on the Device. As my HID Device got the ready made code from Renesas
Rx62N USB Device API. It is working fine. And Even PyUSB device can read
the fields of device descriptor, configuration descriptor, interface
descriptor and endpoint descriptor.
Now I want to send 17bytes of data to my device. My device got only
Interrupt IN Endpoint-3. So I think I need to use Control Transfer to
send data to my device. I tried a lot different ways but it is giving me
error on the following line.
I have tried other bmRequestType but does not work.
dev.ctrl_transfer(bmRequestType=0x42, bRequest=0x09, wValue=0x02,
wIndex=0x00, data_or_wLength=msg, timeout=100)
raise USBError(errmsg)
usb.core.USBError: libusb0-dll:err [control_msg] sending control message
failed, win error: A device attached to the system is not functioning.
I have attached the descriptor files along with the file of my code.
Please guide me, what should I do to get it working. Or where am I wrong?
Thanks,
Regards,
Prashant Patel
/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
*******************************************************************************/
/* Copyright (C) 2010 Renesas Electronics Corporation. All rights reserved. */
/**********************************************************************
* File Name : usbdescriptors.h
* Version : 1.00
* Device : Renesas Generic MCU.
* Tool Chain : HEW
* H/W Platform : RSK Generic
* Description : Descriptors required to enumerate a device as a USB HID Class.
This sets up a Report IN and a Report OUT.
NOTE: This will need to be modified for a particular
product as it includes company/product specific data
including
string descriptors specifying
Manufacturer, Product and Serial Number.
**********************************************************************/
/**********************************************************************
* History : 27.07.2010 Ver. 1.00 First Release
**********************************************************************/
#ifndef FILENAME_USBDESCRIPTORS_H
#define FILENAME_USBDESCRIPTORS_H
/***********************************************************************************
System Includes
***********************************************************************************/
#include "stdint.h"
/***********************************************************************************
User Includes
***********************************************************************************/
#include "usb_hal.h"
#include "usb_core.h"
/***********************************************************************************
Defines
***********************************************************************************/
#define HID_REPORT_DESCRIPTOR_SIZE 34
#define HID_DESCRIPTOR_SIZE 9
#define START_INDEX_OF_HID_WITHIN_CONFIG_DESC 18
/* As specified in the Report Descriptor */
#define OUTPUT_REPORT_SIZE 17
/* As specified in the Report Descriptor */
#define INPUT_REPORT_SIZE 5
/***********************************************************************************
Type Definitions
***********************************************************************************/
/* Descriptor */
typedef struct DESCRIPTOR
{
const uint16_t length;
const uint8_t* pucData;
}DESCRIPTOR;
/* Device Descriptor */
extern const DESCRIPTOR gDeviceDescriptor;
/* Configuration, Interface, HID and Endpoint Descriptor */
extern const DESCRIPTOR gConfigurationDescriptor;
/* Hid Report Descriptor */
extern const DESCRIPTOR gHIDReportDescriptor;
/* String descriptors */
extern const DESCRIPTOR gStringDescriptorLanguageIDs;
extern const DESCRIPTOR gStringDescriptorManufacturer;
extern const DESCRIPTOR gStringDescriptorProduct;
extern const DESCRIPTOR gStringDescriptorSerialNum;
/* FILENAME_USBDESCRIPTORS_H */
#endif
/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
*******************************************************************************/
/* Copyright (C) 2010 Renesas Electronics Corporation. All rights reserved. */
/**********************************************************************
* File Name : usbdescriptors.c
* Version : 1.00
* Device : Renesas Generic MCU.
* Tool Chain : HEW
* H/W Platform : RSK Generic
* Description : Descriptors required to enumerate a device as a USB HID Class.
This sets up a Report IN and a Report OUT.
NOTE: This will need to be modified for a particular
product as it includes company/product specific data
including
string descriptors specifying
Manufacturer, Product and Serial Number.
**********************************************************************/
/**********************************************************************
* History : 27.07.2010 Ver. 1.00 First Release
**********************************************************************/
/***********************************************************************************
User Includes
***********************************************************************************/
#include "USBDescriptors.h"
/***********************************************************************************
Defines
***********************************************************************************/
/*Vendor and Product ID*/
/*NOTE Please use your company Vendor ID when developing a new product.*/
#define VID 0x045B
#define PID 0x2013
/***********************************************************************************
Variables
***********************************************************************************/
/* Device Descriptor */
#define DEVICE_DESCRIPTOR_SIZE 18
static const uint8_t gDeviceDescriptorData[DEVICE_DESCRIPTOR_SIZE] =
{
//--Device Descriptor-----
/*bLength, Size of this descriptor*/
DEVICE_DESCRIPTOR_SIZE,
/*bDescriptorType, Device Descriptor=0x01*/
0x01,
/*bcdUSB, Two Bytes, USB Version 2.0*/
0x00,0x02,
/*bDeviceClass, Class Code - None as HID is defined in the Interface
Descriptor*/
0x00,
/*bDeviceSubclass, Subclass Code*/
0x00,
/*bDeviceProtocol, Protocol Code*/
0x00,
/*bMaxPacketSize0, Max Packet Size for endpoint 0*/
CONTROL_IN_PACKET_SIZE,
/* idVendor, Two bytes */
(uint8_t)(VID & 0xFF), /*Vendor ID LSB*/
(uint8_t)((VID>>8)& 0xFF),/*Vendor ID MSB*/
/* idProduct, Two bytes */
(uint8_t)(PID & 0xFF), /*Product ID LSB*/
(uint8_t)((PID>>8)& 0xFF),/*Product ID MSB*/
/*bcdDevice, Two Bytes, Device Release Number*/
0x00,0x01,
/*iManufacturer, Index of string descriptor for the manufacturer*/
STRING_iMANUFACTURER,
/*iProduct, Index of string descriptor for the product*/
STRING_iPRODUCT,
/*iSerialNumber, Index of string descriptor for the serial number*/
STRING_iSERIAL,
/*bNumConfigurations, Number of Configurations supported*/
0x01
};
const DESCRIPTOR gDeviceDescriptor =
{
DEVICE_DESCRIPTOR_SIZE, gDeviceDescriptorData
};
/*
Configuration Descriptor
For HID this includes Interface Descriptors, HID descriptor and endpoint
descriptor.
Ensure START_INDEX_OF_HID_WITHIN_CONFIG_DESC and HID_DESCRIPTOR_SIZE
are defined according to this.
*/
#define CONFIG_DESCRIPTOR_SIZE_TOTAL 34 /*This includes Interfaces and
Endpoints*/
static const uint8_t gConfigurationDescriptorData[CONFIG_DESCRIPTOR_SIZE_TOTAL]
=
{
//--Configuration Descriptor-----
/*bLength, Size of this descriptor (Just the configuration part)*/
0x09,
/*bDescriptorType, Configuration Descriptor*/
0x02,
/*wTotalLength, Combined length of all descriptors (little endian)*/
CONFIG_DESCRIPTOR_SIZE_TOTAL,0x00,
/*bNumInterfaces, Number of interfaces*/
0x01,
/*bConfigurationValue, Identifier for Set Configuration and Get
Configuration requests*/
0x01,
/*iConfiguration, Index of String Descriptor for this configuration.
0x00 meano no string descriptor*/
0x00,
/*bmAttributes - Self Powered(Not USB bus powered), No Remote Wakeup*/
0xC0,
/*bmAttributes - USB Bus powered, No Remote Wakeup*/
/*0x80,*/
/*bMaxPower (2mA units) 100mA (A unit load is defined as 100mA)*/
50,
//--Interface Descriptor-----
/*bLength, Size of this descriptor*/
0x09,
/*bDescriptorType, INTERFACE Descriptor*/
0x04,
/*bInterfaceNumber, Index(or Number) of this Interface i.e. Interface-0
*/
0x00,
/*bAlternateSetting*/
0x00,
/*bNumEndpoints, Number of Endpoints(Exluding Endpoint 0) (we have used
Interrupt IN)*/
0x01,
/*bInterfaceClass, HID Class code*/
0x03,
/*bInterfaceSubclass, No Subclass*/
0x00,
/*bInterfaceProtocol, No Protocol*/
0x00,
/*iInterface, Index of String Descriptor for this interface. 0x00 means
No String Descriptor*/
0x00,
//--HID Class Descriptor-----
/*Size of this descriptor*/
0x09,
/*HID Descriptor*/
0x21,
/*HID Class Specification Release Number 1.11*/
0x11,0x01,
/*No Target Country*/
0x00,
/*Number of HID Descriptors*/
0x01,
/*Type of HID Descriptor = "Report"*/
0x22,
/*Length Of HID Report Descriptor*/
HID_REPORT_DESCRIPTOR_SIZE,00,
//--Endpoint Descriptor-----
/*bLength, Size of this descriptor*/
0x07,
/*bDescriptorType, ENDPOINT Descriptor*/
0x05,
/*bEndpointAddress - IN endpoint, endpoint number = 3 i.e. Endpoint-3
IN*/
/*
Note: We have only one Endpoint and it is IN Endpoint, so if we need to
send data from PC(or Host), we have to use Endpoint-0 OUT.
*/
0x83,
/*bmAttributes - Endpoint Type is Interrupt*/
0x03,
/*wMaxPacketSize, Max Packet Size*/
INTERRUPT_IN_PACKET_SIZE,0x00,
/*bInterval, Polling Interval in mS*/
0x0A
};
const DESCRIPTOR gConfigurationDescriptor =
{
CONFIG_DESCRIPTOR_SIZE_TOTAL, gConfigurationDescriptorData
};
/*
Report Descriptor
NOTE The size of this must be HID_REPORT_DESCRIPTOR_SIZE
*/
static const uint8_t gHIDReportDescriptorData[HID_REPORT_DESCRIPTOR_SIZE] =
{
/* Usage Page - Vendor defined*/
0x06, 0xA0, 0xFF,
/* Usage ID within this page (Vendor defined)*/
0x09, 0x00,
/* Collection App (Windows requires an Application Collection) */
0xA1, 0x01,
/* *** The INPUT REPORT *** */
/* Usage ID within this page*/
0x09, 0x00,
/*Logical Min 0 */
0x15, 0x00,
/*Logical Max 255 */
0x26, 0xFF, 0x00,
/* Size 8 Bits (Each Field will be 8bits) */
0x75, 0x08,
/* Count (Number of fields(bytes) in INPUT report) */
0x95, INPUT_REPORT_SIZE,
/* Input Report=0x81 - type variable data */
0x81, 0x02,
/* *** The OUTPUR REPORT *** */
/* Usage ID within this page (Vendor defined)*/
0x09, 0x00,
/*Logical Min 0 */
0x15, 0x00,
/*Logical Max 255 */
0x26, 0xFF, 0x00,
/* Size 8 Bits (Each Field will be 8bits) */
0x75, 0x08,
/* Count (Number of fields(bytes) in INPUT report) */
0x95, OUTPUT_REPORT_SIZE,
/* Output Report=0x91 - type variable data */
0x91, 0x02,
/* End collection */
0xC0
};
const DESCRIPTOR gHIDReportDescriptor =
{
/* Must match value specified in HID descriptor! */
HID_REPORT_DESCRIPTOR_SIZE,
gHIDReportDescriptorData
};
/*String Descriptors*/
/*Note Language ID is in USB Core */
/*Manufacturer string*/
#define STRING_MANUFACTURER_SIZE 16
/* "Renesas" */
static const uint8_t
gStringDescriptorManufacturerData[STRING_MANUFACTURER_SIZE] =
{
/* Length of this descriptor*/
STRING_MANUFACTURER_SIZE,
/* Descriptor Type = STRING */
0x03,
/* Descriptor Text (unicode) */
'R', 0x00, 'E', 0x00, 'N', 0x00, 'E', 0x00,
'S', 0x00, 'A', 0x00, 'S', 0x00
};
const DESCRIPTOR gStringDescriptorManufacturer =
{
STRING_MANUFACTURER_SIZE,
gStringDescriptorManufacturerData
};
/*Product string*/
#define STRING_PRODUCT_SIZE 44
/* "HID USB Demonstration" */
static const uint8_t gStringDescriptorProductData[STRING_PRODUCT_SIZE] =
{
/* Length of this descriptor*/
STRING_PRODUCT_SIZE,
/* Descriptor Type = STRING */
0x03,
/* Descriptor Text (unicode) */
'H', 0x00, 'I', 0x00, 'D', 0x00, ' ', 0x00,
'U', 0x00, 'S', 0x00, 'B', 0x00, ' ', 0x00,
'D', 0x00, 'e', 0x00, 'm', 0x00, 'o', 0x00,
'n', 0x00, 's', 0x00, 't', 0x00, 'r', 0x00,
'a', 0x00, 't', 0x00, 'i', 0x00, 'o', 0x00,
'n', 0x00
};
const DESCRIPTOR gStringDescriptorProduct =
{
STRING_PRODUCT_SIZE,
gStringDescriptorProductData
};
/*Serial number string "1.1" */
#define STRING_SERIAL_NUM_SIZE 8
static const uint8_t gStringDescriptorSerialNumData[STRING_SERIAL_NUM_SIZE] =
{
/* Length of this descriptor*/
STRING_SERIAL_NUM_SIZE,
/* Descriptor Type = STRING */
0x03,
/* Descriptor Text (unicode) */
'1', 0x00, '.', 0x00, '1', 0x00
};
const DESCRIPTOR gStringDescriptorSerialNum =
{
STRING_SERIAL_NUM_SIZE,
gStringDescriptorSerialNumData
};
import usb.core
import usb.util
import sys
# find our device
dev = usb.core.find(idVendor=0x045b, idProduct=0x2013)
# was it found?
if dev is None:
raise ValueError('Device not found')
# Display Device Descriptor
sys.stdout.write('dev.bLength=' + str(dev.bLength) + '\n')
sys.stdout.write('dev.bDescriptorType=' + str(dev.bDescriptorType) + '\n')
sys.stdout.write('dev.bcdUSB=' + str(dev.bcdUSB) + '\n')
sys.stdout.write('dev.bDeviceClass=' + str(dev.bDeviceClass) + '\n')
sys.stdout.write('dev.bDeviceSubClass=' + str(dev.bDeviceSubClass) + '\n')
sys.stdout.write('dev.bDeviceProtocol=' + str(dev.bDeviceProtocol) + '\n')
sys.stdout.write('dev.bMaxPacketSize0=' + str(dev.bMaxPacketSize0) + '\n')
sys.stdout.write('dev.idVendor=' + str(dev.idVendor) + '\n')
sys.stdout.write('dev.idProduct=' + str(dev.idProduct) + '\n')
sys.stdout.write('dev.bcdDevice=' + str(dev.bcdDevice) + '\n')
sys.stdout.write('dev.iManufacturer=' + str(dev.iManufacturer) + '\n')
sys.stdout.write('dev.iProduct=' + str(dev.iProduct) + '\n')
sys.stdout.write('dev.iSerialNumber=' + str(dev.iSerialNumber) + '\n')
sys.stdout.write('dev.bNumConfigurations=' + str(dev.bNumConfigurations) + '\n')
#To access the configurations available in the device, you can iterate over the
device
for cfg in dev:
# Display Configuration Descriptor
sys.stdout.write('\t' + 'cfg.bLength=' + str(cfg.bLength) + '\n')
sys.stdout.write('\t' + 'cfg.bDescriptorType=' + str(cfg.bDescriptorType) +
'\n')
sys.stdout.write('\t' + 'cfg.wTotalLength=' + str(cfg.wTotalLength) + '\n')
sys.stdout.write('\t' + 'cfg.bNumInterfaces=' + str(cfg.bNumInterfaces) +
'\n')
sys.stdout.write('\t' + 'cfg.bConfigurationValue=' +
str(cfg.bConfigurationValue) + '\n')
sys.stdout.write('\t' + 'cfg.iConfiguration=' + str(cfg.iConfiguration) +
'\n')
sys.stdout.write('\t' + 'cfg.bmAttributes=' + str(cfg.bmAttributes) + '\n')
sys.stdout.write('\t' + 'cfg.bMaxPower=' + str(cfg.bMaxPower) + '\n')
# Display Interface Descriptor
for intf in cfg:
sys.stdout.write('\t\t' + 'intf.bLength=' + str(intf.bLength) + '\n')
sys.stdout.write('\t\t' + 'intf.bDescriptorType=' +
str(intf.bDescriptorType) + '\n')
sys.stdout.write('\t\t' + 'intf.bInterfaceNumber=' +
str(intf.bInterfaceNumber) + '\n')
sys.stdout.write('\t\t' + 'intf.bAlternateSetting=' +
str(intf.bAlternateSetting) + '\n')
sys.stdout.write('\t\t' + 'intf.bNumEndpoints=' +
str(intf.bNumEndpoints) + '\n')
sys.stdout.write('\t\t' + 'intf.bInterfaceClass=' +
str(intf.bInterfaceClass) + '\n')
sys.stdout.write('\t\t' + 'intf.bInterfaceSubClass=' +
str(intf.bInterfaceSubClass) + '\n')
sys.stdout.write('\t\t' + 'intf.bInterfaceProtocol=' +
str(intf.bInterfaceProtocol) + '\n')
sys.stdout.write('\t\t' + 'intf.iInterface=' + str(intf.iInterface) +
'\n')
# Display Endpoint Descriptor
for ep in intf:
sys.stdout.write('\t\t\t' + 'ep.bLength=' + str(ep.bLength) + '\n')
sys.stdout.write('\t\t\t' + 'ep.bDescriptorType=' +
str(ep.bDescriptorType) + '\n')
sys.stdout.write('\t\t\t' + 'ep.bEndpointAddress=' +
str(ep.bEndpointAddress) + '\n')
sys.stdout.write('\t\t\t' + 'ep.bmAttributes=' +
str(ep.bmAttributes) + '\n')
sys.stdout.write('\t\t\t' + 'ep.wMaxPacketSize=' +
str(ep.wMaxPacketSize) + '\n')
sys.stdout.write('\t\t\t' + 'ep.bInterval=' + str(ep.bInterval) +
'\n')
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
msg = [0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20]
dev.ctrl_transfer(bmRequestType=0x42, bRequest=0x09, wValue=0x02, wIndex=0x00,
data_or_wLength=msg, timeout=100)
#sent_bytes = dev.write(0x00, msg, 0, 100)
#assert dev.ctrl_transfer(0x40, CTRL_LOOPBACK_WRITE, 0, 0, msg) == len(msg)
#ret = dev.ctrl_transfer(0x40, CTRL_LOOPBACK_READ, 0, 0, len(msg))
#sret = ''.join([chr(x) for x in ret])
#assert sret == msg
------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today. Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users