Sunday, September 29, 2013

BladeRF-python problem "ValueError: No backend available" solved and... python has no problems with bladeRF in USB3

Summary

I want to access the bladeRF-board from a Python-program. The simple program below returns an error. By changing the driver from libusbk to libusb-win32 I solved the problem and now I can also access the bladeRF-board from python using an USB3-port! However, the program bladeRF-cli still cannot access the bladeRF-board when plugged in an USB3-port.

Warning: somehow I screw up the layout, I did copy/paste from Word. I have to find out a better way.... Sorry

Running a simple python-program

The following simple python-program:


import usb.backend
import usb.legacy
import usb.core
import usb.util
import usb.control

# find our device
dev = usb.core.find(idVendor=0x1D50, idProduct=0x6066)

# was it found?
if dev is None:
    raise ValueError('Device not found')
else:
    print ('Device found')

    print 'len =', dev.bLength
    print 'bNumConfigurations =', dev.bNumConfigurations
    print 'bDeviceClass =', dev.bDeviceClass
    num = dev.idVendor
    num = "%#x"%(num)
    print 'idVendor = ', num
    #print 'idVendor = ', dev.idVendor
    num = dev.idProduct
    num = "%#x"%(num)
    print 'idProduct = ', num
    #print 'idProduct = ', dev.idProduct
    print 'bcdUSB = ', dev.bcdUSB
    print 'bcdDevice = ', dev.bcdDevice
    print 'iManufacturer = ', dev.iManufacturer
   
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
#dev.set_configuration(0)


Output:


Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>

Traceback (most recent call last):
  File "C:\Users\lonneke\python\bladeRF01.py", line 9, in 
    dev = usb.core.find(idVendor=0x1D50, idProduct=0x6066)
  File "C:\Python27\lib\site-packages\usb\core.py", line 846, in find
    raise ValueError('No backend available')
ValueError: No backend available
>>> 

Trying to solve the problem with environment-variables


Changes to the environment-variables PATH and PYTHONPATH did not solve this problem.

Research on the Internet:


PyUSB should run on any platform with Python >= 2.4, ctypes and at least one of the supported builtin backends.

Do I have ctypes?
Yes: I found a directory C:\Python27\Lib\ctypes


I am using Python 2.6.5, libusb-win32-device.bin-0.1.12.1 and pyusb-1.0.0-a0 on a windows XP system and kept receiving ValueError: No backend available.
Since there wasn't any real help on the web for this problem I spent a lot of time finding that ctypes util.py uses the Path variable to find the library file. My path did not includewindows\system32 and PYUSB didn't find the library. I updated the path variable and now the USB is working.

NOTE: I use libusbk, NOT libusb-win32!!

Here is the libusb wiki:


USB 3.0 support

libusb 1.0 supports USB 3.0 controllers and devices on Windows, from 1.0.9 release onwards. Because of the large number of USB 3.0 controllers, the lack of official USB 3.0 support from Microsoft in Windows 7 and earlier as well as limited testing, be mindful that USB 3.0 support should be considered experimental. Also, if you are using a NEC/Renesas USB 3.0 controller, such as the fairly widespread uPD720200/uPD720200A, you please make sure that your controller drivers are ​version 2.1.16.0 or later. Older versions of the drivers have a bug that prevents access to USB devices when using libusb.

Install a different driver, replace libusbk by libusb-win32


Use zadig.exe to install a different driver, libusb-win32:








Now my bladeRF is a libusb-win32-device

Try bladeRF-cli

What about bladeRF-cli???
Microsoft Windows [versie 6.1.7601] 
Copyright (c) 2009 Microsoft Corporation. Alle rechten voorbehouden. 

C:\bladeRF-master\host\build\output\Debug>bladeRF-cli -p 

Backend: libusb 
Serial: b436de8c8212b9aeaaeba852246866e7 
USB Bus: 2 
USB Address: 3 
C:\bladeRF-master\host\build\output\Debug>bladeRF-cli -l hostedx115.rbf 
[INFO] Using libusb version 1.0.16.10774 
[INFO] Found a bladeRF 
[INFO] Claimed all inferfaces successfully 
[INFO] Change to alternate interface 2 
[INFO] Change to alternate interface 2 
Loading fpga... 
[INFO] Change to alternate interface 0 
[INFO] Change to alternate interface 1 
[INFO] Setting integer sample rate: 1000000 
[INFO] Found r value of: 4 
[INFO] MSx a + b/c: 316 + 4/5 
[INFO] MSx a + b/c: 316 + 4/5 
[INFO] MSx P1: 0x00009c66 (40038) P2: 0x00000002 (2) P3: 0x00000005 (5) 
[INFO] Calculated samplerate: 1000000 + 0/1 
[INFO] Set actual integer sample rate: 1000000 
[INFO] Setting integer sample rate: 1000000 
[INFO] Found r value of: 4 
[INFO] MSx a + b/c: 316 + 4/5 
[INFO] MSx a + b/c: 316 + 4/5 
[INFO] MSx P1: 0x00009c66 (40038) P2: 0x00000002 (2) P3: 0x00000005 (5) 
[INFO] Calculated samplerate: 1000000 + 0/1 
[INFO] Set actual integer sample rate: 1000000 
Done. 

C:\bladeRF-master\host\build\output\Debug> 
And all LEDs are blinking!!
 Try the python-program Now try the same python-program as above:
 

And the output is: 

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
Device found 
len = 18 
bNumConfigurations = 1 
bDeviceClass = 0 
idVendor = 0x1d50 
idProduct = 0x6066 
bcdUSB = 528 
bcdDevice = 0 
iManufacturer = 1 
>>> 

Now try USB3 instead of USB2 with bladeRF-cli

But what about plug bladeRF in an USB3-port with this driver?
BladeRF in USB2-port:


Microsoft Windows [versie 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle rechten voorbehouden.
C:\bladeRF-master\host\build\output\Debug>bladeRF-cli -l hostedx115.rbf
[INFO] Using libusb version 1.0.16.10774
[INFO] Found a bladeRF
[INFO] Claimed all inferfaces successfully
[INFO] Change to alternate interface 2
[INFO] Change to alternate interface 2
Loading fpga...
[INFO] Change to alternate interface 0
[INFO] Change to alternate interface 1
[INFO] Setting integer sample rate: 1000000
[INFO] Found r value of: 4
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx P1: 0x00009c66 (40038) P2: 0x00000002 (2) P3: 0x00000005 (5)
[INFO] Calculated samplerate: 1000000 + 0/1
[INFO] Set actual integer sample rate: 1000000
[INFO] Setting integer sample rate: 1000000
[INFO] Found r value of: 4
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx P1: 0x00009c66 (40038) P2: 0x00000002 (2) P3: 0x00000005 (5)
[INFO] Calculated samplerate: 1000000 + 0/1
[INFO] Set actual integer sample rate: 1000000
Done.
C:\bladeRF-master\host\build\output\Debug>bladeRF-cli -p
Backend:        libusb
Serial:         b436de8c8212b9aeaaeba852246866e7
USB Bus:        2
USB Address:    3

Now unplug and plug in USB3-port:


C:\bladeRF-master\host\build\output\Debug>bladeRF-cli -l hostedx115.rbf
[INFO] Using libusb version 1.0.16.10774
[ERROR] No devices available on the libusb backend.
Failed to open device (NULL): No devices available
Could not open device

bladeRF-cli cannot handle the USB3-port on my laptop.
Unplug and replug in USB2-port:


C:\bladeRF-master\host\build\output\Debug>bladeRF-cli -l hostedx115.rbf
[INFO] Using libusb version 1.0.16.10774
[INFO] Found a bladeRF
[INFO] Claimed all inferfaces successfully
[INFO] Change to alternate interface 2
[INFO] Change to alternate interface 2
Loading fpga...
[INFO] Change to alternate interface 0
[INFO] Change to alternate interface 1
[INFO] Setting integer sample rate: 1000000
[INFO] Found r value of: 4
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx P1: 0x00009c66 (40038) P2: 0x00000002 (2) P3: 0x00000005 (5)
[INFO] Calculated samplerate: 1000000 + 0/1
[INFO] Set actual integer sample rate: 1000000
[INFO] Setting integer sample rate: 1000000
[INFO] Found r value of: 4
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx a + b/c: 316 + 4/5
[INFO] MSx P1: 0x00009c66 (40038) P2: 0x00000002 (2) P3: 0x00000005 (5)
[INFO] Calculated samplerate: 1000000 + 0/1
[INFO] Set actual integer sample rate: 1000000
Done.

C:\bladeRF-master\host\build\output\Debug>

Now try USB3 instead of USB2 with bladeRF-cli

And what about python?
First bladeRF in USB2-port:
Run python-program. 
Output:
>>>
Device found
len = 18
bNumConfigurations = 1
bDeviceClass = 0
idVendor =  0x1d50
idProduct =  0x6066
bcdUSB =  528
bcdDevice =  0
iManufacturer =  1
Now unplug and put in USB3-slot:
>>> ================================ RESTART ================================
>>>
Device found
len = 18
bNumConfigurations = 1
bDeviceClass = 0
idVendor =  0x1d50
idProduct =  0x6066
bcdUSB =  768
bcdDevice =  0
iManufacturer =  1
>>> 

Conclusion

YES!!
Python has no problems with USB3 on my laptop!!
The program bladeRF-cli cannot access the bladeRF-board from USB3, so I cannot flash the
FPGA. I should be able to do that from python in this situation. But how?
Anyway, with bladeRF plugged in an USB2-port I can solve this problem for the moment.
December 2012 in Spain, Santiago.
View on the world from within our caravan.
This weather is perfect for programming but
not so pleasant for a long walk with our dog...


No comments:

Post a Comment