Hello everyone,
I have had some success with PyUSB, but it's all new to me and I am struggling
with something which should be simple. I am using PyUSB v1.0.
I am trying to write to the GPIO pins on a USB sound interface. I have one of
the extremely common USB sound dongles which contains a 3S SSS1623 chip.
This chip has two GPIO pins, and inputs for buttons (volume up/down, mic.
mute and speaker mute). I have the datasheet for the 3S chip which shows me
the pins for GPIO, but it doesn't tell me the HID commands.
Another common device is the C-Media CM108 chip, which has four GPIO pins.
The C-Media chip is compatible (but not identical) and I have the datasheet
for this too, with pinouts *and* HID commands.
So far I have written a simple PyUSB script which identifies the 3S chip,
detaches the HID interface, and loops around continuously reporting the
button states and GPIO states. I used the C-Media HID 'read' documentation
to decode what the 3S chip reports, and it matches. Next I want to write
four bytes to set GPIO0 to ouput, and drive it high or low. I am using the
C-Media 'write' documentation, but it's not working. The documentation might
be incompatible with the 3S chip, or I may have made a stupid mistake.
Below is the code I am using for testing. The main while loop runs forever.
If no buttons are pressed, or no GPIO changes state the endpoint read command
times out and generates an error which causes the loop to restart. If the
read command is successful then the code carries on and prints out the data
(4 bytes) that are received. I am running the code as root because I haven't
sorted out the permissions.
I intend the write command to send a 4-byte packet with a bit set for the GPIO
direction (input or output) and value (high or low). For this experiment I
want to toggle the bit every second. The write command always times out
unless I press a button on the device. Is this the expected result?
The return value from the write command (if it doesn't time out) is 4, which
is the number of bytes in the packet.
The GPIO pin does not change state, so maybe the write command isn't actually
working, or maybe the documentation (for the CM108 chip) doesn't work for
writing to the SSS1623 chip (it works for reading).
Are my expectations correct? Is my code reasonable?
Thanks in advance for any advice.
Andrew
Test code:
import sys
import time
import usb.core
import usb.util
# Search for one of the two devices
VENDOR_ID = 0x0c76
PRODUCT_ID = 0x1607 # SSS1623 chip
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
if device is None:
VENDOR_ID = 0x0c76
PRODUCT_ID = 0x1605 # SSS1621 chip
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
if device is None:
sys.exit("No device found")
print "Found %04x:%04x"%(VENDOR_ID,PRODUCT_ID)
if device.is_kernel_driver_active(0):
try:
device.detach_kernel_driver(3)
except usb.core.USBError:
print "Could not detach kernel driver. Maybe it's already
detached."
# It seems to be okay to carry on here.
# sys.exit("Could not detach kernel driver")
# This is the HID endpoint on this device.
endpoint = device[0][(3,0)][0]
out_state = True
last_second=0
while 1:
this_second = int(time.time())%2
# Write data packet (from CM108 documentation)
# byte 0: 00xx xxxx Write GPIO
# byte 1: xxxx dcba GPIO3-0 output values
# byte 2: xxxx dcba GPIO3-0 data-direction register (1=output)
# byte 3: xxxx xxxx SPDIF
if this_second!=last_second:
if out_state:
print "Want to write '1' to port"
packet=[0x00,0x01,0x01,0x00]
else:
print "Want to write '0' to port"
packet=[0x00,0x00,0x01,0x00]
out_state = not(out_state)
last_second=this_second
try:
endpoint.write(packet,10)
except usb.core.USBError:
print 'Error writing to device'
try:
data=endpoint.read(endpoint.wMaxPacketSize,10)
except usb.core.USBError:
continue
print data
GPIO0=data[1] & 0x01
GPIO1=data[1] & 0x02
volup=data[0] & 0x01
voldn=data[0] & 0x02
spmut=data[0] & 0x04
mcmut=data[0] & 0x08
print GPIO0,GPIO1,volup,voldn,spmut,mcmut
Here is the output of lsusb for the device (from a user account, not root):
lsusb -v -d 0c76:
Bus 003 Device 044: ID 0c76:1607 JMTek, LLC. audio controller
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.10
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x0c76 JMTek, LLC.
idProduct 0x1607 audio controller
bcdDevice 1.00
iManufacturer 0
iProduct 1
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 247
bNumInterfaces 4
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
(Bus Powered)
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 1 Audio
bInterfaceSubClass 1 Control Device
bInterfaceProtocol 0
iInterface 0
AudioControl Interface Descriptor:
bLength 10
bDescriptorType 36
bDescriptorSubtype 1 (HEADER)
bcdADC 1.00
wTotalLength 100
bInCollection 2
baInterfaceNr( 0) 1
baInterfaceNr( 1) 2
AudioControl Interface Descriptor:
bLength 12
bDescriptorType 36
bDescriptorSubtype 2 (INPUT_TERMINAL)
bTerminalID 1
wTerminalType 0x0101 USB Streaming
bAssocTerminal 0
bNrChannels 2
wChannelConfig 0x0003
Left Front (L)
Right Front (R)
iChannelNames 0
iTerminal 0
AudioControl Interface Descriptor:
bLength 12
bDescriptorType 36
bDescriptorSubtype 2 (INPUT_TERMINAL)
bTerminalID 2
wTerminalType 0x0201 Microphone
bAssocTerminal 0
bNrChannels 1
wChannelConfig 0x0001
Left Front (L)
iChannelNames 0
iTerminal 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 3 (OUTPUT_TERMINAL)
bTerminalID 17
wTerminalType 0x0301 Speaker
bAssocTerminal 0
bSourceID 49
iTerminal 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 3 (OUTPUT_TERMINAL)
bTerminalID 18
wTerminalType 0x0101 USB Streaming
bAssocTerminal 2
bSourceID 33
iTerminal 0
AudioControl Interface Descriptor:
bLength 7
bDescriptorType 36
bDescriptorSubtype 5 (SELECTOR_UNIT)
bUnitID 33
bNrInPins 1
baSource( 0) 50
iSelector 0
AudioControl Interface Descriptor:
bLength 10
bDescriptorType 36
bDescriptorSubtype 6 (FEATURE_UNIT)
bUnitID 49
bSourceID 65
bControlSize 1
bmaControls( 0) 0x01
Mute
bmaControls( 1) 0x02
Volume
bmaControls( 2) 0x02
Volume
iFeature 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 6 (FEATURE_UNIT)
bUnitID 50
bSourceID 2
bControlSize 1
bmaControls( 0) 0x43
Mute
Volume
Automatic Gain
bmaControls( 1) 0x00
iFeature 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 6 (FEATURE_UNIT)
bUnitID 51
bSourceID 2
bControlSize 1
bmaControls( 0) 0x03
Mute
Volume
bmaControls( 1) 0x00
iFeature 0
AudioControl Interface Descriptor:
bLength 13
bDescriptorType 36
bDescriptorSubtype 4 (MIXER_UNIT)
bUnitID 65
bNrInPins 2
baSourceID( 0) 1
baSourceID( 1) 51
bNrChannels 2
wChannelConfig 0x0003
Left Front (L)
Right Front (R)
iChannelNames 0
bmControls 0x00
iMixer 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 1
bNumEndpoints 1
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
AudioStreaming Interface Descriptor:
bLength 7
bDescriptorType 36
bDescriptorSubtype 1 (AS_GENERAL)
bTerminalLink 1
bDelay 1 frames
wFormatTag 1 PCM
AudioStreaming Interface Descriptor:
bLength 11
bDescriptorType 36
bDescriptorSubtype 2 (FORMAT_TYPE)
bFormatType 1 (FORMAT_TYPE_I)
bNrChannels 2
bSubframeSize 2
bBitResolution 16
bSamFreqType 1 Discrete
tSamFreq[ 0] 48000
Endpoint Descriptor:
bLength 9
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 9
Transfer Type Isochronous
Synch Type Adaptive
Usage Type Data
wMaxPacketSize 0x00c8 1x 200 bytes
bInterval 1
bRefresh 0
bSynchAddress 0
AudioControl Endpoint Descriptor:
bLength 7
bDescriptorType 37
bDescriptorSubtype 1 (EP_GENERAL)
bmAttributes 0x00
bLockDelayUnits 1 Milliseconds
wLockDelay 1 Milliseconds
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 2
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 2
bAlternateSetting 1
bNumEndpoints 1
bInterfaceClass 1 Audio
bInterfaceSubClass 2 Streaming
bInterfaceProtocol 0
iInterface 0
AudioStreaming Interface Descriptor:
bLength 7
bDescriptorType 36
bDescriptorSubtype 1 (AS_GENERAL)
bTerminalLink 18
bDelay 1 frames
wFormatTag 1 PCM
AudioStreaming Interface Descriptor:
bLength 11
bDescriptorType 36
bDescriptorSubtype 2 (FORMAT_TYPE)
bFormatType 1 (FORMAT_TYPE_I)
bNrChannels 1
bSubframeSize 2
bBitResolution 16
bSamFreqType 1 Discrete
tSamFreq[ 0] 48000
Endpoint Descriptor:
bLength 9
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 5
Transfer Type Isochronous
Synch Type Asynchronous
Usage Type Data
wMaxPacketSize 0x00c8 1x 200 bytes
bInterval 1
bRefresh 0
bSynchAddress 0
AudioControl Endpoint Descriptor:
bLength 7
bDescriptorType 37
bDescriptorSubtype 1 (EP_GENERAL)
bmAttributes 0x00
bLockDelayUnits 0 Undefined
wLockDelay 0 Undefined
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 3
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 0 No Subclass
bInterfaceProtocol 0 None
iInterface 0
HID Device Descriptor:
bLength 9
bDescriptorType 33
bcdHID 1.00
bCountryCode 0 Not supported
bNumDescriptors 1
bDescriptorType 34 Report
wDescriptorLength 50
Report Descriptors:
** UNAVAILABLE **
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0004 1x 4 bytes
bInterval 8
cannot read device status, Operation not permitted (1)
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
pyusb-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyusb-users